Class LevelDBAtomicKVStore

java.lang.Object
io.permazen.kv.util.ForwardingKVStore
io.permazen.kv.leveldb.LevelDBAtomicKVStore
All Implemented Interfaces:
KVStore, AtomicKVStore

@ThreadSafe public class LevelDBAtomicKVStore extends ForwardingKVStore implements AtomicKVStore
An AtomicKVStore view of a LevelDB database.

A database directory is the only required configuration property. Instances may be stopped and (re)started multiple times.

  • Constructor Details

    • LevelDBAtomicKVStore

      public LevelDBAtomicKVStore()
      Constructor.

      Uses the default DBFactory provided by LevelDBUtil.getDefaultDBFactory().

    • LevelDBAtomicKVStore

      public LevelDBAtomicKVStore(org.iq80.leveldb.DBFactory factory)
      Constructor.
      Parameters:
      factory - factory for database
      Throws:
      IllegalArgumentException - if factory is null
  • Method Details

    • getDirectory

      public File getDirectory()
      Get the filesystem directory containing the database.
      Returns:
      database directory
    • setDirectory

      public void setDirectory(File directory)
      Configure the filesystem directory containing the database. Required property.
      Parameters:
      directory - database directory
      Throws:
      IllegalStateException - if this instance is already start()ed
    • getDB

      public org.iq80.leveldb.DB getDB()
      Get the underlying DB associated with this instance.
      Returns:
      the associated DB
      Throws:
      IllegalStateException - if this instance is not start()ed
    • getOptions

      public org.iq80.leveldb.Options getOptions()
      Get the Options this instance will use when opening the database at startup.
      Returns:
      database options
    • setOptions

      public void setOptions(org.iq80.leveldb.Options options)
      Set the Options this instance will use when opening the database at startup. Overwrites any previous options configuration(s).
      Parameters:
      options - database options
      Throws:
      IllegalArgumentException - if options is null
      IllegalStateException - if this instance is already start()ed
    • setBlockRestartInterval

      public void setBlockRestartInterval(int blockRestartInterval)
      Configure the number of keys between restart points for delta encoding of keys. Default 16.
      Parameters:
      blockRestartInterval - restart interval
      Throws:
      IllegalStateException - if this instance is already start()ed
    • setBlockSize

      public void setBlockSize(int blockSize)
      Configure the block size. Default 4K.
      Parameters:
      blockSize - block size
      Throws:
      IllegalStateException - if this instance is already start()ed
    • setCacheSize

      public void setCacheSize(long cacheSize)
      Configure the cache size. Default 8MB.
      Parameters:
      cacheSize - cache size
      Throws:
      IllegalStateException - if this instance is already start()ed
    • setCompressionType

      public void setCompressionType(org.iq80.leveldb.CompressionType compressionType)
      Configure the compression type. Default CompressionType.SNAPPY.
      Parameters:
      compressionType - compression type.
      Throws:
      IllegalArgumentException - if compressionType is null
      IllegalStateException - if this instance is already start()ed
    • setCreateIfMissing

      public void setCreateIfMissing(boolean createIfMissing)
      Configure whether to create the database if missing. Default true.
      Parameters:
      createIfMissing - true to create if missing
      Throws:
      IllegalStateException - if this instance is already start()ed
    • setErrorIfExists

      public void setErrorIfExists(boolean errorIfExists)
      Configure whether to throw an error if the database already exists. Default false.
      Parameters:
      errorIfExists - true for error if database exists
      Throws:
      IllegalStateException - if this instance is already start()ed
    • setMaxOpenFiles

      public void setMaxOpenFiles(int maxOpenFiles)
      Configure the maximum number of open files. Default 1000.
      Parameters:
      maxOpenFiles - maximum number of open files
      Throws:
      IllegalStateException - if this instance is already start()ed
    • setParanoidChecks

      public void setParanoidChecks(boolean paranoidChecks)
      Configure whether paranoid checks are enabled. Default false.
      Parameters:
      paranoidChecks - true to enable
      Throws:
      IllegalStateException - if this instance is already start()ed
    • setVerifyChecksums

      public void setVerifyChecksums(boolean verifyChecksums)
      Configure whether to verify checksums. Default true.
      Parameters:
      verifyChecksums - true to enable
      Throws:
      IllegalStateException - if this instance is already start()ed
    • setWriteBufferSize

      public void setWriteBufferSize(int writeBufferSize)
      Configure the write buffer size. Default 4MB.
      Parameters:
      writeBufferSize - write buffer size
      Throws:
      IllegalStateException - if this instance is already start()ed
    • start

      @PostConstruct public void start()
      Description copied from interface: AtomicKVStore
      Start this instance. This method must be called prior to creating any transactions.

      This method is idempotent: if this instance is already started, nothing happens.

      Whether an instance that has been started and stopped can be restarted is implementation-dependent.

      Specified by:
      start in interface AtomicKVStore
    • stop

      @PreDestroy public void stop()
      Description copied from interface: AtomicKVStore
      Stop this instance.

      Any open AtomicKVStore.readOnlySnapshot()'s should be close()'d before invoking this method; the behavior of those that are not is undefined.

      This method is idempotent: if this instance has not been started, or is already stopped, nothing happens.

      Specified by:
      stop in interface AtomicKVStore
    • delegate

      protected LevelDBKVStore delegate()
      Description copied from class: ForwardingKVStore
      Get the underlying KVStore.
      Specified by:
      delegate in class ForwardingKVStore
      Returns:
      underlying KVStore
    • readOnlySnapshot

      public SnapshotLevelDBKVStore readOnlySnapshot()
      Description copied from interface: AtomicKVStore
      Create a read-only "snapshot" view of this instance equal to its current state.

      The returned CloseableKVStore should be treated as read-only. It may not actually be read-only, but if it's not, then any changes should have no effect on this instance. The returned CloseableKVStore must be completely independent from this instance (subsequent changes to either one do not affect the other).

      The returned CloseableKVStore should be promply close()'d when no longer needed to release any underlying resources.

      Specified by:
      readOnlySnapshot in interface AtomicKVStore
      Returns:
      read-only, snapshot view of this instance
    • apply

      public void apply(Mutations mutations, boolean sync)
      Description copied from interface: AtomicKVStore
      Apply a set of mutations to this instance atomically.

      If this method returns normally, all of the given mutations will have been applied. If this method returns abnormally, then none of the given mutations will have been applied.

      In any case, other threads observing this instance will never see a partial application of the given mutations.

      This method is required to apply the mutations in this order: removes, puts, adjusts.

      If sync is true, the implementation must durably persist the changes before returning.

      Specified by:
      apply in interface AtomicKVStore
      Parameters:
      mutations - the mutations to apply
      sync - if true, caller requires that the changes be durably persisted
    • finalize

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

      public String toString()
      Overrides:
      toString in class Object