neosqlite.collection.query_engine package

Submodules

Module contents

class neosqlite.collection.query_engine.QueryEngine(collection)[source]

Bases: CRUDOperationsMixin, FindOperationsMixin, QueryMethodsMixin

A class that provides methods for querying and manipulating documents in a collection.

The QueryEngine handles all database operations including inserting, updating, deleting, and finding documents. It also supports aggregation pipelines, bulk operations, and various utility methods for counting and retrieving distinct values.

__init__(collection)[source]

Initialize the QueryEngine with a collection.

Parameters:

collection – The collection instance this QueryEngine will operate on.

add_tier_change_callback(callback: Callable[[str | None, str, list], None]) None[source]

Add a callback to be notified when query tier changes.

Callback receives: (previous_tier: str | None, new_tier: str, pipeline: list) where tier is one of: - “tier1” (SQL CTE - new aggregation optimizer) - “tier1_standard” (non-CTE SQL aggregation) - “tier2” (temp table for complex $expr) - “tier3” (Python fallback) - None (before any query)

remove_tier_change_callback(callback: Callable[[str | None, str, list], None]) bool[source]

Remove a tier change callback. Returns True if found.

get_last_tier() str | None[source]

Get the last tier that was used for query execution.

clear_tier_callbacks() None[source]

Clear all tier change callbacks.

_notify_tier_change(new_tier: str, pipeline: list) None[source]

Notify all callbacks of a tier change.

cleanup() None[source]

Clean up resources used by the QueryEngine.

aggregate(pipeline: list[dict[str, Any]], batch_size: int = 101, session: ClientSession | None = None) list[dict[str, Any]][source]

Applies a list of aggregation pipeline stages to the collection.

This method handles both simple and complex queries. For simpler queries, it leverages the database’s native indexing capabilities to optimize performance. For more complex queries, it falls back to a Python-based processing mechanism.

Parameters:
  • pipeline (list[dict[str, Any]]) – A list of aggregation pipeline stages to apply.

  • batch_size (int) – The batch size for fetching results from database.

  • session (ClientSession, optional) – A ClientSession for transactions.

Returns:

The list of documents after applying the aggregation pipeline.

Return type:

list[dict[str, Any]]

aggregate_with_constraints(pipeline: list[dict[str, Any]], batch_size: int = 101, memory_constrained: bool = False, session: ClientSession | None = None) list[dict[str, Any]] | 'CompressedQueue'[source]

Applies a list of aggregation pipeline stages with memory constraints.

Parameters:
  • pipeline (list[dict[str, Any]]) – A list of aggregation pipeline stages to apply.

  • batch_size (int) – The batch size for processing large result sets.

  • memory_constrained (bool) – Whether to use memory-constrained processing.

  • session (ClientSession, optional) – A ClientSession for transactions.

Returns:

The results as either a list or compressed queue.

Return type:

list[dict[str, Any]] | CompressedQueue

explain_aggregation(pipeline: list[dict[str, Any]], session: ClientSession | None = None) dict[str, Any][source]

Explain the execution plan for an aggregation pipeline.

Parameters:
  • pipeline (list[dict[str, Any]]) – The aggregation pipeline to explain.

  • session (ClientSession, optional) – A ClientSession for transactions.

Returns:

The execution plan explanation.

Return type:

dict[str, Any]

aggregate_raw_batches(pipeline: list[dict[str, Any]], batch_size: int = 100, session: ClientSession | None = None) RawBatchCursor[source]

Perform aggregation and retrieve batches of raw JSON.

Similar to the aggregate() method but returns a RawBatchCursor.

This method returns raw JSON batches which can be more efficient for certain use cases where you want to process data in batches rather than individual documents.

Parameters:
  • pipeline (list[dict[str, Any]]) – A list of aggregation pipeline stages to apply.

  • batch_size (int) – The number of documents to include in each batch.

  • session (ClientSession, optional) – A ClientSession for transactions.

Returns:

RawBatchCursor instance.

bulk_write(requests: list[Any], ordered: bool = True, session: ClientSession | None = None) BulkWriteResult[source]

Execute bulk write operations on the collection.

Parameters:
  • requests – List of write operations to execute.

  • ordered – If true, operations will be performed in order and will raise an exception if a single operation fails.

  • session (ClientSession, optional) – A ClientSession for transactions.

Returns:

A result object containing the number of matched,

modified, and inserted documents.

Return type:

BulkWriteResult

_aggregate_with_quez(pipeline: list[dict[str, Any]], batch_size: int = 101) CompressedQueue[source]

Process aggregation pipeline with quez compressed queue for memory efficiency.

Parameters:
  • pipeline (list[dict[str, Any]]) – A list of aggregation pipeline stages to apply.

  • batch_size (int) – The batch size for quez queue processing.

Returns:

A compressed queue containing the results.

Return type:

CompressedQueue

initialize_ordered_bulk_op() BulkOperationExecutor[source]

Initialize an ordered bulk operation.

Returns:

An executor for ordered bulk operations.

Return type:

BulkOperationExecutor

initialize_unordered_bulk_op() BulkOperationExecutor[source]

Initialize an unordered bulk operation.

Returns:

An executor for unordered bulk operations.

Return type:

BulkOperationExecutor

_abc_impl = <_abc._abc_data object>
_is_protocol = False