use of com.google.common.collect.Iterators in project CorfuDB by CorfuDB.
the class CheckpointWriter method appendObjectState.
/** Append zero or more CONTINUATION records to this
* object's stream. Each will contain a fraction of
* the state of the object that we're checkpointing
* (up to batchSize items at a time).
*
* Corfu client transaction management, if desired, is the
* caller's responsibility.
*
* The Iterators class appears to preserve the laziness
* of Stream processing; we don't wish to use more
* memory than strictly necessary to generate the
* checkpoint. NOTE: It would be even more useful if
* the map had a lazy iterator: the eagerness of
* map.keySet().stream() is not ideal, but at least
* it should be much smaller than the entire map.
*
* NOTE: The postAppendFunc lambda is executed in the
* current thread context, i.e., inside of a Corfu
* transaction, and that transaction will be *aborted*
* at the end of this function. Any Corfu data
* modifying ops will be undone by the TXAbort().
*
* @return Stream of global log addresses of the
* CONTINUATION records written.
*/
public List<Long> appendObjectState() {
ImmutableMap<CheckpointEntry.CheckpointDictKey, String> mdKV = ImmutableMap.copyOf(this.mdKV);
List<Long> continuationAddresses = new ArrayList<>();
Iterators.partition(map.keySet().stream().map(k -> {
return new SMREntry("put", new Object[] { keyMutator.apply(k), valueMutator.apply(map.get(k)) }, serializer);
}).iterator(), batchSize).forEachRemaining(entries -> {
MultiSMREntry smrEntries = new MultiSMREntry();
for (int i = 0; i < ((List) entries).size(); i++) {
smrEntries.addTo((SMREntry) ((List) entries).get(i));
}
CheckpointEntry cp = new CheckpointEntry(CheckpointEntry.CheckpointEntryType.CONTINUATION, author, checkpointID, mdKV, smrEntries);
long pos = sv.append(Collections.singleton(streamID), cp, null);
postAppendFunc.accept(cp, pos);
continuationAddresses.add(pos);
numEntries++;
numBytes += cp.getSmrEntriesBytes();
});
return continuationAddresses;
}
Aggregations