Skip to main content

Thread-safe persistence management with pluggable storage backends

Project description

oj-persistence

Thread-safe persistence management with pluggable storage backends.

Installation

pip install oj-persistence

Overview

oj-persistence provides a singleton PersistenceManager that acts as the single I/O interface for all data operations. Callers register named stores with the manager and interact exclusively through it — stores are never touched directly for data operations.

Designed for use with multi-threaded applications and async pipelines (e.g. io_chains).

Quick start

from oj_persistence import PersistenceManager, InMemoryStore, NdjsonFileStore

pm = PersistenceManager()

# Register stores by name
pm.get_or_create('cache', lambda: InMemoryStore())
pm.get_or_create('events', lambda: NdjsonFileStore('events.ndjson'))

# All data ops go through the manager
pm.create('cache', 'user:1', {'name': 'Alice'})
pm.upsert('events', 'evt:1', {'type': 'login', 'user': 'user:1'})

value = pm.read('cache', 'user:1')   # {'name': 'Alice'}
pm.update('cache', 'user:1', {'name': 'Alice', 'role': 'admin'})
pm.delete('cache', 'user:1')

results = pm.list('cache', predicate=lambda v: v.get('role') == 'admin')

CRUDL semantics

Method Behaviour
create Raises KeyError if key already exists
read Returns None if key is missing
update Raises KeyError if key does not exist
upsert Creates or overwrites — never raises
delete No-op if key is missing
list Returns all values, optionally filtered

Relational joins

ON = lambda user, order: user['id'] == order['user_id']

results = pm.join('users', 'orders', on=ON, how='left',
                  where=lambda u, o: o['total'] > 100)
# returns list of (left, right) tuples; unmatched rows have None on the missing side

Supported how values: inner (default), left, right, outer.

Storage backends

Class Format Notes
InMemoryStore In-process dict Fastest; not persistent
FlatFileStore JSON (full load/save) Simple; loads entire file per op
NdjsonFileStore NDJSON (one obj/line) Append-only creates; O(1) writes
IjsonFileStore Standard JSON (streaming) Streaming reads via ijson
CsvFileStore CSV Fieldnames inferred or pre-specified

All file-backed stores are thread-safe via a writer-preferring ReadWriteLock.

Custom stores

Implement AbstractStore:

from oj_persistence.store.base import AbstractStore

class MyStore(AbstractStore):
    def create(self, key, value): ...
    def read(self, key): ...
    def update(self, key, value): ...
    def upsert(self, key, value): ...
    def delete(self, key): ...
    def list(self, predicate=None): ...

pm.register('custom', MyStore())

Requirements

  • Python >= 3.11
  • ijson (for IjsonFileStore)

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

oj_persistence-0.0.2.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oj_persistence-0.0.2-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file oj_persistence-0.0.2.tar.gz.

File metadata

  • Download URL: oj_persistence-0.0.2.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for oj_persistence-0.0.2.tar.gz
Algorithm Hash digest
SHA256 87d5dbcaff2c2d3c574a9f7c0e983589a0ddb87cb0ee034bcb64d9a7809fdf7a
MD5 ca4f208cf9701a81a4f861b5e3cf7a1b
BLAKE2b-256 b7be546c83bd685450b354e249ea3b5a7fc85a080b6b3f49c230c608f3a82de7

See more details on using hashes here.

Provenance

The following attestation bundles were made for oj_persistence-0.0.2.tar.gz:

Publisher: publish.yml on ownjoo-org/persistence

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file oj_persistence-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: oj_persistence-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for oj_persistence-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d8ca3a25d257cd94941cb51ddce6da68682c19b194641e41a27d763e4a30ff
MD5 88b48b151c718b8af5ba5d601fbb343d
BLAKE2b-256 29bbba27cae86c9012d028f785a2ddee5f135e22ffa155ce64c5467f1aeca63b

See more details on using hashes here.

Provenance

The following attestation bundles were made for oj_persistence-0.0.2-py3-none-any.whl:

Publisher: publish.yml on ownjoo-org/persistence

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page