Search in sources :

Example 1 with Iterators

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;
}
Also used : Setter(lombok.Setter) java.util(java.util) TransactionalContext(org.corfudb.runtime.object.transactions.TransactionalContext) Getter(lombok.Getter) ImmutableMap(com.google.common.collect.ImmutableMap) LocalDateTime(java.time.LocalDateTime) AbstractTransactionalContext(org.corfudb.runtime.object.transactions.AbstractTransactionalContext) SMRMap(org.corfudb.runtime.collections.SMRMap) StreamsView(org.corfudb.runtime.view.StreamsView) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) CheckpointEntry(org.corfudb.protocols.logprotocol.CheckpointEntry) MultiSMREntry(org.corfudb.protocols.logprotocol.MultiSMREntry) Consumer(java.util.function.Consumer) SMREntry(org.corfudb.protocols.logprotocol.SMREntry) TokenResponse(org.corfudb.protocols.wireprotocol.TokenResponse) BiConsumer(java.util.function.BiConsumer) TransactionType(org.corfudb.runtime.object.transactions.TransactionType) ISerializer(org.corfudb.util.serializer.ISerializer) Serializers(org.corfudb.util.serializer.Serializers) CheckpointEntry(org.corfudb.protocols.logprotocol.CheckpointEntry) MultiSMREntry(org.corfudb.protocols.logprotocol.MultiSMREntry) MultiSMREntry(org.corfudb.protocols.logprotocol.MultiSMREntry) SMREntry(org.corfudb.protocols.logprotocol.SMREntry)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterators (com.google.common.collect.Iterators)1 LocalDateTime (java.time.LocalDateTime)1 java.util (java.util)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Getter (lombok.Getter)1 Setter (lombok.Setter)1 CheckpointEntry (org.corfudb.protocols.logprotocol.CheckpointEntry)1 MultiSMREntry (org.corfudb.protocols.logprotocol.MultiSMREntry)1 SMREntry (org.corfudb.protocols.logprotocol.SMREntry)1 TokenResponse (org.corfudb.protocols.wireprotocol.TokenResponse)1 SMRMap (org.corfudb.runtime.collections.SMRMap)1 AbstractTransactionalContext (org.corfudb.runtime.object.transactions.AbstractTransactionalContext)1 TransactionType (org.corfudb.runtime.object.transactions.TransactionType)1 TransactionalContext (org.corfudb.runtime.object.transactions.TransactionalContext)1 StreamsView (org.corfudb.runtime.view.StreamsView)1 ISerializer (org.corfudb.util.serializer.ISerializer)1 Serializers (org.corfudb.util.serializer.Serializers)1