Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add insertEdgeBatch function #234 #244

Merged
merged 7 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.jupiter.api.Test;
import org.locationtech.jts.util.Assert;
import org.nebula.contrib.ngbatis.models.data.NgPath;
import org.nebula.contrib.ngbatis.models.data.NgTriplet;
import org.nebula.contrib.ngbatis.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down Expand Up @@ -60,15 +61,15 @@ public void selectBySelective() {
List<Person> people = repository.selectBySelective(person);
System.out.println(JSON.toJSONString(people));
}

@Test
public void selectBySelectiveWithBigDecimal() {
Person person = new Person();
person.setHeight(new BigDecimal("155.55555"));
List<Person> people = repository.selectBySelective(person);
System.out.println(JSON.toJSONString(people));
}

/**
* https://github.com/nebula-contrib/ngbatis/issues/35.
*/
Expand Down Expand Up @@ -107,7 +108,7 @@ public void selectIdBySelectiveStringLike() {
List<String> people = repository.selectIdBySelectiveStringLike(person);
System.out.println(people);
}

@Test
public void selectByMap() {
Map<String, Object> query = new HashMap<>();
Expand Down Expand Up @@ -182,7 +183,7 @@ public void insertSelectiveWithBigDecimal() {
@Test
public void insertBatch() {
long now = System.currentTimeMillis();

Person person1 = new Person();
person1.setName("IB" + now);
person1.setGender("M");
Expand All @@ -192,17 +193,17 @@ public void insertBatch() {
person2.setName("IB" + (now + 1));
person2.setAge(18);
person2.setBirthday(new Date());

Person person3 = new Person();
person3.setName("IB" + (now + 2));
person3.setGender("M");
person3.setBirthday(new Date());

List<Person> people = new ArrayList<>();
people.add(person1);
people.add(person2);;
people.add(person2);
people.add(person3);

repository.insertBatch(people);
}
// endregion
Expand All @@ -215,14 +216,14 @@ public void updateById() {
Person person = new Person();
person.setName(name);
repository.insert(person);

Integer newAge = randomAge();
person.setAge(newAge);
person.setGender("F");
repository.updateById(person);

Person personDb = repository.selectById(name);

Assert.isTrue(newAge.equals(personDb.getAge()));
}

Expand Down Expand Up @@ -266,7 +267,7 @@ public void updateByIdBatch() {

List<Person> people = new ArrayList<>();
people.add(person1);
people.add(person2);;
people.add(person2);
people.add(person3);

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

Integer newAge2 = randomAge();
person2.setAge(newAge2);

Integer newAge3 = randomAge();
person3.setAge(newAge3);

repository.updateByIdBatchSelective(people);

List<String> ids = people.stream().map(Person::getName).collect(Collectors.toList());
List<Person> peopleDb = repository.selectByIds(ids);

Assert.isTrue(peopleDb.size() == 3);

for (Person personDb : peopleDb) {
for (Person person : people) {
if (Objects.equals(personDb.getName(), person.getName())) {
Expand Down Expand Up @@ -384,7 +385,7 @@ public void insertEdgeUseNodeId() {
like.setLikeness(0.202210171102);
repository.insertEdge("吴小极", like, "刘小洲");
}

@Test
public void insertEdgeUseNodeId2() {
LikeWithRank like = new LikeWithRank();
Expand Down Expand Up @@ -425,6 +426,26 @@ public void insertEdgeSelective() {
repository.insertEdgeSelective(person1, likeWithRank, person2);
}

@Test
public void insertEdgeBatch() {
List<NgTriplet<String>> ngTripletList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Person person1 = new Person();
person1.setName("p1_" + i);
repository.insertSelective(person1);

Person person2 = new Person();
person2.setName("p2_" + i);
repository.insertSelective(person2);

Like like = new Like();
like.setLikeness(0.87);

ngTripletList.add(new NgTriplet<>(person1,like,person2));
}
repository.insertEdgeBatch(ngTripletList);
}

@Test
public void upsertEdgeSelective() {
Person person1 = new Person();
Expand Down Expand Up @@ -460,7 +481,7 @@ public void startNode() {
Person whoIsStartForTest = repository.startNode(Like.class, "易小海");
System.out.println(JSON.toJSONString(whoIsStartForTest));
}

@Test
public void shortestPath() {
List<NgPath<String>> ngPaths = repository.shortestPath("吴小极", "刘小洲");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.nebula.contrib.ngbatis.models.data;


public class NgTriplet<I> {
private I srcId;
private I dstId;
private Object startNode;
private Object edge;
private Object endNode;

public NgTriplet(Object startNode, Object edge, Object endNode) {
this.startNode = startNode;
this.edge = edge;
this.endNode = endNode;
}

public I getSrcId() {
return srcId;
}

public void setSrcId(I srcId) {
this.srcId = srcId;
}

public I getDstId() {
return dstId;
}

public void setDstId(I dstId) {
this.dstId = dstId;
}

public Object getStartNode() {
return startNode;
}

public void setStartNode(Object startNode) {
this.startNode = startNode;
}

public Object getEdge() {
return edge;
}

public void setEdge(Object edge) {
this.edge = edge;
}

public Object getEndNode() {
return endNode;
}

public void setEndNode(Object endNode) {
this.endNode = endNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,19 @@ default void insertEdge(@NotNull Object v1, @NotNull Object e, @NotNull Object v
ClassModel classModel = getClassModel(this.getClass());
MapperProxy.invoke(classModel, methodModel, v1, e, v2);
}

/**
* @Author sunhb
* @Description 根据三元组列表的头结点,尾节点和尾节点进行插入
* @Date 2023/10/10 上午11:03
* @Param
* @param triplets 三元组列表
* @return void
**/
default void insertEdgeBatch(List triplets){
MethodModel methodModel = getMethodModel();
ClassModel classModel = getClassModel(this.getClass());
MapperProxy.invoke(classModel, methodModel, triplets);
}
/**
* 根据三元组值, 插入关系
* <p>Selective: 仅处理非空字段</p>
Expand Down
Loading
Loading