Skip to main content

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

Metadata

Release files for lsm 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lsm 0.2.4
File Size Uploaded
lsm-0.2.4.tar.gz 368.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for lsm 0.2.4
File
lsm-0.2.4-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
lsm-0.2.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.5+ x86-64 Details
lsm-0.2.4-cp39-cp39-macosx_10_14_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.14+ x86-64 Details
lsm-0.2.4-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
lsm-0.2.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.5+ x86-64 Details
lsm-0.2.4-cp38-cp38-macosx_10_14_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.14+ x86-64 Details
lsm-0.2.4-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
lsm-0.2.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64 Details
lsm-0.2.4-cp37-cp37m-macosx_10_14_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.14+ x86-64 Details
lsm-0.2.4-cp36-cp36m-win_amd64.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-64 Details
lsm-0.2.4-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64 Details
lsm-0.2.4-cp36-cp36m-macosx_10_14_x86_64.whl CPython 3.6 CPython 3.6 pymalloc macOS 10.14+ x86-64 Details

Total release size: 12.7 MB

Release files / lsm-0.2.4.tar.gz

Download URL lsm-0.2.4.tar.gz
Size 368.1 kB
Tags Source
SHA-256 checksum
How to use checksums
98548066b477d2bb01340aacf3eae1a4f6192aa29ec0306b5f04e4c8fc8de014
BLAKE2b-256 checksum
How to use checksums
4b878e0b4e79f13e15d3ba5d3d7a408928aa7b3e0eced9c136f0bc814f324366
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp39-cp39-win_amd64.whl

Download URL lsm-0.2.4-cp39-cp39-win_amd64.whl
Size 723.1 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
20071ec00580104582656cdeacc3653856ea51eab13482ecc5216cc71c634328
BLAKE2b-256 checksum
How to use checksums
16e8476a787c8e92bcdffbc6c1717237b97628ea79effa388de6b375389fa9d6
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl

Download URL lsm-0.2.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Size 1.5 MB
Tags CPython 3.9 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
34ea869d8db0c734b7bc6abec33867e1948895e86ddb046e0fd5151b9b918121
BLAKE2b-256 checksum
How to use checksums
508c1ddb75bf093600a221a8039f97a4833b3812fd85d5b85ea9a188099c00ef
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp39-cp39-macosx_10_14_x86_64.whl

Download URL lsm-0.2.4-cp39-cp39-macosx_10_14_x86_64.whl
Size 814.9 kB
Tags CPython 3.9 macOS 10.14+ x86-64
SHA-256 checksum
How to use checksums
b29429160f22f5aa408d7b3bc653c2e5409542e336221a144d4de892fb2a7095
BLAKE2b-256 checksum
How to use checksums
23aef0cadae5d1b82bab286ae3184fc67831fdee36ff09201c1768c3e9091c96
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp38-cp38-win_amd64.whl

Download URL lsm-0.2.4-cp38-cp38-win_amd64.whl
Size 723.3 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
7c36db4ebdbf0b5f829a82b43ff1939a407ed17465a15e398980ce1f37c9c877
BLAKE2b-256 checksum
How to use checksums
7a4f0916faa7adcd18ce0607b683876e05211ccb49e172738727987533d869a2
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl

Download URL lsm-0.2.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Size 1.5 MB
Tags CPython 3.8 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
8bcaba78be967c10c7e76a60777e0b9fce5b7ba0620f7ab6d84f45d4529ca2d2
BLAKE2b-256 checksum
How to use checksums
bdbd59dd465a90ee9e83c9e271f4bd364fb0dce16e453bd2e04261737e524ec4
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp38-cp38-macosx_10_14_x86_64.whl

Download URL lsm-0.2.4-cp38-cp38-macosx_10_14_x86_64.whl
Size 814.8 kB
Tags CPython 3.8 macOS 10.14+ x86-64
SHA-256 checksum
How to use checksums
06909db3b903f05cc9e1dfa85ab45778224337446991c66ac397161dee952f49
BLAKE2b-256 checksum
How to use checksums
13a609dcf40a319a38cf2d1cf4ebd68fa3f6841c0cdb017f307c6aceeb3a99c9
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp37-cp37m-win_amd64.whl

Download URL lsm-0.2.4-cp37-cp37m-win_amd64.whl
Size 719.1 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
ff0fbd94030051f9f406f3e83789f21c48fc213adba1b92f4fb9a8df26e8e196
BLAKE2b-256 checksum
How to use checksums
19e77f23ca1eba01b4a4d0838f7ada2042bd975b8f552dad8fe83a01d2d4b56f
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl

Download URL lsm-0.2.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Size 1.5 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
1afd441f4050c1c6797d0693c7e18fa696fadbcb6469013e43717281b6cd32df
BLAKE2b-256 checksum
How to use checksums
86f1f31328e48388759b6a13a082bde3ab99bfca91f9713949f0496c054b0303
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp37-cp37m-macosx_10_14_x86_64.whl

Download URL lsm-0.2.4-cp37-cp37m-macosx_10_14_x86_64.whl
Size 810.7 kB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.14+ x86-64
SHA-256 checksum
How to use checksums
114f8b6742d1922b71e76726349acd019ef33d76997c596d5474c80e2ddcf3a8
BLAKE2b-256 checksum
How to use checksums
c7f71886484353fe62bde7927f14dac4844cb0001b73a99b2eb55d52f75aeb8b
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp36-cp36m-win_amd64.whl

Download URL lsm-0.2.4-cp36-cp36m-win_amd64.whl
Size 719.1 kB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
5deea923dfe9abd5f75c446a937b6e5d184b728f2a0aab0c7ca4d62bcb0976bb
BLAKE2b-256 checksum
How to use checksums
4a61f75243e6a7e09266c3f25a1526bac759851b2495534082b467b74b1b31c1
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl

Download URL lsm-0.2.4-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Size 1.5 MB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
aba21d1ba44c83205e87e2143be0b160d7abc5c8a1ce4e6f93ca3bf716e9f3cc
BLAKE2b-256 checksum
How to use checksums
e235572bd8c2c3786a04629cd06298f20d4546376ee33014a0166e6959eb3869
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lsm-0.2.4-cp36-cp36m-macosx_10_14_x86_64.whl

Download URL lsm-0.2.4-cp36-cp36m-macosx_10_14_x86_64.whl
Size 810.7 kB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.14+ x86-64
SHA-256 checksum
How to use checksums
e70b3a31e8efbeeaf205740903e49dc74921a31cf31226ba6bbf6e70de3d895f
BLAKE2b-256 checksum
How to use checksums
04ad584212e6aab915cb147ddcbcf22b15e17585d6e93e223c7ef588264d8e6e
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

0.5.9

11 release files

0.5.7

7 release files

0.5.6

6 release files

0.5.5

7 release files

0.5.3

26 release files

0.4.9

16 release files

0.4.8

20 release files

0.4.7

20 release files

0.4.6

27 release files

0.4.5

11 release files

0.4.4

24 release files

0.4.3

16 release files

0.4.2

16 release files

0.4.1

16 release files

0.4.0

16 release files

0.3.9

13 release files

0.3.8

13 release files

0.3.7

13 release files

0.3.6

13 release files

0.3.5

13 release files

0.3.2

13 release files

This release

0.2.4 This release

13 release files

0.2.1

13 release files

0.2.0

13 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page