Skip to main content

Interface for any Key-Value DB. Completely exception-free, and backed by `haskellian` for powerful composability

Project description

Key-Value API

ABC for a an async key-value store

pip install kv-api

Spec

class KV(Generic[T]):
  
  async def insert(self, key: str, value: T) -> Either[DBError, None]:
    ...

  async def read(self, key: str) -> Either[ReadError, T]:
    ...

  async def delete(self, key: str) -> Either[DBError | InexistentItem, None]:
    ...

  async def items(self, batch_size: int | None = None) -> AsyncIter[Either[DBError | InvalidData, tuple[str, T]]]:
    ...

  async def has(self, key: str) -> Either[DBError, bool]:
    ...
  
  async def keys(self) -> Either[DBError, Sequence[str]]:
    ...

  async def values(self, batch_size: int | None = None) -> AsyncIter[Either[DBError|InvalidData, T]]:
    ...

  async def copy(self, key: str, to: KV[T], to_key: str) -> Either[DBError|InexistentItem, None]:
    ...

  async def move(self, key: str, to: KV[T], to_key: str) -> Either[DBError|InexistentItem, None]:
    ...

Usage

  • All functions return Either instead of throwing
  • For chaining multiple operations, it is recommended to use a kind of do notation for Either, using exceptions
    • Either.unsafe() unwraps the right value or raises IsLeft
from kv.api import KV, DBError
from haskellian import Either, IsLeft

async def multi_copy(key_from: str, keys_to: str, data: KV[bytes]) -> Either[DBError, None]:
  try:
    value = (await data.read(key_from)).unsafe()
    for key in keys_to:
      (await data.insert(key, value)).unsafe()
    return Right(None)
  except IsLeft as e:
    return Left(e.value)

Extensions

AppendableKV[T]

A KV[Sequence[T]] that supports appending

class AppendableKV(KV[Sequence[T]], Generic[T]):
  async def append(self, id: str, values: Sequence[T], *, create: bool) -> Either[DBError|InexistentItem, None]:
    ...

LocatableKV[T]

A KV[T] whose items can be accessed via some URL

class LocatableKV(KV[T], Generic[T]):
  def url(self, id: str) -> str:
    ...

Implementations

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

kv_api-0.1.24.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

kv_api-0.1.24-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file kv_api-0.1.24.tar.gz.

File metadata

  • Download URL: kv_api-0.1.24.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for kv_api-0.1.24.tar.gz
Algorithm Hash digest
SHA256 9943115350cf75002f6d58ba55bbd9ddb6b8841c466c0f2e2bed9c6e1d732007
MD5 aeba413d0838306fa98a52069c2a3dd3
BLAKE2b-256 3dcc68a509219ea87eddc6c992c2d343421ea7e8f4fcdee4f1614dd9e345ba12

See more details on using hashes here.

File details

Details for the file kv_api-0.1.24-py3-none-any.whl.

File metadata

  • Download URL: kv_api-0.1.24-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for kv_api-0.1.24-py3-none-any.whl
Algorithm Hash digest
SHA256 f3bf059d4baa6ce92be5b9ea75dddb1abf4a040b76c8d401b37ec7da41f10ea0
MD5 9d46a45a7e3b77c352b7fb9ef23b23f6
BLAKE2b-256 adaa241d7c28f20c4ef2bfc2c0f9fda9b82868785793186f8c1fc50ddce0df9a

See more details on using hashes here.

Supported by

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