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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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