Skip to content

Update examples #1322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@
*/
package org.neo4j.docs.driver;

// tag::async-autocommit-transaction-import[]

import org.neo4j.driver.async.AsyncSession;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;
// end::async-autocommit-transaction-import[]

public class AsyncAutocommitTransactionExample extends BaseApplication {
public AsyncAutocommitTransactionExample(String uri, String user, String password) {
Expand All @@ -35,10 +30,10 @@ public AsyncAutocommitTransactionExample(String uri, String user, String passwor

// tag::async-autocommit-transaction[]
public CompletionStage<List<String>> readProductTitles() {
String query = "MATCH (p:Product) WHERE p.id = $id RETURN p.title";
Map<String, Object> parameters = Collections.singletonMap("id", 0);
var query = "MATCH (p:Product) WHERE p.id = $id RETURN p.title";
var parameters = Map.<String, Object>of("id", 0);

AsyncSession session = driver.asyncSession();
var session = driver.asyncSession();

return session.runAsync(query, parameters)
.thenCompose(cursor -> cursor.listAsync(record -> record.get(0).asString()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,21 @@
*/
package org.neo4j.docs.driver;

// tag::async-result-consume-import[]

import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.Query;

import java.util.List;
import java.util.concurrent.CompletionStage;
// end::async-result-consume-import[]

public class AsyncResultConsumeExample extends BaseApplication {
public AsyncResultConsumeExample(String uri, String user, String password) {
super(uri, user, password);
}

@SuppressWarnings("deprecation")
// tag::async-result-consume[]
public CompletionStage<List<String>> getPeople() {

String query = "MATCH (a:Person) RETURN a.name ORDER BY a.name";
AsyncSession session = driver.asyncSession();
return session.readTransactionAsync(tx -> tx.runAsync(query)
var query = new Query("MATCH (a:Person) RETURN a.name ORDER BY a.name");
var session = driver.asyncSession();
return session.executeReadAsync(tx -> tx.runAsync(query)
.thenCompose(cursor -> cursor.listAsync(record -> record.get(0).asString())));
}
// end::async-result-consume[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
*/
package org.neo4j.docs.driver;

// tag::async-result-consume-import[]

import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.async.AsyncTransaction;
import org.neo4j.driver.async.AsyncTransactionContext;
import org.neo4j.driver.async.ResultCursor;
import org.neo4j.driver.summary.ResultSummary;
import org.neo4j.driver.summary.SummaryCounters;
Expand All @@ -31,42 +28,32 @@
import java.util.concurrent.CompletionStage;

import static org.neo4j.driver.Values.parameters;
// end::async-result-consume-import[]

public class AsyncRunMultipleTransactionExample extends BaseApplication {
public AsyncRunMultipleTransactionExample(String uri, String user, String password) {
super(uri, user, password);
}

@SuppressWarnings("deprecation")
// tag::async-multiple-tx[]
public CompletionStage<Integer> addEmployees(final String companyName) {
AsyncSession session = driver.asyncSession();

return session.readTransactionAsync(AsyncRunMultipleTransactionExample::matchPersonNodes)
.thenCompose(
personNames -> session.writeTransactionAsync(tx -> createNodes(tx, companyName, personNames)));
var session = driver.asyncSession();
return session.executeReadAsync(AsyncRunMultipleTransactionExample::matchPersonNodes)
.thenCompose(personNames -> session.executeWriteAsync(tx -> createNodes(tx, companyName, personNames)));
}

private static CompletionStage<List<String>> matchPersonNodes(AsyncTransaction tx) {
private static CompletionStage<List<String>> matchPersonNodes(AsyncTransactionContext tx) {
return tx.runAsync("MATCH (a:Person) RETURN a.name AS name")
.thenCompose(
cursor -> cursor.listAsync(record -> record.get("name").asString()));
.thenCompose(cursor -> cursor.listAsync(record -> record.get("name").asString()));
}

private static CompletionStage<Integer> createNodes(
AsyncTransaction tx, String companyName, List<String> personNames) {
private static CompletionStage<Integer> createNodes(AsyncTransactionContext tx, String companyName, List<String> personNames) {
return personNames.stream()
.map(personName -> createNode(tx, companyName, personName))
.reduce(
CompletableFuture.completedFuture(0),
(stage1, stage2) -> stage1.thenCombine(stage2, Integer::sum));
.reduce(CompletableFuture.completedFuture(0), (stage1, stage2) -> stage1.thenCombine(stage2, Integer::sum));
}

private static CompletionStage<Integer> createNode(AsyncTransaction tx, String companyName, String personName) {
return tx.runAsync(
"MATCH (emp:Person {name: $person_name}) " + "MERGE (com:Company {name: $company_name}) "
+ "MERGE (emp)-[:WORKS_FOR]->(com)",
private static CompletionStage<Integer> createNode(AsyncTransactionContext tx, String companyName, String personName) {
return tx.runAsync("MATCH (emp:Person {name: $person_name}) MERGE (com:Company {name: $company_name}) MERGE (emp)-[:WORKS_FOR]->(com)",
parameters("person_name", personName, "company_name", companyName))
.thenCompose(ResultCursor::consumeAsync)
.thenApply(ResultSummary::counters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,26 @@
*/
package org.neo4j.docs.driver;

// tag::async-transaction-function-import[]

import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.summary.ResultSummary;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletionStage;
// end::async-transaction-function-import[]

public class AsyncTransactionFunctionExample extends BaseApplication {
public AsyncTransactionFunctionExample(String uri, String user, String password) {
super(uri, user, password);
}

@SuppressWarnings("deprecation")
// tag::async-transaction-function[]
public CompletionStage<ResultSummary> printAllProducts() {
String query = "MATCH (p:Product) WHERE p.id = $id RETURN p.title";
Map<String, Object> parameters = Collections.singletonMap("id", 0);

AsyncSession session = driver.asyncSession();

return session.readTransactionAsync(tx -> tx.runAsync(query, parameters)
return session.executeReadAsync(tx -> tx.runAsync(query, parameters)
.thenCompose(cursor -> cursor.forEachAsync(record ->
// asynchronously print every record
System.out.println(record.get(0).asString()))));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
*/
package org.neo4j.docs.driver;

// tag::autocommit-transaction-import[]

import org.neo4j.driver.Session;

import static org.neo4j.driver.Values.parameters;
// end::autocommit-transaction-import[]

public class AutocommitTransactionExample extends BaseApplication {
public AutocommitTransactionExample(String uri, String user, String password) {
Expand All @@ -32,7 +27,7 @@ public AutocommitTransactionExample(String uri, String user, String password) {

// tag::autocommit-transaction[]
public void addPerson(String name) {
try (Session session = driver.session()) {
try (var session = driver.session()) {
session.run("CREATE (a:Person {name: $name})", parameters("name", name));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package org.neo4j.docs.driver;

// tag::basic-auth-import[]

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Result;
// end::basic-auth-import[]

public class BasicAuthExample implements AutoCloseable {
Expand All @@ -40,7 +40,7 @@ public void close() throws RuntimeException {
}

public boolean canConnect() {
Result result = driver.session().run("RETURN 1");
var result = driver.session().run("RETURN 1");
return result.single().get(0).asInt() == 1;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@
*/
package org.neo4j.docs.driver;

// tag::config-connection-pool-import[]

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Config;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Result;

import java.util.concurrent.TimeUnit;
// end::config-connection-pool-import[]

public class ConfigConnectionPoolExample implements AutoCloseable {
private final Driver driver;

// tag::config-connection-pool[]
public ConfigConnectionPoolExample(String uri, String user, String password) {
Config config = Config.builder()
var config = Config.builder()
.withMaxConnectionLifetime(30, TimeUnit.MINUTES)
.withMaxConnectionPoolSize(50)
.withConnectionAcquisitionTimeout(2, TimeUnit.MINUTES)
Expand All @@ -50,7 +46,7 @@ public void close() throws RuntimeException {
}

public boolean canConnect() {
Result result = driver.session().run("RETURN 1");
var result = driver.session().run("RETURN 1");
return result.single().get(0).asInt() == 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.neo4j.docs.driver;

// tag::config-connection-timeout-import[]

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Config;
import org.neo4j.driver.Driver;
Expand All @@ -33,7 +32,9 @@ public class ConfigConnectionTimeoutExample implements AutoCloseable {

// tag::config-connection-timeout[]
public ConfigConnectionTimeoutExample(String uri, String user, String password) {
Config config = Config.builder().withConnectionTimeout(15, SECONDS).build();
var config = Config.builder()
.withConnectionTimeout(15, SECONDS)
.build();

driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password), config);
}
Expand Down
Loading