GitHub

KeyValueStoreService

API reference for KeyValueStoreService.

A string-keyed store of text and bytes. Build one with make (or make_string_only for a backend that holds only text). Every operation fails with KeyValueStoreError when the backend does.

Attributes

AttributeTypeDescription
clearEffect[None, KeyValueStoreError]

Removes every entry.

sizeEffect[int, KeyValueStoreError]

The number of entries.

keysEffect[list[str], KeyValueStoreError]

A snapshot of the keys, in no particular order.

is_emptyEffect[bool, KeyValueStoreError]

Whether the store holds no entries.

Functions

__init__

__init__(*, get, get_bytes, set, remove, clear, clear_prefix, size, keys, modify, modify_bytes, has, is_empty) -> None
ParameterTypeDescription
getCallable[[str], Effect[str | None, KeyValueStoreError]]
get_bytesCallable[[str], Effect[bytes | None, KeyValueStoreError]]
setCallable[[str, Value], Effect[None, KeyValueStoreError]]
removeCallable[[str], Effect[None, KeyValueStoreError]]
clearEffect[None, KeyValueStoreError]
clear_prefixCallable[[str], Effect[None, KeyValueStoreError]]
sizeEffect[int, KeyValueStoreError]
keysEffect[list[str], KeyValueStoreError]
modifyCallable[[str, Callable[[str], str]], Effect[str | None, KeyValueStoreError]]
modify_bytesCallable[[str, Callable[[bytes], bytes]], Effect[bytes | None, KeyValueStoreError]]
hasCallable[[str], Effect[bool, KeyValueStoreError]]
is_emptyEffect[bool, KeyValueStoreError]
def __init__(
    self,
    *,
    get: Callable[[str], Effect[str | None, KeyValueStoreError]],
    get_bytes: Callable[[str], Effect[bytes | None, KeyValueStoreError]],
    set: Callable[[str, Value], Effect[None, KeyValueStoreError]],
    remove: Callable[[str], Effect[None, KeyValueStoreError]],
    clear: Effect[None, KeyValueStoreError],
    clear_prefix: Callable[[str], Effect[None, KeyValueStoreError]],
    size: Effect[int, KeyValueStoreError],
    keys: Effect[list[str], KeyValueStoreError],
    modify: Callable[[str, Callable[[str], str]], Effect[str | None, KeyValueStoreError]],
    modify_bytes: Callable[
        [str, Callable[[bytes], bytes]], Effect[bytes | None, KeyValueStoreError]
    ],
    has: Callable[[str], Effect[bool, KeyValueStoreError]],
    is_empty: Effect[bool, KeyValueStoreError],
) -> None:
    for name, value in (
        ("clear", clear),
        ("size", size),
        ("keys", keys),
        ("is_empty", is_empty),
    ):
        require_effect(value, name)
    self._get = get
    self._get_bytes = get_bytes
    self._set = set
    self._remove = remove
    self._clear = clear
    self._clear_prefix = clear_prefix
    self._size = size
    self._keys = keys
    self._modify = modify
    self._modify_bytes = modify_bytes
    self._has = has
    self._is_empty = is_empty

get

get(key) -> Effect[str | None, KeyValueStoreError]

key's value as text (bytes as Base64); None when missing.

ParameterTypeDescription
keystr

Returns Effect[str | None, KeyValueStoreError]

def get(self, key: str) -> Effect[str | None, KeyValueStoreError]:
    """``key``'s value as text (bytes as Base64); ``None`` when
    missing."""
    return self._get(key)

get_bytes

get_bytes(key) -> Effect[bytes | None, KeyValueStoreError]

key's value as bytes (text as UTF-8); None when missing.

ParameterTypeDescription
keystr

Returns Effect[bytes | None, KeyValueStoreError]

def get_bytes(self, key: str) -> Effect[bytes | None, KeyValueStoreError]:
    """``key``'s value as bytes (text as UTF-8); ``None`` when
    missing."""
    return self._get_bytes(key)

set

set(key, value) -> Effect[None, KeyValueStoreError]

Sets key to value (text or bytes).

ParameterTypeDescription
keystr
valueValue

Returns Effect[None, KeyValueStoreError]

def set(self, key: str, value: Value) -> Effect[None, KeyValueStoreError]:
    """Sets ``key`` to ``value`` (text or bytes)."""
    return self._set(key, value)

remove

remove(key) -> Effect[None, KeyValueStoreError]

Removes key; nothing happens when it is missing.

ParameterTypeDescription
keystr

Returns Effect[None, KeyValueStoreError]

def remove(self, key: str) -> Effect[None, KeyValueStoreError]:
    """Removes ``key``; nothing happens when it is missing."""
    return self._remove(key)

clear_prefix

clear_prefix(prefix) -> Effect[None, KeyValueStoreError]

Removes every entry whose key starts with prefix.

ParameterTypeDescription
prefixstr

Returns Effect[None, KeyValueStoreError]

def clear_prefix(self, prefix: str) -> Effect[None, KeyValueStoreError]:
    """Removes every entry whose key starts with ``prefix``."""
    return self._clear_prefix(prefix)

modify

modify(key, f) -> Effect[str | None, KeyValueStoreError]

Replaces key's value (as text) with f(value) and gives the new value; None, and nothing written, when key is missing.

ParameterTypeDescription
keystr
fCallable[[str], str]

Returns Effect[str | None, KeyValueStoreError]

def modify(self, key: str, f: Callable[[str], str]) -> Effect[str | None, KeyValueStoreError]:
    """Replaces ``key``'s value (as text) with ``f(value)`` and gives the
    new value; ``None``, and nothing written, when ``key`` is
    missing."""
    return self._modify(key, f)

modify_bytes

modify_bytes(key, f) -> Effect[bytes | None, KeyValueStoreError]

Replaces key's value (as bytes) with f(value) and gives the new value; None, and nothing written, when key is missing.

ParameterTypeDescription
keystr
fCallable[[bytes], bytes]

Returns Effect[bytes | None, KeyValueStoreError]

def modify_bytes(
    self, key: str, f: Callable[[bytes], bytes]
) -> Effect[bytes | None, KeyValueStoreError]:
    """Replaces ``key``'s value (as bytes) with ``f(value)`` and gives
    the new value; ``None``, and nothing written, when ``key`` is
    missing."""
    return self._modify_bytes(key, f)

has

has(key) -> Effect[bool, KeyValueStoreError]

Whether the store holds key.

ParameterTypeDescription
keystr

Returns Effect[bool, KeyValueStoreError]

def has(self, key: str) -> Effect[bool, KeyValueStoreError]:
    """Whether the store holds ``key``."""
    return self._has(key)

prefix

prefix(prefix) -> KeyValueStoreService

This store with prefix put before every key. keys (without the prefix), size, is_empty and clear see only the keys starting with prefix: the first three list every key of this store and filter them, clear is clear_prefix(prefix).

ParameterTypeDescription
prefixstr

Returns KeyValueStoreService

def prefix(self, prefix: str) -> KeyValueStoreService:
    """This store with ``prefix`` put before every key. ``keys`` (without
    the prefix), ``size``, ``is_empty`` and ``clear`` see only the keys
    starting with ``prefix``: the first three list every key of this
    store and filter them, ``clear`` is ``clear_prefix(prefix)``."""
    return _prefixed(self, prefix)

to_schema_store

to_schema_store[T, RD, RE](schema) -> SchemaStore[T, RD, RE]

This store holding schema's values, written as JSON text.

ParameterTypeDescription
schemaCodec[T, Any, RD, RE]

Returns SchemaStore[T, RD, RE]

def to_schema_store[T, RD, RE](self, schema: Codec[T, Any, RD, RE]) -> SchemaStore[T, RD, RE]:
    """This store holding ``schema``'s values, written as JSON text."""
    from pyfect.persistence.key_value_store._schema_store import to_schema_store

    return to_schema_store(self, schema)