beaver-db 0.14.0__py3-none-any.whl → 0.15.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of beaver-db might be problematic. Click here for more details.
- beaver/queues.py +78 -15
- {beaver_db-0.14.0.dist-info → beaver_db-0.15.0.dist-info}/METADATA +21 -18
- {beaver_db-0.14.0.dist-info → beaver_db-0.15.0.dist-info}/RECORD +6 -6
- {beaver_db-0.14.0.dist-info → beaver_db-0.15.0.dist-info}/WHEEL +0 -0
- {beaver_db-0.14.0.dist-info → beaver_db-0.15.0.dist-info}/licenses/LICENSE +0 -0
- {beaver_db-0.14.0.dist-info → beaver_db-0.15.0.dist-info}/top_level.txt +0 -0
beaver/queues.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import asyncio
|
|
1
2
|
import json
|
|
2
3
|
import sqlite3
|
|
3
4
|
import time
|
|
@@ -14,8 +15,34 @@ class QueueItem[T](NamedTuple):
|
|
|
14
15
|
data: T
|
|
15
16
|
|
|
16
17
|
|
|
18
|
+
class AsyncQueueManager[T]:
|
|
19
|
+
"""An async wrapper for the producer-consumer priority queue."""
|
|
20
|
+
|
|
21
|
+
def __init__(self, queue: "QueueManager[T]"):
|
|
22
|
+
self._queue = queue
|
|
23
|
+
|
|
24
|
+
async def put(self, data: T, priority: float):
|
|
25
|
+
"""Asynchronously adds an item to the queue with a specific priority."""
|
|
26
|
+
await asyncio.to_thread(self._queue.put, data, priority)
|
|
27
|
+
|
|
28
|
+
@overload
|
|
29
|
+
async def get(self, block: Literal[True] = True, timeout: float | None = None) -> QueueItem[T]: ...
|
|
30
|
+
@overload
|
|
31
|
+
async def get(self, block: Literal[False]) -> QueueItem[T]: ...
|
|
32
|
+
|
|
33
|
+
async def get(self, block: bool = True, timeout: float | None = None) -> QueueItem[T]:
|
|
34
|
+
"""
|
|
35
|
+
Asynchronously and atomically retrieves the highest-priority item.
|
|
36
|
+
This method will run the synchronous blocking logic in a separate thread.
|
|
37
|
+
"""
|
|
38
|
+
return await asyncio.to_thread(self._queue.get, block=block, timeout=timeout)
|
|
39
|
+
|
|
40
|
+
|
|
17
41
|
class QueueManager[T]:
|
|
18
|
-
"""
|
|
42
|
+
"""
|
|
43
|
+
A wrapper providing a Pythonic interface to a persistent, multi-process
|
|
44
|
+
producer-consumer priority queue.
|
|
45
|
+
"""
|
|
19
46
|
|
|
20
47
|
def __init__(self, name: str, conn: sqlite3.Connection, model: Type[T] | None = None):
|
|
21
48
|
self._name = name
|
|
@@ -50,19 +77,13 @@ class QueueManager[T]:
|
|
|
50
77
|
(self._name, priority, time.time(), self._serialize(data)),
|
|
51
78
|
)
|
|
52
79
|
|
|
53
|
-
|
|
54
|
-
def get(self, safe:Literal[True]) -> QueueItem[T] | None: ...
|
|
55
|
-
@overload
|
|
56
|
-
def get(self) -> QueueItem[T]: ...
|
|
57
|
-
|
|
58
|
-
def get(self, safe:bool=False) -> QueueItem[T] | None:
|
|
80
|
+
def _get_item_atomically(self) -> QueueItem[T] | None:
|
|
59
81
|
"""
|
|
60
|
-
|
|
61
|
-
|
|
82
|
+
Performs a single, atomic attempt to retrieve and remove the
|
|
83
|
+
highest-priority item from the queue. Returns None if the queue is empty.
|
|
62
84
|
"""
|
|
63
85
|
with self._conn:
|
|
64
86
|
cursor = self._conn.cursor()
|
|
65
|
-
# The compound index on (queue_name, priority, timestamp) makes this query efficient.
|
|
66
87
|
cursor.execute(
|
|
67
88
|
"""
|
|
68
89
|
SELECT rowid, priority, timestamp, data
|
|
@@ -76,19 +97,61 @@ class QueueManager[T]:
|
|
|
76
97
|
result = cursor.fetchone()
|
|
77
98
|
|
|
78
99
|
if result is None:
|
|
79
|
-
|
|
80
|
-
return None
|
|
81
|
-
else:
|
|
82
|
-
raise IndexError("No item available.")
|
|
100
|
+
return None
|
|
83
101
|
|
|
84
102
|
rowid, priority, timestamp, data = result
|
|
85
|
-
# Delete the retrieved item to ensure it's processed only once.
|
|
86
103
|
cursor.execute("DELETE FROM beaver_priority_queues WHERE rowid = ?", (rowid,))
|
|
87
104
|
|
|
88
105
|
return QueueItem(
|
|
89
106
|
priority=priority, timestamp=timestamp, data=self._deserialize(data)
|
|
90
107
|
)
|
|
91
108
|
|
|
109
|
+
@overload
|
|
110
|
+
def get(self, block: Literal[True] = True, timeout: float | None = None) -> QueueItem[T]: ...
|
|
111
|
+
@overload
|
|
112
|
+
def get(self, block: Literal[False]) -> QueueItem[T]: ...
|
|
113
|
+
|
|
114
|
+
def get(self, block: bool = True, timeout: float | None = None) -> QueueItem[T]:
|
|
115
|
+
"""
|
|
116
|
+
Atomically retrieves and removes the highest-priority item from the queue.
|
|
117
|
+
|
|
118
|
+
This method is designed for producer-consumer patterns and can block
|
|
119
|
+
until an item becomes available.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
block: If True (default), the method will wait until an item is available.
|
|
123
|
+
timeout: If `block` is True, this specifies the maximum number of seconds
|
|
124
|
+
to wait. If the timeout is reached, `TimeoutError` is raised.
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
A `QueueItem` containing the retrieved data.
|
|
128
|
+
|
|
129
|
+
Raises:
|
|
130
|
+
IndexError: If `block` is False and the queue is empty.
|
|
131
|
+
TimeoutError: If `block` is True and the timeout expires.
|
|
132
|
+
"""
|
|
133
|
+
if not block:
|
|
134
|
+
item = self._get_item_atomically()
|
|
135
|
+
if item is None:
|
|
136
|
+
raise IndexError("get from an empty queue.")
|
|
137
|
+
return item
|
|
138
|
+
|
|
139
|
+
start_time = time.time()
|
|
140
|
+
while True:
|
|
141
|
+
item = self._get_item_atomically()
|
|
142
|
+
if item is not None:
|
|
143
|
+
return item
|
|
144
|
+
|
|
145
|
+
if timeout is not None and (time.time() - start_time) > timeout:
|
|
146
|
+
raise TimeoutError("Timeout expired while waiting for an item.")
|
|
147
|
+
|
|
148
|
+
# Sleep for a short interval to avoid busy-waiting and consuming CPU.
|
|
149
|
+
time.sleep(0.1)
|
|
150
|
+
|
|
151
|
+
def as_async(self) -> "AsyncQueueManager[T]":
|
|
152
|
+
"""Returns an async version of the queue manager."""
|
|
153
|
+
return AsyncQueueManager(self)
|
|
154
|
+
|
|
92
155
|
def __len__(self) -> int:
|
|
93
156
|
"""Returns the current number of items in the queue."""
|
|
94
157
|
cursor = self._conn.cursor()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: beaver-db
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.15.0
|
|
4
4
|
Summary: Fast, embedded, and multi-modal DB based on SQLite for AI-powered applications.
|
|
5
5
|
Requires-Python: >=3.13
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
@@ -31,12 +31,13 @@ A fast, single-file, multi-modal database for Python, built with the standard `s
|
|
|
31
31
|
- **Sync/Async High-Efficiency Pub/Sub**: A powerful, thread and process-safe publish-subscribe system for real-time messaging with a fan-out architecture. Sync by default, but with an `as_async` wrapper for async applications.
|
|
32
32
|
- **Namespaced Key-Value Dictionaries**: A Pythonic, dictionary-like interface for storing any JSON-serializable object within separate namespaces with optional TTL for cache implementations.
|
|
33
33
|
- **Pythonic List Management**: A fluent, Redis-like interface for managing persistent, ordered lists.
|
|
34
|
-
- **Persistent Priority Queue**: A high-performance, persistent queue
|
|
34
|
+
- **Persistent Priority Queue**: A high-performance, persistent priority queue perfect for task orchestration across multiple processes. Also with optional async support.
|
|
35
35
|
- **Simple Blob Storage**: A dictionary-like interface for storing medium-sized binary files (like PDFs or images) directly in the database, ensuring transactional integrity with your other data.
|
|
36
36
|
- **High-Performance Vector Storage & Search**: Store vector embeddings and perform fast, crash-safe approximate nearest neighbor searches using a `faiss`-based hybrid index.
|
|
37
37
|
- **Full-Text and Fuzzy Search**: Automatically index and search through document metadata using SQLite's powerful FTS5 engine, enhanced with optional fuzzy search for typo-tolerant matching.
|
|
38
38
|
- **Knowledge Graph**: Create relationships between documents and traverse the graph to find neighbors or perform multi-hop walks.
|
|
39
39
|
- **Single-File & Portable**: All data is stored in a single SQLite file, making it incredibly easy to move, back up, or embed in your application.
|
|
40
|
+
- **Optional Type-Safety:** Although the database is schemaless, you can use a minimalistic typing system for automatic serialization and deserialization that is Pydantic-compatible out of the box.
|
|
40
41
|
|
|
41
42
|
## How Beaver is Implemented
|
|
42
43
|
|
|
@@ -260,21 +261,24 @@ Basically everywhere you can store or get some object in BeaverDB, you can use a
|
|
|
260
261
|
|
|
261
262
|
For more in-depth examples, check out the scripts in the `examples/` directory:
|
|
262
263
|
|
|
263
|
-
- [`
|
|
264
|
-
- [`
|
|
265
|
-
- [`
|
|
266
|
-
- [`
|
|
267
|
-
- [`
|
|
268
|
-
- [`
|
|
269
|
-
- [`
|
|
270
|
-
- [`
|
|
271
|
-
- [`
|
|
272
|
-
- [`
|
|
273
|
-
- [`
|
|
274
|
-
- [`examples/
|
|
275
|
-
- [`
|
|
276
|
-
- [`
|
|
277
|
-
- [`
|
|
264
|
+
- [`async_pubsub.py`](examples/async_pubsub.py): A demonstration of the asynchronous wrapper for the publish/subscribe system.
|
|
265
|
+
- [`blobs.py`](examples/blobs.py): Demonstrates how to store and retrieve binary data in the database.
|
|
266
|
+
- [`cache.py`](examples/cache.py): A practical example of using a dictionary with TTL as a cache for API calls.
|
|
267
|
+
- [`fts.py`](examples/fts.py): A detailed look at full-text search, including targeted searches on specific metadata fields.
|
|
268
|
+
- [`fuzzy.py`](examples/fuzzy.py): Demonstrates fuzzy search capabilities for text search.
|
|
269
|
+
- [`general_test.py`](examples/general_test.py): A general-purpose test to run all operations randomly which allows testing long-running processes and synchronicity issues.
|
|
270
|
+
- [`graph.py`](examples/graph.py): Shows how to create relationships between documents and perform multi-hop graph traversals.
|
|
271
|
+
- [`kvstore.py`](examples/kvstore.py): A comprehensive demo of the namespaced dictionary feature.
|
|
272
|
+
- [`list.py`](examples/list.py): Shows the full capabilities of the persistent list, including slicing and in-place updates.
|
|
273
|
+
- [`pqueue.py`](examples/pqueue.py): A practical example of using the persistent priority queue for task management.
|
|
274
|
+
- [`producer_consumer.py`](examples/producer_consumer.py): A demonstration of the distributed task queue system in a multi-process environment.
|
|
275
|
+
- [`publisher.py`](examples/publisher.py) and [`subscriber.py`](examples/subscriber.py): A pair of examples demonstrating inter-process message passing with the publish/subscribe system.
|
|
276
|
+
- [`pubsub.py`](examples/pubsub.py): A demonstration of the synchronous, thread-safe publish/subscribe system in a single process.
|
|
277
|
+
- [`rerank.py`](examples/rerank.py): Shows how to combine results from vector and text search for more refined results.
|
|
278
|
+
- [`stress_vectors.py`](examples/stress_vectors.py): A stress test for the vector search functionality.
|
|
279
|
+
- [`textual_chat.py`](examples/textual_chat.py): A chat application built with `textual` and `beaver` to illustrate the use of several primitives (lists, dicts, and channels) at the same time.
|
|
280
|
+
- [`type_hints.py](examples/type_hints.py): Shows how to use type hints with `beaver` to get better IDE support and type safety.
|
|
281
|
+
- [`vector.py`](examples/vector.py): Demonstrates how to index and search vector embeddings, including upserts.
|
|
278
282
|
|
|
279
283
|
## Roadmap
|
|
280
284
|
|
|
@@ -283,7 +287,6 @@ For more in-depth examples, check out the scripts in the `examples/` directory:
|
|
|
283
287
|
These are some of the features and improvements planned for future releases:
|
|
284
288
|
|
|
285
289
|
- **Async API**: Extend the async support with on-demand wrappers for all features besides channels.
|
|
286
|
-
- **Type Hints**: Extend type hints for channels and documents.
|
|
287
290
|
|
|
288
291
|
Check out the [roadmap](roadmap.md) for a detailed list of upcoming features and design ideas.
|
|
289
292
|
|
|
@@ -5,11 +5,11 @@ beaver/collections.py,sha256=Uz241TSs0xRABPYeKenDYmkbaM0PKfvcBX5j0lMzMMA,24306
|
|
|
5
5
|
beaver/core.py,sha256=zAPlym786_sOpRkP6LfKkd5BH2DXPwdOPTdAkSYojvQ,12469
|
|
6
6
|
beaver/dicts.py,sha256=1BQ9A_cMkJ7l5ayWbDG-4Wi3WtQ-9BKd7Wj_CB7dGlU,5410
|
|
7
7
|
beaver/lists.py,sha256=0LT2XjuHs8pDgvW48kk_lfVc-Y-Ulmym0gcVWRESPtA,9708
|
|
8
|
-
beaver/queues.py,sha256=
|
|
8
|
+
beaver/queues.py,sha256=IQoeNhcYrVZTuH_4bWhtiEa-EYbFx_2iVKkR254XPnE,5953
|
|
9
9
|
beaver/types.py,sha256=65rDdj97EegghEkKCNjI67bPYtTTI_jyB-leHdIypx4,1249
|
|
10
10
|
beaver/vectors.py,sha256=j7RL2Y_xMAF2tPTi6E2LdJqZerSQXlnEQJOGZkefTsA,18358
|
|
11
|
-
beaver_db-0.
|
|
12
|
-
beaver_db-0.
|
|
13
|
-
beaver_db-0.
|
|
14
|
-
beaver_db-0.
|
|
15
|
-
beaver_db-0.
|
|
11
|
+
beaver_db-0.15.0.dist-info/licenses/LICENSE,sha256=1xrIY5JnMk_QDQzsqmVzPIIyCgZAkWCC8kF2Ddo1UT0,1071
|
|
12
|
+
beaver_db-0.15.0.dist-info/METADATA,sha256=bbeIcJMu28cG3KLBiraaGHEypZJ0h52JIMGBj0u85_U,16101
|
|
13
|
+
beaver_db-0.15.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
14
|
+
beaver_db-0.15.0.dist-info/top_level.txt,sha256=FxA4XnX5Qm5VudEXCduFriqi4dQmDWpQ64d7g69VQKI,7
|
|
15
|
+
beaver_db-0.15.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|