soev for developers

warren.storage.results

Results: worker outputs keyed by document, job and part, with an optional cache.

Classes

BinaryResultsStore

classBinaryResultsStore(document_store, cache=None, result_type=None, name=None, *, cache_ttl_seconds=3600)

Results store for byte payloads.

Focused API — intentionally does not implement ResultsStoreInterface because that Protocol's result: Dict contract doesn't fit byte payloads (and broadening it to Any would ripple through every reader).

paramdocument_storeDocumentStoreInterface

MongoDB-backed store for persistence. Must have a unique composite index on (doc_id, job_id, part_idx) (validated in setup()).

paramcacheCacheInterface[bytes] | None
= None

Optional CacheInterface[bytes] for fast reads. When absent, every read hits MongoDB.

paramresult_typestr | None
= None

Not used. Give each results store its own cache base_key. Two stores that share one cache instance overwrite each other's entries.

paramnamestr | None
= None

Optional logger name.

paramcache_ttl_secondsint
= 3600

Expiry for cached payloads.

methodsetupasync () -> None

Validate the document store's unique composite index.

methodstore_bytesasync (payload, *, doc_id, part_idx=0, job_id=None, metadata=None, do_cache=True, overwrite_existing=True) -> str

Persist payload and populate the cache.

The cache key is doc:<doc_id>:<job_id>, or doc:<doc_id> when job_id is None. This is the key that CachedDocumentFetcher reads.

parampayloadbytes

The bytes to store.

paramdoc_idstr

Document ID.

parampart_idxint
= 0

Part index. 0 for a single-part payload.

paramjob_idstr | None
= None

Job ID, or None if you do not group by job.

parammetadatadict[str, Any] | None
= None

Optional metadata, stored in the field metadata (DefaultResultsStore uses result_metadata).

paramdo_cachebool
= True

Whether to write the payload to the cache.

paramoverwrite_existingbool
= True

Whether to replace a stored record with the same (doc_id, job_id, part_idx).

Returns

str

Result ID from the document store.

Raises:

  • DocumentAlreadyExistsError: If overwrite_existing is False and a record for these keys exists.
methodget_bytesasync (doc_id, *, part_idx=0, job_id=None) -> bytes

Read bytes by business keys. Cache-first, then MongoDB.

paramdoc_idstr
parampart_idxint
= 0
paramjob_idstr | None
= None

Raises:

  • ResultNotFound: If no record exists for the given keys.
methodquery_metadataasync (*, doc_id, job_id=None) -> AsyncGenerator[dict[str, Any], None]

Yield the stored records of a document and job, without their payload.

Use it to list records without the transfer of the payload bytes.

paramdoc_idstr
paramjob_idstr | None
= None

DefaultResultsStore

classDefaultResultsStore(document_store, cache=None, result_type=None, name=None, *, cache_ttl_seconds=3600)

Default async implementation of ResultsStoreInterface.

Stores document processing results using an injected DocumentStoreInterface for persistence and an optional CacheInterface for caching.

The document store must have a unique composite index on (doc_id, job_id, part_idx) to ensure uniqueness and enable efficient queries. The setup() method validates this requirement and raises ValueError if the index is missing.

Call await setup() after construction. create_default_results_store calls it for you.

Initialize the results store.

paramdocument_storeDocumentStoreInterface

Storage backend for persistence. Must have a unique composite index on (doc_id, job_id, part_idx).

paramcacheCacheInterface[dict] | None
= None

Optional cache for read-through/write-through caching.

paramresult_typestr | None
= None

Not used. Give each results store its own cache base_key. Two stores that share one cache instance overwrite each other's entries.

paramnamestr | None
= None

Optional name for logging purposes.

paramcache_ttl_secondsint
= 3600

Expiry for cached results.

methodsetupasync () -> None

Validate that the document store has the required unique index.

Call it after construction, before you use the store. create_default_results_store calls it for you.

Raises:

  • ValueError: If document store lacks required unique index.
methodstoreasync (result, doc_id, part_idx=None, job_id=None, result_metadata=None, do_cache=True, overwrite_existing=True) -> str

Store a processing result.

paramresultdict

The processing result to store.

paramdoc_idstr

Document ID.

parampart_idxint | None
= None

Part index (None or 0 for single-part results).

paramjob_idstr | None
= None

Job ID (None if not using job grouping).

paramresult_metadatadict | None
= None

Optional metadata about how the result was produced.

paramdo_cachebool
= True

Whether to cache the result.

paramoverwrite_existingbool
= True

Whether to overwrite existing result.

Returns

str

Document ID from the document store.

Raises:

  • DocumentAlreadyExistsError: If overwrite_existing is False and a result for these keys exists.
methodstore_manyasync (items) -> None

Upsert results in bulk. For repeated keys, the last item wins.

paramitemsSequence[ResultItem]
methodget_resultasync (doc_id, part_idx=None, job_id=None) -> ResultDoc

Retrieve a processing result by business keys.

paramdoc_idstr

Document ID.

parampart_idxint | None
= None

Part index (None or 0 for single-part results).

paramjob_idstr | None
= None

Job ID (None if not using job grouping).

Returns

ResultDoc

The result document.

Raises:

  • ResultNotFound: If no result exists for the given keys.
methodstream_doc_processing_resultsasync (doc_id, job_id=None, try_cache=True) -> AsyncGenerator[ResultDoc, None]

Stream persisted results in ascending part order, bypassing the cache.

paramdoc_idstr

Document ID.

paramjob_idstr | None
= None

Job ID (None if not using job grouping).

paramtry_cachebool
= True

Deprecated and ignored; retained for compatibility.

Returns

AsyncGenerator[ResultDoc, None]

Async generator yielding result documents.

Raises:

  • DocumentProcessingResultsNotFound: If no results exist.

ResultDoc

modelResultDoc

Document representing a stored processing result.

attributedoc_idstr
attributepart_idxint
= 0
attributejob_idstr | None
= None
attributeresultdict
attributeresult_metadatadict | None
= None
attributecreated_atdatetime | None
= None
attributeresult_idstr | None
= None

ResultItem

dataclassResultItem(result, doc_id, part_idx=None, job_id=None, result_metadata=None)

A processing result and its storage keys.

paramresultdict
paramdoc_idstr
parampart_idxint | None
= None
paramjob_idstr | None
= None
paramresult_metadatadict | None
= None

Protocols

ResultsStoreInterface

protocolResultsStoreInterface

Async document-level results store for a specific type of document processing.

All methods are async — implementations must use non-blocking I/O to avoid blocking the event loop.

Assumes storage and caching capabilities that are aware of the specific result type. The implementation is dependent on the result/worker type.

methodstoreasync (result, doc_id, part_idx=None, job_id=None, result_metadata=None, do_cache=True, overwrite_existing=True) -> str

Store (partial) result for processing a document.

paramresultdict

Document processing (partial) result.

paramdoc_idstr

Original document ID.

parampart_idxint | None
= None

Part index of the result (None or 0 for single-part results).

paramjob_idstr | None
= None

Job ID of the processing job (None if not using job grouping).

paramresult_metadatadict | None
= None

Metadata about how the result was produced.

paramdo_cachebool
= True

Whether to cache the result.

paramoverwrite_existingbool
= True

Whether to overwrite existing result.

Returns

str

ID of the stored result.

methodstore_manyasync (items) -> None

Upsert results in bulk. For repeated keys, the last item wins.

paramitemsSequence[ResultItem]
methodget_resultasync (doc_id, part_idx=None, job_id=None) -> ResultDoc

Retrieves the document processing result for given business keys.

paramdoc_idstr

The document ID.

parampart_idxint | None
= None

The part index (None or 0 for single-part results).

paramjob_idstr | None
= None

The job ID (None if not using job grouping).

Returns

ResultDoc

The result document.

Raises:

  • ResultNotFound: If no result exists for the given keys.
methodstream_doc_processing_results(doc_id, job_id=None, try_cache=True) -> AsyncGenerator[ResultDoc, None]

Stream persisted results in ascending part order, bypassing the cache.

paramdoc_idstr

The document ID.

paramjob_idstr | None
= None

The job ID (None if not using job grouping).

paramtry_cachebool
= True

Deprecated and ignored; retained for compatibility.

Returns

AsyncGenerator[ResultDoc, None]

Async generator yielding result documents.

Raises:

  • DocumentProcessingResultsNotFound: If no results exist for the document and job.

Functions

create_binary_results_store

functioncreate_binary_results_storeasync (collection_name, mongo_client, redis_client=None, *, database_name='document_processing', doc_id_field='result_id', cache_base_key='documents', cache_ttl_seconds=3600, scoped_database=None) -> BinaryResultsStore

Create a BinaryResultsStore wired with MongoDB + RedisBinaryCache.

paramcollection_namestr

MongoDB collection name.

parammongo_clientAsyncMongoClient

Async MongoDB client.

paramredis_clientRedis | None
= None

Async Redis client. None disables caching.

paramdatabase_namestr
= 'document_processing'

MongoDB database name.

paramdoc_id_fieldstr
= 'result_id'

Field name for the per-result primary key.

paramcache_base_keystr
= 'documents'

Redis namespace. The default "documents" is the namespace that CachedDocumentFetcher reads. So a downstream worker that reads through GetDocumentFunc gets these bytes from the cache.

paramcache_ttl_secondsint
= 3600

Expiry for cached payloads in seconds (default 3600).

paramscoped_databaseScopedDatabase | None
= None

Resolve the database from the scope of the current handler (database_name is then not used), and scope the cache keys. The indexes of a scope database are created when the scope is first used.

create_default_results_store

functioncreate_default_results_storeasync (collection_name, mongo_client, redis_client=None, *, database_name='document_processing', doc_id_field='result_id', cache_base_key_prefix='results', cache_ttl_seconds=3600, scoped_database=None) -> DefaultResultsStore

Create a DefaultResultsStore with MongoDB persistence and optional Redis cache.

Returns a fully initialized store (setup() already called on both the document store and the results store).

paramcollection_namestr

MongoDB collection name.

parammongo_clientAsyncMongoClient

Injected async MongoClient instance.

paramredis_clientRedis | None
= None

Injected async Redis instance. If None, no caching.

paramdatabase_namestr
= 'document_processing'

MongoDB database name.

paramdoc_id_fieldstr
= 'result_id'

Field name for document IDs.

paramcache_base_key_prefixstr
= 'results'

Prefix for Redis cache keys. The cache base_key is <cache_base_key_prefix>:<collection_name>.

paramcache_ttl_secondsint
= 3600

Default TTL for cached results.

paramscoped_databaseScopedDatabase | None
= None

Resolve the database from the scope of the current handler (database_name is then not used), and scope the cache keys. The indexes of a scope database are created when the scope is first used.

Errors

DocumentProcessingResultsNotFound

errorDocumentProcessingResultsNotFound

ResultNotFound

errorResultNotFound

On this page