Class Writes

java.lang.Object
io.permazen.kv.mvcc.Writes
All Implemented Interfaces:
Mutations, Cloneable

public class Writes extends Object implements Cloneable, Mutations
Holds a set of writes to a KVStore.

Each mutation is either a key/value put, the removal of a key range (possibly containing only a single key), or a counter adjustment.

Instances are not thread safe.

  • Constructor Details

    • Writes

      public Writes()
  • Method Details

    • getRemoves

      public KeyRanges getRemoves()
      Get the key ranges removals contained by this instance.
      Returns:
      key ranges removed
    • getPuts

      public NavigableMap<byte[],byte[]> getPuts()
      Get the written key/value pairs contained by this instance.

      The caller must not modify any of the returned byte[] arrays.

      Returns:
      mapping from key to corresponding value
    • getAdjusts

      public NavigableMap<byte[],Long> getAdjusts()
      Get the set of counter adjustments contained by this instance.

      The caller must not modify any of the returned byte[] arrays.

      Returns:
      mapping from key to corresponding counter adjustment
    • isEmpty

      public boolean isEmpty()
      Determine whether this instance is empty, i.e., contains zero mutations.
      Returns:
      true if this instance contains zero mutations, otherwise false
    • clear

      public void clear()
      Clear all mutations.
    • getRemoveRanges

      public Stream<KeyRange> getRemoveRanges()
      Description copied from interface: Mutations
      Get the key ranges removals contained by this instance.
      Specified by:
      getRemoveRanges in interface Mutations
      Returns:
      key ranges removed
    • getPutPairs

      public Stream<Map.Entry<byte[],byte[]>> getPutPairs()
      Description copied from interface: Mutations
      Get the written key/value pairs contained by this instance.

      The caller must not modify any of the returned byte[] arrays.

      Specified by:
      getPutPairs in interface Mutations
      Returns:
      mapping from key to corresponding value
    • getAdjustPairs

      public Stream<Map.Entry<byte[],Long>> getAdjustPairs()
      Description copied from interface: Mutations
      Get the counter adjustments contained by this instance.

      The caller must not modify any of the returned byte[] arrays.

      Specified by:
      getAdjustPairs in interface Mutations
      Returns:
      mapping from key to corresponding counter adjustment
    • applyTo

      public void applyTo(KVStore target)
      Apply all mutations contained in this instance to the given KVStore.

      Mutations are applied in this order: removes, puts, counter adjustments.

      Parameters:
      target - target for recorded mutations
      Throws:
      IllegalArgumentException - if target is null
    • apply

      @Deprecated public static void apply(Mutations mutations, KVStore target)
      Apply all the given Mutations to the given KVStore.

      Mutations are applied in this order: removes, puts, counter adjustments.

      Parameters:
      mutations - mutations to apply
      target - target for mutations
      Throws:
      IllegalArgumentException - if either parameter is null
      UnsupportedOperationException - if this instance is immutable
    • serialize

      public void serialize(OutputStream out) throws IOException
      Serialize this instance.
      Parameters:
      out - output
      Throws:
      IOException - if an error occurs
    • serializedLength

      public long serializedLength()
      Calculate the number of bytes required to serialize this instance via serialize().
      Returns:
      number of serialized bytes
    • deserialize

      public static Writes deserialize(InputStream input) throws IOException
      Deserialize a mutable instance created by serialize().

      Equivalent to deserialize(input, false).

      Parameters:
      input - input stream containing data from serialize()
      Returns:
      mutable deserialized instance
      Throws:
      IllegalArgumentException - if input is null
      IllegalArgumentException - if malformed input is detected
      IOException - if an I/O error occurs
    • deserialize

      public static Writes deserialize(InputStream input, boolean immutable) throws IOException
      Deserialize an instance created by serialize().
      Parameters:
      input - input stream containing data from serialize()
      immutable - true for an immutable instance, otherwise false
      Returns:
      deserialized instance
      Throws:
      IllegalArgumentException - if input is null
      IllegalArgumentException - if malformed input is detected
      IOException - if an I/O error occurs
    • deserializeOnline

      public static Mutations deserializeOnline(InputStream input)
      Create an "online" Mutations view of a serialized Writes instance.

      This method returns a Mutations instance that decodes the serialized Writes from the given input in an "online" fashion, i.e., it does not load the whole thing into memory. The returned Mutations iterators may only be accessed once, and they must be accessed in this order: removes, puts, adjusts. Any duplicate or out-of-order access will result in an IllegalStateException.

      If an IOException is thrown by input, the Iterator being used will throw a RuntimeException wrapping it; if invalid data is encountered, the Iterator being used will throw an IllegalArgumentException.

      Parameters:
      input - input stream containing data from serialize()
      Returns:
      online Mutations view of input
      Throws:
      IllegalArgumentException - if input is null
    • clone

      public Writes clone()
      Clone this instance.

      This is a "mostly deep" clone: all of the mutations are copied, but the actual byte[] keys and values, which are already assumed non-mutable, are not copied.

      The returned clone will always be mutable, even if this instance is not.

      Overrides:
      clone in class Object
    • readOnlySnapshot

      public Writes readOnlySnapshot()
      Return an immutable snapshot of this instance.
      Returns:
      immutable snapshot
    • toString

      public String toString()
      Overrides:
      toString in class Object