/
Neo4jService.java
220 lines (189 loc) · 7.44 KB
/
Neo4jService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package org.neo4j.community.console;
import org.neo4j.geoff.except.SubgraphError;
import org.neo4j.geoff.except.SyntaxError;
import org.neo4j.graphdb.*;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.slf4j.Logger;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import static org.neo4j.helpers.collection.MapUtil.map;
/**
* @author mh
* @since 08.04.12
*/
class Neo4jService {
private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(Neo4jService.class);
public static final String VERSION_REGEXP = "(\\d+\\.\\d+(?:\\.experimental|-cost|-rule)?)";
private GraphDatabaseService gdb;
private Index index;
private CypherQueryExecutor cypherQueryExecutor;
private GeoffImportService geoffService;
private GeoffExportService geoffExportService;
private CypherExportService cypherExportService;
private String version;
private boolean initialized;
Neo4jService() throws Throwable {
this(createInMemoryDatabase(),true);
}
private static GraphDatabaseService createInMemoryDatabase() throws Throwable {
try {
Map<String,String> config = Collections.singletonMap("execution_guard_enabled","true");
return new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().setConfig(config).newGraphDatabase();
} catch(RuntimeException re) {
Throwable t=re.getCause();
if (t instanceof RuntimeException) throw (RuntimeException)t;
if (t instanceof Error) throw (Error)t;
throw t;
}
}
Neo4jService(GraphDatabaseService gdb) throws Throwable {
this(gdb,false);
}
private Neo4jService(GraphDatabaseService gdb, boolean ownsDatabase) {
if (gdb == null) throw new IllegalArgumentException("Graph Database must not be null");
this.gdb = gdb;
this.ownsDatabase = ownsDatabase;
index = new Index(this.gdb);
cypherQueryExecutor = new CypherQueryExecutor(gdb,index);
geoffService = new GeoffImportService(gdb, index);
geoffExportService = new GeoffExportService(gdb);
cypherExportService = new CypherExportService(gdb);
}
public Map cypherQueryViz(String query) {
final boolean invalidQuery = query == null || query.trim().isEmpty() || cypherQueryExecutor.isMutatingQuery(query);
return invalidQuery ? cypherQueryViz((CypherQueryExecutor.CypherResult) null) : cypherQueryViz(cypherQuery(query, null));
}
public Map cypherQueryViz(CypherQueryExecutor.CypherResult result) {
try (Transaction tx = gdb.beginTx()) {
final SubGraph subGraph = SubGraph.from(gdb).markSelection(result);
Map<String, Object> viz = map("nodes", subGraph.getNodes().values(), "links", subGraph.getRelationshipsWithIndexedEnds().values());
tx.success();
return viz;
}
}
public String exportToGeoff() {
try (Transaction tx = gdb.beginTx()) {
String result = geoffExportService.export();
tx.success();
return result;
}
}
public String exportToCypher() {
try (Transaction tx = gdb.beginTx()) {
String cypher = cypherExportService.export();
tx.success();
return cypher;
}
}
public Map<String,Object> mergeGeoff(String geoff) {
try (Transaction tx = gdb.beginTx()) {
final Map<String,Object> result = new LinkedHashMap<>();
for (Map.Entry<String, PropertyContainer> entry : geoffService.mergeGeoff(geoff).entrySet()) {
result.put(entry.getKey(),geoffExportService.toMap(entry.getValue()));
}
tx.success();
return result;
} catch (SubgraphError subgraphError) {
throw new RuntimeException("Error merging:\n"+geoff,subgraphError);
} catch (SyntaxError syntaxError) {
throw new RuntimeException("Syntax error merging:\n"+geoff,syntaxError);
}
}
public Collection<Map<String,Object>> cypherQueryResults(String query) {
Collection<Map<String,Object>> result=new ArrayList<>();
for (Map<String, Object> row : cypherQuery(query, null)) {
result.add(row);
}
return result;
}
public CypherQueryExecutor.CypherResult initCypherQuery(String query, Map<String, Object> queryParams) {
return cypherQueryExecutor.cypherQuery(query,null,queryParams);
}
public CypherQueryExecutor.CypherResult cypherQuery(String query, Map<String, Object> queryParams) {
return cypherQueryExecutor.cypherQuery(query,version,queryParams);
}
public String prettify(String query) {
return cypherQueryExecutor.prettify(query);
}
public void stop() {
if (gdb!=null) {
LOG.warn("Shutting down service "+this);
if (ownsDatabase) gdb.shutdown();
index = null;
cypherQueryExecutor=null;
geoffExportService =null;
cypherExportService =null;
geoffService =null;
gdb=null;
}
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
if (version==null || version.trim().isEmpty()) this.version=null;
else {
this.version = checkVersion(version);
}
}
public static String checkVersion(String version) {
version = version.replaceAll("^"+VERSION_REGEXP+".*","$1");
if (!version.matches(VERSION_REGEXP)) throw new IllegalArgumentException("Incorrect version string "+version);
return version;
}
public boolean isMutatingQuery(String query) {
return cypherQueryExecutor.isMutatingQuery(query);
}
public boolean isCypherQuery(String query) {
return cypherQueryExecutor.isCypherQuery(query);
}
public GraphDatabaseService getGraphDatabase() {
return gdb;
}
public void importGraph(SubGraph graph) {
try (Transaction tx = gdb.beginTx()) {
graph.importTo(gdb);
tx.success();
}
}
public URL toUrl(String url) {
try {
return new URL(url);
} catch (MalformedURLException e) {
return null;
}
}
private final boolean ownsDatabase;
public boolean doesOwnDatabase() {
return ownsDatabase;
}
public Neo4jService initializeFrom(SubGraph graph) {
importGraph(graph);
setInitialized();
return this;
}
public boolean isInitialized() {
return initialized;
}
public void setInitialized() {
this.initialized = true;
}
public Map exportToJson(Map<String, Object> graph) {
Map<String,Map<String,Object>> result=new HashMap<>(graph.size());
for (Map.Entry<String, Object> entry : graph.entrySet()) {
Map<String, Object> data = null;
if (entry.getValue() instanceof Map) {
//noinspection unchecked
data = (Map<String, Object>) entry.getValue();
}
if (entry.getValue() instanceof PropertyContainer) {
final PropertyContainer value = (PropertyContainer) entry.getValue();
if (value instanceof Node) data=SubGraph.toMap((Node)value);
if (value instanceof Relationship) data=SubGraph.toMap((Relationship)value);
}
if (data!=null) result.put(entry.getKey(),data);
}
return result;
}
}