Module lmdbjava
Package org.lmdbjava

Class Env<T>

java.lang.Object
org.lmdbjava.Env<T>
Type Parameters:
T - buffer type
All Implemented Interfaces:
AutoCloseable

public final class Env<T> extends Object implements AutoCloseable
LMDB environment.
  • Field Details

    • DISABLE_CHECKS_PROP

      public static final String DISABLE_CHECKS_PROP
      Java system property name that can be set to disable optional checks.
      See Also:
    • SHOULD_CHECK

      public static final boolean SHOULD_CHECK
      Indicates whether optional checks should be applied in LmdbJava. Optional checks are only disabled in critical paths (see package-level JavaDocs). Non-critical paths have optional checks performed at all times, regardless of this property.
  • Method Details

    • create

      public static Env.Builder<ByteBuffer> create()
      Create an Env using the ByteBufferProxy.PROXY_OPTIMAL.
      Returns:
      the environment (never null)
    • create

      public static <T> Env.Builder<T> create(BufferProxy<T> proxy)
      Create an Env using the passed BufferProxy.
      Type Parameters:
      T - buffer type
      Parameters:
      proxy - the proxy to use (required)
      Returns:
      the environment (never null)
    • open

      public static Env<ByteBuffer> open(File path, int size, EnvFlags... flags)
      Opens an environment with a single default database in 0664 mode using the ByteBufferProxy.PROXY_OPTIMAL.
      Parameters:
      path - file system destination
      size - size in megabytes
      flags - the flags for this new environment
      Returns:
      env the environment (never null)
    • close

      public void close()
      Close the handle.

      Will silently return if already closed or never opened.

      Specified by:
      close in interface AutoCloseable
    • copy

      public void copy(File path, CopyFlags... flags)
      Copies an LMDB environment to the specified destination path.

      This function may be used to make a backup of an existing environment. No lockfile is created, since it gets recreated at need.

      If this environment was created using EnvFlags.MDB_NOSUBDIR, the destination path must be a directory that exists but contains no files. If EnvFlags.MDB_NOSUBDIR was used, the destination path must not exist, but it must be possible to create a file at the provided path.

      Note: This call can trigger significant file size growth if run in parallel with write transactions, because it employs a read-only transaction. See long-lived transactions under "Caveats" in the LMDB native documentation.

      Parameters:
      path - writable destination path as described above
      flags - special options for this copy
    • getDbiNames

      public List<byte[]> getDbiNames()
      Obtain the DBI names.

      This method is only compatible with Envs that use named databases. If an unnamed Dbi is being used to store data, this method will attempt to return all such keys from the unnamed database.

      Returns:
      a list of DBI names (never null)
    • setMapSize

      public void setMapSize(long mapSize)
      Set the size of the data memory map.
      Parameters:
      mapSize - the new size, in bytes
    • getMaxKeySize

      public int getMaxKeySize()
      Get the maximum size of keys and MDB_DUPSORT data we can write.
      Returns:
      the maximum size of keys.
    • info

      public EnvInfo info()
      Return information about this environment.
      Returns:
      an immutable information object.
    • isClosed

      public boolean isClosed()
      Indicates whether this environment has been closed.
      Returns:
      true if closed
    • isReadOnly

      public boolean isReadOnly()
      Indicates if this environment was opened with EnvFlags.MDB_RDONLY_ENV.
      Returns:
      true if read-only
    • openDbi

      public Dbi<T> openDbi(String name, DbiFlags... flags)
      Convenience method that opens a Dbi with a UTF-8 database name.
      Parameters:
      name - name of the database (or null if no name is required)
      flags - to open the database with
      Returns:
      a database that is ready to use
    • openDbi

      public Dbi<T> openDbi(String name, Comparator<T> comparator, DbiFlags... flags)
      Convenience method that opens a Dbi with a UTF-8 database name and custom comparator.
      Parameters:
      name - name of the database (or null if no name is required)
      comparator - custom comparator callback (or null to use LMDB default)
      flags - to open the database with
      Returns:
      a database that is ready to use
    • openDbi

      public Dbi<T> openDbi(byte[] name, DbiFlags... flags)
      Open the Dbi.
      Parameters:
      name - name of the database (or null if no name is required)
      flags - to open the database with
      Returns:
      a database that is ready to use
    • openDbi

      public Dbi<T> openDbi(byte[] name, Comparator<T> comparator, DbiFlags... flags)
      Open the Dbi.

      If a custom comparator is specified, this comparator is called from LMDB any time it needs to compare two keys. The comparator must be used any time any time this database is opened, otherwise database corruption may occur. The custom comparator will also be used whenever a CursorIterable is created from the returned Dbi. If a custom comparator is not specified, LMDB's native default lexicographical order is used. The default comparator is typically more efficient (as there is no need for the native library to call back into Java for the comparator result).

      Parameters:
      name - name of the database (or null if no name is required)
      comparator - custom comparator callback (or null to use LMDB default)
      flags - to open the database with
      Returns:
      a database that is ready to use
    • stat

      public Stat stat()
      Return statistics about this environment.
      Returns:
      an immutable statistics object.
    • sync

      public void sync(boolean force)
      Flushes the data buffers to disk.
      Parameters:
      force - force a synchronous flush (otherwise if the environment has the MDB_NOSYNC flag set the flushes will be omitted, and with MDB_MAPASYNC they will be asynchronous)
    • txn

      public Txn<T> txn(Txn<T> parent, TxnFlags... flags)
      Obtain a transaction with the requested parent and flags.
      Parameters:
      parent - parent transaction (may be null if no parent)
      flags - applicable flags (eg for a reusable, read-only transaction)
      Returns:
      a transaction (never null)
    • txnRead

      public Txn<T> txnRead()
      Obtain a read-only transaction.
      Returns:
      a read-only transaction
    • txnWrite

      public Txn<T> txnWrite()
      Obtain a read-write transaction.
      Returns:
      a read-write transaction