Skip to main content

string_store

String store for persisting sets of strings on disk.

This module provides a simple store implementation for tracking strings (e.g., MRNs) that persist across multiple runs. The store is backed by SQLite via diskcache and acts as a Container, so you can use x in store to check membership.

Classes​

StringStore​

class StringStore(store_dir: Path, store_name: str):

A persistent store for strings backed by SQLite via diskcache.

This store acts as a set-like container for strings, allowing membership testing with x in store. The store persists to disk and can be used across multiple runs.

The store uses diskcache (SQLite-backed) with no compression, optimized for simple string storage.

Items in this store are never implicitly removed. All removal mechanisms are disabled:

  • Eviction is disabled (eviction_policy="none")
  • Size limits are disabled (size_limit=None)
  • Automatic culling is disabled (cull_limit=0)
  • Items never expire (no expire parameter on set operations)

Items can only be removed by explicit calls to discard() or remove_many().

Initialize string store.

Arguments

  • store_dir: Directory where the store should be stored.
  • store_name: Name for the store (used as subdirectory name).

Static methods​


add_many_to_store_in_dir​

def add_many_to_store_in_dir(store_dir: Path, store_name: str, values: set[str]) ‑> None:

Add multiple strings to a store in a directory.

This is a thin wrapper around add that performs "best efforts" adding of the set. If an exception occurs partway through, some values may have been added while others were not. For atomic "all-or-nothing" behavior, use transact to wrap the operation.

Arguments

  • store_dir: Directory where the store is stored.
  • store_name: Name of the store.
  • values: Set of strings to add.

Raises

  • TypeError: If any value in values is not a string.

get_all_from_store_in_dir​

def get_all_from_store_in_dir(store_dir: Path, store_name: str) ‑> set[str]:

Get all strings from a store in a directory.

Arguments

  • store_dir: Directory where the store is stored.
  • store_name: Name of the store.

Returns Set of all strings in the store.

open​

def open(    store_dir: Path, store_name: str,) ‑> StringStore:

Get or create a string store for a given directory and name.

The returned store is a context manager and should be used with with:

with StringStore.open(store_dir, store_name) as store:
store.add("value")

Arguments

  • store_dir: Directory where the store should be stored.
  • store_name: Name for the store.

Returns StringStore instance that can be used as a context manager.

remove_many_from_store_in_dir​

def remove_many_from_store_in_dir(    store_dir: Path, store_name: str, values: set[str],) ‑> None:

Remove multiple strings from a store in a directory.

This is a thin wrapper around discard that performs "best efforts" removal of the set. If an exception occurs partway through, some values may have been removed while others were not. For atomic "all-or-nothing" behavior, use transact to wrap the operation.

Arguments

  • store_dir: Directory where the store is stored.
  • store_name: Name of the store.
  • values: Set of strings to remove.

Raises

  • TypeError: If any value in values is not a string.

Methods​


add​

def add(self, value: str) ‑> None:

Add a string to the store.

Arguments

  • value: String to add.

add_many​

def add_many(self, values: set[str]) ‑> None:

Add multiple strings to the store.

This is a thin wrapper around add that performs "best efforts" adding of the set. If an exception occurs partway through, some values may have been added while others were not. For atomic "all-or-nothing" behavior, use transact to wrap the operation.

Arguments

  • values: Set of strings to add.

Raises

  • TypeError: If any value in values is not a string.

close​

def close(self) ‑> None:

Close the store.

delete_store​

def delete_store(self) ‑> None:

Delete the store from disk.

This closes the store and removes the entire store directory from disk.

discard​

def discard(self, value: str) ‑> None:

Remove a string from the store if present.

Arguments

  • value: String to remove.

Raises

  • TypeError: If value is not a string.

get_all​

def get_all(self) ‑> set[str]:

Get all strings from the store.

Returns Set of all strings in the store.

remove_many​

def remove_many(self, values: set[str]) ‑> None:

Remove multiple strings from the store.

This is a thin wrapper around discard that performs "best efforts" removal of the set. If an exception occurs partway through, some values may have been removed while others were not. For atomic "all-or-nothing" behavior, use transact to wrap the operation.

Arguments

  • values: Set of strings to remove.

Raises

  • TypeError: If any value in values is not a string.

transact​

def transact(self) ‑> contextlib.AbstractContextManager[None]:

Get a transaction context manager for atomic operations.

All operations within the transaction block are atomic - either all succeed or all are rolled back if an exception occurs.

Returns Context manager that provides transaction semantics.