1 package org.neo4j.driver;
2
3 import org.neo4j.driver.exception.Neo4jClientException;
4 import org.neo4j.driver.v1.*;
5
6 import java.util.Spliterator;
7 import java.util.stream.Stream;
8 import java.util.stream.StreamSupport;
9
10 import static java.util.Spliterators.spliterator;
11
12
13
14
15 public class Neo4jClient {
16
17
18
19
20 private static Neo4jClient client;
21
22
23
24
25 private Driver driver;
26
27
28
29
30
31 private Neo4jClient() throws Exception {
32
33 Configuration config = new Configuration();
34
35
36 this.driver = GraphDatabase.driver(config.getStringOrDefault("neo4j.url", "bolt://localhost"),
37 AuthTokens.basic(config.getStringOrDefault("neo4j.user", "neo4j"), config.getStringOrDefault("neo4j.password", "neo4j")),
38 config.toDriverConfig());
39 }
40
41
42
43
44
45
46 private static synchronized Neo4jClient getInstance() {
47 if (client == null) {
48 try {
49 client = new Neo4jClient();
50 } catch (Exception e) {
51
52
53 throw new RuntimeException(e);
54 }
55 }
56
57 return client;
58 }
59
60
61
62
63
64 public static void destroy() {
65 if (client != null) {
66 client.driver.close();
67 client = null;
68 }
69 }
70
71
72
73
74
75
76
77
78
79 private static Stream<Record> run(String query, Value parameters, AccessMode mode, String bookmarkId) {
80 UncheckedCloseable close = null;
81 try {
82 Session session = getInstance().driver.session(mode, bookmarkId);
83 close = UncheckedCloseable.wrap(session);
84 StatementResult result = session.run(query, parameters);
85 return StreamSupport.stream(spliterator(result, Long.MAX_VALUE, Spliterator.ORDERED), false).onClose(close);
86 } catch (Exception e) {
87 if (close != null) {
88 try {
89 close.close();
90 } catch (Exception ex) {
91 e.addSuppressed(ex);
92 }
93 }
94 throw new Neo4jClientException(e);
95 }
96 }
97
98
99
100
101 public static Stream<Record> read(String query) {
102 return run(query, Values.EmptyMap, AccessMode.READ, null);
103 }
104
105
106
107
108 public static Stream<Record> read(String query, String bookmarkId) {
109 return run(query, Values.EmptyMap, AccessMode.READ, bookmarkId);
110 }
111
112
113
114
115 public static Stream<Record> read(String query, Value parameters) {
116 return run(query, parameters, AccessMode.READ, null);
117 }
118
119
120
121
122 public static Stream<Record> read(String query, Value parameters, String bookmarkId) {
123 return run(query, parameters, AccessMode.READ, bookmarkId);
124 }
125
126
127
128
129 public static Stream<Record> write(String query) {
130 return run(query, Values.EmptyMap, AccessMode.WRITE, null);
131 }
132
133
134
135
136 public static Stream<Record> write(String query, String bookmarkId) {
137 return run(query, Values.EmptyMap, AccessMode.WRITE, bookmarkId);
138 }
139
140
141
142
143 public static Stream<Record> write(String query, Value parameters) {
144 return run(query, parameters, AccessMode.WRITE, null);
145 }
146
147
148
149
150 public static Stream<Record> write(String query, Value parameters, String bookmarkId) {
151 return run(query, parameters, AccessMode.WRITE, bookmarkId);
152 }
153
154
155
156
157
158
159
160
161
162 private static Neo4jTransaction getTransaction(AccessMode mode, String bookmarkId) {
163 return new Neo4jTransaction(getInstance().driver.session(mode, bookmarkId));
164 }
165
166
167
168
169 public static Neo4jTransaction getReadTransaction() {
170 return getTransaction(AccessMode.READ, null);
171 }
172
173
174
175
176 public static Neo4jTransaction getReadTransaction(String bookmarkId) {
177 return getTransaction(AccessMode.READ, bookmarkId);
178 }
179
180
181
182
183 public static Neo4jTransaction getWriteTransaction() {
184 return getTransaction(AccessMode.WRITE, null);
185 }
186
187
188
189
190 public static Neo4jTransaction getWriteTransaction(String bookmarkId) {
191 return getTransaction(AccessMode.WRITE, bookmarkId);
192 }
193
194 }