Class LevelDBKVStore

java.lang.Object
io.permazen.kv.AbstractKVStore
io.permazen.kv.leveldb.LevelDBKVStore
All Implemented Interfaces:
CloseableKVStore, KVStore, Closeable, AutoCloseable
Direct Known Subclasses:
SnapshotLevelDBKVStore

public class LevelDBKVStore extends AbstractKVStore implements CloseableKVStore
Straightforward KVStore view of a LevelDB database.

Instances must be close()'d when no longer needed to avoid leaking resources associated with iterators.

  • Constructor Details

    • LevelDBKVStore

      public LevelDBKVStore(org.iq80.leveldb.DB db)
      Convenience constructor. Uses default read options and no write batching.
      Parameters:
      db - database
    • LevelDBKVStore

      public LevelDBKVStore(org.iq80.leveldb.DB db, org.iq80.leveldb.ReadOptions readOptions, org.iq80.leveldb.WriteBatch writeBatch)
      Constructor.
      Parameters:
      db - database
      readOptions - read options, or null for the default
      writeBatch - batch for write operations, or null for none
      Throws:
      IllegalArgumentException - if db is null
  • Method Details

    • getDB

      public org.iq80.leveldb.DB getDB()
      Get the DB underlying this instance.
      Returns:
      underlying database
    • get

      public byte[] get(byte[] key)
      Description copied from interface: KVStore
      Get the value associated with the given key, if any.

      Modifications to the returned byte[] array do not affect this instance.

      Specified by:
      get in interface KVStore
      Overrides:
      get in class AbstractKVStore
      Parameters:
      key - key
      Returns:
      value associated with key, or null if not found
    • getRange

      public CloseableIterator<KVPair> getRange(byte[] minKey, byte[] maxKey, boolean reverse)
      Description copied from interface: KVStore
      Iterate the key/value pairs in the specified range. The returned CloseableIterator's remove() method must be supported and should have the same effect as invoking remove() on the corresponding key.

      If keys starting with 0xff are not supported by this instance, and minKey starts with 0xff, then this method returns an empty iteration.

      If keys starting with 0xff are not supported by this instance, and maxKey starts with 0xff, then this method behaves as if maxKey were null.

      The returned CloseableIterator is weakly consistent (see java.util.concurrent). In short, the returned CloseableIterator must not throw ConcurrentModificationException; however, whether or not a "live" CloseableIterator reflects any modifications made after its creation is implementation dependent. Implementations that do make post-creation updates visible in the CloseableIterator, even if the update occurs after some delay, must preserve the order in which the modifications actually occurred.

      The returned CloseableIterator itself is not guaranteed to be thread safe; is should only be used in the thread that created it.

      Invokers of this method are encouraged to close() the returned iterators, though this is not required for correct behavior.

      Modifications to the returned KVPair key and value byte[] arrays do not affect this instance.

      Specified by:
      getRange in interface KVStore
      Parameters:
      minKey - minimum key (inclusive), or null for no minimum (start at the smallest key)
      maxKey - maximum key (exclusive), or null for no maximum (end at the largest key)
      reverse - true to return key/value pairs in reverse order (i.e., keys descending)
      Returns:
      iteration of key/value pairs in the range minKey (inclusive) to maxKey (exclusive)
    • put

      public void put(byte[] key, byte[] value)
      Description copied from interface: KVStore
      Set the value associated with the given key.
      Specified by:
      put in interface KVStore
      Overrides:
      put in class AbstractKVStore
      Parameters:
      key - key
      value - value
    • remove

      public void remove(byte[] key)
      Description copied from interface: KVStore
      Remove the key/value pair with the given key, if it exists.
      Specified by:
      remove in interface KVStore
      Overrides:
      remove in class AbstractKVStore
      Parameters:
      key - key
    • finalize

      protected void finalize() throws Throwable
      Finalize this instance. Invokes close() to close any unclosed iterators.
      Overrides:
      finalize in class Object
      Throws:
      Throwable
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • close

      public void close()
      Close this instance.

      This closes any unclosed iterators returned from getRange(). This does not close the underlying DB or any associated WriteBatch.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface CloseableKVStore