Skip to main content

lazy_db

badge badge

A lazily loaded key:value db intended for use with large datasets that are too big to be loaded into memory. The database supports integers, strings, lists of integers, bytes, and dictionaries. This database is meant to strike a good balance of retrieval/insertion speed and memory usage. This database best fits a scenario where each key has a lot of data stored under it. Scenarios where values are under 100 bytes in size this database is not very well suited for.

Install with pip install lazy-database

Example usage:

from lazy_db import LazyDb

# Simple example usage
db = LazyDb("test.lazy")
db.write("test_value", "value")
print(db.read("test_value"))  # prints "value"
db.close()

# Or use a with statement to insure the database file is closed cleanly and avoid having to call db.close() on your own
with LazyDb("test2.lazy") as db:
    db.write("test_value", "value")
    print(db.read("test_value"))

How it works

File layout

All text in database files are encoded in utf-8 format. Each database has a json string at the start of the file that denotes the database's settings, who's end is marked with a NUL byte (00 in hex)

Each database entry is appended at the end of the file and is laid out in this format:

Name Size (bytes) Purpose
NUL 1 Marks the start of the entry. When the initial headers index, the starting byte of each entry is checked for this NUL byte to be sure the database hasn't been corrupted. This is the beginning to what is considered the "header" for the entry (NUL bytes carry a hex value of 0x00)
Key type 1 Marks if the key is an integer or a string.
Key any The key for the database entry.
NUL 1 Marks the end of the key. This is necessary since string keys don't have a set size.
Content length content_int_size An integer (little endian) depicting the length of the content (including the content type). Defaults to 4 bytes long. This is the end to what is considered the "header" for the entry
Content type 1 Marks if the content is a string, int, int list, dict, or bytes.
Content Content length Stores the content

Content type labels

Name Hex type value Type description
String 0x01 A utf-8 string
Int 0x02 An integer
Dict 0x03 A dictionary (internally stored as a utf-8 json string)
Int list 0x04 A list of integers. Max integer size is defined by int_list_size (default: 4 bytes)
Bytes 0x05 A bytes object

The algorithm

When loading a database, all entry headers are scanned for their key value and lengths. This allows for values to be retrieved very quickly without having to load the content of every entry, at the cost of having to store the key and content length in memory though. This approach makes the database best for cases where your database will be storing a lot of data in each key that you can't afford to store in memory, however you can afford to store the name values and lengths of each element in memory.

Metadata

Release files for lazy-database 1.1.0

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

Source distribution (sdist)

Source distribution for lazy-database 1.1.0
File Size Uploaded Provenance
lazy_database-1.1.0.tar.gz 10.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for lazy-database 1.1.0
File Interpreter ABI Platform
lazy_database-1.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 17.9 kB

Release files / lazy_database-1.1.0.tar.gz

Download URL lazy_database-1.1.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
98664d61c8d3ebbf6caaf25210887dd9d7c9bcadc98faf654b18f1f34dfba431
BLAKE2b-256 checksum
How to use checksums
202637cca88d66bb9491f71e9ee8180546bfa01127bbdd94bc08907355cbb003
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release files / lazy_database-1.1.0-py2.py3-none-any.whl

Download URL lazy_database-1.1.0-py2.py3-none-any.whl
Size 7.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
c539ae0728e961417e8be8d50f5bcb21f2d778abb7720c2c5b70ed01d19c8ef6
BLAKE2b-256 checksum
How to use checksums
2153ddeb1617c4ce1f46515b80304afcc630f5d86380d1ed7a08db568b64719c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Security for lazy-database 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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