Public API

class openleveldb.LevelDB(db_path: Optional[Union[str, pathlib.Path]], server_address: Optional[str] = None, dbconnector: Optional[Union[openleveldb.backend.connectorlocal.LevelDBLocal, openleveldb.backend.connectorclient.LevelDBClient]] = None, read_only: bool = False)[source]
__init__(db_path: Optional[Union[str, pathlib.Path]], server_address: Optional[str] = None, dbconnector: Optional[Union[openleveldb.backend.connectorlocal.LevelDBLocal, openleveldb.backend.connectorclient.LevelDBClient]] = None, read_only: bool = False)None[source]

Provide access to a leveldb database, it if does not exists one is created.

Local databases do not support multiprocessing. It is possible to access a local db with:

>>> db = LevelDB(db_path)

Remote databases support multiprocessing. Once the a leveldb server is running, it is possible to access the remote db with:

>>> # db = LevelDB(remote_db_path, server_address)
Parameters
  • db_path – the path in the filesystem to the database

  • server_address – the address of the remote server

  • read_only – if true the db can not be modified

  • dbconnector – provide directly an existing dbconnector

prefixed_iter(prefixes: Optional[Union[str, Iterable[str]]] = None, starting_by: Optional[Union[str, Iterable[str]]] = None, include_key=True, include_value=True)Iterable[source]

Builds a custom iterator.

The parameters include_key and include_value define what should be yielded:

>>> list(db)
[('a1', 'value1'), ('b1', 'value2'), ('b2', 'value3'), ('c1', 'value4')]
>>> list(db.prefixed_iter(include_key=False, include_value=False))
[None, None, None, None]
>>> list(db.prefixed_iter(include_key=True, include_value=False))
['a1', 'b1', 'b2', 'c1']
>>> list(db.prefixed_iter(include_key=False, include_value=True))
['value1', 'value2', 'value3', 'value4']

The prefixes and starting_by parameters have a similar meaning. They determine over which keys it should iterate. The difference is that starting by preserves the prefix in the returned key. The iterations stops when all the available keys with the given prefixes have been yielded

>>> list(db)
[('a1', 'value1'), ('b1', 'value2'), ('b2', 'value3'), ('c1', 'value4')]
>>> list(db.prefixed_iter(prefixes=["b"]))
[('1', 'value2'), ('2', 'value3')]
>>> list(db.prefixed_iter(prefixes=["b", "1"]))
[('', 'value2')]
>>> list(db.prefixed_iter(starting_by="b"))
[('b1', 'value2'), ('b2', 'value3')]
>>> list(db.prefixed_iter(starting_by=["b", "1"]))
[('b1', 'value2')]
Parameters
  • include_key – if False do not yield the keys

  • include_value – if False do not yield the values

  • prefixes – prefixes of the desired keys The prefixes will be removed from the keys returned

  • starting_by – prefixes of the desired keys The prefixes will be preserved from the keys returned

Returns

the iterable over the keys and/or values

prefixed_len(prefixes: Optional[Union[str, Iterable[str]]] = None, starting_by: Optional[str] = None)int[source]

Utility function to compute the number of keys with a given prefix, see :py:meth:~database.LevelDB.prefixed_iter for more details.

>>> list(db)
[('a1', 'value1'), ('b1', 'value2'), ('b2', 'value3'), ('c1', 'value4')]
>>> db.prefixed_len(prefixes=["b"])
2
>>> db.prefixed_len(prefixes=["b", "1"])
1
Parameters
  • prefixes – prefixes of the desired keys The prefixes will be removed from the keys returned

  • starting_by – prefixes of the desired keys The prefixes will be preserved from the keys returned

Returns

the number of matching keys

__iter__()Iterator[source]

Iterator over (key, value) sorted by key

>>> list(db)
[('a1', 'value1'), ('b1', 'value2'), ('b2', 'value3'), ('c1', 'value4')]
Returns

the iterator over the items

__len__()int[source]

Computes the number of element in the database.

>>> list(db)
[('a1', 'value1'), ('b1', 'value2'), ('b2', 'value3'), ('c1', 'value4')]
>>> len(db)
4
Returns

number of elements in the database

__setitem__(key: Union[str, Iterable[str]], value: Any)None[source]

Store the couple (key, value) in leveldb. The key and the value are automatically encoded together with the obj type information, in order to be able to automate the decoding.

>>> import numpy as np
>>> db["array"] = np.array([1, 2, 3], dtype=np.int8)
>>> db["array"]
array([1, 2, 3], dtype=int8)
>>> del db["array"]

The key may be one or more strings to specify prefixes. The last element is always the key:

>>> db["prefix1", "prefix2", "key"] = "myvalue"
>>> db["prefix1", "prefix2", "key"]
'myvalue'
>>> del db["prefix1", "prefix2", "key"]
Parameters

key – one or more strings to specify prefixes

Returns

the value associated to the key in leveldb

__getitem__(key: Union[str, Iterable[Union[str, ellipsis]]])Any[source]

Retrieve the couple (key, value) from leveldb.

>>> db["a1"]
'value1'
>>> db["a", "1"]
'value1'

The value is automatically decoded into its original type. The value must have been stored with :py:~:py:meth:~database.LevelDB.__setitem__

>>> import numpy as np
>>> db["array"] = np.array([1, 2, 3], dtype=np.int8)
>>> db["array"]
array([1, 2, 3], dtype=int8)
>>> del db["array"]

The key may be one or more strings, to specify prefixes. The last element is always the key:

>>> db["prefix1", "prefix2", "key"] = "myvalue"
>>> db["prefix1", "prefix2", "key"]
'myvalue'
>>> del db["prefix1", "prefix2", "key"]

It is possible to retrieve a stateful instance of :py:class:~database.LevelDB that accounts for prefixes using Ellipsis as key:

>>> list(db)
[('a1', 'value1'), ('b1', 'value2'), ('b2', 'value3'), ('c1', 'value4')]
>>> db_b = db['b', ...]
>>> db_b["1"]
'value2'
>>> list(db_b)
[('1', 'value2'), ('2', 'value3')]
>>> list(db["c", ...])
[('1', 'value4')]
Parameters

key – one or more strings to specify prefixes. It’s possible to specify sub-db using the Ellipsis as key.

Returns

the value associated to the key in leveldb or a sub-db.

__delitem__(key: Union[str, Iterable[str]])None[source]

Delete the couple (key, value) from leveldb.

The key may be one or more strings, to specify prefixes. The last element is always the key:

>>> db["prefix", "key"] = "value"
>>> db["prefix", "key"]
'value'
>>> db["prefixkey"]
'value'
>>> del db["prefix", "key"]
>>> print(db["prefix", "key"])
None
>>> print(db["prefixkey"])
None
Parameters

key – one or more strings to specify prefixes.

close()None[source]

Close the database