Class LMDBKVStore<T>

java.lang.Object
io.permazen.kv.AbstractKVStore
io.permazen.kv.lmdb.LMDBKVStore<T>
Type Parameters:
T - buffer type
All Implemented Interfaces:
CloseableKVStore, KVStore, Closeable, AutoCloseable
Direct Known Subclasses:
ByteArrayLMDBKVStore

public abstract class LMDBKVStore<T> extends AbstractKVStore implements CloseableKVStore
Straightforward KVStore view of a LMDB transaction.

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

  • Field Details

    • log

      protected final Logger log
  • Constructor Details

    • LMDBKVStore

      protected LMDBKVStore(org.lmdbjava.Dbi<T> db, org.lmdbjava.Txn<T> tx)
      Constructor.

      Closing this instance does not close the underlying transaction.

      Parameters:
      db - LMDB database
      tx - LMDB transaction
      Throws:
      IllegalArgumentException - if db or tx is null
  • Method Details

    • getTransaction

      public org.lmdbjava.Txn<T> getTransaction()
      Get the Txn associated with this instance.
      Returns:
      associated transaction
    • getDatabase

      public org.lmdbjava.Dbi<T> getDatabase()
      Get the Dbi associated with this instance.
      Returns:
      associated database handle
    • isClosed

      public boolean isClosed()
      Determine if this instance is closed.
      Returns:
      true if closed, false if still open
    • 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
    • removeRange

      public void removeRange(byte[] minKey, byte[] maxKey)
      Description copied from interface: KVStore
      Remove all key/value pairs whose keys are in a given range.

      The minKey must be less than or equal to maxKey; if they equal (and not null) then nothing happens; if they are both null then all entries are deleted.

      If keys starting with 0xff are not supported by this instance, then:

      • If minKey starts with 0xff, then no change occurs
      • If maxKey starts with 0xff, then this method behaves as if maxKey were null
      Specified by:
      removeRange in interface KVStore
      Overrides:
      removeRange in class AbstractKVStore
      Parameters:
      minKey - minimum key (inclusive), or null for no minimum
      maxKey - maximum key (exclusive), or null for no maximum
    • getKeyRange

      public org.lmdbjava.KeyRange<T> getKeyRange(byte[] minKey, byte[] maxKey, boolean reverse)
      Get the KeyRange corresponding to the given parameters.
      Parameters:
      minKey - minimum key (inclusive), or null for none
      maxKey - maximum key (exclusive), or null for none
      reverse - true for reverse ordering, false for forward ordering
      Returns:
      KeyRange instance
    • wrap

      protected abstract T wrap(byte[] buf, boolean copy)
      Wrap the given byte array in a buffer appropriate for this instance.
      Parameters:
      buf - byte array data, or possibly null
      copy - if true, then changes to the data in either buf or the returned buffer must not affect the other
      Returns:
      null if buf is null, otherwise a buffer containing the data in buf
    • unwrap

      protected abstract byte[] unwrap(T buf, boolean copy)
      Unwrap the given buffer, returning its contents as a byte array.
      Parameters:
      buf - a buffer containing byte[] array data, or possibly null
      copy - if true, then changes to the data in either buf or the returned array must not affect the other
      Returns:
      byte array data in buf, or null if buf is null
    • close

      public void close()
      Close this instance.

      This closes any unclosed iterators; it does not close the underlying transaction.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface CloseableKVStore
    • 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