Simple append-only database with rich record formats and compression
Project description
AmoreDB
Simple append-only database for Python with rich record formats and compression.
For impatients
pip install amoredb
Example 1
import asyncio
from amoredb import AmoreDB
async def main():
async with AmoreDB('test', 'w') as db:
await db.append(b'foo')
await db.append(b'bar')
await db.append(b'baz')
async for record in db:
print(record)
asyncio.run(main())
Result:
b'foo'
b'bar'
b'baz'
Example 2
import asyncio
from amoredb.json import JsonAmoreDB
async def main():
async with JsonAmoreDB('test.json', 'w') as db:
await db.append({'foo': 'bar'})
await db.append({'bar': 'foo'})
async for record in db:
print(record)
asyncio.run(main())
Result:
{'foo': 'bar'}
{'bar': 'foo'}
Record formats
The basic format for database records is bytes object. Subclasses may support other formats, as demonstrated above in the Example 2. AmoreDB provides support for the following formats:
- JSON:
JsonMixin
,JsonAmoreDB
from amoredb.json - strings:
StrMixin
,StrAmoreDB
from amoredb.str - structures:
StructMixin
,StructAmoreDB
from amoredb.struct - BSON:
BsonMixin
,BsonAmoreDB
from amoredb.bson, requires simple_bson package
Records are converted to the binary data by mixins and AmoreDB provides predefined classes, such, for example, as
class JsonAmoreDB(JsonMixin, AmoreDB):
pass
Record compression
Similar to record format conversion, compression is implemented by mix-ins. AmoreDB provides a few for the following formats:
- gzip:
GzipMixin
from amoredb.gzip - lzma:
LzmaMixin
from amoredb.lzma - lz4:
Lz4Mixin
from amoredb.lzma, requires lz4 package - brotli:
BrotliMixin
from amoredb.brotli, requires brotli package - snappy:
SnappyMixin
from amoredb.snappy, requires python-snappy package
There are no predefined classes for compression, it's up to end users to define ones for their needs. For example,
from amoredb import AmoreDB
from amoredb.json import JsonMixin
from amoredb.gzip import GzipMixin
class MyDB(JsonMixin, GzipMixin, AmoreDB):
pass
async with MyDB('test.json.gz', 'w', compresslevel=5) as db:
await db.append({'foo': 'bar'})
await db.append({'bar': 'foo'})
async for record in db:
print(record)
Record transformation pipeline
Records in AmoreDB are processed by the following methods:
def record_to_raw_data(self, record_data):
# do custom conversion here
# ...
# call base method
return super().record_to_raw_data(record_data)
def record_from_raw_data(self, record_data):
# do custom conversion here
# ...
# call base method
return super().record_from_raw_data(record_data)
Mix-ins override these methods and to make pipeline working, mix-ins should be defined in the right order. As we have seen above,
class MyDB(JsonMixin, GzipMixin, AmoreDB):
pass
GzipMixin
is placed in between, because compression takes place after converting record from JSON to binary data
and before writing this data to file. Same for opposite direction.
Database structure
The database consists of data file and index file. Optional metadata file in JSON format may contain the structure of database class.
Index file contains positions of records except the first one which is always zero. The first element in index file is the offset of the next record. Thus, the number of items in the index file equals to the number of records.
Record id is implicit, it is the index of the record. Thus, to get a record by id, read its offset from the index file and then read the record from data file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file amoredb-0.0.3.tar.gz
.
File metadata
- Download URL: amoredb-0.0.3.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a63252518f9e1b47430e91eae329a892ea91a6adcf999ccf1e19761e0937db58 |
|
MD5 | 3b81b122be7d2a40bd9714f4cd61bcfc |
|
BLAKE2b-256 | 65ac17ee6c710ade22acf6f494e74849feb2f04c814cad3461fa17534ff35cc9 |
File details
Details for the file amoredb-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: amoredb-0.0.3-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a20588a1a5b1e9cbc6b92b3c5eddec61cfc7d2b7c39f28455363067b41304964 |
|
MD5 | c4957718f7c92dfa6aee4caa90e0b5c8 |
|
BLAKE2b-256 | 5248eaf744d1131782420fada6080d95c4e5b5201b608df3b28fd05f599f2b51 |