diff --git a/client/src/main/java/com/vesoft/nebula/client/graph/data/PathWrapper.java b/client/src/main/java/com/vesoft/nebula/client/graph/data/PathWrapper.java index d47ec6c58..ce5f021da 100644 --- a/client/src/main/java/com/vesoft/nebula/client/graph/data/PathWrapper.java +++ b/client/src/main/java/com/vesoft/nebula/client/graph/data/PathWrapper.java @@ -21,6 +21,7 @@ public class PathWrapper extends BaseDataObject { private List segments = new ArrayList<>(); private List nodes = new ArrayList<>(); private List relationships = new ArrayList<>(); + private Path path = null; public static class Segment { Node startNode; @@ -153,6 +154,7 @@ public PathWrapper(Path path) throws InvalidValueException, UnsupportedEncodingE this.segments = new ArrayList<>(); return; } + this.path = path; nodes.add((Node) new Node(path.src) .setDecodeType(getDecodeType()) .setTimezoneOffset(getTimezoneOffset())); @@ -191,7 +193,7 @@ public PathWrapper(Path path) throws InvalidValueException, UnsupportedEncodingE step.props); Relationship relationShip = (Relationship) new Relationship(edge) .setDecodeType(getDecodeType()) - .setTimezoneOffset(getTimezoneOffset());; + .setTimezoneOffset(getTimezoneOffset()); relationships.add(relationShip); Segment segment = new Segment(startNode, relationShip, endNode); if (segment.getStartNode() != nodes.get(nodes.size() - 1) @@ -207,48 +209,30 @@ public PathWrapper(Path path) throws InvalidValueException, UnsupportedEncodingE @Override public String toString() { try { - Node startNode = getStartNode(); List edgeStrs = new ArrayList<>(); - if (segments.size() >= 1) { + for (int i = 0; i < relationships.size(); i++) { + Relationship relationship = relationships.get(i); List propStrs = new ArrayList<>(); - Map props = segments.get(0).getRelationShip().properties(); + Map props = relationship.properties(); for (String key : props.keySet()) { propStrs.add(key + ": " + props.get(key).toString()); } - if (segments.get(0).getStartNode() == startNode) { - edgeStrs.add(String.format("-[:%s@%d{%s}]->%s", - segments.get(0).getRelationShip().edgeName(), - segments.get(0).getRelationShip().ranking(), - String.join(", ", propStrs), - segments.get(0).getEndNode().toString())); - } else { - edgeStrs.add(String.format("<-[:%s@%d{%s}]-%s", - segments.get(0).getRelationShip().edgeName(), - segments.get(0).getRelationShip().ranking(), - String.join(", ", propStrs), - segments.get(0).getStartNode().toString())); - } - - } - - for (int i = 1; i < segments.size(); i++) { - List propStrs = new ArrayList<>(); - Map props = segments.get(i).getRelationShip().properties(); - for (String key : props.keySet()) { - propStrs.add(key + ": " + props.get(key).toString()); - } - if (segments.get(i).getStartNode() == segments.get(i - 1).getEndNode()) { + Step step = path.steps.get(i); + Node node = (Node) new Node(step.dst) + .setDecodeType(getDecodeType()) + .setTimezoneOffset(getTimezoneOffset()); + if (step.type > 0) { edgeStrs.add(String.format("-[:%s@%d{%s}]->%s", - segments.get(i).getRelationShip().edgeName(), - segments.get(i).getRelationShip().ranking(), + relationship.edgeName(), + relationship.ranking(), String.join(", ", propStrs), - segments.get(i).getEndNode().toString())); + node.toString())); } else { edgeStrs.add(String.format("<-[:%s@%d{%s}]-%s", - segments.get(i).getRelationShip().edgeName(), - segments.get(i).getRelationShip().ranking(), + relationship.edgeName(), + relationship.ranking(), String.join(", ", propStrs), - segments.get(i).getStartNode().toString())); + node.toString())); } } return String.format("%s%s", diff --git a/client/src/test/java/com/vesoft/nebula/client/graph/data/TestDataFromServer.java b/client/src/test/java/com/vesoft/nebula/client/graph/data/TestDataFromServer.java index 6b415ac52..ce66fc5ee 100644 --- a/client/src/test/java/com/vesoft/nebula/client/graph/data/TestDataFromServer.java +++ b/client/src/test/java/com/vesoft/nebula/client/graph/data/TestDataFromServer.java @@ -37,7 +37,7 @@ public class TestDataFromServer { public void setUp() throws Exception { NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig(); nebulaPoolConfig.setMaxConnSize(1); - Assert.assertTrue(pool.init(Arrays.asList(new HostAddress("127.0.0.1", 9671)), + Assert.assertTrue(pool.init(Arrays.asList(new HostAddress("127.0.0.1", 9670)), nebulaPoolConfig)); session = pool.getSession("root", "nebula", true); ResultSet resp = session.execute("CREATE SPACE IF NOT EXISTS test_data" @@ -340,6 +340,46 @@ public void testPath() { } } + @Test + public void tesDataset() { + try { + ResultSet result = session.execute( + "CREATE TAG IF NOT EXISTS player(name string, age int);" + + "CREATE EDGE IF NOT EXISTS like(likeness int);"); + Assert.assertTrue(result.getErrorMessage(), result.isSucceeded()); + TimeUnit.SECONDS.sleep(6); + result = session.execute( + "INSERT VERTEX player(name, age) values \"a\":(\"a\", 1); " + + "INSERT VERTEX player(name, age) values \"b\":(\"b\", 2); " + + "INSERT VERTEX player(name, age) values \"c\":(\"c\", 3); " + + "INSERT VERTEX player(name, age) values \"d\":(\"d\", 4);" + + "INSERT VERTEX player(name, age) values \"f\":(\"f\", 5);" + + "INSERT VERTEX player(name, age) values \"g\":(\"g\", 6);" + + "INSERT EDGE like(likeness) values \"d\" -> \"a\":(10); " + + "INSERT EDGE like(likeness) values \"d\" -> \"c\":(10);" + + "INSERT EDGE like(likeness) values \"b\" -> \"a\":(10); " + + "INSERT EDGE like(likeness) values \"c\" -> \"b\":(10);" + + "INSERT EDGE like(likeness) values \"a\" -> \"f\":(10); " + + "INSERT EDGE like(likeness) values \"c\" -> \"f\":(10);" + + "INSERT EDGE like(likeness) values \"a\" -> \"g\":(10); " + + "INSERT EDGE like(likeness) values \"g\" -> \"c\":(10);"); + Assert.assertTrue(result.getErrorMessage(), result.isSucceeded()); + result = session.execute( + "FIND NOLOOP PATH FROM \"a\" TO \"c\" OVER like BIDIRECT UPTO 5 STEPS"); + Assert.assertTrue(result.getErrorMessage(), result.isSucceeded()); + Assert.assertEquals(4, result.rowsSize()); + String expectString = "ColumnName: [path], " + + "Rows: [(\"a\" )-[:like@0{}]->(\"g\" )-[:like@0{}]->(\"c\" ), " + + "(\"a\" )<-[:like@0{}]-(\"d\" )-[:like@0{}]->(\"c\" ), " + + "(\"a\" )<-[:like@0{}]-(\"b\" )<-[:like@0{}]-(\"c\" ), " + + "(\"a\" )-[:like@0{}]->(\"f\" )<-[:like@0{}]-(\"c\" )]"; + Assert.assertEquals(expectString, result.toString()); + } catch (IOErrorException | InterruptedException e) { + e.printStackTrace(); + assert false; + } + } + @Test public void testErrorResult() { try {