View Javadoc
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   * Main class of this project that handles database actions.
14   */
15  public class Neo4jClient {
16  
17      /**
18       * Singleton instance of the client.
19       */
20      private static Neo4jClient client;
21  
22      /**
23       * Neo4j driver instance.
24       */
25      private Driver driver;
26  
27      /**
28       * Constructor of Neo4j client.
29       * It just create a Neo4j driver instance with the help of the <code>neo4j-driver.properties</code> file.
30       */
31      private Neo4jClient() throws Exception {
32          // Load the configuration
33          Configuration config = new Configuration();
34  
35          // Create the Neo4j driver instance
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       * Retrieve the singleton instance.
43       *
44       * @return The Neo4jClient
45       */
46      private static synchronized Neo4jClient getInstance() {
47          if (client == null) {
48              try {
49                  client = new Neo4jClient();
50              } catch (Exception e) {
51                  // just cast the exception to a runtime,
52                  // to avoid to have a signature error on this method.
53                  throw new RuntimeException(e);
54              }
55          }
56  
57          return client;
58      }
59  
60      /**
61       * Destructor of this object.
62       * It close all underlying object if needed.
63       */
64      public static void destroy() {
65          if (client != null) {
66              client.driver.close();
67              client = null;
68          }
69      }
70  
71  
72      /*-------------------------------*/
73      /*       Auto Commit mode        */
74      /*-------------------------------*/
75  
76      /**
77       * Execute a read cypher query with parameters to Neo4j.
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       * Execute a read cypher query to Neo4j.
100      */
101     public static Stream<Record> read(String query) {
102         return run(query, Values.EmptyMap, AccessMode.READ, null);
103     }
104 
105     /**
106      * Execute a read cypher query to Neo4j with a bookmarkId.
107      */
108     public static Stream<Record> read(String query, String bookmarkId) {
109         return run(query, Values.EmptyMap, AccessMode.READ, bookmarkId);
110     }
111 
112     /**
113      * Execute a read cypher query to Neo4j with parameters.
114      */
115     public static Stream<Record> read(String query, Value parameters) {
116         return run(query, parameters, AccessMode.READ, null);
117     }
118 
119     /**
120      * Execute a read cypher query to Neo4j with parameters and bookmarkId.
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      * Execute a write cypher query to Neo4j.
128      */
129     public static Stream<Record> write(String query) {
130         return run(query, Values.EmptyMap, AccessMode.WRITE, null);
131     }
132 
133     /**
134      * Execute a write cypher query to Neo4j with a bookmarkId.
135      */
136     public static Stream<Record> write(String query, String bookmarkId) {
137         return run(query, Values.EmptyMap, AccessMode.WRITE, bookmarkId);
138     }
139 
140     /**
141      * Execute a write cypher query to Neo4j with parameters.
142      */
143     public static Stream<Record> write(String query, Value parameters) {
144         return run(query, parameters, AccessMode.WRITE, null);
145     }
146 
147     /**
148      * Execute a write cypher query to Neo4j with parameters and bookmarkId.
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 	/*       Transaction mode        */
157 	/*-------------------------------*/
158 
159     /**
160      * Return a Client transaction corresponding the <code>mode</code>\ and <code>bookmarkId</code>.
161      */
162     private static Neo4jTransaction getTransaction(AccessMode mode, String bookmarkId) {
163         return new Neo4jTransaction(getInstance().driver.session(mode, bookmarkId));
164     }
165 
166     /**
167      * Retrieve a read transaction.
168      */
169     public static Neo4jTransaction getReadTransaction() {
170         return getTransaction(AccessMode.READ, null);
171     }
172 
173     /**
174      * Retrieve a read transaction with a bookmarkId.
175      */
176     public static Neo4jTransaction getReadTransaction(String bookmarkId) {
177         return getTransaction(AccessMode.READ, bookmarkId);
178     }
179 
180     /**
181      * Retrieve a write.
182      */
183     public static Neo4jTransaction getWriteTransaction() {
184         return getTransaction(AccessMode.WRITE, null);
185     }
186 
187     /**
188      * Retrieve a write transaction with a bookmarkId.
189      */
190     public static Neo4jTransaction getWriteTransaction(String bookmarkId) {
191         return getTransaction(AccessMode.WRITE, bookmarkId);
192     }
193 
194 }