/** * @throws SessionExpiredException when server no longer serves writes anymore */ @Nonnull private Optional<BoltResult> getBoltResult(@Nonnull String cypher, @Nonnull Map<String, Object> queryParams) throws SessionExpiredException { StatementResult statementResult = session.run(new Statement(cypher, queryParams)); if (statementResult == null) { return Optional.empty(); } // calling list() is what actually executes cypher on the server List<Record> list = statementResult.list(); return Optional.of(new BoltResult(list, statementResult)); }
public BoltContentHandler(StatementResult statementResult, ResultHandler rh) { if (statementResult != null) this.records = statementResult.list(); // don't use streaming mode else this.records = new ArrayList<Record>(); this.reloaded = new Reloaded(rh.getDbAccess()); }
public BoltContentHandler(StatementResult statementResult, ResultHandler rh) { if (statementResult != null) this.records = statementResult.list(); // don't use streaming mode else this.records = new ArrayList<Record>(); this.reloaded = new Reloaded(rh.getDbAccess()); }
private Optional<List<BoltResult>> captureResults(@Nonnull List<Statement> transactionStatements) { List<BoltResult> results = executeWithRetry(transactionStatements, (statement, transaction) -> { // calling list() is what actually executes cypher on the server StatementResult sr = transaction.run(statement); List<Record> list = sr.list(); return new BoltResult(list, sr); }); clearTransactionStatements(); if (results == null || results.isEmpty()) { return Optional.empty(); } return Optional.of(results); }
public RestModelResponse(StatementResult result, TransactionManager transactionManager, BoltEntityAdapter entityAdapter) { super(result, transactionManager); this.restModelAdapter = new BoltRestModelAdapter(entityAdapter); this.resultProjection = result.list().iterator(); this.statisticsModel = new StatisticsModelAdapter().adapt(result); }
@Override public Iterator<Map<String, Object>> query(String query, Map<String, Object> params) { try (Session session = driver.session()) { List<Map<String, Object>> list = session.run(query, params) .list( r -> r.asMap(BoltCypherExecutor::convert)); return list.iterator(); } }
public RestModelResponse(StatementResult result, TransactionManager transactionManager) { super(result, transactionManager); this.restModelAdapter = new BoltRestModelAdapter(); resultProjection = result.list().iterator(); statisticsModel = new StatisticsModelAdapter().adapt(result); }
StatementResult result = session.run(DUPLICATE_FLOWS_QUERY); Map<String, List<Long>> flowsToUpdate = new HashMap<>(); for (Record record : result.list()) { String flowid = record.get("affected_flow_id").asString(); List<Long> priors = flowsToUpdate.computeIfAbsent(flowid, empty -> new ArrayList<>()); StatementResult result = session.run(DUPLICATE_COOKIES_QUERY); List<String> flowsToUpdate = new ArrayList<>(); for (Record record : result.list()) { flowsToUpdate.add(record.get("affected_flow_id").asString());
private StatementResult mockResult(List<String> cols, Object... data) { StatementResult result = mock(StatementResult.class); Statement statement = mock(Statement.class); ResultSummary summary = mock(ResultSummary.class); when(summary.statement()).thenReturn(statement); when(result.keys()).thenReturn(cols); List<Record> records = new ArrayList<>(); List<Object> input = asList(data); int width = cols.size(); for (int row = 0; row < input.size() / width; row++) { records.add(record(cols, input.subList(row * width, (row + 1) * width))); } when(result.list()).thenReturn(records); when(result.consume()).thenReturn(summary); when(result.summary()).thenReturn(summary); return result; }
@Test public void map() { List<Map<String, Object>> results = asList( getRow(1), getRow(2) ); StatementResult statementResult = new GremlinServerStatementResult(serverInfo, statement, results.iterator(), converter); assertThat(statementResult.list(r -> r.get(KEY2).asString())) .containsExactly("value1", "value2"); }
@Test public void triesAgainOnSessionExpired() throws Exception { Session sessionMock = mock(Session.class); StatementResult versionMock = mock(StatementResult.class); StatementResult resultMock = mock(StatementResult.class); Record recordMock = mock(Record.class); Value valueMock = mock(Value.class); Driver driverMock = stubVersionInAnOpenSession(versionMock, sessionMock, "neo4j-version"); when(resultMock.list()).thenReturn(asList(recordMock)); when(valueMock.toString()).thenReturn("999"); when(recordMock.get(0)).thenReturn(valueMock); when(sessionMock.run(any(Statement.class))) .thenThrow(new SessionExpiredException("leaderswitch")) .thenReturn(resultMock); OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock); boltStateHandler.connect(); BoltResult boltResult = boltStateHandler.runCypher("RETURN 999", new HashMap<>()).get(); verify(driverMock, times(2)).session(any(), anyString()); verify(sessionMock, times(2)).run(any(Statement.class)); assertEquals("999", boltResult.getRecords().get(0).get(0).toString()); }
@Test public void shouldRunCypherQuery() throws CommandException { Session sessionMock = mock(Session.class); StatementResult versionMock = mock(StatementResult.class); StatementResult resultMock = mock(StatementResult.class); Record recordMock = mock(Record.class); Value valueMock = mock(Value.class); Driver driverMock = stubVersionInAnOpenSession(versionMock, sessionMock, "neo4j-version"); when(resultMock.list()).thenReturn(asList(recordMock)); when(valueMock.toString()).thenReturn("999"); when(recordMock.get(0)).thenReturn(valueMock); when(sessionMock.run(any(Statement.class))).thenReturn(resultMock); OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock); boltStateHandler.connect(); BoltResult boltResult = boltStateHandler.runCypher("RETURN 999", new HashMap<>()).get(); verify(sessionMock).run(any(Statement.class)); assertEquals("999", boltResult.getRecords().get(0).get(0).toString()); }
private String formatResult(StatementResult result) { // calling list() is what actually executes cypher on the server List<Record> list = result.list(); return new TableOutputFormatter().format(new BoltResult(list, result)); }
@Test public void create() { List<Map<String, Object>> results = asList( getRow(1), getRow(2), getRow(3) ); StatementResult statementResult = new GremlinServerStatementResult(serverInfo, statement, results.iterator(), converter); assertThat(statementResult.keys()).containsExactly(KEY1, KEY2); assertThat(statementResult.peek().get(KEY1).asInt()).isEqualTo(1); assertThat(statementResult.hasNext()).isTrue(); assertThat(statementResult.next().get(KEY1).asInt()).isEqualTo(1); assertThat(statementResult.hasNext()).isTrue(); assertThat(statementResult.next().get(KEY1).asInt()).isEqualTo(2); assertThat(statementResult.hasNext()).isTrue(); //see org.neo4j.driver.internal.InternalStatementResult#list() assertThat(statementResult.list()).hasSize(1); assertThat(statementResult.hasNext()).isFalse(); assertThat(statementResult.summary().server()).isEqualTo(serverInfo); }
@Test public void tinkerGraph() { TinkerGraph tinkerGraph = TinkerFactory.createModern(); GraphTraversalSource graphTraversalSource = tinkerGraph.traversal(); GremlinGraphDriver driver = new GremlinGraphDriver(graphTraversalSource); List<Map<String, Object>> results; try (Session session = driver.session()) { StatementResult result = session.run( "MATCH (n:person) " + "WHERE n.age = $age " + "RETURN n.name", parameters("age", 29) ); results = result.list().stream() .map(Record::asMap) .collect(toList()); } assertThat(results) .extracting("n.name") .containsExactly("marko"); } }
@Override public GraphQueryResult execute(String query, Map<String, Object> statementParameters) { try { Driver driver = GraphDatabase.driver(url, auth); try { try (Session session = driver.session()) { Neo4jBoltBuffer buffer = new Neo4jBoltBuffer(); long startTime = System.currentTimeMillis(); StatementResult statementResult = session.run(query, statementParameters); buffer.addColumns(statementResult.keys()); for (Record record : statementResult.list()) { // Add row buffer.addRow(record.asMap()); } buffer.addResultSummary(statementResult.consume()); long endTime = System.currentTimeMillis(); return new Neo4jBoltQueryResult(endTime - startTime, buffer); } } finally { driver.closeAsync(); } } catch (UnresolvedAddressException e) { throw new ClientException(e.getMessage()); } }
@Test public void multipleRows() { Cluster cluster = Cluster.build() .addContactPoints("localhost") .port(server.getPort()) .create(); Driver driver = GremlinDatabase.driver(cluster); try (Session session = driver.session()) { StatementResult result = session.run("MATCH (p:person) RETURN p.name, p.age"); List<String> rows = result.list(r -> r.get("p.name").asString() + r.get("p.age").asInt()); assertThat(rows).containsExactly("marko29", "vadas27", "josh32", "peter35"); } }
Stream<Relationship> result = session.run(String.format(querySourceStates, entityA.id())).list().stream() .map(r -> r.get("r").asRelationship());
@Test public void ignoreIds() throws ExecutionException, InterruptedException { gremlinServer.gremlinClient().submit("g.addV('stringId1').property(id, 'string1')").all().get(); gremlinServer.gremlinClient().submit("g.addV('stringId1').property(id, 'string2')").all().get(); // freshReadmeSnippet: ignoreIds Config config = Config.build() .ignoreIds() .toConfig(); // freshReadmeSnippet: ignoreIds Driver driver = GremlinDatabase.driver("//localhost:" + gremlinServer.getPort(), config); try (Session session = driver.session()) { StatementResult result = session.run("MATCH (n:stringId1) RETURN n"); assertThat(result.list()) .extracting(r -> r.get("n").asNode().id()) .containsExactly(-1L, -1L); } }