public TransactionalEventQueue(final Graph graph) { if (!graph.features().graph().supportsTransactions()) throw new IllegalStateException(String.format("%s requires the graph to support transactions", EventStrategy.class.getName())); // since this is a transactional graph events are enqueued so the events should be fired/reset only after // transaction is committed/rolled back as tied to a graph transaction graph.tx().addTransactionListener(status -> { if (status == Transaction.Status.COMMIT) fireEventQueue(); else if (status == Transaction.Status.ROLLBACK) resetEventQueue(); else throw new RuntimeException(String.format("The %s is not aware of this status: %s", EventQueue.class.getName(), status)); }); }
/** * Utility method that commits if the graph supports transactions and executes an assertion function before and * after the commit. It assumes that the assertion should be true before and after the commit. */ public void tryCommit(final Graph graph, final Consumer<Graph> assertFunction) { assertFunction.accept(graph); if (graph.features().graph().supportsTransactions()) { graph.tx().commit(); assertFunction.accept(graph); } }
/** * Commit transactions across all {@link Graph} objects. */ public final void commitAll() { graphs.entrySet().forEach(e -> { final Graph graph = e.getValue(); if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) graph.tx().commit(); }); }
/** * Rollback transactions across all {@link Graph} objects. */ public final void rollbackAll() { graphs.entrySet().forEach(e -> { final Graph graph = e.getValue(); if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) graph.tx().rollback(); }); }
private void closeTx(final Set<String> graphSourceNamesToCloseTxOn, final Transaction.Status tx) { final Set<Graph> graphsToCloseTxOn = new HashSet<>(); graphSourceNamesToCloseTxOn.forEach(name -> { if (this.graphs.containsKey(name)) { graphsToCloseTxOn.add(this.graphs.get(name)); } }); graphsToCloseTxOn.forEach(graph -> { if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) { if (tx == Transaction.Status.COMMIT) { graph.tx().commit(); } else { graph.tx().rollback(); } } }); }
/** * Selectively close transactions on the specified graphs or the graphs of traversal sources. */ private void closeTx(final Set<String> graphSourceNamesToCloseTxOn, final Transaction.Status tx) { final Set<Graph> graphsToCloseTxOn = new HashSet<>(); // by the time this method has been called, it should be validated that the source/graph is present. // might be possible that it could have been removed dynamically, but that i'm not sure how one would do // that as of right now unless they were embedded in which case they'd need to know what they were doing // anyway graphSourceNamesToCloseTxOn.forEach(r -> { if (graphs.containsKey(r)) graphsToCloseTxOn.add(graphs.get(r)); else graphsToCloseTxOn.add(traversalSources.get(r).getGraph()); }); graphsToCloseTxOn.forEach(graph -> { if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) { if (tx == Transaction.Status.COMMIT) graph.tx().commit(); else graph.tx().rollback(); } }); } }
if (g.features().graph().supportsTransactions()) {
final Map<Object, Vertex> cache = new HashMap<>(); final AtomicLong counter = new AtomicLong(0); final boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions(); final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge(); final Graph.Features.VertexFeatures vertexFeatures = graphToWriteTo.features().vertex();
public void rollbackAll() { this.graphs.values().forEach(graph -> { if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) { graph.tx().rollback(); } }); }
public void commitAll() { this.graphs.values().forEach(graph -> { if (graph.features().graph().supportsTransactions() && graph.tx().isOpen()) { graph.tx().commit(); } }); }
throw new RuntimeException(e); } finally { if (fromGraph.features().graph().supportsTransactions()) fromGraph.tx().rollback(); if (toGraph.features().graph().supportsTransactions()) toGraph.tx().rollback();
final AtomicLong counter = new AtomicLong(0); final boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions(); final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge();
/** * Determines if a graph meets requirements for execution. All gremlin process tests should check this * method as part of a call to {@code assumeTrue} to ensure that the test doesn't require the computer * feature or if it does require the computer feature then ensure that the graph being tested supports it. */ protected boolean graphMeetsTestRequirements() { return !hasGraphComputerRequirement() || graph.features().graph().supportsComputer(); }
return; if (null != graph) { if (graph.features().graph().supportsTransactions()) { LOGGER.info("Committing transaction on Graph instance: {} [{} mutations]", graph, listener.mutations()); try {
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldUseActualVertexWhenAdded() { final AtomicBoolean triggered = new AtomicBoolean(false); final AtomicReference<Vertex> eventedVertex = new AtomicReference<>(); final MutationListener listener = new AbstractMutationListener() { @Override public void vertexAdded(final Vertex element) { eventedVertex.set(element); assertEquals("thing", element.label()); assertThat(element.properties("here").hasNext(), is(false)); triggered.set(true); } }; final EventStrategy.Builder builder = EventStrategy.build().addListener(listener).detach(EventStrategy.Detachment.REFERENCE); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final GraphTraversalSource gts = create(eventStrategy); final Vertex v = gts.addV("thing").property("here", "there").next(); tryCommit(graph); assertVertexEdgeCounts(graph, 1, 0); assertThat(triggered.get(), is(true)); assertEquals(v, eventedVertex.get()); }
final AtomicLong counter = new AtomicLong(0); final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge(); final boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions();
/** * Utility method that commits if the graph supports transactions. */ public void tryCommit(final Graph graph) { if (graph.features().graph().supportsTransactions()) graph.tx().commit(); }
/** * Utility method that rollsback if the graph supports transactions. */ public void tryRollback(final Graph graph) { if (graph.features().graph().supportsTransactions()) graph.tx().rollback(); }
final Map<Object,Vertex> cache = new HashMap<>(); final AtomicLong counter = new AtomicLong(0); final boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions(); final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge(); final Graph.Features.VertexFeatures vertexFeatures = graphToWriteTo.features().vertex();
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldDetachVertexWhenAdded() { final AtomicBoolean triggered = new AtomicBoolean(false); final MutationListener listener = new AbstractMutationListener() { @Override public void vertexAdded(final Vertex element) { assertThat(element, instanceOf(DetachedVertex.class)); assertEquals("thing", element.label()); assertEquals("there", element.value("here")); triggered.set(true); } }; final EventStrategy.Builder builder = EventStrategy.build().addListener(listener); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final GraphTraversalSource gts = create(eventStrategy); gts.addV("thing").property("here", "there").iterate(); tryCommit(graph); assertVertexEdgeCounts(graph, 1, 0); assertThat(triggered.get(), is(true)); }