use of io.pravega.segmentstore.server.tables.DeltaIteratorState in project pravega by pravega.
the class PravegaRequestProcessor method readTableEntriesDelta.
@Override
public void readTableEntriesDelta(WireCommands.ReadTableEntriesDelta readTableEntriesDelta) {
final String segment = readTableEntriesDelta.getSegment();
final String operation = "readTableEntriesDelta";
if (!verifyToken(segment, readTableEntriesDelta.getRequestId(), readTableEntriesDelta.getDelegationToken(), operation)) {
return;
}
final int suggestedEntryCount = readTableEntriesDelta.getSuggestedEntryCount();
final long fromPosition = readTableEntriesDelta.getFromPosition();
log.info(readTableEntriesDelta.getRequestId(), "Iterate Table Entries Delta: Segment={} Count={} FromPositon={}.", readTableEntriesDelta.getSegment(), readTableEntriesDelta.getSuggestedEntryCount(), readTableEntriesDelta.getFromPosition());
val timer = new Timer();
val result = new DeltaIteratorResult<BufferView, Map.Entry<WireCommands.TableKey, WireCommands.TableValue>>(segment.getBytes().length + WireCommands.TableEntriesRead.HEADER_BYTES);
tableStore.entryDeltaIterator(segment, fromPosition, TIMEOUT).thenCompose(itr -> itr.collectRemaining(e -> {
if (result.getItemCount() >= suggestedEntryCount || result.getSizeBytes() >= MAX_READ_SIZE) {
return false;
}
TableEntry entry = e.getEntries().iterator().next();
DeltaIteratorState state = DeltaIteratorState.deserialize(e.getState());
// Store all TableEntries.
val k = new WireCommands.TableKey(toByteBuf(entry.getKey().getKey()), entry.getKey().getVersion());
val v = new WireCommands.TableValue(toByteBuf(entry.getValue()));
if (state.isDeletionRecord()) {
result.remove(entry.getKey().getKey(), k.size() + v.size());
} else {
Map.Entry<WireCommands.TableKey, WireCommands.TableValue> old = result.getItem(entry.getKey().getKey());
if (old != null && old.getKey().getKeyVersion() < entry.getKey().getVersion()) {
int sizeBytes = (k.size() + v.size()) - (old.getKey().size() + old.getValue().size());
result.add(entry.getKey().getKey(), new AbstractMap.SimpleImmutableEntry<>(k, v), sizeBytes);
} else {
result.add(entry.getKey().getKey(), new AbstractMap.SimpleImmutableEntry<>(k, v), k.size() + v.size());
}
}
result.setState(state);
// Update total read data.
return true;
})).thenAccept(v -> {
log.debug(readTableEntriesDelta.getRequestId(), "Iterate Table Segment Entries Delta complete ({}).", result.getItemCount());
connection.send(new WireCommands.TableEntriesDeltaRead(readTableEntriesDelta.getRequestId(), segment, new WireCommands.TableEntries(result.getItems()), result.getState().isShouldClear(), result.getState().isReachedEnd(), result.getState().getFromPosition()));
this.tableStatsRecorder.iterateEntries(readTableEntriesDelta.getSegment(), result.getItemCount(), timer.getElapsed());
}).exceptionally(e -> handleException(readTableEntriesDelta.getRequestId(), segment, operation, e));
}
Aggregations