Search in sources :

Example 1 with AbstractCloseableIterator

use of co.cask.cdap.api.dataset.lib.AbstractCloseableIterator in project cdap by caskdata.

the class LevelDBMessageTable method read.

@Override
protected CloseableIterator<RawMessageTableEntry> read(byte[] startRow, byte[] stopRow) throws IOException {
    final DBScanIterator iterator = new DBScanIterator(levelDB, startRow, stopRow);
    final RawMessageTableEntry tableEntry = new RawMessageTableEntry();
    return new AbstractCloseableIterator<RawMessageTableEntry>() {

        private boolean closed = false;

        @Override
        protected RawMessageTableEntry computeNext() {
            if (closed || (!iterator.hasNext())) {
                return endOfData();
            }
            Map.Entry<byte[], byte[]> row = iterator.next();
            Map<String, byte[]> columns = decodeValue(row.getValue());
            return tableEntry.set(row.getKey(), columns.get(TX_COL), columns.get(PAYLOAD_COL));
        }

        @Override
        public void close() {
            try {
                iterator.close();
            } finally {
                endOfData();
                closed = true;
            }
        }
    };
}
Also used : AbstractCloseableIterator(co.cask.cdap.api.dataset.lib.AbstractCloseableIterator) RawMessageTableEntry(co.cask.cdap.messaging.store.RawMessageTableEntry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with AbstractCloseableIterator

use of co.cask.cdap.api.dataset.lib.AbstractCloseableIterator in project cdap by caskdata.

the class LevelDBPayloadTable method read.

@Override
protected CloseableIterator<RawPayloadTableEntry> read(byte[] startRow, byte[] stopRow, final int limit) throws IOException {
    final DBScanIterator iterator = new DBScanIterator(levelDB, startRow, stopRow);
    return new AbstractCloseableIterator<RawPayloadTableEntry>() {

        private final RawPayloadTableEntry tableEntry = new RawPayloadTableEntry();

        private boolean closed = false;

        private int maxLimit = limit;

        @Override
        protected RawPayloadTableEntry computeNext() {
            if (closed || maxLimit <= 0 || (!iterator.hasNext())) {
                return endOfData();
            }
            Map.Entry<byte[], byte[]> row = iterator.next();
            maxLimit--;
            return tableEntry.set(row.getKey(), row.getValue());
        }

        @Override
        public void close() {
            try {
                iterator.close();
            } finally {
                endOfData();
                closed = true;
            }
        }
    };
}
Also used : AbstractCloseableIterator(co.cask.cdap.api.dataset.lib.AbstractCloseableIterator) RawPayloadTableEntry(co.cask.cdap.messaging.store.RawPayloadTableEntry) Map(java.util.Map)

Example 3 with AbstractCloseableIterator

use of co.cask.cdap.api.dataset.lib.AbstractCloseableIterator in project cdap by caskdata.

the class FileLogReader method getLog.

@Override
public CloseableIterator<LogEvent> getLog(LoggingContext loggingContext, final long fromTimeMs, final long toTimeMs, Filter filter) {
    try {
        final Filter logFilter = new AndFilter(ImmutableList.of(LoggingContextHelper.createFilter(loggingContext), filter));
        LOG.trace("Using fromTimeMs={}, toTimeMs={}", fromTimeMs, toTimeMs);
        List<LogLocation> sortedFilesInRange = fileMetadataReader.listFiles(LoggingContextHelper.getLogPathIdentifier(loggingContext), fromTimeMs, toTimeMs);
        if (sortedFilesInRange.isEmpty()) {
            // return empty iterator
            return new AbstractCloseableIterator<LogEvent>() {

                @Override
                protected LogEvent computeNext() {
                    return endOfData();
                }

                @Override
                public void close() {
                // no-op
                }
            };
        }
        final Iterator<LogLocation> filesIter = sortedFilesInRange.iterator();
        CloseableIterator<CloseableIterator<LogEvent>> closeableIterator = new CloseableIterator<CloseableIterator<LogEvent>>() {

            private CloseableIterator<LogEvent> curr = null;

            @Override
            public void close() {
                if (curr != null) {
                    curr.close();
                }
            }

            @Override
            public boolean hasNext() {
                return filesIter.hasNext();
            }

            @Override
            public CloseableIterator<LogEvent> next() {
                if (curr != null) {
                    curr.close();
                }
                LogLocation file = filesIter.next();
                LOG.trace("Reading file {}", file);
                curr = file.readLog(logFilter, fromTimeMs, toTimeMs, Integer.MAX_VALUE);
                return curr;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Remove not supported");
            }
        };
        return concat(closeableIterator);
    } catch (Throwable e) {
        LOG.error("Got exception: ", e);
        throw Throwables.propagate(e);
    }
}
Also used : AndFilter(co.cask.cdap.logging.filter.AndFilter) AbstractCloseableIterator(co.cask.cdap.api.dataset.lib.AbstractCloseableIterator) CloseableIterator(co.cask.cdap.api.dataset.lib.CloseableIterator) AbstractCloseableIterator(co.cask.cdap.api.dataset.lib.AbstractCloseableIterator) Filter(co.cask.cdap.logging.filter.Filter) AndFilter(co.cask.cdap.logging.filter.AndFilter) LogLocation(co.cask.cdap.logging.write.LogLocation)

Example 4 with AbstractCloseableIterator

use of co.cask.cdap.api.dataset.lib.AbstractCloseableIterator in project cdap by caskdata.

the class HBaseMessageTable method read.

@Override
protected CloseableIterator<RawMessageTableEntry> read(byte[] startRow, byte[] stopRow) throws IOException {
    Scan scan = tableUtil.buildScan().setStartRow(startRow).setStopRow(stopRow).setCaching(scanCacheRows).build();
    try {
        final ResultScanner scanner = DistributedScanner.create(hTable, scan, rowKeyDistributor, scanExecutor);
        final RawMessageTableEntry tableEntry = new RawMessageTableEntry();
        return new AbstractCloseableIterator<RawMessageTableEntry>() {

            private boolean closed = false;

            @Override
            protected RawMessageTableEntry computeNext() {
                if (closed) {
                    return endOfData();
                }
                Result result;
                try {
                    result = scanner.next();
                } catch (IOException e) {
                    throw exceptionHandler.handleAndWrap(e);
                }
                if (result == null) {
                    return endOfData();
                }
                return tableEntry.set(rowKeyDistributor.getOriginalKey(result.getRow()), result.getValue(columnFamily, TX_COL), result.getValue(columnFamily, PAYLOAD_COL));
            }

            @Override
            public void close() {
                try {
                    scanner.close();
                } finally {
                    endOfData();
                    closed = true;
                }
            }
        };
    } catch (IOException e) {
        throw exceptionHandler.handle(e);
    }
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) AbstractCloseableIterator(co.cask.cdap.api.dataset.lib.AbstractCloseableIterator) RawMessageTableEntry(co.cask.cdap.messaging.store.RawMessageTableEntry) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result)

Example 5 with AbstractCloseableIterator

use of co.cask.cdap.api.dataset.lib.AbstractCloseableIterator in project cdap by caskdata.

the class HBasePayloadTable method read.

@Override
public CloseableIterator<RawPayloadTableEntry> read(byte[] startRow, byte[] stopRow, final int limit) throws IOException {
    Scan scan = tableUtil.buildScan().setStartRow(startRow).setStopRow(stopRow).setCaching(scanCacheRows).build();
    final ResultScanner scanner = DistributedScanner.create(hTable, scan, rowKeyDistributor, scanExecutor);
    return new AbstractCloseableIterator<RawPayloadTableEntry>() {

        private final RawPayloadTableEntry tableEntry = new RawPayloadTableEntry();

        private boolean closed = false;

        private int maxLimit = limit;

        @Override
        protected RawPayloadTableEntry computeNext() {
            if (closed || maxLimit <= 0) {
                return endOfData();
            }
            Result result;
            try {
                result = scanner.next();
            } catch (IOException e) {
                throw exceptionHandler.handleAndWrap(e);
            }
            if (result == null) {
                return endOfData();
            }
            maxLimit--;
            return tableEntry.set(rowKeyDistributor.getOriginalKey(result.getRow()), result.getValue(columnFamily, COL));
        }

        @Override
        public void close() {
            try {
                scanner.close();
            } finally {
                endOfData();
                closed = true;
            }
        }
    };
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) AbstractCloseableIterator(co.cask.cdap.api.dataset.lib.AbstractCloseableIterator) Scan(org.apache.hadoop.hbase.client.Scan) RawPayloadTableEntry(co.cask.cdap.messaging.store.RawPayloadTableEntry) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result)

Aggregations

AbstractCloseableIterator (co.cask.cdap.api.dataset.lib.AbstractCloseableIterator)6 Map (java.util.Map)3 RawMessageTableEntry (co.cask.cdap.messaging.store.RawMessageTableEntry)2 RawPayloadTableEntry (co.cask.cdap.messaging.store.RawPayloadTableEntry)2 IOException (java.io.IOException)2 Result (org.apache.hadoop.hbase.client.Result)2 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)2 Scan (org.apache.hadoop.hbase.client.Scan)2 CloseableIterator (co.cask.cdap.api.dataset.lib.CloseableIterator)1 AndFilter (co.cask.cdap.logging.filter.AndFilter)1 Filter (co.cask.cdap.logging.filter.Filter)1 LogLocation (co.cask.cdap.logging.write.LogLocation)1 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)1 TopicId (co.cask.cdap.proto.id.TopicId)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1