Skip to content

Commit

Permalink
graph.clear() has clear non-transactional semantics across all Transa…
Browse files Browse the repository at this point in the history
…ctionalGraphs.
  • Loading branch information
okram committed Sep 15, 2011
1 parent 6b6417a commit 2647062
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,17 @@ public void shutdown() {
this.rawGraph.shutdown();
}

/**
* This operation does not respect the transaction buffer. A clear will eradicate the graph and commit the results immediately.
*/
public void clear() {
try {
this.autoStartTransaction();
for (final Index index : this.getIndices()) {
this.dropIndex(index.getIndexName());
}
this.autoStartTransaction();
this.stopTransaction(Conclusion.SUCCESS);
this.startTransaction();
for (final Node node : this.rawGraph.getAllNodes()) {
for (final Relationship relationship : node.getRelationships()) {
try {
Expand All @@ -376,11 +381,13 @@ public void clear() {
} catch (IllegalStateException e) {
}
}
this.autoStopTransaction(Conclusion.SUCCESS);
this.stopTransaction(Conclusion.SUCCESS);
this.autoStartTransaction();
this.createAutomaticIndex(Index.VERTICES, Neo4jVertex.class, null);
this.createAutomaticIndex(Index.EDGES, Neo4jEdge.class, null);
this.stopTransaction(Conclusion.SUCCESS);
} catch (Exception e) {
this.autoStopTransaction(Conclusion.FAILURE);
this.stopTransaction(Conclusion.FAILURE);
throw new RuntimeException(e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,10 @@ public void removeEdge(final Edge edge) {
}
}

/**
* This operation does not respect the transaction buffer. A clear will eradicate the graph and commit the results immediately.
*/
public void clear() {


final OrientGraphContext context = getContext(true);
final int previousMaxBufferSize = context.txBuffer;
for (Index<? extends Element> index : this.getIndices()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,15 @@ public void loadRDF(final InputStream input, final String baseURI, final String
}
}

/**
* This operation does not respect the transaction buffer. A clear will eradicate the graph and commit the results immediately.
*/
public void clear() {
try {
this.sailConnection.get().clear();
this.autoStopTransaction(Conclusion.SUCCESS);
this.stopTransaction(Conclusion.SUCCESS);
} catch (SailException e) {
this.autoStopTransaction(Conclusion.FAILURE);
this.stopTransaction(Conclusion.FAILURE);
throw new RuntimeException(e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,54 @@ public void testStopTransactionEmptyBuffer() {
public void testCurrentBufferSizeConsistencyAfterMutatingOperations() {
TransactionalGraph graph = (TransactionalGraph) graphTest.getGraphInstance();
assertEquals(graph.getMaxBufferSize(), 1);
assertEquals(graph.getCurrentBufferSize(), 0);
if (graph instanceof IndexableGraph) {
((IndexableGraph) graph).dropIndex(Index.VERTICES);
((IndexableGraph) graph).dropIndex(Index.EDGES);
}
assertEquals(graph.getCurrentBufferSize(), 0);

graph.setMaxBufferSize(15);
assertEquals(graph.getMaxBufferSize(), 15);
Vertex v = graph.addVertex(null);
// sail does not increment the buffer for vertex creation as there is no mutation
Vertex a = graph.addVertex(null);
Vertex b = graph.addVertex(null);

int currentBuffer = graph.getCurrentBufferSize();
assertEquals(graph.getMaxBufferSize(), 15);
Edge e = graph.addEdge(null, graph.addVertex(null), graph.addVertex(null), convertId("test"));

Edge e = graph.addEdge(null, a, b, convertId("test"));
assertEquals(graph.getCurrentBufferSize(), currentBuffer + 1);
assertEquals(graph.getMaxBufferSize(), 15);

e.setProperty("ng", convertId("test2"));
assertEquals(graph.getCurrentBufferSize(), currentBuffer + 2);
assertEquals(graph.getMaxBufferSize(), 15);

graph.removeEdge(e);
assertEquals(graph.getCurrentBufferSize(), currentBuffer + 3);
assertEquals(graph.getMaxBufferSize(), 15);

graph.removeVertex(a);
if (!graphTest.isRDFModel) {
assertEquals(graph.getCurrentBufferSize(), currentBuffer + 4);
}
assertEquals(graph.getMaxBufferSize(), 15);
graph.removeVertex(v);

graph.removeVertex(b);
if (!graphTest.isRDFModel) {
assertEquals(graph.getCurrentBufferSize(), currentBuffer + 5);
}
assertEquals(graph.getMaxBufferSize(), 15);

graph.clear();
assertEquals(graph.getCurrentBufferSize(), 0);
assertEquals(graph.getMaxBufferSize(), 15);

if (!graphTest.isRDFModel) {

}

graph.shutdown();
}

Expand Down

0 comments on commit 2647062

Please sign in to comment.