jacinle.storage.kv.lmdb#

Classes

LMDBKVStore

A LMDB-based key-value store.

Class LMDBKVStore

class LMDBKVStore[source]#

Bases: KVStoreBase

A LMDB-based key-value store. This class also supports sub-databases.

See examples/kv-lmdb for more usage.

Basic usage with a single database:
kv = LMDBKVStore('/tmp/test_1.lmdb', readonly=False)

with kv.transaction():
    kv['a'] = 1
    kv['b'] = 2

assert 'a' in kv and kv['a'] == 1
assert 'b' in kv and kv['b'] == 2
assert 'c' not in kv

for k in kv.keys():
    print(k, kv[k])
__init__(lmdb_path, readonly=True, max_dbs=0, keys=None)[source]#

Initialize the LMDBKVStore.

Parameters:
  • lmdb_path (str) – the path to the LMDB file. By default, this path is a directory.

  • readonly (bool) – whether to open the LMDB in readonly mode.

  • max_dbs (int) – the maximum number of sub-databases. 0 means the single-database mode.

  • keys (Iterable[Any] | None) – the keys in the main database. For new databases, use None.

__new__(**kwargs)#
erase(key, **kwargs)#

Erase the key from the KVStore.

Parameters:

key – the key.

get(key, default=None, **kwargs)#

Get the value of the key.

Parameters:
  • key – the key.

  • default – the default value if the key does not exist.

get_subdb(name=None)[source]#

Get a sub-database by name.

has(key, **kwargs)#

Whether the key exists in the KVStore.

Return type:

bool

keys(**kwargs)#

Get all keys in the KVStore.

Return type:

Iterable[Any]

put(key, value, replace=True, **kwargs)#

Put the value of the key. If the key already exists, the value will be replaced if replace is True.

Parameters:
  • key – the key.

  • value – the value.

  • replace (bool) – whether to replace the value if the key already exists.

set_dirty(db=None)[source]#

Set the dirty flag for the given sub-database.

transaction(*args, **kwargs)#

Create a transaction context.

update(key, value, **kwargs)#

Update the value of the key. If the key does not exist, the value will be put.

Parameters:
  • key – the key.

  • value – the value.

property lmdb#

The LMDB environment.

property readonly#

Whether the KVStore is readonly.

property txn#