org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
protected Traverser.Admin<E> processNextStart() throws NoSuchElementException {
    if (this.first) {
        this.first = false;
        this.localTraversal.addStart(this.starts.next());
    }
    while (true) {
        if (this.localTraversal.hasNext())
            return this.localTraversal.nextTraverser();
        else if (this.starts.hasNext()) {
            this.localTraversal.reset();
            this.localTraversal.addStart(this.starts.next());
        } else {
            throw FastNoSuchElementException.instance();
        }
    }
}
 
Example #2
Source File: IoStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
protected Traverser.Admin<S> processNextStart() {
    if (mode == Mode.UNSET) throw new IllegalStateException("IO mode was not set to read() or write()");
    if (!this.first) throw FastNoSuchElementException.instance();

    this.first = false;
    final File file = new File(this.file);

    if (mode == Mode.READING) {
        if (!file.exists()) throw new IllegalStateException(this.file + " does not exist");
        return read(file);
    } else if (mode == Mode.WRITING) {
        return write(file);
    } else {
        throw new IllegalStateException("Invalid ReadWriting.Mode configured in IoStep: " + mode.name());
    }
}
 
Example #3
Source File: CoreTraversalTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldThrowFastNoSuchElementExceptionInNestedTraversals() {
    //The nested traversal should throw a regular FastNoSuchElementException

    final GraphTraversal<Object, Object> nestedTraversal = __.has("name", "foo");
    final GraphTraversal<Vertex, Object> traversal = g.V().has("name", "marko").branch(nestedTraversal);

    final GraphTraversal.Admin<Object, Object> nestedTraversalAdmin = nestedTraversal.asAdmin();
    nestedTraversalAdmin.reset();
    nestedTraversalAdmin.addStart(nestedTraversalAdmin.getTraverserGenerator().generate(g.V().has("name", "marko").next(), (Step) traversal.asAdmin().getStartStep(), 1l));

    try {
        nestedTraversal.next();
    } catch (NoSuchElementException e) {
        assertEquals(FastNoSuchElementException.class, e.getClass());
    }
}
 
Example #4
Source File: MultiIterator.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public T next() {
    if (this.iterators.isEmpty()) throw FastNoSuchElementException.instance();

    Iterator<T> currentIterator = iterators.get(this.current);
    while (true) {
        if (currentIterator.hasNext()) {
            return currentIterator.next();
        } else {
            this.current++;
            if (this.current >= iterators.size())
                break;
            currentIterator = iterators.get(current);
        }
    }
    throw FastNoSuchElementException.instance();
}
 
Example #5
Source File: GraphStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
protected Traverser.Admin<E> processNextStart() {
    while (true) {
        if (this.iterator.hasNext()) {
            return this.isStart ? this.getTraversal().getTraverserGenerator().generate(this.iterator.next(), (Step) this, 1l) : this.head.split(this.iterator.next(), this);
        } else {
            if (this.isStart) {
                if (this.done)
                    throw FastNoSuchElementException.instance();
                else {
                    this.done = true;
                    this.iterator = null == this.iteratorSupplier ? EmptyIterator.instance() : this.iteratorSupplier.get();
                }
            } else {
                this.head = this.starts.next();
                this.iterator = null == this.iteratorSupplier ? EmptyIterator.instance() : this.iteratorSupplier.get();
            }
        }
    }
}
 
Example #6
Source File: TextIterator.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public String next() {
    try {
        if (this.available) {
            this.available = false;
            return this.line;
        } else {
            while (true) {
                if (this.readers.isEmpty())
                    throw FastNoSuchElementException.instance();
                if ((this.line = this.readers.peek().readLine()) != null) {
                    return this.line;
                } else
                    this.readers.remove().close();
            }
        }
    } catch (final IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #7
Source File: HadoopVertexIterator.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Vertex next() {
    try {
        if (this.nextVertex != null) {
            final Vertex temp = this.nextVertex;
            this.nextVertex = null;
            return temp;
        } else {
            while (!this.readers.isEmpty()) {
                if (this.readers.peek().nextKeyValue())
                    return new HadoopVertex(this.readers.peek().getCurrentValue().get(), this.graph);
                else
                    this.readers.remove().close();
            }
        }
        throw FastNoSuchElementException.instance();
    } catch (final Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #8
Source File: HadoopEdgeIterator.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Edge next() {
    try {
        while (true) {
            if (this.edgeIterator.hasNext())
                return new HadoopEdge(this.edgeIterator.next(), this.graph);
            if (this.readers.isEmpty())
                throw FastNoSuchElementException.instance();
            if (this.readers.peek().nextKeyValue()) {
                this.edgeIterator = this.readers.peek().getCurrentValue().get().edges(Direction.OUT);
            } else {
                this.readers.remove().close();
            }
        }
    } catch (final Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #9
Source File: CloseableIteratorUtils.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@Override
public T next() {
    if (this.iterators.isEmpty()) throw FastNoSuchElementException.instance();

    Iterator<T> currentIterator = iterators.get(this.current);
    while (true) {
        if (currentIterator.hasNext()) {
            return currentIterator.next();
        } else {
            this.current++;
            if (this.current >= iterators.size())
                break;
            currentIterator = iterators.get(current);
        }
    }
    throw FastNoSuchElementException.instance();
}
 
Example #10
Source File: SqlgLocalStepBarrier.java    From sqlg with MIT License 6 votes vote down vote up
@Override
protected Traverser.Admin<E> processNextStart() throws NoSuchElementException {
    if (this.first) {
        this.first = false;
        while (this.starts.hasNext()) {
            this.localTraversal.addStart(this.starts.next());
        }
        while (this.localTraversal.hasNext()) {
            this.results.add(this.localTraversal.nextTraverser());
        }
        this.results.sort((o1, o2) -> {
            SqlgTraverser x = (SqlgTraverser) o1;
            SqlgTraverser y = (SqlgTraverser) o2;
            return Long.compare(x.getStartElementIndex(), y.getStartElementIndex());
        });
        this.resultIterator = this.results.iterator();
    }
    if (this.resultIterator.hasNext()) {
        return this.resultIterator.next();
    } else {
        throw FastNoSuchElementException.instance();
    }
}
 
Example #11
Source File: SqlgGraphStep.java    From sqlg with MIT License 6 votes vote down vote up
private boolean applyRange(Emit<E> emit) {
    if (this.lastReplacedStep.hasRange() && this.lastReplacedStep.applyInStep() && this.lastReplacedStep.getDepth() == emit.getReplacedStepDepth()) {
        if (this.lastReplacedStep.getSqlgRangeHolder().hasRange()) {
            if (this.lastReplacedStep.getSqlgRangeHolder().getRange().isBefore(this.rangeCount + 1)) {
                throw FastNoSuchElementException.instance();
            }
            if (this.lastReplacedStep.getSqlgRangeHolder().getRange().isAfter(this.rangeCount)) {
                this.rangeCount++;
                return true;
            }
        } else {
            Preconditions.checkState(this.lastReplacedStep.getSqlgRangeHolder().hasSkip(), "If not a range query then it must be a skip.");
            if (this.rangeCount < this.lastReplacedStep.getSqlgRangeHolder().getSkip()) {
                this.rangeCount++;
                return true;
            }
        }
        this.rangeCount++;
    }
    return false;
}
 
Example #12
Source File: SqlgAddVertexStartStep.java    From sqlg with MIT License 6 votes vote down vote up
@Override
protected Traverser.Admin<Vertex> processNextStart() {
    if (this.first) {
        this.first = false;
        final SqlgTraverserGenerator generator = SqlgTraverserGenerator.instance();
        final Vertex vertex = this.getTraversal().getGraph().get().addVertex(
                this.parameters.getKeyValues(
                        generator.generate(false, (Step)this, 1L, false, false)
                ));
        if (this.callbackRegistry != null && !this.callbackRegistry.getCallbacks().isEmpty()) {
            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 generator.generate(vertex, (Step)this, 1L, false, false);
    } else
        throw FastNoSuchElementException.instance();
}
 
Example #13
Source File: TLongMultiIterator.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public long next() {
  if (iterators.isEmpty()) throw FastNoSuchElementException.instance();

  TLongIterator currentIterator = iterators.get(this.current);
  while (true) {
    if (currentIterator.hasNext()) {
      return currentIterator.next();
    } else {
      this.current++;
      if (this.current >= iterators.size())
        break;
      currentIterator = iterators.get(current);
    }
  }
  throw FastNoSuchElementException.instance();
}
 
Example #14
Source File: TinkerCountGlobalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #15
Source File: MapIterator.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple2<K, V> next() {
    if (!this.queue.isEmpty())
        return this.queue.remove();
    else if (!this.inputIterator.hasNext()) {
        this.mapReduce.workerEnd(MapReduce.Stage.MAP);
        throw FastNoSuchElementException.instance();
    } else {
        this.processNext();
        return this.next();
    }
}
 
Example #16
Source File: ReduceIterator.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple2<OK, OV> next() {
    if (!this.queue.isEmpty())
        return this.queue.remove();
    else if (!this.inputIterator.hasNext()) {
        this.mapReduce.workerEnd(MapReduce.Stage.REDUCE);
        throw FastNoSuchElementException.instance();
    } else {
        this.processNext();
        return this.next();
    }
}
 
Example #17
Source File: SqlgVertexStep.java    From sqlg with MIT License 5 votes vote down vote up
private boolean applyRange(Emit<E> emit) {
    if (this.lastReplacedStep.hasRange() && this.lastReplacedStep.applyInStep() && this.lastReplacedStep.getDepth() == emit.getReplacedStepDepth()) {
        if (this.lastReplacedStep.getSqlgRangeHolder().getRange().isBefore(this.rangeCount + 1)) {
            throw FastNoSuchElementException.instance();
        }
        if (this.lastReplacedStep.getSqlgRangeHolder().getRange().isAfter(this.rangeCount)) {
            this.rangeCount++;
            return true;
        }
        this.rangeCount++;
    }
    return false;
}
 
Example #18
Source File: SqlgRepeatStepBarrier.java    From sqlg with MIT License 5 votes vote down vote up
private boolean applyRange() {
    if (this.sqlgRangeHolder.getRange().isBefore(this.rangeCount + 1)) {
        throw FastNoSuchElementException.instance();
    }
    if (this.sqlgRangeHolder.getRange().isAfter(this.rangeCount)) {
        this.rangeCount++;
        return true;
    }
    this.rangeCount++;
    return false;
}
 
Example #19
Source File: SqlgExpandableStepIterator.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public Traverser.Admin<S> next() {
    if (!this.traversers.isEmpty())
        return this.traversers.poll();
    /////////////
    if (this.hostStep.getPreviousStep().hasNext())
        return this.hostStep.getPreviousStep().next();
    /////////////
    throw FastNoSuchElementException.instance();
}
 
Example #20
Source File: PixyCutStep.java    From pixy with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean filter(Traverser.Admin traverser) {
	if (wasCut) {
		throw FastNoSuchElementException.instance();
	} else {
		wasCut = true;
		return true;
	}
}
 
Example #21
Source File: IteratorUtilsTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test(expected = FastNoSuchElementException.class)
public void shouldThrowOnFlatMapIfIteratorExceeded() {
    final Iterator<Iterator<Integer>> itty = Arrays.asList(Arrays.asList(1).iterator(), new ArrayList<Integer>().iterator(), Arrays.asList(4).iterator()).iterator();
    final Iterator<Integer> limitedItty = IteratorUtils.flatMap(itty, x -> IteratorUtils.map(x, i -> i * 10));
    limitedItty.next();
    limitedItty.next();
    limitedItty.next();
}
 
Example #22
Source File: CombineIterator.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple2<OK, OV> next() {
    if (!this.combineMap.isEmpty())
        return this.nextFromCombineMap();
    else if (!this.inputIterator.hasNext()) {
        this.mapReduce.workerEnd(MapReduce.Stage.COMBINE);
        throw FastNoSuchElementException.instance();
    } else {
        this.processNext();
        return this.next();
    }
}
 
Example #23
Source File: DoubleIteratorTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test(expected = FastNoSuchElementException.class)
public void shouldThrowFastNoSuchElementException() {
    final Iterator<String> itty = new DoubleIterator<>("test1", "test2");
    itty.next();
    itty.next();
    itty.next();
}
 
Example #24
Source File: MultiIteratorTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test(expected = FastNoSuchElementException.class)
public void shouldThrowFastNoSuchElementExceptionIfEmptyIteratorsAreAdded() {
    final MultiIterator<String> itty = new MultiIterator<>();
    itty.addIterator(EmptyIterator.instance());
    itty.addIterator(EmptyIterator.instance());
    itty.addIterator(EmptyIterator.instance());
    itty.addIterator(EmptyIterator.instance());
    itty.next();
}
 
Example #25
Source File: SingleIterator.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public T next() {
    if (!this.alive)
        throw FastNoSuchElementException.instance();
    else {
        this.alive = false;
        return t;
    }
}
 
Example #26
Source File: IteratorUtils.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static final <S, E> Iterator<E> flatMap(final Iterator<S> iterator, final Function<S, Iterator<E>> function) {
    return new Iterator<E>() {

        private Iterator<E> currentIterator = Collections.emptyIterator();

        @Override
        public boolean hasNext() {
            if (this.currentIterator.hasNext())
                return true;
            else {
                while (iterator.hasNext()) {
                    this.currentIterator = function.apply(iterator.next());
                    if (this.currentIterator.hasNext())
                        return true;
                }
            }
            return false;
        }

        @Override
        public void remove() {
            iterator.remove();
        }

        @Override
        public E next() {
            if (this.hasNext())
                return this.currentIterator.next();
            else
                throw FastNoSuchElementException.instance();
        }
    };
}
 
Example #27
Source File: DoubleIterator.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public T next() {
    if (this.current == 'x')
        throw FastNoSuchElementException.instance();
    else {
        if (this.current == 'a') {
            this.current = 'b';
            return this.a;
        } else {
            this.current = 'x';
            return this.b;
        }
    }
}
 
Example #28
Source File: ArrayIterator.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public T next() {
    if (this.hasNext()) {
        this.current++;
        return this.array[this.current - 1];
    } else {
        throw FastNoSuchElementException.instance();
    }
}
 
Example #29
Source File: TraverserSet.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Traverser.Admin<S> remove() {  // pop, exception if empty
    final Iterator<Traverser.Admin<S>> iterator = this.map.values().iterator();
    if (!iterator.hasNext())
        throw FastNoSuchElementException.instance();
    final Traverser.Admin<S> next = iterator.next();
    iterator.remove();
    return next;
}
 
Example #30
Source File: BranchStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
protected Iterator<Traverser.Admin<E>> standardAlgorithm() {
    while (true) {
        if (!this.first) {
            // this block is ignored on the first pass through the while(true) giving the opportunity for
            // the traversalOptions to be prepared. Iterate all of them and simply return the ones that yield
            // results. applyCurrentTraverser() will have only injected the current traverser into the options
            // that met the choice requirements.  Note that in addGlobalChildOption an IdentityStep was added to
            // be a holder for that current traverser. That allows us to check that first step for an injected
            // traverser as part of the condition for using that traversal option in the output. This is necessary
            // because barriers like fold(), max(), etc. will always return true for hasNext() even if a traverser
            // was not seeded in applyCurrentTraverser().
            for (final Traversal.Admin<S, E> option : getGlobalChildren()) {
                if (option.getStartStep().hasNext() && option.hasNext())
                    return option.getEndStep();
            }
        }

        this.first = false;

        // pass the current traverser to applyCurrentTraverser() which will make the "choice" of traversal to
        // apply with the given traverser. as this is in a while(true) this phase essentially prepares the options
        // for execution above
        if (this.hasBarrier) {
            if (!this.starts.hasNext())
                throw FastNoSuchElementException.instance();
            while (this.starts.hasNext()) {
                this.applyCurrentTraverser(this.starts.next());
            }
        } else {
            this.applyCurrentTraverser(this.starts.next());
        }
    }
}