Skip to content

Commit

Permalink
Merge pull request #293 from CorvusYe/master
Browse files Browse the repository at this point in the history
fix: the issue of not being able to handle Set type.
  • Loading branch information
wey-gu authored Apr 18, 2024
2 parents 324f8c3 + 9ff7938 commit 0db5df3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ This source code is licensed under Apache 2.0 License.
## Bugfix

- fix: complete the error code of ResultSet into QueryException.
- fix: the issue of not being able to handle Set type.
- fix: the issue of the ranking value of the edge object cannot be filled.

## Dependencies upgrade

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public interface TestRepository extends NebulaDaoBasic<Person, String> {
Boolean spaceFromParam(@Param("specifySpace") String specifySpace);

List<Person> dynamicSpaceWithPage(Page<Person> page, @Param("space") String space);


NgSubgraph<String> resultContainingSet();

class DynamicNode {
@Id
private String vid;
Expand Down
8 changes: 8 additions & 0 deletions ngbatis-demo/src/main/resources/mapper/TestRepository.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@
RETURN n
</select>

<select id="resultContainingSet" resultType="org.nebula.contrib.ngbatis.models.data.NgSubgraph">
GO 1 STEPS FROM "Tom" OVER like REVERSELY
YIELD $^ AS src, EDGE AS destination, $$ AS dst
| YIELD
toSet( collect( $-.dst ) ) AS nodes,
toSet( collect( $-.destination ) ) AS relationships
</select>


</mapper>

Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,11 @@ public void insertWithTimestamp() {
person.setBirthday(new Date());
repository.insertWithTimestamp(person);
}

@Test
public void testResultContainingSet() {
NgSubgraph<String> rs = repository.resultContainingSet();
System.out.println(JSON.toJSONString(rs));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.vesoft.nebula.client.graph.data.ResultSet;
import com.vesoft.nebula.client.graph.data.ResultSet.Record;
import com.vesoft.nebula.client.graph.data.ValueWrapper;
import java.util.ArrayList;
import java.util.List;
import org.nebula.contrib.ngbatis.models.data.NgEdge;
import org.nebula.contrib.ngbatis.models.data.NgSubgraph;
Expand Down Expand Up @@ -36,7 +37,7 @@ public NgSubgraph<?> handle(NgSubgraph<?> newResult, ResultSet result, Class res
public NgSubgraph<?> handle(NgSubgraph<?> newResult, Record row) {
ValueWrapper nodes = row.get("nodes");
if (nodes != null) {
List<ValueWrapper> nodesValueWrapper = nodes.asList();
List<ValueWrapper> nodesValueWrapper = getCollection(nodes);
nodesValueWrapper.forEach(nodeValueWrapper -> {
List vertexes = newResult.getVertexes();
NgVertex<?> vertex = new NgVertex<>();
Expand All @@ -47,7 +48,7 @@ public NgSubgraph<?> handle(NgSubgraph<?> newResult, Record row) {

ValueWrapper relationships = row.get("relationships");
if (relationships != null) {
List<ValueWrapper> edgesValueWrapper = relationships.asList();
List<ValueWrapper> edgesValueWrapper = getCollection(relationships);
edgesValueWrapper.forEach(edgeValueWrapper -> {
List edges = newResult.getEdges();
NgEdge<?> edge = new NgEdge<>();
Expand All @@ -57,4 +58,15 @@ public NgSubgraph<?> handle(NgSubgraph<?> newResult, Record row) {
}
return newResult;
}

/**
* Supports both List and Set as param,
* and returns a List in order to be compatible with the original code.
* @param listOrSetWrapper original data from ResultSet
* @return List of ValueWrapper
*/
private List<ValueWrapper> getCollection(ValueWrapper listOrSetWrapper) {
return listOrSetWrapper.isList() ? listOrSetWrapper.asList()
: new ArrayList<>(listOrSetWrapper.asSet());
}
}
30 changes: 26 additions & 4 deletions src/main/java/org/nebula/contrib/ngbatis/utils/ResultSetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@
import com.vesoft.nebula.client.graph.data.TimeWrapper;
import com.vesoft.nebula.client.graph.data.ValueWrapper;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.nebula.contrib.ngbatis.exception.ResultHandleException;
import org.nebula.contrib.ngbatis.models.MapperContext;
Expand Down Expand Up @@ -90,7 +89,7 @@ public static <T> T getValue(ValueWrapper value) {
: value.isEdge() ? transformRelationship(value)
: value.isPath() ? value.asPath()
: value.isList() ? transformList(value.asList())
: value.isSet() ? transformList(value.asList())
: value.isSet() ? transformSet(value.asSet())
: value.isMap() ? transformMap(value.asMap())
: value.isDuration() ? transformDuration(value.asDuration())
: null;
Expand Down Expand Up @@ -243,6 +242,10 @@ private static Object transformList(ArrayList<ValueWrapper> list) {
return list.stream().map(ResultSetUtil::getValue).collect(Collectors.toList());
}

private static Set<Object> transformSet(Set<ValueWrapper> set) {
return set.stream().map(ResultSetUtil::getValue).collect(Collectors.toSet());
}

/**
* <p>数据库中的节点类型转接口类型(节点orm)</p>
* @param v 结果集中的 node 数据值
Expand Down Expand Up @@ -302,6 +305,7 @@ public static <T> T relationshipToResultType(Relationship r, Class<T> resultType
for (Map.Entry<String, ValueWrapper> entry : properties.entrySet()) {
ReflectUtil.setValue(t, entry.getKey(), ResultSetUtil.getValue(entry.getValue()));
}
setRanking(t, resultType, r);
} catch (UnsupportedEncodingException | InstantiationException
| NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
Expand Down Expand Up @@ -339,13 +343,31 @@ public static void relationshipToResultType(Object o, String fieldName,
* v 中,通过 id(n) 获取到的类型不匹配时报错
*/
public static void setId(Object obj, Class<?> resultType, Node v)
throws IllegalAccessException {
throws IllegalAccessException {
Field pkField = getPkField(resultType);
ValueWrapper idWrapper = v.getId();
Object id = ResultSetUtil.getValue(idWrapper);
ReflectUtil.setValue(obj, pkField, id);
}

/**
* <p> 从 resultType 中获取到用 @Id 注解的属性,
* 并将 relationship 对象的 ranking 属性的值填入 </p>
* @param obj 边的 java 对象
* @param resultType 边的 java 对象的类型
* @param e nebula 中的关系对象
* @throws IllegalAccessException
*/
public static void setRanking(Object obj, Class<?> resultType, Relationship e)
throws IllegalAccessException {
Field pkField = getPkField(resultType, false);
if (pkField == null) {
return;
}
long ranking = e.ranking();
ReflectUtil.setValue(obj, pkField, ranking);
}

/**
* Set java entity attributes from Node's properties.
* 兼容多标签,对 java 对象进行按标签设属性值。
Expand Down

0 comments on commit 0db5df3

Please sign in to comment.