@Override public Iterator<M> receiveMessages() { return IteratorUtils.of(this.message); }
@Override public Iterator<Vertex> vertices(final Direction direction) { if (removed) return Collections.emptyIterator(); switch (direction) { case OUT: return IteratorUtils.of(this.outVertex); case IN: return IteratorUtils.of(this.inVertex); default: return IteratorUtils.of(this.outVertex, this.inVertex); } }
@Override public <V> Iterator<Property<V>> properties(final String... propertyKeys) { if (null == this.properties) return Collections.emptyIterator(); if (propertyKeys.length == 1) { final Property<V> property = this.properties.get(propertyKeys[0]); return null == property ? Collections.emptyIterator() : IteratorUtils.of(property); } else return (Iterator) this.properties.entrySet().stream().filter(entry -> ElementHelper.keyExists(entry.getKey(), propertyKeys)).map(entry -> entry.getValue()).collect(Collectors.toList()).iterator(); } }
@Override public <U> Iterator<Property<U>> properties(final String... propertyKeys) { if (null == this.properties) return Collections.emptyIterator(); if (propertyKeys.length == 1) { final Property<U> property = this.properties.get(propertyKeys[0]); return null == property ? Collections.emptyIterator() : IteratorUtils.of(property); } else return (Iterator) this.properties.entrySet().stream().filter(entry -> ElementHelper.keyExists(entry.getKey(), propertyKeys)).map(entry -> entry.getValue()).collect(Collectors.toList()).iterator(); } }
public static Iterator asIterator(final Object o) { final Iterator itty; if (o instanceof Iterable) itty = ((Iterable) o).iterator(); else if (o instanceof Iterator) itty = (Iterator) o; else if (o instanceof Object[]) itty = new ArrayIterator<>((Object[]) o); else if (o instanceof Stream) itty = ((Stream) o).iterator(); else if (o instanceof Map) itty = ((Map) o).entrySet().iterator(); else if (o instanceof Throwable) itty = of(((Throwable) o).getMessage()); else itty = of(o); return itty; }
@Override protected Iterator<E> flatMap(final Traverser.Admin<S> traverser) { final S s = traverser.get(); if (s instanceof Iterator) return (Iterator) s; else if (s instanceof Iterable) return ((Iterable) s).iterator(); else if (s instanceof Map) return ((Map) s).entrySet().iterator(); else if (s.getClass().isArray()) return handleArrays(s); else return IteratorUtils.of((E) s); }
@Override public <U> Iterator<Property<U>> properties(final String... propertyKeys) { final Map<String, Object> properties = null == metaProperties ? null : metaProperties.get(this.id); if (null == properties || properties.isEmpty()) return Collections.emptyIterator(); else if (propertyKeys.length == 0) return (Iterator) properties.entrySet().stream() .map(entry -> new StarProperty<>(entry.getKey(), entry.getValue(), this)) .iterator(); else if (propertyKeys.length == 1) { final Object v = properties.get(propertyKeys[0]); return null == v ? Collections.emptyIterator() : (Iterator) IteratorUtils.of(new StarProperty<>(propertyKeys[0], v, this)); } else { return (Iterator) properties.entrySet().stream() .filter(entry -> ElementHelper.keyExists(entry.getKey(), propertyKeys)) .map(entry -> new StarProperty<>(entry.getKey(), entry.getValue(), this)) .iterator(); } }
@Override public <V> Iterator<Property<V>> properties(final String... propertyKeys) { Map<String, Object> properties = null == edgeProperties ? null : edgeProperties.get(this.id); if (null == properties || properties.isEmpty()) return Collections.emptyIterator(); else if (propertyKeys.length == 0) return (Iterator) properties.entrySet().stream() .map(entry -> new StarProperty<>(entry.getKey(), entry.getValue(), this)) .iterator(); else if (propertyKeys.length == 1) { final Object v = properties.get(propertyKeys[0]); return null == v ? Collections.emptyIterator() : (Iterator) IteratorUtils.of(new StarProperty<>(propertyKeys[0], v, this)); } else { return (Iterator) properties.entrySet().stream() .filter(entry -> ElementHelper.keyExists(entry.getKey(), propertyKeys)) .map(entry -> new StarProperty<>(entry.getKey(), entry.getValue(), this)) .iterator(); } }
@Override public Iterator<Vertex> vertices(final Direction direction) { if (direction.equals(Direction.OUT)) return IteratorUtils.of(this.outVertex()); else if (direction.equals(Direction.IN)) return IteratorUtils.of(this.inVertex()); else return IteratorUtils.of(this.outVertex(), this.inVertex()); }
@Override public <V> Iterator<VertexProperty<V>> properties(final String... propertyKeys) { if (this.removed) return Collections.emptyIterator(); if (TinkerHelper.inComputerMode((TinkerGraph) graph())) return (Iterator) ((TinkerGraph) graph()).graphComputerView.getProperties(TinkerVertex.this).stream().filter(p -> ElementHelper.keyExists(p.key(), propertyKeys)).iterator(); else { if (null == this.properties) return Collections.emptyIterator(); if (propertyKeys.length == 1) { final List<VertexProperty> properties = this.properties.getOrDefault(propertyKeys[0], Collections.emptyList()); if (properties.size() == 1) { return IteratorUtils.of(properties.get(0)); } else if (properties.isEmpty()) { return Collections.emptyIterator(); } else { return (Iterator) new ArrayList<>(properties).iterator(); } } else return (Iterator) this.properties.entrySet().stream().filter(entry -> ElementHelper.keyExists(entry.getKey(), propertyKeys)).flatMap(entry -> entry.getValue().stream()).collect(Collectors.toList()).iterator(); } } }
@Override public Iterator<Vertex> vertices(final Direction direction) { switch (direction) { case OUT: return IteratorUtils.of(this.graph.vertices(getBaseEdge().vertices(Direction.OUT).next().id())).next(); case IN: return IteratorUtils.of(this.graph.vertices(getBaseEdge().vertices(Direction.IN).next().id())).next(); default: { final Iterator<Vertex> iterator = getBaseEdge().vertices(Direction.BOTH); return IteratorUtils.of(this.graph.vertices(iterator.next().id()).next(), this.graph.vertices(iterator.next().id()).next()); } } }
/** * Uses a {@link LoadBalancingStrategy} to choose the best {@link Host} and then selects the best connection * from that host's connection pool. */ @Override protected Connection chooseConnection(final RequestMessage msg) throws TimeoutException, ConnectionException { final Iterator<Host> possibleHosts; if (msg.optionalArgs(Tokens.ARGS_HOST).isPresent()) { // TODO: not sure what should be done if unavailable - select new host and re-submit traversal? final Host host = (Host) msg.getArgs().get(Tokens.ARGS_HOST); msg.getArgs().remove(Tokens.ARGS_HOST); possibleHosts = IteratorUtils.of(host); } else { possibleHosts = this.cluster.loadBalancingStrategy().select(msg); } // you can get no possible hosts in more than a few situations. perhaps the servers are just all down. // or perhaps the client is not configured properly (disables ssl when ssl is enabled on the server). if (!possibleHosts.hasNext()) throw new TimeoutException("Timed out while waiting for an available host - check the client configuration and connectivity to the server if this message persists"); final Host bestHost = possibleHosts.next(); final ConnectionPool pool = hostConnectionPools.get(bestHost); return pool.borrowConnection(cluster.connectionPoolSettings().maxWaitForConnection, TimeUnit.MILLISECONDS); }
@Override public Iterator<Vertex> vertices(final Direction direction) { this.graph.tx().readWrite(); switch (direction) { case OUT: return IteratorUtils.of(new Neo4jVertex(this.getBaseEdge().start(), this.graph)); case IN: return IteratorUtils.of(new Neo4jVertex(this.getBaseEdge().end(), this.graph)); default: return IteratorUtils.of(new Neo4jVertex(this.getBaseEdge().start(), this.graph), new Neo4jVertex(this.getBaseEdge().end(), this.graph)); } }
@Override protected Iterator<Traverser.Admin<S>> computerAlgorithm() throws NoSuchElementException { if (null == this.repeatTraversal) throw new IllegalStateException("The repeat()-traversal was not defined: " + this); final Traverser.Admin<S> start = this.starts.next(); if (doUntil(start, true)) { start.setStepId(this.getNextStep().getId()); start.addLabels(this.labels); return IteratorUtils.of(start); } else { start.setStepId(this.repeatTraversal.getStartStep().getId()); start.initialiseLoops(start.getStepId(), this.loopName); if (doEmit(start, true)) { final Traverser.Admin<S> emitSplit = start.split(); emitSplit.resetLoops(); emitSplit.setStepId(this.getNextStep().getId()); return IteratorUtils.of(start, emitSplit); } else { return IteratorUtils.of(start); } } }
@Override protected Iterator<Traverser.Admin<S>> standardAlgorithm() throws NoSuchElementException { if (null == this.repeatTraversal) throw new IllegalStateException("The repeat()-traversal was not defined: " + this); while (true) { if (this.repeatTraversal.getEndStep().hasNext()) { return this.repeatTraversal.getEndStep(); } else { final Traverser.Admin<S> start = this.starts.next(); start.initialiseLoops(this.getId(), this.loopName); if (doUntil(start, true)) { start.resetLoops(); return IteratorUtils.of(start); } this.repeatTraversal.addStart(start); if (doEmit(start, true)) { final Traverser.Admin<S> emitSplit = start.split(); emitSplit.resetLoops(); return IteratorUtils.of(emitSplit); } } } }
@Override protected Iterator<Traverser.Admin<S>> standardAlgorithm() throws NoSuchElementException { final RepeatStep<S> repeatStep = (RepeatStep<S>) this.getTraversal().getParent(); while (true) { final Traverser.Admin<S> start = this.starts.next(); start.incrLoops(); if (repeatStep.doUntil(start, false)) { start.resetLoops(); return IteratorUtils.of(start); } else { if (!repeatStep.untilFirst && !repeatStep.emitFirst) repeatStep.repeatTraversal.addStart(start); else repeatStep.addStart(start); if (repeatStep.doEmit(start, false)) { final Traverser.Admin<S> emitSplit = start.split(); emitSplit.resetLoops(); return IteratorUtils.of(emitSplit); } } } }
@Override protected Iterator<Traverser.Admin<S>> computerAlgorithm() throws NoSuchElementException { final RepeatStep<S> repeatStep = (RepeatStep<S>) this.getTraversal().getParent(); final Traverser.Admin<S> start = this.starts.next(); start.incrLoops(); if (repeatStep.doUntil(start, false)) { start.resetLoops(); start.setStepId(repeatStep.getNextStep().getId()); start.addLabels(repeatStep.labels); return IteratorUtils.of(start); } else { start.setStepId(repeatStep.getId()); if (repeatStep.doEmit(start, false)) { final Traverser.Admin<S> emitSplit = start.split(); emitSplit.resetLoops(); emitSplit.setStepId(repeatStep.getNextStep().getId()); emitSplit.addLabels(repeatStep.labels); return IteratorUtils.of(start, emitSplit); } return IteratorUtils.of(start); } } }