Package org.rocksdb

Class WriteBatchWithIndex

All Implemented Interfaces:
AutoCloseable, WriteBatchInterface

public class WriteBatchWithIndex extends AbstractWriteBatch
Similar to WriteBatch but with a binary searchable index built for all the keys inserted.

Calling put, merge, remove or putLogData calls the same function as with WriteBatch whilst also building an index.

A user can call newIterator() to create an iterator over the write batch or newIteratorWithBase(org.rocksdb.RocksIterator) to get an iterator for the database with Read-Your-Own-Writes like capability

  • Constructor Details

    • WriteBatchWithIndex

      public WriteBatchWithIndex()
      Creates a WriteBatchWithIndex where no bytes are reserved up-front, byte wise comparison is used for fallback key comparisons, and duplicate keys operations are retained
    • WriteBatchWithIndex

      public WriteBatchWithIndex(boolean overwriteKey)
      Creates a WriteBatchWithIndex where no bytes are reserved up-front, byte wise comparison is used for fallback key comparisons, and duplicate key assignment is determined by the constructor argument
      Parameters:
      overwriteKey - if true, overwrite the key in the index when inserting a duplicate key, in this way an iterator will never show two entries with the same key.
    • WriteBatchWithIndex

      public WriteBatchWithIndex(AbstractComparator fallbackIndexComparator, int reservedBytes, boolean overwriteKey)
      Creates a WriteBatchWithIndex
      Parameters:
      fallbackIndexComparator - We fall back to this comparator to compare keys within a column family if we cannot determine the column family and so look up its comparator.
      reservedBytes - reserved bytes in underlying WriteBatch
      overwriteKey - if true, overwrite the key in the index when inserting a duplicate key, in this way an iterator will never show two entries with the same key.
  • Method Details

    • newIterator

      public WBWIRocksIterator newIterator(ColumnFamilyHandle columnFamilyHandle)
      Create an iterator of a column family. User can call RocksIteratorInterface.seek(byte[]) to search to the next entry of or after a key. Keys will be iterated in the order given by index_comparator. For multiple updates on the same key, each update will be returned as a separate entry, in the order of update time.
      Parameters:
      columnFamilyHandle - The column family to iterate over
      Returns:
      An iterator for the Write Batch contents, restricted to the column family
    • newIterator

      public WBWIRocksIterator newIterator()
      Create an iterator of the default column family. User can call RocksIteratorInterface.seek(byte[]) to search to the next entry of or after a key. Keys will be iterated in the order given by index_comparator. For multiple updates on the same key, each update will be returned as a separate entry, in the order of update time.
      Returns:
      An iterator for the Write Batch contents
    • newIteratorWithBase

      public RocksIterator newIteratorWithBase(ColumnFamilyHandle columnFamilyHandle, RocksIterator baseIterator)
      Provides Read-Your-Own-Writes like functionality by creating a new Iterator that will use WBWIRocksIterator as a delta and baseIterator as a base

      Updating write batch with the current key of the iterator is not safe. We strongly recommend users not to do it. It will invalidate the current key() and value() of the iterator. This invalidation happens even before the write batch update finishes. The state may recover after Next() is called.

      Parameters:
      columnFamilyHandle - The column family to iterate over
      baseIterator - The base iterator, e.g. RocksDB.newIterator()
      Returns:
      An iterator which shows a view comprised of both the database point-in-time from baseIterator and modifications made in this write batch.
    • newIteratorWithBase

      public RocksIterator newIteratorWithBase(ColumnFamilyHandle columnFamilyHandle, RocksIterator baseIterator, ReadOptions readOptions)
      Provides Read-Your-Own-Writes like functionality by creating a new Iterator that will use WBWIRocksIterator as a delta and baseIterator as a base

      Updating write batch with the current key of the iterator is not safe. We strongly recommend users not to do it. It will invalidate the current key() and value() of the iterator. This invalidation happens even before the write batch update finishes. The state may recover after Next() is called.

      Parameters:
      columnFamilyHandle - The column family to iterate over
      baseIterator - The base iterator, e.g. RocksDB.newIterator()
      readOptions - the read options, or null
      Returns:
      An iterator which shows a view comprised of both the database point-in-time from baseIterator and modifications made in this write batch.
    • newIteratorWithBase

      public RocksIterator newIteratorWithBase(RocksIterator baseIterator)
      Provides Read-Your-Own-Writes like functionality by creating a new Iterator that will use WBWIRocksIterator as a delta and baseIterator as a base. Operates on the default column family.
      Parameters:
      baseIterator - The base iterator, e.g. RocksDB.newIterator()
      Returns:
      An iterator which shows a view comprised of both the database point-in-time from baseIterator and modifications made in this write batch.
    • newIteratorWithBase

      public RocksIterator newIteratorWithBase(RocksIterator baseIterator, ReadOptions readOptions)
      Provides Read-Your-Own-Writes like functionality by creating a new Iterator that will use WBWIRocksIterator as a delta and baseIterator as a base. Operates on the default column family.
      Parameters:
      baseIterator - The base iterator, e.g. RocksDB.newIterator()
      readOptions - the read options, or null
      Returns:
      An iterator which shows a view comprised of both the database point-in-time from baseIterator and modifications made in this write batch.
    • getFromBatch

      public byte[] getFromBatch(ColumnFamilyHandle columnFamilyHandle, DBOptions options, byte[] key) throws RocksDBException
      Similar to RocksDB.get(ColumnFamilyHandle, byte[]) but will only read the key from this batch.
      Parameters:
      columnFamilyHandle - The column family to retrieve the value from
      options - The database options to use
      key - The key to read the value for
      Returns:
      a byte array storing the value associated with the input key if any. null if it does not find the specified key.
      Throws:
      RocksDBException - if the batch does not have enough data to resolve Merge operations, MergeInProgress status may be returned.
    • getFromBatch

      public byte[] getFromBatch(DBOptions options, byte[] key) throws RocksDBException
      Similar to RocksDB.get(byte[]) but will only read the key from this batch.
      Parameters:
      options - The database options to use
      key - The key to read the value for
      Returns:
      a byte array storing the value associated with the input key if any. null if it does not find the specified key.
      Throws:
      RocksDBException - if the batch does not have enough data to resolve Merge operations, MergeInProgress status may be returned.
    • getFromBatchAndDB

      public byte[] getFromBatchAndDB(RocksDB db, ColumnFamilyHandle columnFamilyHandle, ReadOptions options, byte[] key) throws RocksDBException
      Similar to RocksDB.get(ColumnFamilyHandle, byte[]) but will also read writes from this batch.

      This function will query both this batch and the DB and then merge the results using the DB's merge operator (if the batch contains any merge requests).

      Setting ReadOptions.setSnapshot(Snapshot) will affect what is read from the DB but will NOT change which keys are read from the batch (the keys in this batch do not yet belong to any snapshot and will be fetched regardless).

      Parameters:
      db - The Rocks database
      columnFamilyHandle - The column family to retrieve the value from
      options - The read options to use
      key - The key to read the value for
      Returns:
      a byte array storing the value associated with the input key if any. null if it does not find the specified key.
      Throws:
      RocksDBException - if the value for the key cannot be read
    • getFromBatchAndDB

      public byte[] getFromBatchAndDB(RocksDB db, ReadOptions options, byte[] key) throws RocksDBException
      Similar to RocksDB.get(byte[]) but will also read writes from this batch.

      This function will query both this batch and the DB and then merge the results using the DB's merge operator (if the batch contains any merge requests).

      Setting ReadOptions.setSnapshot(Snapshot) will affect what is read from the DB but will NOT change which keys are read from the batch (the keys in this batch do not yet belong to any snapshot and will be fetched regardless).

      Parameters:
      db - The Rocks database
      options - The read options to use
      key - The key to read the value for
      Returns:
      a byte array storing the value associated with the input key if any. null if it does not find the specified key.
      Throws:
      RocksDBException - if the value for the key cannot be read
    • disposeInternal

      protected final void disposeInternal(long handle)
      Specified by:
      disposeInternal in class RocksObject