private Map<String, Object> toMap(SummaryCounters resultSummary) { return map( "nodesCreated", resultSummary.nodesCreated(), "nodesDeleted", resultSummary.nodesDeleted(), "labelsAdded", resultSummary.labelsAdded(), "labelsRemoved", resultSummary.labelsRemoved(), "relationshipsCreated", resultSummary.relationshipsCreated(), "relationshipsDeleted", resultSummary.relationshipsDeleted(), "propertiesSet", resultSummary.propertiesSet(), "constraintsAdded", resultSummary.constraintsAdded(), "constraintsRemoved", resultSummary.constraintsRemoved(), "indexesAdded", resultSummary.indexesAdded(), "indexesRemoved", resultSummary.indexesRemoved() ); }
@Procedure() @Description("apoc.bolt.load(url-or-key, kernelTransaction, params, config) - access to other databases via bolt for read") public Stream<RowResult> load(@Name("url") String url, @Name("kernelTransaction") String statement, @Name(value = "params", defaultValue = "{}") Map<String, Object> params, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) throws URISyntaxException { if (params == null) params = Collections.emptyMap(); if (config == null) config = Collections.emptyMap(); boolean virtual = (boolean) config.getOrDefault("virtual", false); boolean addStatistics = (boolean) config.getOrDefault("statistics", false); boolean readOnly = (boolean) config.getOrDefault("readOnly", true); Config driverConfig = toDriverConfig(config.getOrDefault("driverConfig", map())); UriResolver uri = new UriResolver(url, "bolt"); uri.initialize(); try (Driver driver = GraphDatabase.driver(uri.getConfiguredUri(), uri.getToken(), driverConfig); Session session = driver.session()) { if (addStatistics) return Stream.of(new RowResult(toMap(runStatement(statement, session, params, readOnly).summary().counters()))); else return getRowResultStream(virtual, session, params, statement, readOnly); } catch (Exception e) { throw new RuntimeException("It's not possible to create a connection due to: " + e.getMessage()); } }
@Nonnull static Map<String, Value> info(@Nonnull ResultSummary summary) { Map<String, Value> result = new LinkedHashMap<>(); if (!summary.hasPlan()) return result; Plan plan = summary.plan(); result.put("Plan", Values.value(summary.hasProfile() ? "PROFILE" : "EXPLAIN")); result.put("Statement", Values.value(summary.statementType().name())); Map<String, Value> arguments = plan.arguments(); Value defaultValue = Values.value(""); for (String key : INFO) { Value value = arguments.getOrDefault(key, arguments.getOrDefault(key.toLowerCase(), defaultValue)); result.put(key, value); } result.put("Time", Values.value(summary.resultAvailableAfter(MILLISECONDS)+summary.resultConsumedAfter(MILLISECONDS))); if ( summary.hasProfile() ) result.put( "DbHits", Values.value( collectHits( summary.profile() ) ) ); if (summary.hasProfile()) result.put("Rows", Values.value( summary.profile().records() )); return result; }
@Test public void returnStatisticsForDefaultFormatting() throws Exception { // given ResultSummary resultSummary = mock(ResultSummary.class); SummaryCounters summaryCounters = mock(SummaryCounters.class); when(resultSummary.counters()).thenReturn(summaryCounters); when(summaryCounters.labelsAdded()).thenReturn(1); when(summaryCounters.nodesCreated()).thenReturn(10); // when String actual = new StatisticsCollector(Format.VERBOSE).collect(resultSummary); // then assertThat(actual, is("Added 10 nodes, Added 1 labels")); }
private void profileToString(StringBuilder sb, ProfiledPlan profile, int depth) { for (ProfiledPlan childProfile : profile.children()) { profileToString(sb, childProfile, depth + 1); } String line = repeat("-", depth); sb.append(line) .append(format("> %s[records: %s; dbHits: %s] {identifiers: %s; arguments: %s}\n", profile.operatorType(), profile.records(), profile.dbHits(), profile.identifiers(), profile.arguments())); }
public Optional<GraphQueryPlan> getQueryPlan() { if (!hasPlan()) { return Optional.empty(); } Plan plan = resultSummary.plan(); return Optional.of(new Neo4jBoltQueryPlan(plan.operatorType(), getArguments(plan), plan.identifiers(), getPlanChildren(plan.children()))); }
private void planToString(StringBuilder sb, Plan plan, int depth) { for (Plan childPlan : plan.children()) { planToString(sb, childPlan, depth + 1); } String line = repeat("-", depth); sb.append(line) .append(format("> %s {identifiers: %s; arguments: %s}\n", plan.operatorType(), plan.identifiers(), plan.arguments())); }
private static List<Neo4jBoltQueryPlan> getPlanChildren(List<? extends Plan> childrenPlans) { return childrenPlans.stream() .map(plan -> new Neo4jBoltQueryPlan(plan.operatorType(), getArguments(plan), plan.identifiers(), getPlanChildren(plan.children()))) .collect(toList()); }
public List<GraphQueryNotification> getNotifications() { if (resultSummary == null) { return Collections.emptyList(); } if (notifications != null) { return notifications; } notifications = resultSummary.notifications().stream() .map(notification -> new Neo4jBoltQueryNotification(notification.title(), notification.description(), Optional.ofNullable(notification.position()) .map(InputPosition::offset) .orElse(null))) .collect(toList()); return notifications; }
@Nonnull public String formatFooter(@Nonnull BoltResult result) { int rows = result.getRecords().size(); ResultSummary summary = result.getSummary(); return String.format("%d row%s available after %d ms, " + "consumed after another %d ms", rows, rows != 1 ? "s" : "", summary.resultAvailableAfter(MILLISECONDS), summary.resultConsumedAfter(MILLISECONDS)); }
private static StatementType extractStatementType( Map<String,Value> metadata ) { Value typeValue = metadata.get( "type" ); if ( typeValue != null ) { return StatementType.fromCode( typeValue.asString() ); } return null; }
private int updatesCount(ResultSummary summary) { int updates = 0; if ( summary.counters().containsUpdates() ) { updates += summary.counters().constraintsAdded(); updates += summary.counters().constraintsRemoved(); updates += summary.counters().nodesCreated(); updates += summary.counters().nodesDeleted(); updates += summary.counters().relationshipsCreated(); updates += summary.counters().relationshipsDeleted(); updates += summary.counters().labelsAdded(); updates += summary.counters().labelsRemoved(); updates += summary.counters().indexesAdded(); updates += summary.counters().indexesRemoved(); updates += summary.counters().propertiesSet(); } return updates; }
@Test public void returnStatisticsForEmptyRecords() throws Exception { // given ResultSummary resultSummary = mock(ResultSummary.class); SummaryCounters summaryCounters = mock(SummaryCounters.class); BoltResult result = mock(BoltResult.class); when(result.getRecords()).thenReturn(Collections.emptyList()); when(result.getSummary()).thenReturn(resultSummary); when(resultSummary.counters()).thenReturn(summaryCounters); when(summaryCounters.labelsAdded()).thenReturn(1); when(summaryCounters.nodesCreated()).thenReturn(10); // when String actual = verbosePrinter.format(result); // then assertThat(actual, containsString("Added 10 nodes, Added 1 labels")); }
@Override public QueryStatisticsModel adapt(StatementResult result) { QueryStatisticsModel queryStatisticsModel = new QueryStatisticsModel(); SummaryCounters stats = result.consume().counters(); queryStatisticsModel.setContains_updates(stats.containsUpdates()); queryStatisticsModel.setNodes_created(stats.nodesCreated()); queryStatisticsModel.setNodes_deleted(stats.nodesDeleted()); queryStatisticsModel.setProperties_set(stats.propertiesSet()); queryStatisticsModel.setRelationships_created(stats.relationshipsCreated()); queryStatisticsModel.setRelationship_deleted(stats.relationshipsDeleted()); queryStatisticsModel.setLabels_added(stats.labelsAdded()); queryStatisticsModel.setLabels_removed(stats.labelsRemoved()); queryStatisticsModel.setIndexes_added(stats.indexesAdded()); queryStatisticsModel.setIndexes_removed(stats.indexesRemoved()); queryStatisticsModel.setConstraints_added(stats.constraintsAdded()); queryStatisticsModel.setConstraints_removed(stats.constraintsRemoved()); return queryStatisticsModel; } }
private Driver stubVersionInAnOpenSession(StatementResult versionMock, Session sessionMock, String value) { Driver driverMock = mock(Driver.class); ResultSummary resultSummary = mock(ResultSummary.class); ServerInfo serverInfo = mock(ServerInfo.class); when(resultSummary.server()).thenReturn(serverInfo); when(serverInfo.version()).thenReturn(value); when(versionMock.summary()).thenReturn(resultSummary); when(sessionMock.isOpen()).thenReturn(true); when(sessionMock.run("RETURN 1")).thenReturn(versionMock); when(driverMock.session(any(), anyString())).thenReturn(sessionMock); return driverMock; }
@Override public QueryStatisticsModel adapt(StatementResult result) { QueryStatisticsModel queryStatisticsModel = new QueryStatisticsModel(); SummaryCounters stats = result.consume().counters(); queryStatisticsModel.setContains_updates(stats.containsUpdates()); queryStatisticsModel.setNodes_created(stats.nodesCreated()); queryStatisticsModel.setNodes_deleted(stats.nodesDeleted()); queryStatisticsModel.setProperties_set(stats.propertiesSet()); queryStatisticsModel.setRelationships_created(stats.relationshipsCreated()); queryStatisticsModel.setRelationship_deleted(stats.relationshipsDeleted()); queryStatisticsModel.setLabels_added(stats.labelsAdded()); queryStatisticsModel.setLabels_removed(stats.labelsRemoved()); queryStatisticsModel.setIndexes_added(stats.indexesAdded()); queryStatisticsModel.setIndexes_removed(stats.indexesRemoved()); queryStatisticsModel.setConstraints_added(stats.constraintsAdded()); queryStatisticsModel.setConstraints_removed(stats.constraintsRemoved()); return queryStatisticsModel; } }