Skip to content

Commit c67c017

Browse files
authored
Merge pull request #244 from shbone/master
Feat: add insertEdgeBatch function #234
2 parents 5e75595 + b27d4e2 commit c67c017

File tree

5 files changed

+534
-380
lines changed

5 files changed

+534
-380
lines changed

ngbatis-demo/src/test/java/ye/weicheng/ngbatis/demo/NebulaBasicDaoTests.java

+63-17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.junit.jupiter.api.Test;
1818
import org.locationtech.jts.util.Assert;
1919
import org.nebula.contrib.ngbatis.models.data.NgPath;
20+
import org.nebula.contrib.ngbatis.models.data.NgTriplet;
2021
import org.nebula.contrib.ngbatis.utils.Page;
2122
import org.springframework.beans.factory.annotation.Autowired;
2223
import org.springframework.boot.test.context.SpringBootTest;
@@ -60,15 +61,15 @@ public void selectBySelective() {
6061
List<Person> people = repository.selectBySelective(person);
6162
System.out.println(JSON.toJSONString(people));
6263
}
63-
64+
6465
@Test
6566
public void selectBySelectiveWithBigDecimal() {
6667
Person person = new Person();
6768
person.setHeight(new BigDecimal("155.55555"));
6869
List<Person> people = repository.selectBySelective(person);
6970
System.out.println(JSON.toJSONString(people));
7071
}
71-
72+
7273
/**
7374
* https://github.com/nebula-contrib/ngbatis/issues/35.
7475
*/
@@ -107,7 +108,7 @@ public void selectIdBySelectiveStringLike() {
107108
List<String> people = repository.selectIdBySelectiveStringLike(person);
108109
System.out.println(people);
109110
}
110-
111+
111112
@Test
112113
public void selectByMap() {
113114
Map<String, Object> query = new HashMap<>();
@@ -182,7 +183,7 @@ public void insertSelectiveWithBigDecimal() {
182183
@Test
183184
public void insertBatch() {
184185
long now = System.currentTimeMillis();
185-
186+
186187
Person person1 = new Person();
187188
person1.setName("IB" + now);
188189
person1.setGender("M");
@@ -192,17 +193,17 @@ public void insertBatch() {
192193
person2.setName("IB" + (now + 1));
193194
person2.setAge(18);
194195
person2.setBirthday(new Date());
195-
196+
196197
Person person3 = new Person();
197198
person3.setName("IB" + (now + 2));
198199
person3.setGender("M");
199200
person3.setBirthday(new Date());
200201

201202
List<Person> people = new ArrayList<>();
202203
people.add(person1);
203-
people.add(person2);;
204+
people.add(person2);
204205
people.add(person3);
205-
206+
206207
repository.insertBatch(people);
207208
}
208209
// endregion
@@ -215,14 +216,14 @@ public void updateById() {
215216
Person person = new Person();
216217
person.setName(name);
217218
repository.insert(person);
218-
219+
219220
Integer newAge = randomAge();
220221
person.setAge(newAge);
221222
person.setGender("F");
222223
repository.updateById(person);
223-
224+
224225
Person personDb = repository.selectById(name);
225-
226+
226227
Assert.isTrue(newAge.equals(personDb.getAge()));
227228
}
228229

@@ -266,7 +267,7 @@ public void updateByIdBatch() {
266267

267268
List<Person> people = new ArrayList<>();
268269
people.add(person1);
269-
people.add(person2);;
270+
people.add(person2);
270271
people.add(person3);
271272

272273
repository.insertBatch(people);
@@ -276,17 +277,17 @@ public void updateByIdBatch() {
276277

277278
Integer newAge2 = randomAge();
278279
person2.setAge(newAge2);
279-
280+
280281
Integer newAge3 = randomAge();
281282
person3.setAge(newAge3);
282-
283+
283284
repository.updateByIdBatchSelective(people);
284-
285+
285286
List<String> ids = people.stream().map(Person::getName).collect(Collectors.toList());
286287
List<Person> peopleDb = repository.selectByIds(ids);
287288

288289
Assert.isTrue(peopleDb.size() == 3);
289-
290+
290291
for (Person personDb : peopleDb) {
291292
for (Person person : people) {
292293
if (Objects.equals(personDb.getName(), person.getName())) {
@@ -330,6 +331,31 @@ public void deleteById() {
330331
int row = repository.deleteById("赵小洋");
331332
System.out.println(row);
332333
}
334+
335+
@Test
336+
public void deleteByIdBatch() {
337+
long now = System.currentTimeMillis();
338+
Person person1 = new Person();
339+
person1.setName("UBB" + now);
340+
341+
Person person2 = new Person();
342+
person2.setName("UBB" + (now + 1));
343+
344+
Person person3 = new Person();
345+
person3.setName("UBB" + (now + 2));
346+
347+
List<Person> people = new ArrayList<>();
348+
people.add(person1);
349+
people.add(person2);
350+
people.add(person3);
351+
repository.insertBatch(people);
352+
353+
List<String> peopleIds = new ArrayList<>();
354+
peopleIds.add(person1.getName());
355+
peopleIds.add(person2.getName());
356+
peopleIds.add(person3.getName());
357+
Assert.equals(repository.deleteByIdBatch(peopleIds),1);
358+
}
333359
// endregion
334360

335361
// region graph special
@@ -384,7 +410,7 @@ public void insertEdgeUseNodeId() {
384410
like.setLikeness(0.202210171102);
385411
repository.insertEdge("吴小极", like, "刘小洲");
386412
}
387-
413+
388414
@Test
389415
public void insertEdgeUseNodeId2() {
390416
LikeWithRank like = new LikeWithRank();
@@ -425,6 +451,26 @@ public void insertEdgeSelective() {
425451
repository.insertEdgeSelective(person1, likeWithRank, person2);
426452
}
427453

454+
@Test
455+
public void insertEdgeBatch() {
456+
List<NgTriplet<String>> ngTripletList = new ArrayList<>();
457+
for (int i = 0; i < 3; i++) {
458+
Person person1 = new Person();
459+
person1.setName("p1_" + i);
460+
repository.insertSelective(person1);
461+
462+
Person person2 = new Person();
463+
person2.setName("p2_" + i);
464+
repository.insertSelective(person2);
465+
466+
Like like = new Like();
467+
like.setLikeness(0.87);
468+
469+
ngTripletList.add(new NgTriplet<>(person1,like,person2));
470+
}
471+
repository.insertEdgeBatch(ngTripletList);
472+
}
473+
428474
@Test
429475
public void upsertEdgeSelective() {
430476
Person person1 = new Person();
@@ -460,7 +506,7 @@ public void startNode() {
460506
Person whoIsStartForTest = repository.startNode(Like.class, "易小海");
461507
System.out.println(JSON.toJSONString(whoIsStartForTest));
462508
}
463-
509+
464510
@Test
465511
public void shortestPath() {
466512
List<NgPath<String>> ngPaths = repository.shortestPath("吴小极", "刘小洲");

pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
<name>dieyi</name>
3333
<email>[email protected]</email>
3434
</developer>
35+
<developer>
36+
<name>shbone</name>
37+
<email>[email protected]</email>
38+
</developer>
3539
</developers>
3640

3741
<properties>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.nebula.contrib.ngbatis.models.data;
2+
3+
4+
public class NgTriplet<I> {
5+
private I srcId;
6+
private I dstId;
7+
private Object startNode;
8+
private Object edge;
9+
private Object endNode;
10+
11+
public NgTriplet(Object startNode, Object edge, Object endNode) {
12+
this.startNode = startNode;
13+
this.edge = edge;
14+
this.endNode = endNode;
15+
}
16+
17+
public I getSrcId() {
18+
return srcId;
19+
}
20+
21+
public void setSrcId(I srcId) {
22+
this.srcId = srcId;
23+
}
24+
25+
public I getDstId() {
26+
return dstId;
27+
}
28+
29+
public void setDstId(I dstId) {
30+
this.dstId = dstId;
31+
}
32+
33+
public Object getStartNode() {
34+
return startNode;
35+
}
36+
37+
public void setStartNode(Object startNode) {
38+
this.startNode = startNode;
39+
}
40+
41+
public Object getEdge() {
42+
return edge;
43+
}
44+
45+
public void setEdge(Object edge) {
46+
this.edge = edge;
47+
}
48+
49+
public Object getEndNode() {
50+
return endNode;
51+
}
52+
53+
public void setEndNode(Object endNode) {
54+
this.endNode = endNode;
55+
}
56+
}

src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,22 @@ default int deleteById(I id) {
314314
ResultSet resultSet = (ResultSet) MapperProxy.invoke(classModel, methodModel, id);
315315
return resultSet.isSucceeded() ? 1 : 0;
316316
}
317+
317318
// endregion
319+
/**
320+
* <p>通过 主键批量删除当前记录</p>
321+
*
322+
* @param ids 表记录主键列表
323+
* @return 是否删除成功,成功 1,失败 0
324+
*/
325+
default int deleteByIdBatch(List<I> ids) {
326+
MethodModel methodModel = getMethodModel();
327+
methodModel.setReturnType(ResultSet.class);
328+
methodModel.setResultType(ResultSet.class);
329+
ClassModel classModel = getClassModel(this.getClass());
330+
ResultSet resultSet = (ResultSet) MapperProxy.invoke(classModel, methodModel, ids);
331+
return resultSet.isSucceeded() ? 1 : 0;
332+
}
318333

319334
// region graph special
320335
/**
@@ -333,6 +348,17 @@ default void insertEdge(@NotNull Object v1, @NotNull Object e, @NotNull Object v
333348
MapperProxy.invoke(classModel, methodModel, v1, e, v2);
334349
}
335350

351+
/**
352+
* @Author sunhb
353+
* @Description 根据三元组列表的头结点,尾节点和尾节点进行插入
354+
* @Date 2023/10/10 上午11:03
355+
**/
356+
default void insertEdgeBatch(List triplets) {
357+
MethodModel methodModel = getMethodModel();
358+
ClassModel classModel = getClassModel(this.getClass());
359+
MapperProxy.invoke(classModel, methodModel, triplets);
360+
}
361+
336362
/**
337363
* 根据三元组值, 插入关系
338364
* <p>Selective: 仅处理非空字段</p>
@@ -450,7 +476,9 @@ default <E> E startNode(Class<E> startType, Class<?> edgeType, I endId) {
450476
* Find the shortest path by srcId and dstId.
451477
* @param srcId the start node's id
452478
* @param dstId the end node's id
453-
* @return Shortest path list. entities containing vertext in path. If you want to obtain attributes within an entity, you need to use “with prop” in the nGQL.
479+
* @return Shortest path list. entities containing vertext in path.
480+
* If you want to obtain attributes within an entity,
481+
* you need to use “with prop” in the nGQL.
454482
*/
455483
default List<NgPath<I>> shortestPath(@Param("srcId") I srcId, @Param("dstId") I dstId) {
456484
MethodModel methodModel = getMethodModel();
@@ -459,6 +487,8 @@ default List<NgPath<I>> shortestPath(@Param("srcId") I srcId, @Param("dstId") I
459487
ClassModel classModel = getClassModel(this.getClass());
460488
return (List<NgPath<I>>) MapperProxy.invoke(classModel, methodModel, srcId, dstId);
461489
}
490+
491+
462492
// endregion
463493

464494
}

0 commit comments

Comments
 (0)