Skip to content
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 @@ -17,24 +17,24 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;

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

import org.neo4j.driver.async.AsyncSession;

// end::async-autocommit-transaction-import[]
public class AsyncAutocommitTransactionExample extends BaseApplication
{
public AsyncAutocommitTransactionExample( String uri, String user, String password )
{
super( uri, user, password );
}

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

Expand All @@ -49,6 +49,6 @@ public CompletionStage<List<String>> readProductTitles()
return Collections.emptyList();
} )
.thenCompose( titles -> session.closeAsync().thenApply( ignore -> titles ) );
// end::async-autocommit-transaction[]
}
// end::async-autocommit-transaction[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2002-2019 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.docs.driver;

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

import java.util.List;
import java.util.concurrent.CompletionStage;

import org.neo4j.driver.async.AsyncSession;
// end::async-result-consume-import[]

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

// 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 )
.thenCompose( cursor -> cursor.listAsync( record ->
record.get( 0 ).asString() ) )
);
}
// end::async-result-consume[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;

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

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

// end::async-transaction-function-import[]
public class AsyncTransactionFunctionExample extends BaseApplication
{
public AsyncTransactionFunctionExample( String uri, String user, String password )
{
super( uri, user, password );
}

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

Expand All @@ -46,6 +46,6 @@ public CompletionStage<ResultSummary> printAllProducts()
// asynchronously print every record
System.out.println( record.get( 0 ).asString() ) ) )
);
// end::async-transaction-function[]
}
// end::async-transaction-function[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;

// tag::async-unmanaged-transaction-import[]
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletionStage;
Expand All @@ -26,6 +26,7 @@
import org.neo4j.driver.async.AsyncSession;
import org.neo4j.driver.async.AsyncTransaction;
import org.neo4j.driver.async.ResultCursor;
// end::async-unmanaged-transaction-import[]

public class AsyncUnmanagedTransactionExample extends BaseApplication
{
Expand All @@ -34,9 +35,9 @@ public AsyncUnmanagedTransactionExample(String uri, String user, String password
super( uri, user, password );
}

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

Expand Down Expand Up @@ -69,6 +70,6 @@ public CompletionStage<Void> printSingleProduct()
return null;
} )
.thenCompose( ignore -> session.closeAsync() );
// end::async-unmanaged-transaction[]
}
// end::async-unmanaged-transaction[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ public void addPerson( String name )
}
}
// end::autocommit-transaction[]

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;

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

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;

// end::config-connection-pool-import[]
public class ConfigConnectionPoolExample implements AutoCloseable
{
private final Driver driver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*/
package org.neo4j.docs.driver;

// tag::config-custom-resolver-import[]
import java.util.Arrays;
import java.util.HashSet;

Expand All @@ -32,7 +32,7 @@

import static org.neo4j.driver.Values.parameters;
import static org.neo4j.driver.SessionConfig.builder;

// end::config-custom-resolver-import[]
public class ConfigCustomResolverExample implements AutoCloseable
{
private final Driver driver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import java.util.ArrayList;
import java.util.List;

import org.neo4j.driver.Session;
import org.neo4j.driver.Result;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.TransactionWork;
import org.neo4j.driver.Session;
// end::result-consume-import[]

public class ResultConsumeExample extends BaseApplication
Expand All @@ -41,27 +39,16 @@ public List<String> getPeople()
{
try ( Session session = driver.session() )
{
return session.readTransaction( new TransactionWork<List<String>>()
{
@Override
public List<String> execute( Transaction tx )
return session.readTransaction( tx -> {
List<String> names = new ArrayList<>();
Result result = tx.run( "MATCH (a:Person) RETURN a.name ORDER BY a.name" );
while ( result.hasNext() )
{
return matchPersonNodes( tx );
names.add( result.next().get( 0 ).asString() );
}
return names;
} );
}
}

private static List<String> matchPersonNodes( Transaction tx )
{
List<String> names = new ArrayList<>();
Result result = tx.run( "MATCH (a:Person) RETURN a.name ORDER BY a.name" );
while ( result.hasNext() )
{
names.add( result.next().get( 0 ).asString() );
}
return names;
}
// end::result-consume[]

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package org.neo4j.docs.driver;

import io.reactivex.Flowable;
// tag::rx-autocommit-transaction-import[]
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.Map;

import org.neo4j.driver.reactive.RxSession;
// end::rx-autocommit-transaction-import[]

public class RxAutocommitTransactionExample extends BaseApplication
{
Expand All @@ -34,21 +36,21 @@ public RxAutocommitTransactionExample( String uri, String user, String password
super( uri, user, password );
}

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

return Flux.usingWhen( Mono.fromSupplier( driver::rxSession ),
session -> Flux.from( session.run( query, parameters ).records() ).map( record -> record.get( 0 ).asString() ),
RxSession::close );
// end::reactor-autocommit-transaction[]
}
// end::rx-autocommit-transaction[]

// tag::RxJava-autocommit-transaction[]
public Flowable<String> readProductTitlesRxJava()
{
// tag::RxJava-autocommit-transaction[]
String query = "MATCH (p:Product) WHERE p.id = $id RETURN p.title";
Map<String,Object> parameters = Collections.singletonMap( "id", 0 );

Expand All @@ -59,6 +61,6 @@ public Flowable<String> readProductTitlesRxJava()
// We still rethrows the original error here. In a real application, you may want to handle the error directly here.
return Flowable.<String>fromPublisher( session.close() ).concatWith( Flowable.error( error ) );
} );
// end::RxJava-autocommit-transaction[]
}
// end::RxJava-autocommit-transaction[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2002-2019 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.docs.driver;

import io.reactivex.Flowable;
// tag::rx-result-consume-import[]
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.Map;

import org.neo4j.driver.reactive.RxResult;
import org.neo4j.driver.reactive.RxSession;
// end::rx-result-consume-import[]

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

// tag::rx-result-consume[]
public Flux<String> getPeople()
{
String query = "MATCH (a:Person) RETURN a.name ORDER BY a.name";

return Flux.usingWhen( Mono.fromSupplier( driver::rxSession ),
session -> session.readTransaction( tx -> {
RxResult result = tx.run( query );
return Flux.from( result.records() )
.map( record -> record.get( 0 ).asString() );
}
), RxSession::close );
}
// end::rx-result-consume[]

// tag::RxJava-result-consume[]
public Flowable<String> getPeopleRxJava()
{
String query = "MATCH (a:Person) RETURN a.name ORDER BY a.name";
Map<String,Object> parameters = Collections.singletonMap( "id", 0 );

RxSession session = driver.rxSession();
return Flowable.fromPublisher( session.readTransaction( tx -> {
RxResult result = tx.run( query, parameters );
return Flowable.fromPublisher( result.records() )
.map( record -> record.get( 0 ).asString() );
} ) ).onErrorResumeNext( error -> {
// We rollback and rethrow the error. For a real application, you may want to handle the error directly here
return Flowable.<String>fromPublisher( session.close() ).concatWith( Flowable.error( error ) );
} );
}
// end::RxJava-result-consume[]
}
Loading