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();
}
};
}
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;
}
}
};
}
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);
}
}
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;
}
}
};
}
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;
}
}
};
}
Aggregations