Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 498756b

Browse files
author
Achim Brandt
committed
fixed some sonar issues
1 parent ce1c6d1 commit 498756b

File tree

4 files changed

+142
-67
lines changed

4 files changed

+142
-67
lines changed

src/main/java/com/arangodb/blueprints/ArangoDBGraph.java

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,7 @@ public ArangoDBGraph(ArangoDBConfiguration configuration, String name, String ve
162162
try {
163163
GraphEntity graph = client.getGraph(name);
164164
if (graph != null) {
165-
boolean error = false;
166-
167-
List<EdgeDefinitionEntity> edgeDefinitions = graph.getEdgeDefinitions();
168-
169-
if (edgeDefinitions.size() != 1 || CollectionUtils.isNotEmpty(graph.getOrphanCollections())) {
170-
error = true;
171-
} else {
172-
EdgeDefinitionEntity edgeDefinitionEntity = edgeDefinitions.get(0);
173-
if (!edgesCollectionName.equals(edgeDefinitionEntity.getCollection())
174-
|| edgeDefinitionEntity.getFrom().size() != 1 || edgeDefinitionEntity.getTo().size() != 1
175-
|| !verticesCollectionName.equals(edgeDefinitionEntity.getFrom().get(0))
176-
|| !verticesCollectionName.equals(edgeDefinitionEntity.getTo().get(0))) {
177-
error = true;
178-
}
179-
}
165+
boolean error = graphHasError(verticesCollectionName, edgesCollectionName, graph);
180166
if (error) {
181167
throw new ArangoDBGraphException("Graph with that name already exists but with other settings");
182168
}
@@ -194,6 +180,25 @@ public ArangoDBGraph(ArangoDBConfiguration configuration, String name, String ve
194180
}
195181
}
196182

183+
private boolean graphHasError(String verticesCollectionName, String edgesCollectionName, GraphEntity graph) {
184+
boolean error = false;
185+
186+
List<EdgeDefinitionEntity> edgeDefinitions = graph.getEdgeDefinitions();
187+
188+
if (edgeDefinitions.size() != 1 || CollectionUtils.isNotEmpty(graph.getOrphanCollections())) {
189+
error = true;
190+
} else {
191+
EdgeDefinitionEntity edgeDefinitionEntity = edgeDefinitions.get(0);
192+
if (!edgesCollectionName.equals(edgeDefinitionEntity.getCollection())
193+
|| edgeDefinitionEntity.getFrom().size() != 1 || edgeDefinitionEntity.getTo().size() != 1
194+
|| !verticesCollectionName.equals(edgeDefinitionEntity.getFrom().get(0))
195+
|| !verticesCollectionName.equals(edgeDefinitionEntity.getTo().get(0))) {
196+
error = true;
197+
}
198+
}
199+
return error;
200+
}
201+
197202
@Override
198203
public Features getFeatures() {
199204
return FEATURES;
@@ -295,26 +300,30 @@ public <T extends Element> void dropKeyIndex(String key, Class<T> elementClass)
295300
logger.warn("error while reading an index", e);
296301
}
297302

298-
String n = ArangoDBUtil.normalizeKey(key);
303+
String normalizedKey = ArangoDBUtil.normalizeKey(key);
299304

300305
if (indices != null) {
301-
for (ArangoDBIndex i : indices) {
302-
if (i.getFields().size() == 1) {
303-
String field = i.getFields().get(0);
304-
305-
if (field.equals(n)) {
306-
try {
307-
client.deleteIndex(i.getId());
308-
} catch (ArangoDBException e) {
309-
logger.warn("error while deleting an index", e);
310-
}
311-
}
306+
for (ArangoDBIndex index : indices) {
307+
if (index.getFields().size() == 1) {
308+
deleteIndexByKey(normalizedKey, index);
312309
}
313310
}
314311
}
315312

316313
}
317314

315+
private void deleteIndexByKey(String normalizedKey, ArangoDBIndex index) {
316+
String field = index.getFields().get(0);
317+
318+
if (field.equals(normalizedKey)) {
319+
try {
320+
client.deleteIndex(index.getId());
321+
} catch (ArangoDBException e) {
322+
logger.warn("error while deleting an index", e);
323+
}
324+
}
325+
}
326+
318327
@SuppressWarnings("rawtypes")
319328
@Override
320329
public <T extends Element> void createKeyIndex(String key, Class<T> elementClass, Parameter... indexParameters) {
@@ -376,12 +385,7 @@ public <T extends Element> Set<String> getIndexedKeys(Class<T> elementClass) {
376385

377386
for (ArangoDBIndex i : indices) {
378387
if (i.getFields().size() == 1) {
379-
String key = i.getFields().get(0);
380-
381-
// ignore system index
382-
if (key.charAt(0) != '_') {
383-
result.add(ArangoDBUtil.denormalizeKey(key));
384-
}
388+
addNotSystemKey(result, i);
385389
}
386390
}
387391

@@ -392,6 +396,15 @@ public <T extends Element> Set<String> getIndexedKeys(Class<T> elementClass) {
392396
return result;
393397
}
394398

399+
private void addNotSystemKey(HashSet<String> result, ArangoDBIndex i) {
400+
String key = i.getFields().get(0);
401+
402+
// ignore system index
403+
if (key.charAt(0) != '_') {
404+
result.add(ArangoDBUtil.denormalizeKey(key));
405+
}
406+
}
407+
395408
@Override
396409
public GraphQuery query() {
397410
return new ArangoDBGraphQuery(this);

src/main/java/com/arangodb/blueprints/ArangoDBGraphFactory.java

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package com.arangodb.blueprints;
1010

11+
import org.apache.log4j.Logger;
12+
1113
import com.arangodb.blueprints.client.ArangoDBException;
1214
import com.tinkerpop.blueprints.Vertex;
1315

@@ -21,6 +23,18 @@
2123

2224
public class ArangoDBGraphFactory {
2325

26+
private static final String WEIGHT = "weight";
27+
private static final String KNOWS = "knows";
28+
private static final String CREATED = "created";
29+
/**
30+
* the logger
31+
*/
32+
private static final Logger logger = Logger.getLogger(ArangoDBGraphFactory.class);
33+
34+
private ArangoDBGraphFactory() {
35+
// this is a factory class
36+
}
37+
2438
/**
2539
* Static function to create a new ArangoDB graph.
2640
*
@@ -32,6 +46,18 @@ public static ArangoDBGraph createArangoDBGraph() {
3246
return createArangoDBGraph("localhost", 8529);
3347
}
3448

49+
/**
50+
* Static function to create a new ArangoDB graph with example vertices and
51+
* edges.
52+
*
53+
* Connects to ArangoDB database on localhost:8529.
54+
*
55+
* @return the new graph
56+
*/
57+
public static ArangoDBGraph createExampleArangoDBGraph() {
58+
return createExampleArangoDBGraph("localhost", 8529);
59+
}
60+
3561
/**
3662
* Static function to create a new ArangoDB graph.
3763
*
@@ -47,21 +73,38 @@ public static ArangoDBGraph createArangoDBGraph(String host, int port) {
4773
try {
4874
graph = new ArangoDBGraph(host, port, "factory_graph", "factory_vertices", "factory_edges");
4975
} catch (ArangoDBGraphException e) {
50-
e.printStackTrace();
51-
System.out.println("Could not get or create the graph.");
76+
logger.error("Could not get or create the graph.", e);
77+
}
78+
return graph;
79+
}
80+
81+
/**
82+
* Static function to create a new ArangoDB graph with example vertices and
83+
* edges.
84+
*
85+
* @param host
86+
* Host name of the ArangoDB
87+
* @param port
88+
* Port number of ArangoDB
89+
* @return the new graph
90+
*/
91+
public static ArangoDBGraph createExampleArangoDBGraph(String host, int port) {
92+
93+
ArangoDBGraph graph = createArangoDBGraph(host, port);
94+
if (graph == null) {
5295
return null;
5396
}
5497

5598
try {
5699
graph.getClient().truncateCollection(graph.getRawGraph().getVertexCollection());
57100
} catch (ArangoDBException e) {
58-
System.out.println("Could not truncate vertices collection.");
101+
logger.error("Could not truncate vertices collection.", e);
59102
}
60103

61104
try {
62105
graph.getClient().truncateCollection(graph.getRawGraph().getEdgeCollection());
63106
} catch (ArangoDBException e) {
64-
System.out.println("Could not truncate edges collection.");
107+
logger.error("Could not truncate edges collection.", e);
65108
}
66109

67110
Vertex marko = graph.addVertex("1");
@@ -88,14 +131,14 @@ public static ArangoDBGraph createArangoDBGraph(String host, int port) {
88131
peter.setProperty("name", "peter");
89132
peter.setProperty("age", 35);
90133

91-
graph.addEdge("7", marko, vadas, "knows").setProperty("weight", 0.5f);
92-
graph.addEdge("8", marko, josh, "knows").setProperty("weight", 1.0f);
93-
graph.addEdge("9", marko, lop, "created").setProperty("weight", 0.4f);
134+
graph.addEdge("7", marko, vadas, KNOWS).setProperty(WEIGHT, 0.5f);
135+
graph.addEdge("8", marko, josh, KNOWS).setProperty(WEIGHT, 1.0f);
136+
graph.addEdge("9", marko, lop, CREATED).setProperty(WEIGHT, 0.4f);
94137

95-
graph.addEdge("10", josh, ripple, "created").setProperty("weight", 1.0f);
96-
graph.addEdge("11", josh, lop, "created").setProperty("weight", 0.4f);
138+
graph.addEdge("10", josh, ripple, CREATED).setProperty(WEIGHT, 1.0f);
139+
graph.addEdge("11", josh, lop, CREATED).setProperty(WEIGHT, 0.4f);
97140

98-
graph.addEdge("12", peter, lop, "created").setProperty("weight", 0.2f);
141+
graph.addEdge("12", peter, lop, CREATED).setProperty(WEIGHT, 0.2f);
99142

100143
return graph;
101144
}

src/main/java/com/arangodb/blueprints/batch/ArangoDBBatchEdge.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.HashMap;
1212
import java.util.Map;
1313

14+
import org.apache.log4j.Logger;
15+
1416
import com.arangodb.blueprints.client.ArangoDBBaseDocument;
1517
import com.arangodb.blueprints.client.ArangoDBException;
1618
import com.arangodb.blueprints.client.ArangoDBSimpleEdge;
@@ -29,6 +31,12 @@
2931
*/
3032

3133
public class ArangoDBBatchEdge extends ArangoDBBatchElement implements Edge {
34+
35+
/**
36+
* the logger
37+
*/
38+
private static final Logger logger = Logger.getLogger(ArangoDBBatchEdge.class);
39+
3240
/**
3341
* the _from vertex
3442
*/
@@ -83,6 +91,7 @@ static ArangoDBBatchEdge create(
8391
if (e.errorNumber() == 1210) {
8492
throw ExceptionFactory.vertexWithIdAlreadyExists(id);
8593
}
94+
logger.warn("could not create batch edge", e);
8695
throw new IllegalArgumentException(e.getMessage());
8796
}
8897
}
@@ -107,6 +116,7 @@ static ArangoDBBatchEdge load(ArangoDBBatchGraph graph, Object id) {
107116
return build(graph, v, null, null);
108117
} catch (ArangoDBException e) {
109118
// do nothing
119+
logger.warn("could not load batch edge", e);
110120
return null;
111121
}
112122
}
@@ -131,7 +141,7 @@ static ArangoDBBatchEdge build(
131141
}
132142

133143
@Override
134-
public Vertex getVertex(Direction direction) throws IllegalArgumentException {
144+
public Vertex getVertex(Direction direction) {
135145
throw new UnsupportedOperationException();
136146
}
137147

src/main/java/com/arangodb/blueprints/batch/ArangoDBBatchGraph.java

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,7 @@ public ArangoDBBatchGraph(ArangoDBConfiguration configuration, String name, Stri
198198
try {
199199
GraphEntity graph = client.getGraph(name);
200200
if (graph != null) {
201-
boolean error = false;
202-
203-
List<EdgeDefinitionEntity> edgeDefinitions = graph.getEdgeDefinitions();
204-
205-
if (edgeDefinitions.size() != 1 || CollectionUtils.isNotEmpty(graph.getOrphanCollections())) {
206-
error = true;
207-
} else {
208-
EdgeDefinitionEntity edgeDefinitionEntity = edgeDefinitions.get(0);
209-
if (!edgesCollectionName.equals(edgeDefinitionEntity.getCollection())
210-
|| edgeDefinitionEntity.getFrom().size() != 1 || edgeDefinitionEntity.getTo().size() != 1
211-
|| !verticesCollectionName.equals(edgeDefinitionEntity.getFrom().get(0))
212-
|| !verticesCollectionName.equals(edgeDefinitionEntity.getTo().get(0))) {
213-
error = true;
214-
}
215-
}
201+
boolean error = graphHasError(verticesCollectionName, edgesCollectionName, graph);
216202
if (error) {
217203
throw new ArangoDBGraphException("Graph with that name already exists but with other settings");
218204
}
@@ -231,6 +217,25 @@ public ArangoDBBatchGraph(ArangoDBConfiguration configuration, String name, Stri
231217
}
232218
}
233219

220+
private boolean graphHasError(String verticesCollectionName, String edgesCollectionName, GraphEntity graph) {
221+
boolean error = false;
222+
223+
List<EdgeDefinitionEntity> edgeDefinitions = graph.getEdgeDefinitions();
224+
225+
if (edgeDefinitions.size() != 1 || CollectionUtils.isNotEmpty(graph.getOrphanCollections())) {
226+
error = true;
227+
} else {
228+
EdgeDefinitionEntity edgeDefinitionEntity = edgeDefinitions.get(0);
229+
if (!edgesCollectionName.equals(edgeDefinitionEntity.getCollection())
230+
|| edgeDefinitionEntity.getFrom().size() != 1 || edgeDefinitionEntity.getTo().size() != 1
231+
|| !verticesCollectionName.equals(edgeDefinitionEntity.getFrom().get(0))
232+
|| !verticesCollectionName.equals(edgeDefinitionEntity.getTo().get(0))) {
233+
error = true;
234+
}
235+
}
236+
return error;
237+
}
238+
234239
@Override
235240
public Features getFeatures() {
236241
return FEATURES;
@@ -416,14 +421,9 @@ public <T extends Element> Set<String> getIndexedKeys(Class<T> elementClass) {
416421
indices = client.getEdgeIndices(rawGraph);
417422
}
418423

419-
for (ArangoDBIndex i : indices) {
420-
if (i.getFields().size() == 1) {
421-
String key = i.getFields().get(0);
422-
423-
// ignore system index
424-
if (key.charAt(0) != '_') {
425-
result.add(ArangoDBUtil.denormalizeKey(key));
426-
}
424+
for (ArangoDBIndex index : indices) {
425+
if (index.getFields().size() == 1) {
426+
addIndex(result, index);
427427
}
428428
}
429429

@@ -434,6 +434,15 @@ public <T extends Element> Set<String> getIndexedKeys(Class<T> elementClass) {
434434
return result;
435435
}
436436

437+
private void addIndex(HashSet<String> result, ArangoDBIndex index) {
438+
String key = index.getFields().get(0);
439+
440+
// ignore system index
441+
if (key.charAt(0) != '_') {
442+
result.add(ArangoDBUtil.denormalizeKey(key));
443+
}
444+
}
445+
437446
/**
438447
* Save changed vertices. This functions has to be called in the shutdown
439448
* function.

0 commit comments

Comments
 (0)