Search in sources :

Example 6 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 7 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)

Example 8 with AbstractCloseableIterator

use of io.cdap.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(io.cdap.cdap.logging.filter.AndFilter) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) AbstractCloseableIterator(io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator) Filter(io.cdap.cdap.logging.filter.Filter) AndFilter(io.cdap.cdap.logging.filter.AndFilter) LogLocation(io.cdap.cdap.logging.write.LogLocation)

Example 9 with AbstractCloseableIterator

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

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 10 with AbstractCloseableIterator

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

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)

Aggregations

AbstractCloseableIterator (io.cdap.cdap.api.dataset.lib.AbstractCloseableIterator)12 TopicMetadata (io.cdap.cdap.messaging.TopicMetadata)4 RawPayloadTableEntry (io.cdap.cdap.messaging.store.RawPayloadTableEntry)4 IOException (java.io.IOException)4 Map (java.util.Map)4 Result (org.apache.hadoop.hbase.client.Result)4 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)4 Scan (org.apache.hadoop.hbase.client.Scan)4 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)2 AndFilter (io.cdap.cdap.logging.filter.AndFilter)2 Filter (io.cdap.cdap.logging.filter.Filter)2 LogLocation (io.cdap.cdap.logging.write.LogLocation)2 MessageTableKey (io.cdap.cdap.messaging.store.MessageTableKey)2 RawMessageTableEntry (io.cdap.cdap.messaging.store.RawMessageTableEntry)2 TopicId (io.cdap.cdap.proto.id.TopicId)2 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)2 TreeMap (java.util.TreeMap)2