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