Skip to content

Commit d03d93e

Browse files
authored
Merge pull request #660 from zhenlineo/4.0-doc-examples
Adding example tags in the example files for driver documentation
2 parents 7c65cea + e45e854 commit d03d93e

15 files changed

+221
-73
lines changed

examples/src/main/java/org/neo4j/docs/driver/AsyncAutocommitTransactionExample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@
1717
* limitations under the License.
1818
*/
1919
package org.neo4j.docs.driver;
20-
20+
// tag::async-autocommit-transaction-import[]
2121
import java.util.Collections;
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.concurrent.CompletionStage;
2525

2626
import org.neo4j.driver.async.AsyncSession;
27-
27+
// end::async-autocommit-transaction-import[]
2828
public class AsyncAutocommitTransactionExample extends BaseApplication
2929
{
3030
public AsyncAutocommitTransactionExample( String uri, String user, String password )
3131
{
3232
super( uri, user, password );
3333
}
3434

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

@@ -49,6 +49,6 @@ public CompletionStage<List<String>> readProductTitles()
4949
return Collections.emptyList();
5050
} )
5151
.thenCompose( titles -> session.closeAsync().thenApply( ignore -> titles ) );
52-
// end::async-autocommit-transaction[]
5352
}
53+
// end::async-autocommit-transaction[]
5454
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.docs.driver;
20+
21+
// tag::async-result-consume-import[]
22+
23+
import java.util.List;
24+
import java.util.concurrent.CompletionStage;
25+
26+
import org.neo4j.driver.async.AsyncSession;
27+
// end::async-result-consume-import[]
28+
29+
public class AsyncResultConsumeExample extends BaseApplication
30+
{
31+
public AsyncResultConsumeExample( String uri, String user, String password )
32+
{
33+
super( uri, user, password );
34+
}
35+
36+
// tag::async-result-consume[]
37+
public CompletionStage<List<String>> getPeople()
38+
{
39+
40+
String query = "MATCH (a:Person) RETURN a.name ORDER BY a.name";
41+
AsyncSession session = driver.asyncSession();
42+
return session.readTransactionAsync( tx ->
43+
tx.runAsync( query )
44+
.thenCompose( cursor -> cursor.listAsync( record ->
45+
record.get( 0 ).asString() ) )
46+
);
47+
}
48+
// end::async-result-consume[]
49+
}

examples/src/main/java/org/neo4j/docs/driver/AsyncTransactionFunctionExample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@
1717
* limitations under the License.
1818
*/
1919
package org.neo4j.docs.driver;
20-
20+
// tag::async-transaction-function-import[]
2121
import java.util.Collections;
2222
import java.util.Map;
2323
import java.util.concurrent.CompletionStage;
2424

2525
import org.neo4j.driver.async.AsyncSession;
2626
import org.neo4j.driver.summary.ResultSummary;
27-
27+
// end::async-transaction-function-import[]
2828
public class AsyncTransactionFunctionExample extends BaseApplication
2929
{
3030
public AsyncTransactionFunctionExample( String uri, String user, String password )
3131
{
3232
super( uri, user, password );
3333
}
3434

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

@@ -46,6 +46,6 @@ public CompletionStage<ResultSummary> printAllProducts()
4646
// asynchronously print every record
4747
System.out.println( record.get( 0 ).asString() ) ) )
4848
);
49-
// end::async-transaction-function[]
5049
}
50+
// end::async-transaction-function[]
5151
}

examples/src/main/java/org/neo4j/docs/driver/AsyncUnmanagedTransactionExample.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* limitations under the License.
1818
*/
1919
package org.neo4j.docs.driver;
20-
20+
// tag::async-unmanaged-transaction-import[]
2121
import java.util.Collections;
2222
import java.util.Map;
2323
import java.util.concurrent.CompletionStage;
@@ -26,6 +26,7 @@
2626
import org.neo4j.driver.async.AsyncSession;
2727
import org.neo4j.driver.async.AsyncTransaction;
2828
import org.neo4j.driver.async.ResultCursor;
29+
// end::async-unmanaged-transaction-import[]
2930

3031
public class AsyncUnmanagedTransactionExample extends BaseApplication
3132
{
@@ -34,9 +35,9 @@ public AsyncUnmanagedTransactionExample(String uri, String user, String password
3435
super( uri, user, password );
3536
}
3637

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

@@ -69,6 +70,6 @@ public CompletionStage<Void> printSingleProduct()
6970
return null;
7071
} )
7172
.thenCompose( ignore -> session.closeAsync() );
72-
// end::async-unmanaged-transaction[]
7373
}
74+
// end::async-unmanaged-transaction[]
7475
}

examples/src/main/java/org/neo4j/docs/driver/AutocommitTransactionExample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,4 @@ public void addPerson( String name )
4141
}
4242
}
4343
// end::autocommit-transaction[]
44-
4544
}

examples/src/main/java/org/neo4j/docs/driver/ConfigConnectionPoolExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
* limitations under the License.
1818
*/
1919
package org.neo4j.docs.driver;
20-
20+
// tag::config-connection-pool-import[]
2121
import java.util.concurrent.TimeUnit;
2222

2323
import org.neo4j.driver.AuthTokens;
2424
import org.neo4j.driver.Config;
2525
import org.neo4j.driver.Driver;
2626
import org.neo4j.driver.GraphDatabase;
2727
import org.neo4j.driver.Result;
28-
28+
// end::config-connection-pool-import[]
2929
public class ConfigConnectionPoolExample implements AutoCloseable
3030
{
3131
private final Driver driver;

examples/src/main/java/org/neo4j/docs/driver/ConfigCustomResolverExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* limitations under the License.
1818
*/
1919
package org.neo4j.docs.driver;
20-
20+
// tag::config-custom-resolver-import[]
2121
import java.util.Arrays;
2222
import java.util.HashSet;
2323

@@ -32,7 +32,7 @@
3232

3333
import static org.neo4j.driver.Values.parameters;
3434
import static org.neo4j.driver.SessionConfig.builder;
35-
35+
// end::config-custom-resolver-import[]
3636
public class ConfigCustomResolverExample implements AutoCloseable
3737
{
3838
private final Driver driver;

examples/src/main/java/org/neo4j/docs/driver/ResultConsumeExample.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import java.util.ArrayList;
2424
import java.util.List;
2525

26-
import org.neo4j.driver.Session;
2726
import org.neo4j.driver.Result;
28-
import org.neo4j.driver.Transaction;
29-
import org.neo4j.driver.TransactionWork;
27+
import org.neo4j.driver.Session;
3028
// end::result-consume-import[]
3129

3230
public class ResultConsumeExample extends BaseApplication
@@ -41,27 +39,16 @@ public List<String> getPeople()
4139
{
4240
try ( Session session = driver.session() )
4341
{
44-
return session.readTransaction( new TransactionWork<List<String>>()
45-
{
46-
@Override
47-
public List<String> execute( Transaction tx )
42+
return session.readTransaction( tx -> {
43+
List<String> names = new ArrayList<>();
44+
Result result = tx.run( "MATCH (a:Person) RETURN a.name ORDER BY a.name" );
45+
while ( result.hasNext() )
4846
{
49-
return matchPersonNodes( tx );
47+
names.add( result.next().get( 0 ).asString() );
5048
}
49+
return names;
5150
} );
5251
}
5352
}
54-
55-
private static List<String> matchPersonNodes( Transaction tx )
56-
{
57-
List<String> names = new ArrayList<>();
58-
Result result = tx.run( "MATCH (a:Person) RETURN a.name ORDER BY a.name" );
59-
while ( result.hasNext() )
60-
{
61-
names.add( result.next().get( 0 ).asString() );
62-
}
63-
return names;
64-
}
6553
// end::result-consume[]
66-
6754
}

examples/src/main/java/org/neo4j/docs/driver/RxAutocommitTransactionExample.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
package org.neo4j.docs.driver;
2020

2121
import io.reactivex.Flowable;
22+
// tag::rx-autocommit-transaction-import[]
2223
import reactor.core.publisher.Flux;
2324
import reactor.core.publisher.Mono;
2425

2526
import java.util.Collections;
2627
import java.util.Map;
2728

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

3032
public class RxAutocommitTransactionExample extends BaseApplication
3133
{
@@ -34,21 +36,21 @@ public RxAutocommitTransactionExample( String uri, String user, String password
3436
super( uri, user, password );
3537
}
3638

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

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

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

@@ -59,6 +61,6 @@ public Flowable<String> readProductTitlesRxJava()
5961
// We still rethrows the original error here. In a real application, you may want to handle the error directly here.
6062
return Flowable.<String>fromPublisher( session.close() ).concatWith( Flowable.error( error ) );
6163
} );
62-
// end::RxJava-autocommit-transaction[]
6364
}
65+
// end::RxJava-autocommit-transaction[]
6466
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.docs.driver;
20+
21+
import io.reactivex.Flowable;
22+
// tag::rx-result-consume-import[]
23+
import reactor.core.publisher.Flux;
24+
import reactor.core.publisher.Mono;
25+
26+
import java.util.Collections;
27+
import java.util.Map;
28+
29+
import org.neo4j.driver.reactive.RxResult;
30+
import org.neo4j.driver.reactive.RxSession;
31+
// end::rx-result-consume-import[]
32+
33+
public class RxResultConsumeExample extends BaseApplication
34+
{
35+
public RxResultConsumeExample( String uri, String user, String password )
36+
{
37+
super( uri, user, password );
38+
}
39+
40+
// tag::rx-result-consume[]
41+
public Flux<String> getPeople()
42+
{
43+
String query = "MATCH (a:Person) RETURN a.name ORDER BY a.name";
44+
45+
return Flux.usingWhen( Mono.fromSupplier( driver::rxSession ),
46+
session -> session.readTransaction( tx -> {
47+
RxResult result = tx.run( query );
48+
return Flux.from( result.records() )
49+
.map( record -> record.get( 0 ).asString() );
50+
}
51+
), RxSession::close );
52+
}
53+
// end::rx-result-consume[]
54+
55+
// tag::RxJava-result-consume[]
56+
public Flowable<String> getPeopleRxJava()
57+
{
58+
String query = "MATCH (a:Person) RETURN a.name ORDER BY a.name";
59+
Map<String,Object> parameters = Collections.singletonMap( "id", 0 );
60+
61+
RxSession session = driver.rxSession();
62+
return Flowable.fromPublisher( session.readTransaction( tx -> {
63+
RxResult result = tx.run( query, parameters );
64+
return Flowable.fromPublisher( result.records() )
65+
.map( record -> record.get( 0 ).asString() );
66+
} ) ).onErrorResumeNext( error -> {
67+
// We rollback and rethrow the error. For a real application, you may want to handle the error directly here
68+
return Flowable.<String>fromPublisher( session.close() ).concatWith( Flowable.error( error ) );
69+
} );
70+
}
71+
// end::RxJava-result-consume[]
72+
}

0 commit comments

Comments
 (0)