Skip to main content

Python bindings for SQLite's LSM key/value engine

Project description

lsm

Fast Python bindings for SQLite's LSM key/value store. The LSM storage engine was initially written as part of the experimental SQLite4 rewrite (now abandoned). More recently, the LSM source code was moved into the SQLite3 source tree and has seen some improvements and fixes. This project uses the LSM code from the SQLite3 source tree.

Features:

  • Embedded zero-conf database.
  • Keys support in-order traversal using cursors.
  • Transactional (including nested transactions).
  • Single writer/multiple reader MVCC based transactional concurrency model.
  • On-disk database stored in a single file.
  • Data is durable in the face of application or power failure.
  • Thread-safe.
  • Releases GIL for read and write operations (each connection has own mutex)
  • Page compression (lz4 or zstd)
  • Zero dependency static library
  • Python 3.x.

Limitations:

The source for Python lsm is hosted on GitHub.

If you encounter any bugs in the library, please open an issue, including a description of the bug and any related traceback.

Quick-start

Below is a sample interactive console session designed to show some of the basic features and functionality of the lsm Python library.

To begin, instantiate a LSM object, specifying a path to a database file.

>>> from lsm import LSM
>>> db = LSM('test.ldb')
>>> db.open()
>>> print(db)
<LSM at "/tmp/test.ldb" as 0x10951e450>

More pythonic variant is using context manager:

>>> from lsm import LSM
>>> with LSM("/tmp/test.ldb") as db:
...     print(db)
<LSM at "/tmp/test.ldb" as 0x10951e450>

Binary/string mode

You should select mode for opening the database with binary: bool = True argument.

For example when you want to store strings just pass binary=False:

>>> from lsm import LSM
>>> with LSM("/tmp/test.ldb", binary=False) as db:
...    db['foo'] = 'bar'   # must be str for keys and values
...    print(db['foo'])
bar

Otherwise, you must pass keys and values ad bytes (default behaviour):

>>> from lsm import LSM
>>> with LSM("/tmp/test.ldb") as db:
...    db[b'foo'] = b'bar'   # must be bytes for keys and values
...    print(db[b'foo'])
b'bar'

Key/Value Features

lsm is a key/value store, and has a dictionary-like API:

>>> from lsm import LSM
>>> with LSM("/tmp/test.ldb", binary=False) as db:
...    db['foo'] = 'bar'
...    print(db['foo'])
bar

Database apply changes as soon as possible:

>>> from lsm import LSM
>>> db = LSM("/tmp/test.ldb", binary=False)
>>> db.open()
True
>>> for i in range(4):
...     db[f'k{i}'] = str(i)
...
>>> 'k3' in db
True
>>> 'k4' in db
False
>>> del db['k3']
>>> db['k3']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: "Key 'k3' was not found"

By default when you attempt to look up a key, lsm will search for an exact match. You can also search for the closest key, if the specific key you are searching for does not exist:

>>> from lsm import, LSM, SEEK_LE, SEEK_GE
>>> db = LSM("/tmp/test.ldb", binary=False)
>>> db.open()
True
>>> db['k1xx', SEEK_LE]  # Here we will match "k1".
'1'
>>> db['k1xx', SEEK_GE]  # Here we will match "k2".
'2'

LSM supports other common dictionary methods such as:

  • keys()
  • values()
  • items()
  • update()

Slices and Iteration

The database can be iterated through directly, or sliced. When you are slicing the database the start and end keys need not exist -- lsm will find the closest key (details can be found in the LSM.fetch_range() documentation).

>>> [item for item in db.items()]
[('foo', 'bar'), ('k0', '0'), ('k1', '1'), ('k2', '2')]

>>> db['k0':'k99']
<lsm_slice object at 0x10d4f3500>

>>> list(db['k0':'k99'])
[('k0', '0'), ('k1', '1'), ('k2', '2')]

You can use open-ended slices. If the lower- or upper-bound is outside the range of keys an empty list is returned.

>>> list(db['k0':])
[('k0', '0'), ('k1', '1'), ('k2', '2')]

>>> list(db[:'k1'])
[('foo', 'bar'), ('k0', '0'), ('k1', '1')]

>>> list(db[:'aaa'])
[]

To retrieve keys in reverse order or stepping over more then one item, simply use a third slice argument as usual. Negative step value means reverse order, but first and second arguments must be ordinary ordered.

>>> list(db['k0':'k99':2])
[('k0', '0'), ('k2', '2')]

>>> list(db['k0'::-1])
[('k2', '2'), ('k1', '1'), ('k0', '0')]

>>> list(db['k0'::-2])
[('k2', '2'), ('k0', '0')]


>>> list(db['k0'::3])
[('k0', '0')]

You can also delete slices of keys, but note that the delete will not include the keys themselves:

>>> del db['k0':'k99']

>>> list(db)  # Note that 'k0' still exists.
[('foo', 'bar'), ('k0', '0')]

Cursors

While slicing may cover most use-cases, for finer-grained control you can use cursors for traversing records.

>>> with db.cursor() as cursor:
...     for key, value in cursor:
...         print(key, '=>', value)
...
foo => bar
k0 => 0

>>> db.update({'k1': '1', 'k2': '2', 'k3': '3', 'foo': 'bar'})

>>> with db.cursor() as cursor:
...     cursor.first()
...     print(cursor.key())
...     cursor.last()
...     print(cursor.key())
...     cursor.previous()
...     print(cursor.key())
...
foo
k3
k2

>>> with db.cursor() as cursor:
...     cursor.seek('k0', SEEK_GE)
...     print(list(cursor.fetch_until('k99')))
...
[('k0', '0'), ('k1', '1'), ('k2', '2'), ('k3', '3')]

It is very important to close a cursor when you are through using it. For this reason, it is recommended you use the LSM.cursor() context-manager, which ensures the cursor is closed properly.

Transactions

lsm supports nested transactions. The simplest way to use transactions is with the LSM.transaction() method, which doubles as a context-manager or decorator.

>>> with db.transaction() as txn:
...     db['k1'] = '1-mod'
...     with db.transaction() as txn2:
...         db['k2'] = '2-mod'
...         txn2.rollback()
...
True
>>> print(db['k1'], db['k2'])
1-mod 2

You can commit or roll-back transactions part-way through a wrapped block:

>>> with db.transaction() as txn:
...    db['k1'] = 'outer txn'
...    txn.commit()  # The write is preserved.
...
...    db['k1'] = 'outer txn-2'
...    with db.transaction() as txn2:
...        db['k1'] = 'inner-txn'  # This is commited after the block ends.
...    print(db['k1']  # Prints "inner-txn".)
...    txn.rollback()  # Rolls back both the changes from txn2 and the preceding write.
...    print(db['k1'])
...
1              <- Return value from call to commit().
inner-txn      <- Printed after end of txn2.
True           <- Return value of call to rollback().
outer txn      <- Printed after rollback.

If you like, you can also explicitly call LSM.begin(), LSM.commit(), and LSM.rollback().

>>> db.begin()
>>> db['foo'] = 'baze'
>>> print(db['foo'])
baze
>>> db.rollback()
True
>>> print(db['foo'])
bar

Thanks to

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lsm-0.3.2.tar.gz (871.1 kB view details)

Uploaded Source

Built Distributions

lsm-0.3.2-cp39-cp39-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.9 Windows x86-64

lsm-0.3.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

lsm-0.3.2-cp39-cp39-macosx_10_14_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

lsm-0.3.2-cp38-cp38-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.8 Windows x86-64

lsm-0.3.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

lsm-0.3.2-cp38-cp38-macosx_10_14_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

lsm-0.3.2-cp37-cp37m-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.7m Windows x86-64

lsm-0.3.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.5+ x86-64

lsm-0.3.2-cp37-cp37m-macosx_10_14_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

lsm-0.3.2-cp36-cp36m-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.6m Windows x86-64

lsm-0.3.2-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.6m manylinux: glibc 2.5+ x86-64

lsm-0.3.2-cp36-cp36m-macosx_10_14_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.6m macOS 10.14+ x86-64

File details

Details for the file lsm-0.3.2.tar.gz.

File metadata

  • Download URL: lsm-0.3.2.tar.gz
  • Upload date:
  • Size: 871.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for lsm-0.3.2.tar.gz
Algorithm Hash digest
SHA256 dea597ab3cbc9d87b512dd087b519eae9f9c76fcef8bc17da7609d8c464cb6ad
MD5 65345ea90ee88f453f2e9ba19ff022ac
BLAKE2b-256 767aea35948c730e49357b582ae785a92b62d7d2c4c8b49f9416094c7e0c8f77

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for lsm-0.3.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 2bdc1ef116d7c96fff86b25e0b2e7d25f779bf8cd588485caffa2e23e7b5717c
MD5 6dc3948c898aa6801f98ab19bf8b5aab
BLAKE2b-256 232faa1ed3827fdb76deb06cbb0b67b45843fc65882e337ad59b64f5b092244f

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.1 MB
  • Tags: CPython 3.9, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for lsm-0.3.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6c997c42052332ec62de41fafd3d15d544d63d23921e4063608cff8fe85fcd27
MD5 dfffcae711546ed0b96524274e7aeef1
BLAKE2b-256 240fdc912dd641c877f3553f0c1457d673be109e85679475fc1340d59b5a43c4

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.9, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for lsm-0.3.2-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d82c423aa1eb808c6ce5cfc69381f8e36b9c7b32d65427f8f03a88af5edc918c
MD5 a86f5e9575408019d2c9fe19a0d333fa
BLAKE2b-256 32c30ad497facedcb5ef6e5f5f0e0ed96ca3f5e8a3e711e9ceeb3f48860623ae

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

File hashes

Hashes for lsm-0.3.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 1bc1144b497ac8deba315323b55146befc552cb7753dec02d2df1a8db82fef3b
MD5 db46dbac6d489c824a9984acca746810
BLAKE2b-256 2835a64e4ed5cb42022de758f0baa54c85cca8d611643dd892523615208fce47

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.1 MB
  • Tags: CPython 3.8, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for lsm-0.3.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4baf1715815e25925ecd2dced6991894507356a44b79f0827b6eba5bbb88bd7a
MD5 e9647608e0f8515194d71038ecd58de3
BLAKE2b-256 c958664fd60462f6545c21ed3cafccd6f7e2566e6e707b8d4fff53df378815c9

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.10

File hashes

Hashes for lsm-0.3.2-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2302c46a95d0713fd5c23447b1e2d8789ff24aa7672cf9aea75e48425a1ac4b1
MD5 80df7367d14f0bb3e51fab5c36a3c842
BLAKE2b-256 7fc8a25589c0dae6e3b4d7d3c94a2850c4e834433e22ee14ae24031cbda1f50f

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.7.9

File hashes

Hashes for lsm-0.3.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 3374741487d5d408fcbe0db1b55c2febdedc5d6ec9bf2818ba2d266abd731bb2
MD5 c8a457abc4b2e3bf69e33abb2d7b7db0
BLAKE2b-256 a050dfd2533eaf192cf51e19a7cb3fa01051b303d6424ee69b2c88e9f0440093

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.1 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for lsm-0.3.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 06df727a440a2e14f167ea95669ced38b6bbaacea413b34ead0096ba25dae9d7
MD5 6ff1e118197b695f4c86be820fab5168
BLAKE2b-256 e6a06995518fef5dacce28b444920536bca11b765f7af656e2b37320dec5dade

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.7.10

File hashes

Hashes for lsm-0.3.2-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 e86a12b74d6e8b31b53a8310d3720da0afd1028b85b6f5f0b2fada8bfc20e014
MD5 cfdfd7bbde95d1d13925f91c657e219e
BLAKE2b-256 b853dfdab082328da1efcd65896357df7642a0bf46c3ba0e3d030c56cfb0ca4e

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.6.8

File hashes

Hashes for lsm-0.3.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 fa2a397bda265a0b32bb781e41f5a351df8862ee42e92d6cfa4cb4f4135d6dad
MD5 f0423e54fe48fd2a3f5da7abac1e4a4f
BLAKE2b-256 ae6b2ee3fde3c88d749eff3a218698953079ae7f88935360ca967b79a2149104

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.1 MB
  • Tags: CPython 3.6m, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for lsm-0.3.2-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8415f0da6e167956f6d5497c7dd94d063b740acf1d29bedc706d1c320616800d
MD5 9e0423827bd0eb3e24a16ca1eb55c276
BLAKE2b-256 13ddb1f683b093c147acd30689efc236b157e5824fb4e405bf9da052e9e94e1e

See more details on using hashes here.

File details

Details for the file lsm-0.3.2-cp36-cp36m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: lsm-0.3.2-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.6m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.6.13

File hashes

Hashes for lsm-0.3.2-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 380eb36336d1c5f0ac3bbfd7c9447201025c34febaeb2d8c3499ea6b3a3aa72c
MD5 971e55061efd7a706324b2539dfd2854
BLAKE2b-256 815df0d703f2a75d718ee01c9eb816841278bc49fb9728c2e95cf210bbc17d8e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page