public static TitanTransaction getTx(Traversal.Admin<?, ?> traversal) { TitanTransaction tx = null; Optional<Graph> optGraph = TraversalHelper.getRootTraversal(traversal.asAdmin()).getGraph(); if (traversal instanceof FulgoraElementTraversal) { tx = (TitanTransaction) optGraph.get(); } else { if (!optGraph.isPresent()) throw new IllegalArgumentException("Traversal is not bound to a graph: " + traversal); Graph graph = optGraph.get(); if (graph instanceof TitanTransaction) tx = (TitanTransaction) graph; else if (graph instanceof TitanBlueprintsGraph) tx = ((TitanBlueprintsGraph) graph).getCurrentThreadTx(); else throw new IllegalArgumentException("Traversal is not bound to a Titan Graph, but: " + graph); } if (tx == null) throw new IllegalArgumentException("Not a valid start step for a Titan traversal: " + traversal); if (tx.isOpen()) return tx; else return ((StandardTitanTx) tx).getNextTx(); }
public static JanusGraphTransaction getTx(Traversal.Admin<?, ?> traversal) { final JanusGraphTransaction tx; Optional<Graph> optGraph = TraversalHelper.getRootTraversal(traversal.asAdmin()).getGraph(); if (traversal instanceof FulgoraElementTraversal) { tx = (JanusGraphTransaction) optGraph.get(); } else { if (!optGraph.isPresent()) throw new IllegalArgumentException("Traversal is not bound to a graph: " + traversal); Graph graph = optGraph.get(); if (graph instanceof JanusGraphTransaction) tx = (JanusGraphTransaction) graph; else if (graph instanceof JanusGraphBlueprintsGraph) tx = ((JanusGraphBlueprintsGraph) graph).getCurrentThreadTx(); else throw new IllegalArgumentException("Traversal is not bound to a JanusGraph Graph, but: " + graph); } if (tx == null) throw new IllegalArgumentException("Not a valid start step for a JanusGraph traversal: " + traversal); if (tx.isOpen()) return tx; else return ((StandardJanusGraphTx) tx).getNextTx(); }
public JanusGraphStep(final GraphStep<S, E> originalStep) { super(originalStep.getTraversal(), originalStep.getReturnClass(), originalStep.isStartStep(), originalStep.getIds()); originalStep.getLabels().forEach(this::addLabel); this.setIteratorSupplier(() -> { if (this.ids == null) { return Collections.emptyIterator(); } else if (this.ids.length > 0) { final Graph graph = (Graph)traversal.asAdmin().getGraph().get(); return iteratorList((Iterator)graph.vertices(this.ids)); } if (hasLocalContainers.isEmpty()) { hasLocalContainers.put(new ArrayList<>(), new QueryInfo(new ArrayList<>(), 0, BaseQuery.NO_LIMIT)); } final JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(traversal); final GraphCentricQuery globalQuery = buildGlobalGraphCentricQuery(tx); final Multimap<Integer, GraphCentricQuery> queries = ArrayListMultimap.create(); if (globalQuery != null && !globalQuery.getSubQuery(0).getBackendQuery().isEmpty()) { queries.put(0, globalQuery); } else { hasLocalContainers.entrySet().forEach(c -> queries.put(c.getValue().getLowLimit(), buildGraphCentricQuery(tx, c))); } final GraphCentricQueryBuilder builder = (GraphCentricQueryBuilder) tx.query(); final List<Iterator<E>> responses = new ArrayList<>(); queries.entries().forEach(q -> executeGraphCentryQuery(builder, responses, q)); return new MultiDistinctOrderedIterator<E>(lowLimit, highLimit, responses, orders); }); }
private Iterator<? extends Vertex> vertices() { final TinkerGraph graph = (TinkerGraph) this.getTraversal().getGraph().get(); final HasContainer indexedContainer = getIndexKey(Vertex.class); // ids are present, filter on them first if (null == this.ids) return Collections.emptyIterator(); else if (this.ids.length > 0) return this.iteratorList(graph.vertices(this.ids)); else return null == indexedContainer ? this.iteratorList(graph.vertices()) : IteratorUtils.filter(TinkerHelper.queryVertexIndex(graph, indexedContainer.getKey(), indexedContainer.getPredicate().getValue()).iterator(), vertex -> HasContainer.testAll(vertex, this.hasContainers)); }
@Override public Optional<Graph> getGraph() { return null == this.bypassTraversal ? Optional.empty() : this.bypassTraversal.getGraph(); }
@Override protected Vertex map(final Traverser.Admin<S> traverser) { final Vertex vertex = this.getTraversal().getGraph().get().addVertex(this.parameters.getKeyValues(traverser)); if (this.callbackRegistry != null) { final EventStrategy eventStrategy = getTraversal().getStrategies().getStrategy(EventStrategy.class).get(); final Event.VertexAddedEvent vae = new Event.VertexAddedEvent(eventStrategy.detach(vertex)); this.callbackRegistry.getCallbacks().forEach(c -> c.accept(vae)); } return vertex; }
@Override protected void sideEffect(final Traverser.Admin<Edge> traverser) { parentGraphFeatures = ((Graph) traversal.getGraph().get()).features().vertex(); if (null == subgraph) { subgraph = traverser.sideEffects(sideEffectKey); if (!subgraph.features().vertex().supportsUserSuppliedIds() || !subgraph.features().edge().supportsUserSuppliedIds()) throw new IllegalArgumentException("The provided subgraph must support user supplied ids for vertices and edges: " + this.subgraph); } subgraphSupportsMetaProperties = subgraph.features().vertex().supportsMetaProperties(); addEdgeToSubgraph(traverser.get()); }
public static void applySingleLevelStrategies(final Traversal.Admin<?, ?> parentTraversal, final Traversal.Admin<?, ?> childTraversal, final Class<? extends TraversalStrategy> stopAfterStrategy) { childTraversal.setStrategies(parentTraversal.getStrategies()); childTraversal.setSideEffects(parentTraversal.getSideEffects()); parentTraversal.getGraph().ifPresent(childTraversal::setGraph); for (final TraversalStrategy<?> strategy : parentTraversal.getStrategies().toList()) { strategy.apply(childTraversal); if (null != stopAfterStrategy && stopAfterStrategy.isInstance(strategy)) break; } }
protected Traverser.Admin<S> read(final File file) { try (final InputStream stream = new FileInputStream(file)) { final Graph graph = (Graph) this.traversal.getGraph().get(); constructReader().readGraph(stream, graph); return EmptyTraverser.instance(); } catch (IOException ioe) { throw new IllegalStateException(String.format("Could not read file %s into graph", this.file), ioe); } }
public static void convAllHasSteps(Traversal.Admin<?, ?> traversal) { // Extract all has steps in traversal @SuppressWarnings("rawtypes") List<HasStep> steps = TraversalHelper .getStepsOfAssignableClassRecursively( HasStep.class, traversal); HugeGraph graph = (HugeGraph) traversal.getGraph().get(); for (HasStep<?> step : steps) { TraversalUtil.convHasStep(graph, step); } }
private Iterator<? extends Vertex> vertices() { if (null == this.ids) return Collections.emptyIterator(); final Neo4jGraph graph = (Neo4jGraph) this.getTraversal().getGraph().get(); return graph.getTrait().lookupVertices(graph, this.hasContainers, this.ids); }
private Iterator<? extends Edge> edges() { if (null == this.ids) return Collections.emptyIterator(); return IteratorUtils.filter(this.getTraversal().getGraph().get().edges(this.ids), edge -> HasContainer.testAll(edge, this.hasContainers)); }
private HasContainer getIndexKey(final Class<? extends Element> indexedClass) { final Set<String> indexedKeys = ((TinkerGraph) this.getTraversal().getGraph().get()).getIndexedKeys(indexedClass); final Iterator<HasContainer> itty = IteratorUtils.filter(hasContainers.iterator(), c -> c.getPredicate().getBiPredicate() == Compare.eq && indexedKeys.contains(c.getKey())); return itty.hasNext() ? itty.next() : null; }
public GraphStep(final Traversal.Admin traversal, final Class<E> returnClass, final boolean isStart, final Object... ids) { super(traversal); this.returnClass = returnClass; this.ids = (ids.length == 1 && ids[0] instanceof Collection) ? ((Collection) ids[0]).toArray(new Object[((Collection) ids[0]).size()]) : ids; this.isStart = isStart; this.iteratorSupplier = () -> (Iterator<E>) (Vertex.class.isAssignableFrom(this.returnClass) ? this.getTraversal().getGraph().get().vertices(this.ids) : this.getTraversal().getGraph().get().edges(this.ids)); }
@Override protected Traverser.Admin<Long> processNextStart() throws NoSuchElementException { if (!this.done) { this.done = true; final TinkerGraph graph = (TinkerGraph) this.getTraversal().getGraph().get(); return this.getTraversal().getTraverserGenerator().generate(Vertex.class.isAssignableFrom(this.elementClass) ? (long) TinkerHelper.getVertices(graph).size() : (long) TinkerHelper.getEdges(graph).size(), (Step) this, 1L); } else throw FastNoSuchElementException.instance(); }
protected Traverser.Admin<S> write(final File file) { try (final OutputStream stream = new FileOutputStream(file)) { final Graph graph = (Graph) this.traversal.getGraph().get(); constructWriter().writeGraph(stream, graph); return EmptyTraverser.instance(); } catch (IOException ioe) { throw new IllegalStateException(String.format("Could not write file %s from graph", this.file), ioe); } }
@Override public void apply(final Traversal.Admin<?, ?> traversal) { if (TraversalHelper.onGraphComputer(traversal)) return; TraversalHelper.getStepsOfClass(GraphStep.class, traversal).forEach(originalGraphStep -> { if (originalGraphStep.getIds() == null || originalGraphStep.getIds().length == 0) { //Try to optimize for index calls final JanusGraphStep<?, ?> janusGraphStep = new JanusGraphStep<>(originalGraphStep); TraversalHelper.replaceStep(originalGraphStep, janusGraphStep, traversal); HasStepFolder.foldInIds(janusGraphStep, traversal); HasStepFolder.foldInHasContainer(janusGraphStep, traversal, traversal); HasStepFolder.foldInOrder(janusGraphStep, janusGraphStep.getNextStep(), traversal, traversal, janusGraphStep.returnsVertex(), null); HasStepFolder.foldInRange(janusGraphStep, JanusGraphTraversalUtil.getNextNonIdentityStep(janusGraphStep), traversal, null); } else { //Make sure that any provided "start" elements are instantiated in the current transaction final Object[] ids = originalGraphStep.getIds(); ElementUtils.verifyArgsMustBeEitherIdOrElement(ids); if (ids[0] instanceof Element) { //GraphStep constructor ensures that the entire array is elements final Object[] elementIds = new Object[ids.length]; for (int i = 0; i < ids.length; i++) { elementIds[i] = ((Element) ids[i]).id(); } originalGraphStep.setIteratorSupplier(() -> originalGraphStep.returnsVertex() ? ((Graph) originalGraphStep.getTraversal().getGraph().get()).vertices(elementIds) : ((Graph) originalGraphStep.getTraversal().getGraph().get()).edges(elementIds)); } } }); }