Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException#instance()

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException#instance() . 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: 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 2
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 3
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 4
Source File: RangeGlobalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean filter(final Traverser.Admin<S> traverser) {
    if (this.bypass) return true;

    if (this.high != -1 && this.counter.get() >= this.high) {
        // This is a global step and this place would be the end of the traversal.
        // Close the traversal to free up resources.
        CloseableIterator.closeIterator(traversal);
        throw FastNoSuchElementException.instance();
    }

    long avail = traverser.bulk();
    if (this.counter.get() + avail <= this.low) {
        // Will not surpass the low w/ this traverser. Skip and filter the whole thing.
        this.counter.getAndAdd(avail);
        return false;
    }

    // Skip for the low and trim for the high. Both can happen at once.

    long toSkip = 0;
    if (this.counter.get() < this.low) {
        toSkip = this.low - this.counter.get();
    }

    long toTrim = 0;
    if (this.high != -1 && this.counter.get() + avail >= this.high) {
        toTrim = this.counter.get() + avail - this.high;
    }

    long toEmit = avail - toSkip - toTrim;
    this.counter.getAndAdd(toSkip + toEmit);
    traverser.setBulk(toEmit);

    return true;
}
 
Example 5
Source File: DedupGlobalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Map<Object, Traverser.Admin<S>> nextBarrier() throws NoSuchElementException {
    final Map<Object, Traverser.Admin<S>> map = null != this.barrier ? this.barrier : new HashMap<>();
    while (this.starts.hasNext()) {
        final Traverser.Admin<S> traverser = this.starts.next();
        final Object object;
        if (null != this.dedupLabels) {
            object = new ArrayList<>(this.dedupLabels.size());
            for (final String label : this.dedupLabels) {
                ((List) object).add(TraversalUtil.applyNullable((S) this.getSafeScopeValue(Pop.last, label, traverser), this.dedupTraversal));
            }
        } else {
            object = TraversalUtil.applyNullable(traverser, this.dedupTraversal);
        }
        if (!map.containsKey(object)) {
            traverser.setBulk(1L);

            // DetachedProperty and DetachedVertexProperty both have a transient for the Host element. that causes
            // trouble for olap which ends up requiring the Host later. can't change the transient without some
            // consequences: (1) we break gryo formatting and io tests start failing (2) storing the element with
            // the property has the potential to bloat detached Element instances as it basically stores that data
            // twice. Not sure if it's smart to change that at least in 3.4.x and not without some considerable
            // thought as to what might be major changes. To work around the problem we will detach properties as
            // references so that the parent element goes with it. Also, given TINKERPOP-2318 property comparisons
            // have changed in such a way that allows this to work properly
            if (traverser.get() instanceof Property)
                traverser.set(ReferenceFactory.detach(traverser.get()));
            else
                traverser.set(DetachedFactory.detach(traverser.get(), true));
            map.put(object, traverser);
        }
    }
    this.barrier = null;
    this.barrierIterator = null;
    if (map.isEmpty())
        throw FastNoSuchElementException.instance();
    else
        return map;
}
 
Example 6
Source File: MeanLocalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
protected Number map(final Traverser.Admin<S> traverser) {
    final Iterator<E> iterator = traverser.get().iterator();
    if (iterator.hasNext()) {
        Long counter = 1L;
        E result = iterator.next();
        while (iterator.hasNext()) {
            result = (E) NumberHelper.add(result, iterator.next());
            counter++;
        }
        return NumberHelper.div(result, counter, true);
    }
    throw FastNoSuchElementException.instance();
}
 
Example 7
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 8
Source File: AggregateGlobalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public TraverserSet<S> nextBarrier() throws NoSuchElementException {
    if (this.barrier.isEmpty()) {
        this.processAllStarts();
    }
    if (this.barrier.isEmpty())
        throw FastNoSuchElementException.instance();
    else {
        final TraverserSet<S> temp = this.barrier;
        this.barrier = new TraverserSet<>();
        return temp;
    }
}
 
Example 9
Source File: NoOpBarrierStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public TraverserSet<S> nextBarrier() throws NoSuchElementException {
    this.processAllStarts();
    if (this.barrier.isEmpty())
        throw FastNoSuchElementException.instance();
    else {
        final TraverserSet<S> temp = this.barrier;
        this.barrier = new TraverserSet<>();
        return temp;
    }
}
 
Example 10
Source File: TailGlobalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public TraverserSet<S> nextBarrier() throws NoSuchElementException {
    if (!this.starts.hasNext())
        throw FastNoSuchElementException.instance();
    final TraverserSet<S> barrier = new TraverserSet<>();
    while (this.starts.hasNext()) {
        barrier.add(this.starts.next());
    }
    return barrier;
}
 
Example 11
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 12
Source File: HugeCountStep.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
protected Admin<Long> processNextStart() throws NoSuchElementException {
    if (this.done) {
        throw FastNoSuchElementException.instance();
    }
    this.done = true;
    @SuppressWarnings({ "unchecked", "rawtypes" })
    Step<Long, Long> step = (Step) this;
    return this.getTraversal().getTraverserGenerator()
               .generate(this.originGraphStep.count(), step, 1L);
}
 
Example 13
Source File: RangeGlobalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public TraverserSet<S> nextBarrier() throws NoSuchElementException {
    if(!this.starts.hasNext())
        throw FastNoSuchElementException.instance();
    final TraverserSet<S> barrier = new TraverserSet<>();
    while (this.starts.hasNext()) {
        barrier.add(this.starts.next());
    }
    return barrier;
}
 
Example 14
Source File: CloseableIteratorUtils.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
public static <S, E> Iterator<E> flatMap(final Iterator<S> iterator, final Function<S, Iterator<E>> function) {
    return new CloseableIterator<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();
        }

        @Override
        public void close() {
            CloseableIterator.closeIterator(iterator);
        }
    };
}
 
Example 15
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 16
Source File: JanusGraphVertexStep.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private void initialize() {
    initialized = true;
    if (useMultiQuery) {
        setParentMultiQueryStep();

        if (!starts.hasNext()) {
            throw FastNoSuchElementException.instance();
        }
        final List<Admin<Vertex>> vertices = new ArrayList<>();
        starts.forEachRemaining(vertices::add);
        starts.add(vertices.iterator());
        initializeMultiQuery(vertices);
    }
}
 
Example 17
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 18
Source File: EmptyIterator.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public S next() {
    throw FastNoSuchElementException.instance();
}
 
Example 19
Source File: CloseableIteratorUtils.java    From hgraphdb with Apache License 2.0 4 votes vote down vote up
public static <S> Iterator<S> filter(final Iterator<S> iterator, final Predicate<S> predicate) {
    return new CloseableIterator<S>() {
        S nextResult = null;

        @Override
        public boolean hasNext() {
            if (null != this.nextResult) {
                return true;
            } else {
                advance();
                return null != this.nextResult;
            }
        }

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

        @Override
        public S next() {
            try {
                if (null != this.nextResult) {
                    return this.nextResult;
                } else {
                    advance();
                    if (null != this.nextResult)
                        return this.nextResult;
                    else
                        throw FastNoSuchElementException.instance();
                }
            } finally {
                this.nextResult = null;
            }
        }

        private void advance() {
            this.nextResult = null;
            while (iterator.hasNext()) {
                final S s = iterator.next();
                if (predicate.test(s)) {
                    this.nextResult = s;
                    return;
                }
            }
        }

        @Override
        public void close() {
            CloseableIterator.closeIterator(iterator);
        }
    };
}
 
Example 20
Source File: EmptyStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public Traverser.Admin<E> next() {
    throw FastNoSuchElementException.instance();
}