Search in sources :

Example 21 with AbstractIterator

use of com.google.common.collect.AbstractIterator in project presto by prestodb.

the class PagesIndex method getSortedPages.

public Iterator<Page> getSortedPages() {
    return new AbstractIterator<Page>() {

        private int currentPosition;

        private final PageBuilder pageBuilder = new PageBuilder(types);

        private final int[] outputChannels = new int[types.size()];

        {
            Arrays.setAll(outputChannels, IntUnaryOperator.identity());
        }

        @Override
        public Page computeNext() {
            currentPosition = buildPage(currentPosition, outputChannels, pageBuilder);
            if (pageBuilder.isEmpty()) {
                return endOfData();
            }
            Page page = pageBuilder.build();
            pageBuilder.reset();
            return page;
        }
    };
}
Also used : Page(com.facebook.presto.common.Page) AbstractIterator(com.google.common.collect.AbstractIterator) PageBuilder(com.facebook.presto.common.PageBuilder)

Example 22 with AbstractIterator

use of com.google.common.collect.AbstractIterator in project presto by prestodb.

the class FileFragmentResultCacheManager method closeWhenExhausted.

private static <T> Iterator<T> closeWhenExhausted(Iterator<T> iterator, Closeable resource) {
    requireNonNull(iterator, "iterator is null");
    requireNonNull(resource, "resource is null");
    return new AbstractIterator<T>() {

        @Override
        protected T computeNext() {
            if (iterator.hasNext()) {
                return iterator.next();
            }
            try {
                resource.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return endOfData();
        }
    };
}
Also used : UncheckedIOException(java.io.UncheckedIOException) AbstractIterator(com.google.common.collect.AbstractIterator) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 23 with AbstractIterator

use of com.google.common.collect.AbstractIterator in project presto by prestodb.

the class FileSingleStreamSpiller method closeWhenExhausted.

private static <T> Iterator<T> closeWhenExhausted(Iterator<T> iterator, Closeable resource) {
    requireNonNull(iterator, "iterator is null");
    requireNonNull(resource, "resource is null");
    return new AbstractIterator<T>() {

        @Override
        protected T computeNext() {
            if (iterator.hasNext()) {
                return iterator.next();
            }
            try {
                resource.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return endOfData();
        }
    };
}
Also used : UncheckedIOException(java.io.UncheckedIOException) AbstractIterator(com.google.common.collect.AbstractIterator) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 24 with AbstractIterator

use of com.google.common.collect.AbstractIterator in project voldemort by voldemort.

the class AdminCommandStream method readEntriesBinary.

private static Iterator<Pair<ByteArray, Versioned<byte[]>>> readEntriesBinary(File inputDir, String storeName) throws IOException {
    File inputFile = new File(inputDir, storeName + ".entries");
    if (!inputFile.exists()) {
        throw new FileNotFoundException("File " + inputFile.getAbsolutePath() + " does not exist!");
    }
    final DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
    return new AbstractIterator<Pair<ByteArray, Versioned<byte[]>>>() {

        @Override
        protected Pair<ByteArray, Versioned<byte[]>> computeNext() {
            try {
                int length = dis.readInt();
                byte[] keyBytes = new byte[length];
                ByteUtils.read(dis, keyBytes);
                length = dis.readInt();
                byte[] versionBytes = new byte[length];
                ByteUtils.read(dis, versionBytes);
                length = dis.readInt();
                byte[] valueBytes = new byte[length];
                ByteUtils.read(dis, valueBytes);
                ByteArray key = new ByteArray(keyBytes);
                VectorClock version = new VectorClock(versionBytes);
                Versioned<byte[]> value = new Versioned<byte[]>(valueBytes, version);
                return new Pair<ByteArray, Versioned<byte[]>>(key, value);
            } catch (EOFException e) {
                try {
                    dis.close();
                } catch (IOException ie) {
                    ie.printStackTrace();
                }
                return endOfData();
            } catch (IOException e) {
                try {
                    dis.close();
                } catch (IOException ie) {
                    ie.printStackTrace();
                }
                throw new VoldemortException("Error reading from input file ", e);
            }
        }
    };
}
Also used : Versioned(voldemort.versioning.Versioned) VectorClock(voldemort.versioning.VectorClock) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) VoldemortException(voldemort.VoldemortException) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) EOFException(java.io.EOFException) ByteArray(voldemort.utils.ByteArray) AbstractIterator(com.google.common.collect.AbstractIterator) File(java.io.File) Pair(voldemort.utils.Pair)

Example 25 with AbstractIterator

use of com.google.common.collect.AbstractIterator in project xtext-core by eclipse.

the class SemanticRegionIterable method iterator.

@Override
public Iterator<ISemanticRegion> iterator() {
    return new AbstractIterator<ISemanticRegion>() {

        private ISemanticRegion next = first;

        @Override
        protected ISemanticRegion computeNext() {
            if (next == null)
                return endOfData();
            ISemanticRegion result = next;
            next = next.getNextSemanticRegion();
            if (result == last)
                next = null;
            return result;
        }
    };
}
Also used : AbstractIterator(com.google.common.collect.AbstractIterator) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)

Aggregations

AbstractIterator (com.google.common.collect.AbstractIterator)55 IOException (java.io.IOException)15 Iterator (java.util.Iterator)14 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 File (java.io.File)5 EOFException (java.io.EOFException)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)3 SMALLINT (com.facebook.presto.common.type.SmallintType.SMALLINT)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Deque (java.util.Deque)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 Test (org.junit.Test)3 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)2 TopicId (co.cask.cdap.proto.id.TopicId)2