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

Uploaded Source

Built Distributions

lsm-0.4.9-cp311-cp311-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.11 Windows x86-64

lsm-0.4.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

lsm-0.4.9-cp311-cp311-macosx_10_9_universal2.whl (2.2 MB view details)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

lsm-0.4.9-cp310-cp310-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

lsm-0.4.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

lsm-0.4.9-cp310-cp310-macosx_11_0_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.10 macOS 11.0+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

lsm-0.4.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

lsm-0.4.9-cp39-cp39-macosx_11_0_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.9 macOS 11.0+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

lsm-0.4.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

lsm-0.4.9-cp38-cp38-macosx_10_15_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

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

Uploaded CPython 3.7m Windows x86-64

lsm-0.4.9-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

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

lsm-0.4.9-cp37-cp37m-macosx_10_15_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: lsm-0.4.9.tar.gz
  • Upload date:
  • Size: 890.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for lsm-0.4.9.tar.gz
Algorithm Hash digest
SHA256 e5af0121d896dc2a251deb2248a3c47e900f8945b8613fb385496bc8622a1d56
MD5 b99a8298574dcdf0cf9a35a60874ba4a
BLAKE2b-256 7e9bbb93a00cd09a315de0c0c980eb556d21e4efa6987cbfc7918a6daaf6a7ab

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: lsm-0.4.9-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for lsm-0.4.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bbb1391f80662fbc6ca1549b8cc091eae6eef40e33a1faefdccccfae9b03b451
MD5 e467502ff19009341714d30c11afe70f
BLAKE2b-256 0f0f9c7d979b1a2e33c524a8d311df44fe708305db7316de7def642ff2ec4dac

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c00b410b51a945fbdea6a5af3d79da40b1880e3f335b954b5f58f092848e7c46
MD5 3d25b44828347dc52f63e83152ba4c95
BLAKE2b-256 b39d12b45dc3d2c7bb48d15453c9e726b7f7ba0de1c783a3a2da3a94b11a9b32

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 6e4d86322a140baf40429740b5217e2e0d528bf4f60c4ed5c49da59344a7d823
MD5 bb1554b9318808b28ecf567c72dc48bb
BLAKE2b-256 0659b345595c52b87c05b33b653c0fbb80561e2738d203cb26a9c444dd4c3918

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: lsm-0.4.9-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for lsm-0.4.9-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 cbab91b5bd022918905f00bc14997ca057ccb46226a22bb7769b49c4590d2890
MD5 1380b056c24878fd3200e40f5f637433
BLAKE2b-256 7f80082105358c85d843f12fbd19ebf7e3231f73da61448176459cb5ee130f1b

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8b90e1a5cede76cb46ab8dabd62bf7d3d52c3ad2147eee5d9049e8482399efb9
MD5 a76a397e93a09c90456c693ec5ef1a10
BLAKE2b-256 8fb15eeb128cbcba856ee4464b03db0d40256cd09523c591837ca3dc61d21bff

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 8bcd3332eaa48e451a5d2b5abad7f81f5fbe4de717206909234f4a9bd5f161ae
MD5 104e69ed843c9358fb2eaad637f4130f
BLAKE2b-256 d9c53ad63bdab56466da0bf27805da81b4843d350524e1c36da39e47836800c0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.4.9-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/4.0.2 CPython/3.9.13

File hashes

Hashes for lsm-0.4.9-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 7ee0fd388ef6a2d57ccd192bdf37d30231492bbecb3e0d9750744f7871c53439
MD5 e8324c81790b1a525c54749370ead120
BLAKE2b-256 21e87582a6c88979f3e7bf9c8d001d7ec0681baec9812915a1dacad271c5db2f

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 13eb546ebba55623160504ef8f542746c21cb4f02854c5e7cec8f5494e0ab0ee
MD5 a26642f9fdab790ff0f3d5888f4e160a
BLAKE2b-256 8ac00143b4a79c6bdcf22aaa181af96f0e67b117010a198177e63132206ab05c

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp39-cp39-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 717c3e0db56bf047f1ee390bff9f37951d2c31bd39f8fc23b4c29a80aa51b06c
MD5 93bcef129069ee859f904ccc7e01c80e
BLAKE2b-256 dea301d7f75592f0d636f9107969a68cc03cb6e2d8b3b0c3b40815477f4356c0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.4.9-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/4.0.2 CPython/3.8.10

File hashes

Hashes for lsm-0.4.9-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 3fcea354697e8d24dc0827f5616c94f650657e8e5d3d8f090387e8dece442fc7
MD5 c649a607e56ecf5c0b7fbef283c80fd0
BLAKE2b-256 c066f53d8893813e843daf170efd8e9953f533411fa0d0eaa3c38019e82ff569

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fc507f4eb6175cdb2331c99e10d70715ae5adc4ae96391fd695adfe99a0da6c5
MD5 ff2942b7d1011e1c57cc3624bc5b3e2d
BLAKE2b-256 a808af1055038f4c034222452236b8afd22e1ff74759ebbb9abcb61a4b7555a8

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 45c8e6b58999714d9cfa0d5c657124ca0921b38db5ed607c823d683234427ebf
MD5 229421fbe6d113a79452a00e6f81dc40
BLAKE2b-256 8ea8e258d9c09835fa2d2687d0df0ace81ff9d51ee120431da525bed944d5856

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lsm-0.4.9-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/4.0.2 CPython/3.7.9

File hashes

Hashes for lsm-0.4.9-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 54a7223a6779348f830736ec394cb5d6a336b0a085bbdbd67325cc33a03867e2
MD5 5ead1be511e7f9d23e4d416429aad7de
BLAKE2b-256 c5179dcd95bf395da3731b23e907a9bb8f3b32ca10c5de4b4e362262f69374aa

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0d51c95e604f471ee95f1c6995489698a7bd00dfdf3af1a58e01490f61468dc4
MD5 3574d3ab31c0492c1769be60b3a71f9d
BLAKE2b-256 0c70a1d1dc3f304157175b64915ca92628062853d459828f1e93eb6a440088ec

See more details on using hashes here.

File details

Details for the file lsm-0.4.9-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for lsm-0.4.9-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 6406b03e4e9d77a1b82f811e41c6bd4d333a6a24f975767d6fc9950163f06949
MD5 49e94e80983287ff2e9e3254f8916060
BLAKE2b-256 a6cf23fcc22b83b26b1b04dffef3f47e2e4e1cb405bdbb498933270fce7fb431

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