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.2.4.tar.gz (368.1 kB view details)

Uploaded Source

Built Distributions

lsm-0.2.4-cp39-cp39-win_amd64.whl (723.1 kB view details)

Uploaded CPython 3.9 Windows x86-64

lsm-0.2.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

lsm-0.2.4-cp39-cp39-macosx_10_14_x86_64.whl (814.9 kB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

lsm-0.2.4-cp38-cp38-win_amd64.whl (723.3 kB view details)

Uploaded CPython 3.8 Windows x86-64

lsm-0.2.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

lsm-0.2.4-cp38-cp38-macosx_10_14_x86_64.whl (814.8 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

lsm-0.2.4-cp37-cp37m-win_amd64.whl (719.1 kB view details)

Uploaded CPython 3.7m Windows x86-64

lsm-0.2.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.5 MB view details)

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

lsm-0.2.4-cp37-cp37m-macosx_10_14_x86_64.whl (810.7 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

lsm-0.2.4-cp36-cp36m-win_amd64.whl (719.1 kB view details)

Uploaded CPython 3.6m Windows x86-64

lsm-0.2.4-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.5 MB view details)

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

lsm-0.2.4-cp36-cp36m-macosx_10_14_x86_64.whl (810.7 kB view details)

Uploaded CPython 3.6m macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: lsm-0.2.4.tar.gz
  • Upload date:
  • Size: 368.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.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.2.4.tar.gz
Algorithm Hash digest
SHA256 98548066b477d2bb01340aacf3eae1a4f6192aa29ec0306b5f04e4c8fc8de014
MD5 c128eb566dd5bfed12708b2f069bf115
BLAKE2b-256 4b878e0b4e79f13e15d3ba5d3d7a408928aa7b3e0eced9c136f0bc814f324366

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 723.1 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.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.2.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 20071ec00580104582656cdeacc3653856ea51eab13482ecc5216cc71c634328
MD5 87ad869960c35a5f356df30da520e490
BLAKE2b-256 16e8476a787c8e92bcdffbc6c1717237b97628ea79effa388de6b375389fa9d6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 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.4.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.2.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 34ea869d8db0c734b7bc6abec33867e1948895e86ddb046e0fd5151b9b918121
MD5 511082d3f94fef6f6565c29306c92461
BLAKE2b-256 508c1ddb75bf093600a221a8039f97a4833b3812fd85d5b85ea9a188099c00ef

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 814.9 kB
  • Tags: CPython 3.9, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.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.2.4-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b29429160f22f5aa408d7b3bc653c2e5409542e336221a144d4de892fb2a7095
MD5 744e2386c2fcedf49e1d68db8dd3e8eb
BLAKE2b-256 23aef0cadae5d1b82bab286ae3184fc67831fdee36ff09201c1768c3e9091c96

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 723.3 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.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.2.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 7c36db4ebdbf0b5f829a82b43ff1939a407ed17465a15e398980ce1f37c9c877
MD5 20efbf752fcd4f42b77c14190fd4a7a1
BLAKE2b-256 7a4f0916faa7adcd18ce0607b683876e05211ccb49e172738727987533d869a2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 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.4.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.2.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8bcaba78be967c10c7e76a60777e0b9fce5b7ba0620f7ab6d84f45d4529ca2d2
MD5 7e1cdb6563bd615b836cfa67b8ed4b83
BLAKE2b-256 bdbd59dd465a90ee9e83c9e271f4bd364fb0dce16e453bd2e04261737e524ec4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 814.8 kB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.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.2.4-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 06909db3b903f05cc9e1dfa85ab45778224337446991c66ac397161dee952f49
MD5 8152175af2488050acf289ea13ad7cee
BLAKE2b-256 13a609dcf40a319a38cf2d1cf4ebd68fa3f6841c0cdb017f307c6aceeb3a99c9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 719.1 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.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.2.4-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 ff0fbd94030051f9f406f3e83789f21c48fc213adba1b92f4fb9a8df26e8e196
MD5 50c022ba2d347d9bb308a6e9ef4cb653
BLAKE2b-256 19e77f23ca1eba01b4a4d0838f7ada2042bd975b8f552dad8fe83a01d2d4b56f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 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.4.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.2.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1afd441f4050c1c6797d0693c7e18fa696fadbcb6469013e43717281b6cd32df
MD5 ca0a4fc7a6cd80e3ee71878bc401a964
BLAKE2b-256 86f1f31328e48388759b6a13a082bde3ab99bfca91f9713949f0496c054b0303

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 810.7 kB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.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.2.4-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 114f8b6742d1922b71e76726349acd019ef33d76997c596d5474c80e2ddcf3a8
MD5 4b77cc5c05f1c17bd947e5d875fbcf8a
BLAKE2b-256 c7f71886484353fe62bde7927f14dac4844cb0001b73a99b2eb55d52f75aeb8b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 719.1 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.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.2.4-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 5deea923dfe9abd5f75c446a937b6e5d184b728f2a0aab0c7ca4d62bcb0976bb
MD5 f063cda10314ddeb6108f0d1d1a4ca05
BLAKE2b-256 4a61f75243e6a7e09266c3f25a1526bac759851b2495534082b467b74b1b31c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 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.4.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.2.4-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 aba21d1ba44c83205e87e2143be0b160d7abc5c8a1ce4e6f93ca3bf716e9f3cc
MD5 a2bfac3c95e38c9b24b0abe6c3626921
BLAKE2b-256 e235572bd8c2c3786a04629cd06298f20d4546376ee33014a0166e6959eb3869

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.2.4-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 810.7 kB
  • Tags: CPython 3.6m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.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.2.4-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 e70b3a31e8efbeeaf205740903e49dc74921a31cf31226ba6bbf6e70de3d895f
MD5 95176db2d5a8224808201b29f2f63645
BLAKE2b-256 04ad584212e6aab915cb147ddcbcf22b15e17585d6e93e223c7ef588264d8e6e

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