A very simple and fast key-value store but persisting data to disk, with a 'localStorage-like' API for python.
Project description
py_scdb
A very simple and fast key-value (as UTF-8 strings) store but persisting data to disk, with a "localStorage-like" API.
This is the python version of the original scdb
scdb may not be production-ready yet. It works, quite well but it requires more rigorous testing.
Purpose
Coming from front-end web
development, localStorage was always
a convenient way of quickly persisting data to be used later by a given application even after a restart.
Its API was extremely simple i.e. localStorage.getItem()
, localStorage.setItem()
, localStorage.removeItem()
, localStorage.clear()
.
Coming to the backend (or even desktop) development, such an embedded persistent data store with a simple API was hard to come by.
scdb is meant to be like the 'localStorage' of backend and desktop (and possibly mobile) systems. Of course to make it a little more appealing, it has some extra features like:
- Time-to-live (TTL) where a key-value pair expires after a given time
- Non-blocking reads from separate processes, and threads.
- Fast Sequential writes to the store, queueing any writes from multiple processes and threads.
- Optional searching of keys that begin with a given subsequence. This option is turned on when
scdb::new()
is called. Note: When searching is enabled,delete
,get
,compact
,clear
become considerably slower.
Dependencies
- python +v3.7
Quick Start (Synchronous)
- Install the package
pip install py_scdb
- Import the
Store
and use accordingly
if __name__ == "__main__":
from py_scdb import Store
# These need to be UTF-8 strings
records = [
("hey", "English"),
("hi", "English"),
("salut", "French"),
("bonjour", "French"),
("hola", "Spanish"),
("oi", "Portuguese"),
("mulimuta", "Runyoro"),
]
keys = [k for (k, _) in records]
store = Store(
store_path="db",
max_keys=1000000,
redundant_blocks=1,
pool_capacity=10,
compaction_interval=1800,
is_search_enabled=True,
)
# inserting without ttl
for (k, v) in records[:3]:
store.set(k=k, v=v)
# inserting with ttl of 5 seconds
for (k, v) in records[3:]:
store.set(k=k, v=v, ttl=5)
# updating - just set them again
updates = [
("hey", "Jane"),
("hi", "John"),
("hola", "Santos"),
("oi", "Ronaldo"),
("mulimuta", "Aliguma"),
]
for (k, v) in updates:
store.set(k=k, v=v)
# getting
for k in keys:
v = store.get(k=k)
print(f"Key: {k}, Value: {v}")
# searching without pagination
results = store.search(term="h")
print(f"Search 'h' (no pagination):\n{results}\n")
# searching with pagination
results = store.search(term="h", skip=1, limit=2)
print(f"Search 'h' (skip=1, limit=2):\n{results}\n")
# deleting
for k in keys[:3]:
store.delete(k=k)
# clearing
store.clear()
# compacting (Use sparingly, say if database file is too big)
store.compact()
- Run the module
python <module_name>.py
# e.g. python main.py
Quick Start (Asynchronous)
- Install the package
pip install py_scdb
- Import the
AsyncStore
and use accordingly
import asyncio
from py_scdb import AsyncStore
# These need to be UTF-8 strings
records = [
("hey", "English"),
("hi", "English"),
("salut", "French"),
("bonjour", "French"),
("hola", "Spanish"),
("oi", "Portuguese"),
("mulimuta", "Runyoro"),
]
keys = [k for (k, _) in records]
async def run_async_example():
store = AsyncStore(
store_path="db",
max_keys=1000000,
redundant_blocks=1,
pool_capacity=10,
compaction_interval=1800,
is_search_enabled=True,
)
# inserting without ttl
for (k, v) in records[:3]:
await store.set(k=k, v=v)
# inserting with ttl of 5 seconds
for (k, v) in records[3:]:
await store.set(k=k, v=v, ttl=5)
# updating - just set them again
updates = [
("hey", "Jane"),
("hi", "John"),
("hola", "Santos"),
("oi", "Ronaldo"),
("mulimuta", "Aliguma"),
]
for (k, v) in updates:
await store.set(k=k, v=v)
# getting
for k in keys:
v = await store.get(k=k)
print(f"Key: {k}, Value: {v}")
# searching without pagination
results = await store.search(term="h")
print(f"Search 'h' (no pagination):\n{results}\n")
# searching with pagination
results = await store.search(term="h", skip=1, limit=2)
print(f"Search 'h' (skip=1, limit=2):\n{results}\n")
# deleting
for k in keys[:3]:
await store.delete(k=k)
# clearing
await store.clear()
# compacting (Use sparingly, say if database file is too big)
await store.compact()
asyncio.run(run_async_example())
- Run the module
python <module_name>.py
# e.g. python main.py
Contributing
Contributions are welcome. The docs have to maintained, the code has to be made cleaner, more idiomatic and faster, and there might be need for someone else to take over this repo in case I move on to other things. It happens!
Please look at the CONTRIBUTIONS GUIDELINES
You can also look in the ./docs folder of the rust scdb to get up to speed with the internals of scdb e.g.
Bindings
scdb is meant to be used in multiple languages of choice. However, the bindings for most of them are yet to be developed.
For other programming languages, see the main README
How to Test
- Clone the repo and enter its root folder
git clone https://github.com/sopherapps/py_scdb.git && cd py_scdb
- Create a virtual environment and activate it
virtualenv -p /usr/bin/python3.7 env && source env/bin/activate
- Install the dependencies
pip install -r requirements.txt
- Install scdb package in the virtual environment
maturin develop
For optimized build use:
maturin develop -r
- Run the tests command
pytest --benchmark-disable
- Run benchmarks
pytest --benchmark-compare --benchmark-autosave
OR the summary
# synchronous API
pytest test/test_benchmarks.py --benchmark-columns=mean,min,max --benchmark-name=short --benchmark-sort=NAME
Benchmarks
On an average PC (17Core, 16 GB RAM)
Synchronous
--------------------------------------------------------- benchmark: 40 tests ---------------------------------------------------------
Name (time in us) Mean Min Max
---------------------------------------------------------------------------------------------------------------------------------------
benchmark_clear[sync_store] 100.3807 (127.70) 83.0300 (116.78) 311.0190 (6.71)
benchmark_clear_with_search[sync_searchable_store] 171.0275 (217.57) 133.6630 (187.99) 433.9330 (9.36)
benchmark_compact[sync_store] 110,236.5678 (>1000.0) 106,076.8240 (>1000.0) 117,694.6450 (>1000.0)
benchmark_compact_with_search[sync_searchable_store] 111,180.5895 (>1000.0) 102,213.0760 (>1000.0) 127,501.7640 (>1000.0)
benchmark_delete[sync_store-hey] 3.7683 (4.79) 3.4310 (4.83) 82.3730 (1.78)
benchmark_delete[sync_store-hi] 3.7664 (4.79) 3.4440 (4.84) 54.3430 (1.17)
benchmark_delete_with_search[sync_searchable_store-hey] 20.7839 (26.44) 15.0590 (21.18) 122.8020 (2.65)
benchmark_delete_with_search[sync_searchable_store-hi] 20.7891 (26.45) 14.7560 (20.75) 123.6690 (2.67)
benchmark_get[sync_store-hey] 0.7861 (1.0) 0.7110 (1.0) 64.0260 (1.38)
benchmark_get[sync_store-hi] 0.7877 (1.00) 0.7190 (1.01) 55.0760 (1.19)
benchmark_get_with_search[sync_searchable_store-hey] 0.7991 (1.02) 0.7220 (1.02) 49.2670 (1.06)
benchmark_get_with_search[sync_searchable_store-hi] 0.7899 (1.00) 0.7230 (1.02) 46.3370 (1.0)
benchmark_paginated_search[sync_searchable_store-b] 12.9258 (16.44) 12.1430 (17.08) 156.1050 (3.37)
benchmark_paginated_search[sync_searchable_store-ba] 12.9307 (16.45) 12.1640 (17.11) 115.5400 (2.49)
benchmark_paginated_search[sync_searchable_store-ban] 7.0885 (9.02) 6.6530 (9.36) 71.0410 (1.53)
benchmark_paginated_search[sync_searchable_store-bar] 6.9927 (8.90) 6.6160 (9.31) 82.8730 (1.79)
benchmark_paginated_search[sync_searchable_store-f] 13.2767 (16.89) 12.0420 (16.94) 21,129.0600 (455.99)
benchmark_paginated_search[sync_searchable_store-fo] 12.9289 (16.45) 12.1860 (17.14) 439.6100 (9.49)
benchmark_paginated_search[sync_searchable_store-foo] 12.9970 (16.53) 12.3170 (17.32) 102.5070 (2.21)
benchmark_paginated_search[sync_searchable_store-for] 6.9771 (8.88) 6.6010 (9.28) 94.3090 (2.04)
benchmark_paginated_search[sync_searchable_store-p] 6.9895 (8.89) 6.5870 (9.26) 149.4620 (3.23)
benchmark_paginated_search[sync_searchable_store-pi] 6.9171 (8.80) 6.6080 (9.29) 69.2660 (1.49)
benchmark_paginated_search[sync_searchable_store-pig] 6.9475 (8.84) 6.5980 (9.28) 67.4790 (1.46)
benchmark_paginated_search[sync_searchable_store-pigg] 6.9717 (8.87) 6.6070 (9.29) 111.5230 (2.41)
benchmark_search[sync_searchable_store-b] 15.5810 (19.82) 14.6940 (20.67) 105.9520 (2.29)
benchmark_search[sync_searchable_store-ba] 15.6253 (19.88) 14.7780 (20.78) 107.5030 (2.32)
benchmark_search[sync_searchable_store-ban] 10.5073 (13.37) 9.9340 (13.97) 118.7900 (2.56)
benchmark_search[sync_searchable_store-bar] 10.8698 (13.83) 9.8550 (13.86) 97.3030 (2.10)
benchmark_search[sync_searchable_store-f] 20.7594 (26.41) 19.5820 (27.54) 116.9510 (2.52)
benchmark_search[sync_searchable_store-fo] 20.8279 (26.50) 19.6790 (27.68) 196.4210 (4.24)
benchmark_search[sync_searchable_store-foo] 15.7794 (20.07) 14.8380 (20.87) 94.2340 (2.03)
benchmark_search[sync_searchable_store-for] 10.4470 (13.29) 9.8640 (13.87) 128.4710 (2.77)
benchmark_search[sync_searchable_store-p] 10.3812 (13.21) 9.7870 (13.77) 102.3630 (2.21)
benchmark_search[sync_searchable_store-pi] 10.3460 (13.16) 9.8380 (13.84) 84.1260 (1.82)
benchmark_search[sync_searchable_store-pig] 10.4069 (13.24) 9.8400 (13.84) 78.9080 (1.70)
benchmark_search[sync_searchable_store-pigg] 6.9492 (8.84) 6.6000 (9.28) 86.0650 (1.86)
benchmark_set[sync_store-hey-English] 10.6412 (13.54) 9.2990 (13.08) 110.4370 (2.38)
benchmark_set[sync_store-hi-English] 10.7570 (13.68) 9.1760 (12.91) 100.9780 (2.18)
benchmark_set_with_search[sync_searchable_store-hey-English] 37.2746 (47.42) 32.7920 (46.12) 8,647.2350 (186.62)
benchmark_set_with_search[sync_searchable_store-hi-English] 28.0105 (35.63) 25.5050 (35.87) 105.3320 (2.27)
---------------------------------------------------------------------------------------------------------------------------------------
Acknowledgement
- The asyncio was got using pyo3-asyncio
- The python-rust bindings were got from the pyo3 project
License
Licensed under both the MIT License and the APACHE (2.0) License
Copyright (c) 2022 Martin Ahindura
Copyright (c) 2017-present PyO3 Project and Contributors
Gratitude
"Come to Me (Christ Jesus), all you who labor and are heavy laden, and I will give you rest. Take My yoke upon you and learn from Me, for I am gentle and lowly in heart, and you will find rest for your souls. For My yoke is easy and My burden is light."
-- Matthew 11: 28-30
All glory be to God.
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
Built Distributions
Hashes for py_scdb-0.2.2-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b99eccc009efd03edaee50e301a6da6ef6a4179b7f2d39b9229630e04785f06 |
|
MD5 | 19849896a9d8befe6dc07a108f1f6fd9 |
|
BLAKE2b-256 | b845bc80a203b163cd580de7ab6331c4f6d9f8f4e187106d27ac3810e41b9aff |
Hashes for py_scdb-0.2.2-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 236aac527d9c575650f75c9d59a99e14336dfc39f52997d42f790cce58677a8c |
|
MD5 | d94b6c1eea8bc4c66d8b7a1a958b3ca8 |
|
BLAKE2b-256 | 373d7b15811fac5e8887c473fc9df0b9e6273b0a86051a4ac7c0a2093951c912 |
Hashes for py_scdb-0.2.2-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6a60a52ddd3825667ad425f68de6be8a852581dade94d1e1c9b206882c3cde2 |
|
MD5 | 16fdfa4ec26d1b015cfc048bac8ba379 |
|
BLAKE2b-256 | 24221580d8ba3994b20b685a51d8f1a894a8d6697622ae2dc4bc85c4341b450f |
Hashes for py_scdb-0.2.2-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46b20036af8fc000fd57cdc9808cabb3909e4024194e1e041f9639a9d2f46e44 |
|
MD5 | f46d8291a66de8c3d6013a57b2b4967d |
|
BLAKE2b-256 | 44f2ec6638fb6698dad8f0f400d83db100f580ccfe9c6215d916259420f1a59a |
Hashes for py_scdb-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4f471d998d3b2a00c27f480be70dfd1b79c7af218d346e36e01581a6181fdd5 |
|
MD5 | 760098babb8d9ab9d9fc4c09f7094471 |
|
BLAKE2b-256 | 1a1867438a081f4a8f9931e69b262929d14e7eeb8cd571d2b3e5b4d9dba94fc1 |
Hashes for py_scdb-0.2.2-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 657e58e2d9a97190edb0f335b5d9c3eceb55938dc2676ed4bf32c6ba804f50ac |
|
MD5 | 4b6358f5514ab2e7fcd8481d92e663db |
|
BLAKE2b-256 | f957dbf056a70c0ac0c47847a3ed5032eece0dab1becce8a230038cb6000fbc7 |
Hashes for py_scdb-0.2.2-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5906ef400fec60e7a363d5048755b9567f8b63ab7e46674563635782b96c1329 |
|
MD5 | 7ec54721b0e66f0c1498f716221bec25 |
|
BLAKE2b-256 | 71aa3c1cf5a770c8b921592b82474fcb32b65a5fe0a9ac262bee98fad07cdcb5 |
Hashes for py_scdb-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28b2e74eb095d1f2583b35c8d5ac0dd07c2556c5fae0543d871911c6f7ae0994 |
|
MD5 | fae4c3d9b8a17796b8b9ebbb0dc3fcb9 |
|
BLAKE2b-256 | c4358e6c3d4bda98c7dd63a140f0533179f00cac44a7082dee9328447afae6d7 |
Hashes for py_scdb-0.2.2-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a65dae5520c5564d80c2d50ea35df6a530af915f14126386b75cebe106969c20 |
|
MD5 | f96b4c4e0f6a09c036368ed949f22f6e |
|
BLAKE2b-256 | a80a1956ecd94419971e22947af685cdb0cf7f32d43281bd0ceda364d9782f2b |
Hashes for py_scdb-0.2.2-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac3b6b018cb6dae98c09ce3bb8a92c00eac2f930ff8418493beae5522a009106 |
|
MD5 | 69dfaa4cc4d4fa44ca833efb3fddc4a9 |
|
BLAKE2b-256 | 5106623a29a550f63f04114abf7f0e138df56ef11d19f45dd94afd4b0d1059ca |
Hashes for py_scdb-0.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86f03a3f95003168befb309978246b76991ed062ffc25203711c75255c82b626 |
|
MD5 | f3bbabd295f74f529896b6685dcb34b8 |
|
BLAKE2b-256 | 866b537b347bccde085a4fbb9b8ba037b40bdcbe6820be56e4d5206ca5c25dd2 |
Hashes for py_scdb-0.2.2-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 007c1ace18f96b4a6f62d7688db0db1aa4dfbe92187e778d66da33e6854dd183 |
|
MD5 | 14dd9d2f31be8f007916d4a15b85e7be |
|
BLAKE2b-256 | d6fd7102e705e6dca5ca5dcaafddd1fde464444b1fe925f076f8688bcbc392f6 |
Hashes for py_scdb-0.2.2-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1713649fa415b6472cbf338e8e724f0f741207d235628c83b449a44c6061a42 |
|
MD5 | effa92073e122e4e974bc05d00217d90 |
|
BLAKE2b-256 | 3ce143e3d763f88c7764f9b6649944425759b469dc0dac05bc1214c0fc51fc80 |
Hashes for py_scdb-0.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 558193bd13bbfa333d005d45287c8f1b6a977a89f429c814a4e58c3e7a707cca |
|
MD5 | d7c0e72d924507a67cddb64da141261c |
|
BLAKE2b-256 | 3320f39d72290b0b80f8b648e1c64d5a2214d6e256dee5d3bd15a1481fde31d2 |
Hashes for py_scdb-0.2.2-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f87d464177a08ff058f84627446283d42a17e2f5bced7613607682948f0ce53d |
|
MD5 | a266d5f285a42117b6edc594a04c28d3 |
|
BLAKE2b-256 | fa021345d79b9f99e8f36e1b6ffeb91ea6cc367fdcd1364a3cc7f5253eee9f34 |
Hashes for py_scdb-0.2.2-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 406a768aaa05dd1330e28ec7a64c96098c9262871bfdf2437bf1ad1b1c4f458a |
|
MD5 | 1f180f5331c318d8740f2ca1d3c7b5d0 |
|
BLAKE2b-256 | d3baba1623f8c9c32e818a524f90f6e35df4872175ed8af0e8dfa31d3d5dc933 |
Hashes for py_scdb-0.2.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3eb46668dbc9b81f991299a98b680f66c46ea7122ddb34984aa837e7b1b4ddb |
|
MD5 | 8ab34bdbec5a524a8bd248862aa1db42 |
|
BLAKE2b-256 | 149f5f78c2a093d45243b794643569ca0a21d96bdb79d901aa0b352fdc2c226b |
Hashes for py_scdb-0.2.2-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 895f96b47f564c030c2234492cc8d70b5279cded03e7dc7e341fddd948023e7d |
|
MD5 | 072d450c92c6dafa9146efbeea08ad52 |
|
BLAKE2b-256 | 9c8dcbec3f80fc206f9632af0afa1f9c2ac670eed88610c821e3fae1fcc21914 |