use of io.cdap.cdap.messaging.store.RawMessageTableEntry in project cdap by caskdata.
the class LevelDBMessageTable method persist.
@Override
protected void persist(Iterator<RawMessageTableEntry> entries) throws IOException {
// entries are sorted by publish time. accumulate all entries for a partition into a batch and
// write the batch when the next entry is outside of the current partition
LevelDBPartition partition = null;
WriteBatch writeBatch = null;
while (entries.hasNext()) {
RawMessageTableEntry entry = entries.next();
byte[] rowKey = entry.getKey().getRowKey();
long publishTime = entry.getKey().getPublishTimestamp();
// check if this entry belongs in a different partition. If so, write the current batch.
if (partition == null || publishTime < partition.getStartTime() || publishTime >= partition.getEndTime()) {
if (partition != null) {
try {
partition.getLevelDB().write(writeBatch, WRITE_OPTIONS);
} finally {
writeBatch.close();
}
}
partition = partitionManager.getOrCreatePartition(publishTime);
writeBatch = partition.getLevelDB().createWriteBatch();
}
// LevelDB doesn't make copies, and since we reuse RawMessageTableEntry object, we need to create copies.
writeBatch.put(Arrays.copyOf(rowKey, rowKey.length), encodeValue(entry.getTxPtr(), entry.getPayload()));
}
if (partition != null) {
try {
partition.getLevelDB().write(writeBatch, WRITE_OPTIONS);
} finally {
writeBatch.close();
}
}
}
use of io.cdap.cdap.messaging.store.RawMessageTableEntry in project cdap by caskdata.
the class HBaseMessageTable method persist.
@Override
protected void persist(Iterator<RawMessageTableEntry> entries) throws IOException {
List<Put> batchPuts = new ArrayList<>();
while (entries.hasNext()) {
RawMessageTableEntry entry = entries.next();
PutBuilder putBuilder = tableUtil.buildPut(rowKeyDistributor.getDistributedKey(entry.getKey().getRowKey()));
if (entry.getTxPtr() != null) {
putBuilder.add(columnFamily, TX_COL, entry.getTxPtr());
}
if (entry.getPayload() != null) {
putBuilder.add(columnFamily, PAYLOAD_COL, entry.getPayload());
}
batchPuts.add(putBuilder.build());
}
try {
if (!batchPuts.isEmpty()) {
mutator.mutate(batchPuts);
mutator.flush();
}
} catch (IOException e) {
throw exceptionHandler.handle(e);
}
}
use of io.cdap.cdap.messaging.store.RawMessageTableEntry 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.messaging.store.RawMessageTableEntry in project cdap by caskdata.
the class LevelDBMessageTable method scan.
@Override
protected CloseableIterator<RawMessageTableEntry> scan(ScanRequest scanRequest) throws IOException {
Collection<LevelDBPartition> partitions = partitionManager.getPartitions(scanRequest.getStartTime());
if (partitions.isEmpty()) {
return CloseableIterator.empty();
}
RawMessageTableEntry tableEntry = new RawMessageTableEntry();
TopicMetadata topicMetadata = scanRequest.getTopicMetadata();
byte[] topic = MessagingUtils.toDataKeyPrefix(topicMetadata.getTopicId(), topicMetadata.getGeneration());
MessageTableKey messageTableKey = MessageTableKey.fromTopic(topic);
BiFunction<byte[], byte[], RawMessageTableEntry> decodeFunction = (key, value) -> {
Map<String, byte[]> columns = decodeValue(value);
messageTableKey.setFromRowKey(key);
return tableEntry.set(messageTableKey, columns.get(TX_COL), columns.get(PAYLOAD_COL));
};
return new PartitionedDBScanIterator<>(partitions.iterator(), scanRequest.getStartRow(), scanRequest.getStopRow(), decodeFunction);
}
Aggregations