Search in sources :

Example 1 with AbstractCloseableIterator

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

the class AppMetadataStore method queryProgramRuns.

/**
 * Iterate over a range of run records, filter by predicates and pass each run record to the consumer.
 * @param range to scan runRecordsTable with
 * @param keyPredicate to filter the row keys by. If null, then does not filter.
 * @param predicate to filter the runRecordMetas by. If null, then does not filter.
 * @param limit the maximum number of entries to return
 */
private CloseableIterator<RunRecordDetail> queryProgramRuns(Range range, @Nullable Predicate<StructuredRow> keyPredicate, @Nullable Predicate<RunRecordDetail> predicate, int limit) throws IOException {
    CloseableIterator<StructuredRow> iterator = getRunRecordsTable().scan(range, predicate == null && keyPredicate == null ? limit : Integer.MAX_VALUE);
    return new AbstractCloseableIterator<RunRecordDetail>() {

        private int currentLimit = limit;

        @Override
        protected RunRecordDetail computeNext() {
            if (currentLimit <= 0) {
                return endOfData();
            }
            while (iterator.hasNext()) {
                StructuredRow row = iterator.next();
                if (keyPredicate != null && !keyPredicate.test(row)) {
                    continue;
                }
                RunRecordDetail recordMeta = deserializeRunRecordMeta(row);
                if (predicate == null || predicate.test(recordMeta)) {
                    currentLimit--;
                    return recordMeta;
                }
            }
            return endOfData();
        }

        @Override
        public void close() {
            iterator.close();
        }
    };
}
Also used : AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) StructuredRow(io.cdap.cdap.spi.data.StructuredRow)

Example 2 with AbstractCloseableIterator

use of io.cdap.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(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) RawPayloadTableEntry(io.cdap.cdap.messaging.store.RawPayloadTableEntry) Map(java.util.Map)

Example 3 with AbstractCloseableIterator

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

the class HBaseMessageTable method scan.

@Override
protected CloseableIterator<RawMessageTableEntry> scan(ScanRequest scanRequest) throws IOException {
    Scan scan = tableUtil.buildScan().setStartRow(scanRequest.getStartRow()).setStopRow(scanRequest.getStopRow()).setCaching(scanCacheRows).build();
    TopicMetadata topicMetadata = scanRequest.getTopicMetadata();
    byte[] topic = MessagingUtils.toDataKeyPrefix(topicMetadata.getTopicId(), topicMetadata.getGeneration());
    MessageTableKey messageTableKey = MessageTableKey.fromTopic(topic);
    try {
        final ResultScanner scanner = DistributedScanner.create(table, 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();
                }
                byte[] originalKey = rowKeyDistributor.getOriginalKey(result.getRow());
                messageTableKey.setFromRowKey(originalKey);
                return tableEntry.set(messageTableKey, 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(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) RawMessageTableEntry(io.cdap.cdap.messaging.store.RawMessageTableEntry) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) MessageTableKey(io.cdap.cdap.messaging.store.MessageTableKey) Result(org.apache.hadoop.hbase.client.Result)

Example 4 with AbstractCloseableIterator

use of io.cdap.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(table, 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(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) Scan(org.apache.hadoop.hbase.client.Scan) RawPayloadTableEntry(io.cdap.cdap.messaging.store.RawPayloadTableEntry) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result)

Example 5 with AbstractCloseableIterator

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

the class LevelDBMetadataTable method scanTopics.

private CloseableIterator<TopicMetadata> scanTopics(@Nullable byte[] startKey, @Nullable byte[] stopKey) throws IOException {
    final CloseableIterator<Map.Entry<byte[], byte[]>> iterator = new DBScanIterator(levelDB, startKey, stopKey);
    return new AbstractCloseableIterator<TopicMetadata>() {

        private boolean closed = false;

        @Override
        protected TopicMetadata computeNext() {
            if (closed || (!iterator.hasNext())) {
                return endOfData();
            }
            Map.Entry<byte[], byte[]> entry = iterator.next();
            TopicId topicId = MessagingUtils.toTopicId(entry.getKey());
            Map<String, String> properties = GSON.fromJson(Bytes.toString(entry.getValue()), MAP_TYPE);
            return new TopicMetadata(topicId, properties);
        }

        @Override
        public void close() {
            try {
                iterator.close();
            } finally {
                endOfData();
                closed = true;
            }
        }
    };
}
Also used : AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) TopicId(io.cdap.cdap.proto.id.TopicId) TreeMap(java.util.TreeMap) Map(java.util.Map) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata)

Aggregations

AbstractCloseableIterator (io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator)6 TopicMetadata (io.cdap.cdap.messaging.TopicMetadata)2 RawPayloadTableEntry (io.cdap.cdap.messaging.store.RawPayloadTableEntry)2 IOException (java.io.IOException)2 Map (java.util.Map)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 (io.cdap.cdap.api.dataset.lib.CloseableIterator)1 AndFilter (io.cdap.cdap.logging.filter.AndFilter)1 Filter (io.cdap.cdap.logging.filter.Filter)1 LogLocation (io.cdap.cdap.logging.write.LogLocation)1 MessageTableKey (io.cdap.cdap.messaging.store.MessageTableKey)1 RawMessageTableEntry (io.cdap.cdap.messaging.store.RawMessageTableEntry)1 TopicId (io.cdap.cdap.proto.id.TopicId)1 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)1 TreeMap (java.util.TreeMap)1