Search in sources :

Example 11 with Value

use of org.neo4j.driver.Value in project neo4j by neo4j.

the class OutputFormatterTest method shouldReportTotalDBHits.

@Test
public void shouldReportTotalDBHits() {
    Value labelScan = buildOperator("NodeByLabelScan", 1002L, 1001L, null);
    Value filter = buildOperator("Filter", 1402, 280, labelScan);
    Value planMap = buildOperator("ProduceResults", 0, 280, filter);
    ProfiledPlan plan = PROFILED_PLAN_FROM_VALUE.apply(planMap);
    ResultSummary summary = new InternalResultSummary(new Query("PROFILE MATCH (n:LABEL) WHERE 20 < n.age < 35 return n"), new InternalServerInfo("agent", new BoltServerAddress("localhost:7687"), ServerVersion.vInDev, BoltProtocolV43.VERSION), new InternalDatabaseInfo("neo4j"), QueryType.READ_ONLY, null, plan, plan, Collections.emptyList(), 39, 55);
    // When
    Map<String, Value> info = OutputFormatter.info(summary);
    // Then
    assertThat(info.get("DbHits").asLong(), equalTo(2404L));
}
Also used : ProfiledPlan(org.neo4j.driver.summary.ProfiledPlan) Query(org.neo4j.driver.Query) InternalDatabaseInfo(org.neo4j.driver.internal.summary.InternalDatabaseInfo) MapValue(org.neo4j.driver.internal.value.MapValue) Value(org.neo4j.driver.Value) ListValue(org.neo4j.driver.internal.value.ListValue) InternalResultSummary(org.neo4j.driver.internal.summary.InternalResultSummary) ResultSummary(org.neo4j.driver.summary.ResultSummary) InternalServerInfo(org.neo4j.driver.internal.summary.InternalServerInfo) InternalResultSummary(org.neo4j.driver.internal.summary.InternalResultSummary) BoltServerAddress(org.neo4j.driver.internal.BoltServerAddress) Test(org.junit.Test)

Example 12 with Value

use of org.neo4j.driver.Value in project neo4j by neo4j.

the class PrettyPrinterTest method printRelationshipsAndNodesWithEscapingForSpecialCharacters.

@Test
public void printRelationshipsAndNodesWithEscapingForSpecialCharacters() {
    // given
    Record record = mock(Record.class);
    Value relVal = mock(Value.class);
    Value nodeVal = mock(Value.class);
    Relationship relationship = mock(Relationship.class);
    HashMap<String, Object> relProp = new HashMap<>();
    relProp.put("prop1", "\"prop1, value\"");
    relProp.put("prop2", "prop2_value");
    Node node = mock(Node.class);
    HashMap<String, Object> nodeProp = new HashMap<>();
    nodeProp.put("prop1", "\"prop1:value\"");
    nodeProp.put("1prop2", "\"\"");
    nodeProp.put("ä", "not-escaped");
    when(relVal.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.RELATIONSHIP());
    when(nodeVal.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.NODE());
    when(relVal.asRelationship()).thenReturn(relationship);
    when(relationship.type()).thenReturn("RELATIONSHIP,TYPE");
    when(relationship.asMap(anyObject())).thenReturn(unmodifiableMap(relProp));
    when(nodeVal.asNode()).thenReturn(node);
    when(node.labels()).thenReturn(asList("label `1", "label2"));
    when(node.asMap(anyObject())).thenReturn(unmodifiableMap(nodeProp));
    when(record.keys()).thenReturn(asList("rel", "node"));
    when(record.values()).thenReturn(asList(relVal, nodeVal));
    BoltResult result = new ListBoltResult(asList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("rel, node" + NEWLINE + "[:`RELATIONSHIP,TYPE` {prop2: prop2_value, prop1: \"prop1, value\"}], " + "(:`label ``1`:label2 {prop1: \"prop1:value\", `1prop2`: \"\", ä: not-escaped})" + NEWLINE));
}
Also used : ListBoltResult(org.neo4j.shell.state.ListBoltResult) HashMap(java.util.HashMap) Relationship(org.neo4j.driver.types.Relationship) Node(org.neo4j.driver.types.Node) Value(org.neo4j.driver.Value) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) Matchers.anyObject(org.mockito.Matchers.anyObject) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Example 13 with Value

use of org.neo4j.driver.Value in project neo4j by neo4j.

the class PrettyPrinterTest method prettyPrintThreeSegmentPath.

@Test
public void prettyPrintThreeSegmentPath() {
    // given
    Record record = mock(Record.class);
    Value value = mock(Value.class);
    Node start = mock(Node.class);
    when(start.labels()).thenReturn(asList("start"));
    when(start.id()).thenReturn(1L);
    Node second = mock(Node.class);
    when(second.labels()).thenReturn(asList("second"));
    when(second.id()).thenReturn(2L);
    Node third = mock(Node.class);
    when(third.labels()).thenReturn(asList("third"));
    when(third.id()).thenReturn(3L);
    Node end = mock(Node.class);
    when(end.labels()).thenReturn(asList("end"));
    when(end.id()).thenReturn(4L);
    Path path = mock(Path.class);
    when(path.start()).thenReturn(start);
    Relationship relationship = mock(Relationship.class);
    when(relationship.type()).thenReturn("RELATIONSHIP_TYPE");
    when(relationship.startNodeId()).thenReturn(1L).thenReturn(3L).thenReturn(3L);
    Path.Segment segment1 = mock(Path.Segment.class);
    when(segment1.start()).thenReturn(start);
    when(segment1.end()).thenReturn(second);
    when(segment1.relationship()).thenReturn(relationship);
    Path.Segment segment2 = mock(Path.Segment.class);
    when(segment2.start()).thenReturn(second);
    when(segment2.end()).thenReturn(third);
    when(segment2.relationship()).thenReturn(relationship);
    Path.Segment segment3 = mock(Path.Segment.class);
    when(segment3.start()).thenReturn(third);
    when(segment3.end()).thenReturn(end);
    when(segment3.relationship()).thenReturn(relationship);
    when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.PATH());
    when(value.asPath()).thenReturn(path);
    when(path.iterator()).thenReturn(asList(segment1, segment2, segment3).iterator());
    when(record.keys()).thenReturn(asList("path"));
    when(record.values()).thenReturn(asList(value));
    BoltResult result = new ListBoltResult(singletonList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("path" + NEWLINE + "(:start)-[:RELATIONSHIP_TYPE]->" + "(:second)<-[:RELATIONSHIP_TYPE]-(:third)-[:RELATIONSHIP_TYPE]->(:end)" + NEWLINE));
}
Also used : Path(org.neo4j.driver.types.Path) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Node(org.neo4j.driver.types.Node) Relationship(org.neo4j.driver.types.Relationship) Value(org.neo4j.driver.Value) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Example 14 with Value

use of org.neo4j.driver.Value in project neo4j by neo4j.

the class TablePlanFormatter method details.

@Nonnull
private Map<String, Cell> details(@Nonnull Plan plan, @Nonnull Map<String, Integer> columns) {
    Map<String, Value> args = plan.arguments();
    Stream<Optional<Pair<String, Cell>>> formattedPlan = args.entrySet().stream().map(e -> {
        Value value = e.getValue();
        switch(e.getKey()) {
            case "EstimatedRows":
                return mapping(ESTIMATED_ROWS, new RightJustifiedCell(format(value.asDouble())), columns);
            case "Rows":
                return mapping(ROWS, new RightJustifiedCell(value.asNumber().toString()), columns);
            case "DbHits":
                return mapping(HITS, new RightJustifiedCell(value.asNumber().toString()), columns);
            case "PageCacheHits":
                return mapping(PAGE_CACHE, new RightJustifiedCell(String.format("%s/%s", value.asNumber(), args.getOrDefault("PageCacheMisses", ZERO_VALUE).asNumber())), columns);
            case "Time":
                return mapping(TIME, new RightJustifiedCell(String.format("%.3f", value.asLong() / 1000000.0d)), columns);
            case "Order":
                return mapping(ORDER, new LeftJustifiedCell(String.format("%s", value.asString())), columns);
            case "Details":
                return mapping(DETAILS, new LeftJustifiedCell(splitDetails(value.asString())), columns);
            case "Memory":
                return mapping(MEMORY, new RightJustifiedCell(String.format("%s", value.asNumber().toString())), columns);
            default:
                return Optional.empty();
        }
    });
    return Stream.concat(formattedPlan, Stream.of(Optional.of(Pair.of(IDENTIFIERS, new LeftJustifiedCell(identifiers(plan, columns)))), Optional.of(Pair.of(OTHER, new LeftJustifiedCell(other(plan, columns)))))).filter(Optional::isPresent).collect(toMap(o -> o.get()._1, o -> o.get()._2));
}
Also used : Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) Plan(org.neo4j.driver.summary.Plan) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) Value(org.neo4j.driver.Value) Values(org.neo4j.driver.Values) List(java.util.List) Matcher(java.util.regex.Matcher) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) OutputFormatter.repeat(org.neo4j.shell.prettyprint.OutputFormatter.repeat) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) NEWLINE(org.neo4j.shell.prettyprint.OutputFormatter.NEWLINE) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Nonnull(javax.annotation.Nonnull) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) Optional(java.util.Optional) Value(org.neo4j.driver.Value) Nonnull(javax.annotation.Nonnull)

Example 15 with Value

use of org.neo4j.driver.Value in project zeppelin by apache.

the class Neo4jCypherInterpreter method runQuery.

private InterpreterResult runQuery(String cypherQuery, InterpreterContext interpreterContext) {
    if (StringUtils.isBlank(cypherQuery)) {
        return new InterpreterResult(Code.SUCCESS);
    }
    try {
        Iterator<Record> result = this.neo4jConnectionManager.execute(cypherQuery, interpreterContext).iterator();
        Set<Node> nodes = new HashSet<>();
        Set<Relationship> relationships = new HashSet<>();
        List<String> columns = new ArrayList<>();
        List<List<String>> lines = new ArrayList<List<String>>();
        while (result.hasNext()) {
            Record record = result.next();
            List<Pair<String, Value>> fields = record.fields();
            List<String> line = new ArrayList<>();
            for (Pair<String, Value> field : fields) {
                if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.NODE())) {
                    nodes.add(field.value().asNode());
                } else if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.RELATIONSHIP())) {
                    relationships.add(field.value().asRelationship());
                } else if (field.value().hasType(InternalTypeSystem.TYPE_SYSTEM.PATH())) {
                    nodes.addAll(Iterables.asList(field.value().asPath().nodes()));
                    relationships.addAll(Iterables.asList(field.value().asPath().relationships()));
                } else {
                    setTabularResult(field.key(), field.value(), columns, line, InternalTypeSystem.TYPE_SYSTEM);
                }
            }
            if (!line.isEmpty()) {
                lines.add(line);
            }
        }
        if (!nodes.isEmpty()) {
            return renderGraph(nodes, relationships);
        } else {
            return renderTable(columns, lines);
        }
    } catch (Exception e) {
        LOGGER.error("Exception while interpreting cypher query", e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    }
}
Also used : Node(org.neo4j.driver.types.Node) ArrayList(java.util.ArrayList) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Relationship(org.neo4j.driver.types.Relationship) Value(org.neo4j.driver.Value) Record(org.neo4j.driver.Record) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Pair(org.neo4j.driver.util.Pair)

Aggregations

Value (org.neo4j.driver.Value)37 Test (org.junit.Test)29 ListBoltResult (org.neo4j.shell.state.ListBoltResult)22 Record (org.neo4j.driver.Record)21 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)16 BoltResult (org.neo4j.shell.state.BoltResult)15 ResultSummary (org.neo4j.driver.summary.ResultSummary)14 HashMap (java.util.HashMap)12 StringContains.containsString (org.hamcrest.core.StringContains.containsString)9 DurationValue (org.neo4j.driver.internal.value.DurationValue)9 NodeValue (org.neo4j.driver.internal.value.NodeValue)9 PathValue (org.neo4j.driver.internal.value.PathValue)9 PointValue (org.neo4j.driver.internal.value.PointValue)9 RelationshipValue (org.neo4j.driver.internal.value.RelationshipValue)9 InternalRecord (org.neo4j.driver.internal.InternalRecord)8 Plan (org.neo4j.driver.summary.Plan)7 Node (org.neo4j.driver.types.Node)7 Relationship (org.neo4j.driver.types.Relationship)7 ProfiledPlan (org.neo4j.driver.summary.ProfiledPlan)6 FloatValue (org.neo4j.driver.internal.value.FloatValue)5