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