beaver-db 0.1.0__py3-none-any.whl → 0.2.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/core.py +337 -113
- beaver_db-0.2.0.dist-info/METADATA +109 -0
- beaver_db-0.2.0.dist-info/RECORD +6 -0
- beaver_db-0.1.0.dist-info/METADATA +0 -117
- beaver_db-0.1.0.dist-info/RECORD +0 -6
- {beaver_db-0.1.0.dist-info → beaver_db-0.2.0.dist-info}/WHEEL +0 -0
- {beaver_db-0.1.0.dist-info → beaver_db-0.2.0.dist-info}/top_level.txt +0 -0
beaver/core.py
CHANGED
|
@@ -2,162 +2,386 @@ import asyncio
|
|
|
2
2
|
import json
|
|
3
3
|
import sqlite3
|
|
4
4
|
import time
|
|
5
|
-
from typing import Any, AsyncIterator
|
|
5
|
+
from typing import Any, AsyncIterator, Union
|
|
6
6
|
|
|
7
|
-
# --- SQL Schema ---
|
|
8
|
-
# These statements are executed once to set up the database.
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
);
|
|
16
|
-
"""
|
|
8
|
+
class BeaverDB:
|
|
9
|
+
"""
|
|
10
|
+
An embedded, multi-modal database in a single SQLite file.
|
|
11
|
+
Currently supports async pub/sub and a synchronous key-value store.
|
|
12
|
+
"""
|
|
17
13
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"""
|
|
14
|
+
def __init__(self, db_path: str):
|
|
15
|
+
"""
|
|
16
|
+
Initializes the database connection and creates necessary tables.
|
|
22
17
|
|
|
18
|
+
Args:
|
|
19
|
+
db_path: The path to the SQLite database file.
|
|
20
|
+
"""
|
|
21
|
+
self._db_path = db_path
|
|
22
|
+
# Enable WAL mode for better concurrency between readers and writers
|
|
23
|
+
self._conn = sqlite3.connect(self._db_path, check_same_thread=False)
|
|
24
|
+
self._conn.execute("PRAGMA journal_mode=WAL;")
|
|
25
|
+
self._conn.row_factory = sqlite3.Row
|
|
26
|
+
self._create_pubsub_table()
|
|
27
|
+
self._create_kv_table()
|
|
28
|
+
self._create_list_table()
|
|
23
29
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
def _create_pubsub_table(self):
|
|
31
|
+
"""Creates the pub/sub log table if it doesn't exist."""
|
|
32
|
+
with self._conn:
|
|
33
|
+
self._conn.execute("""
|
|
34
|
+
CREATE TABLE IF NOT EXISTS beaver_pubsub_log (
|
|
35
|
+
timestamp REAL PRIMARY KEY,
|
|
36
|
+
channel_name TEXT NOT NULL,
|
|
37
|
+
message_payload TEXT NOT NULL
|
|
38
|
+
)
|
|
39
|
+
""")
|
|
40
|
+
self._conn.execute("""
|
|
41
|
+
CREATE INDEX IF NOT EXISTS idx_pubsub_channel_timestamp
|
|
42
|
+
ON beaver_pubsub_log (channel_name, timestamp)
|
|
43
|
+
""")
|
|
27
44
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
self._polling_task = None
|
|
45
|
+
def _create_kv_table(self):
|
|
46
|
+
"""Creates the key-value store table if it doesn't exist."""
|
|
47
|
+
with self._conn:
|
|
48
|
+
self._conn.execute("""
|
|
49
|
+
CREATE TABLE IF NOT EXISTS _beaver_kv_store (
|
|
50
|
+
key TEXT PRIMARY KEY,
|
|
51
|
+
value TEXT NOT NULL
|
|
52
|
+
)
|
|
53
|
+
""")
|
|
38
54
|
|
|
39
|
-
|
|
40
|
-
"""
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
55
|
+
def _create_list_table(self):
|
|
56
|
+
"""Creates the lists table if it doesn't exist."""
|
|
57
|
+
with self._conn:
|
|
58
|
+
self._conn.execute("""
|
|
59
|
+
CREATE TABLE IF NOT EXISTS beaver_lists (
|
|
60
|
+
list_name TEXT NOT NULL,
|
|
61
|
+
item_order REAL NOT NULL,
|
|
62
|
+
item_value TEXT NOT NULL,
|
|
63
|
+
PRIMARY KEY (list_name, item_order)
|
|
46
64
|
)
|
|
65
|
+
""")
|
|
47
66
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
self._last_seen_timestamp = timestamp
|
|
67
|
+
def close(self):
|
|
68
|
+
"""Closes the database connection."""
|
|
69
|
+
if self._conn:
|
|
70
|
+
self._conn.close()
|
|
53
71
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
# --- Key-Value Store Methods ---
|
|
73
|
+
|
|
74
|
+
def set(self, key: str, value: Any):
|
|
75
|
+
"""
|
|
76
|
+
Stores a JSON-serializable value for a given key.
|
|
77
|
+
This operation is synchronous.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
key: The unique string identifier for the value.
|
|
81
|
+
value: A JSON-serializable Python object (dict, list, str, int, etc.).
|
|
82
|
+
|
|
83
|
+
Raises:
|
|
84
|
+
TypeError: If the key is not a string or the value is not JSON-serializable.
|
|
85
|
+
"""
|
|
86
|
+
if not isinstance(key, str):
|
|
87
|
+
raise TypeError("Key must be a string.")
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
json_value = json.dumps(value)
|
|
91
|
+
except TypeError as e:
|
|
92
|
+
raise TypeError("Value must be JSON-serializable.") from e
|
|
93
|
+
|
|
94
|
+
with self._conn:
|
|
95
|
+
self._conn.execute(
|
|
96
|
+
"INSERT OR REPLACE INTO _beaver_kv_store (key, value) VALUES (?, ?)",
|
|
97
|
+
(key, json_value)
|
|
73
98
|
)
|
|
74
|
-
return cursor.fetchall()
|
|
75
99
|
|
|
76
|
-
|
|
77
|
-
"""
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return self
|
|
100
|
+
def get(self, key: str) -> Any:
|
|
101
|
+
"""
|
|
102
|
+
Retrieves a value for a given key.
|
|
103
|
+
This operation is synchronous.
|
|
81
104
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if self._polling_task:
|
|
85
|
-
self._polling_task.cancel()
|
|
86
|
-
await asyncio.gather(self._polling_task, return_exceptions=True)
|
|
105
|
+
Args:
|
|
106
|
+
key: The string identifier for the value.
|
|
87
107
|
|
|
88
|
-
|
|
89
|
-
|
|
108
|
+
Returns:
|
|
109
|
+
The deserialized Python object, or None if the key is not found.
|
|
90
110
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
111
|
+
Raises:
|
|
112
|
+
TypeError: If the key is not a string.
|
|
113
|
+
"""
|
|
114
|
+
if not isinstance(key, str):
|
|
115
|
+
raise TypeError("Key must be a string.")
|
|
94
116
|
|
|
117
|
+
cursor = self._conn.cursor()
|
|
118
|
+
cursor.execute("SELECT value FROM _beaver_kv_store WHERE key = ?", (key,))
|
|
119
|
+
result = cursor.fetchone()
|
|
120
|
+
cursor.close()
|
|
95
121
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
122
|
+
if result:
|
|
123
|
+
return json.loads(result['value'])
|
|
124
|
+
return None
|
|
99
125
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
""
|
|
103
|
-
def __init__(self, db_path: str = "beaver.db"):
|
|
126
|
+
# --- List Methods ---
|
|
127
|
+
|
|
128
|
+
def list(self, name: str) -> "ListWrapper":
|
|
104
129
|
"""
|
|
105
|
-
|
|
130
|
+
Returns a wrapper object for interacting with a specific list.
|
|
106
131
|
|
|
107
132
|
Args:
|
|
108
|
-
|
|
133
|
+
name: The name of the list.
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
A ListWrapper instance bound to the given list name.
|
|
109
137
|
"""
|
|
110
|
-
|
|
111
|
-
|
|
138
|
+
if not isinstance(name, str) or not name:
|
|
139
|
+
raise TypeError("List name must be a non-empty string.")
|
|
140
|
+
return ListWrapper(name, self._conn)
|
|
112
141
|
|
|
113
|
-
|
|
114
|
-
"""Creates the necessary tables and indices if they don't exist."""
|
|
115
|
-
with sqlite3.connect(self.db_path) as conn:
|
|
116
|
-
cursor = conn.cursor()
|
|
117
|
-
cursor.execute(PUBSUB_TABLE_SQL)
|
|
118
|
-
cursor.execute(PUBSUB_INDEX_SQL)
|
|
119
|
-
conn.commit()
|
|
142
|
+
# --- Asynchronous Pub/Sub Methods ---
|
|
120
143
|
|
|
121
144
|
async def publish(self, channel_name: str, payload: Any):
|
|
122
145
|
"""
|
|
123
146
|
Publishes a JSON-serializable message to a channel.
|
|
124
|
-
|
|
125
|
-
Args:
|
|
126
|
-
channel_name: The name of the channel.
|
|
127
|
-
payload: A JSON-serializable Python object (e.g., dict, list).
|
|
147
|
+
This operation is asynchronous.
|
|
128
148
|
"""
|
|
129
149
|
if not isinstance(channel_name, str) or not channel_name:
|
|
130
150
|
raise ValueError("Channel name must be a non-empty string.")
|
|
131
|
-
|
|
132
151
|
try:
|
|
133
152
|
json_payload = json.dumps(payload)
|
|
134
153
|
except TypeError as e:
|
|
135
154
|
raise TypeError("Message payload must be JSON-serializable.") from e
|
|
136
155
|
|
|
137
|
-
# Run the blocking DB write in a thread to keep this method non-blocking
|
|
138
156
|
await asyncio.to_thread(
|
|
139
157
|
self._write_publish_to_db, channel_name, json_payload
|
|
140
158
|
)
|
|
141
159
|
|
|
142
|
-
def _write_publish_to_db(self, channel_name
|
|
143
|
-
"""The synchronous part of the publish operation
|
|
144
|
-
with
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
"INSERT INTO _beaver_pubsub_log (timestamp, channel_name, message_payload) "
|
|
148
|
-
"VALUES (?, ?, ?)",
|
|
160
|
+
def _write_publish_to_db(self, channel_name, json_payload):
|
|
161
|
+
"""The synchronous part of the publish operation."""
|
|
162
|
+
with self._conn:
|
|
163
|
+
self._conn.execute(
|
|
164
|
+
"INSERT INTO beaver_pubsub_log (timestamp, channel_name, message_payload) VALUES (?, ?, ?)",
|
|
149
165
|
(time.time(), channel_name, json_payload)
|
|
150
166
|
)
|
|
151
|
-
conn.commit()
|
|
152
167
|
|
|
153
|
-
def subscribe(self, channel_name: str) -> Subscriber:
|
|
168
|
+
def subscribe(self, channel_name: str) -> "Subscriber":
|
|
154
169
|
"""
|
|
155
|
-
Subscribes to a channel.
|
|
170
|
+
Subscribes to a channel, returning an async iterator.
|
|
171
|
+
"""
|
|
172
|
+
return Subscriber(self._conn, channel_name)
|
|
156
173
|
|
|
157
|
-
Returns a 'Subscriber' object that can be used in an 'async with'
|
|
158
|
-
block and `async for` loop to receive messages.
|
|
159
174
|
|
|
160
|
-
|
|
161
|
-
|
|
175
|
+
class ListWrapper:
|
|
176
|
+
"""A wrapper providing a Pythonic interface to a list in the database."""
|
|
177
|
+
|
|
178
|
+
def __init__(self, name: str, conn: sqlite3.Connection):
|
|
179
|
+
self._name = name
|
|
180
|
+
self._conn = conn
|
|
181
|
+
|
|
182
|
+
def __len__(self) -> int:
|
|
183
|
+
"""Returns the number of items in the list (e.g., `len(my_list)`)."""
|
|
184
|
+
cursor = self._conn.cursor()
|
|
185
|
+
cursor.execute("SELECT COUNT(*) FROM beaver_lists WHERE list_name = ?", (self._name,))
|
|
186
|
+
count = cursor.fetchone()[0]
|
|
187
|
+
cursor.close()
|
|
188
|
+
return count
|
|
189
|
+
|
|
190
|
+
def __getitem__(self, key: Union[int, slice]) -> Any:
|
|
162
191
|
"""
|
|
163
|
-
|
|
192
|
+
Retrieves an item or slice from the list (e.g., `my_list[0]`, `my_list[1:3]`).
|
|
193
|
+
"""
|
|
194
|
+
if isinstance(key, slice):
|
|
195
|
+
start, stop, step = key.indices(len(self))
|
|
196
|
+
if step != 1:
|
|
197
|
+
raise ValueError("Slicing with a step is not supported.")
|
|
198
|
+
|
|
199
|
+
limit = stop - start
|
|
200
|
+
if limit <= 0:
|
|
201
|
+
return []
|
|
202
|
+
|
|
203
|
+
cursor = self._conn.cursor()
|
|
204
|
+
cursor.execute(
|
|
205
|
+
"SELECT item_value FROM beaver_lists WHERE list_name = ? ORDER BY item_order ASC LIMIT ? OFFSET ?",
|
|
206
|
+
(self._name, limit, start)
|
|
207
|
+
)
|
|
208
|
+
results = [json.loads(row['item_value']) for row in cursor.fetchall()]
|
|
209
|
+
cursor.close()
|
|
210
|
+
return results
|
|
211
|
+
|
|
212
|
+
elif isinstance(key, int):
|
|
213
|
+
list_len = len(self)
|
|
214
|
+
if key < -list_len or key >= list_len:
|
|
215
|
+
raise IndexError("List index out of range.")
|
|
216
|
+
|
|
217
|
+
offset = key if key >= 0 else list_len + key
|
|
218
|
+
|
|
219
|
+
cursor = self._conn.cursor()
|
|
220
|
+
cursor.execute(
|
|
221
|
+
"SELECT item_value FROM beaver_lists WHERE list_name = ? ORDER BY item_order ASC LIMIT 1 OFFSET ?",
|
|
222
|
+
(self._name, offset)
|
|
223
|
+
)
|
|
224
|
+
result = cursor.fetchone()
|
|
225
|
+
cursor.close()
|
|
226
|
+
return json.loads(result['item_value']) if result else None
|
|
227
|
+
|
|
228
|
+
else:
|
|
229
|
+
raise TypeError("List indices must be integers or slices.")
|
|
230
|
+
|
|
231
|
+
def _get_order_at_index(self, index: int) -> float:
|
|
232
|
+
"""Helper to get the float `item_order` at a specific index."""
|
|
233
|
+
cursor = self._conn.cursor()
|
|
234
|
+
cursor.execute(
|
|
235
|
+
"SELECT item_order FROM beaver_lists WHERE list_name = ? ORDER BY item_order ASC LIMIT 1 OFFSET ?",
|
|
236
|
+
(self._name, index)
|
|
237
|
+
)
|
|
238
|
+
result = cursor.fetchone()
|
|
239
|
+
cursor.close()
|
|
240
|
+
|
|
241
|
+
if result:
|
|
242
|
+
return result[0]
|
|
243
|
+
|
|
244
|
+
raise IndexError(f"{index} out of range.")
|
|
245
|
+
|
|
246
|
+
def push(self, value: Any):
|
|
247
|
+
"""Pushes an item to the end of the list."""
|
|
248
|
+
with self._conn:
|
|
249
|
+
cursor = self._conn.cursor()
|
|
250
|
+
cursor.execute("SELECT MAX(item_order) FROM beaver_lists WHERE list_name = ?", (self._name,))
|
|
251
|
+
max_order = cursor.fetchone()[0] or 0.0
|
|
252
|
+
new_order = max_order + 1.0
|
|
253
|
+
|
|
254
|
+
cursor.execute(
|
|
255
|
+
"INSERT INTO beaver_lists (list_name, item_order, item_value) VALUES (?, ?, ?)",
|
|
256
|
+
(self._name, new_order, json.dumps(value))
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
def prepend(self, value: Any):
|
|
260
|
+
"""Prepends an item to the beginning of the list."""
|
|
261
|
+
with self._conn:
|
|
262
|
+
cursor = self._conn.cursor()
|
|
263
|
+
cursor.execute("SELECT MIN(item_order) FROM beaver_lists WHERE list_name = ?", (self._name,))
|
|
264
|
+
min_order = cursor.fetchone()[0] or 0.0
|
|
265
|
+
new_order = min_order - 1.0
|
|
266
|
+
|
|
267
|
+
cursor.execute(
|
|
268
|
+
"INSERT INTO beaver_lists (list_name, item_order, item_value) VALUES (?, ?, ?)",
|
|
269
|
+
(self._name, new_order, json.dumps(value))
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
def insert(self, index: int, value: Any):
|
|
273
|
+
"""Inserts an item at a specific index."""
|
|
274
|
+
list_len = len(self)
|
|
275
|
+
if index <= 0:
|
|
276
|
+
self.prepend(value)
|
|
277
|
+
return
|
|
278
|
+
if index >= list_len:
|
|
279
|
+
self.push(value)
|
|
280
|
+
return
|
|
281
|
+
|
|
282
|
+
# Midpoint insertion
|
|
283
|
+
order_before = self._get_order_at_index(index - 1)
|
|
284
|
+
order_after = self._get_order_at_index(index)
|
|
285
|
+
|
|
286
|
+
new_order = order_before + (order_after - order_before) / 2.0
|
|
287
|
+
|
|
288
|
+
with self._conn:
|
|
289
|
+
self._conn.execute(
|
|
290
|
+
"INSERT INTO beaver_lists (list_name, item_order, item_value) VALUES (?, ?, ?)",
|
|
291
|
+
(self._name, new_order, json.dumps(value))
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
def pop(self) -> Any:
|
|
295
|
+
"""Removes and returns the last item from the list."""
|
|
296
|
+
with self._conn:
|
|
297
|
+
cursor = self._conn.cursor()
|
|
298
|
+
cursor.execute(
|
|
299
|
+
"SELECT rowid, item_value FROM beaver_lists WHERE list_name = ? ORDER BY item_order DESC LIMIT 1",
|
|
300
|
+
(self._name,)
|
|
301
|
+
)
|
|
302
|
+
result = cursor.fetchone()
|
|
303
|
+
if not result:
|
|
304
|
+
return None
|
|
305
|
+
|
|
306
|
+
rowid_to_delete, value_to_return = result
|
|
307
|
+
cursor.execute("DELETE FROM beaver_lists WHERE rowid = ?", (rowid_to_delete,))
|
|
308
|
+
return json.loads(value_to_return)
|
|
309
|
+
|
|
310
|
+
def deque(self) -> Any:
|
|
311
|
+
"""Removes and returns the first item from the list."""
|
|
312
|
+
with self._conn:
|
|
313
|
+
cursor = self._conn.cursor()
|
|
314
|
+
cursor.execute(
|
|
315
|
+
"SELECT rowid, item_value FROM beaver_lists WHERE list_name = ? ORDER BY item_order ASC LIMIT 1",
|
|
316
|
+
(self._name,)
|
|
317
|
+
)
|
|
318
|
+
result = cursor.fetchone()
|
|
319
|
+
if not result:
|
|
320
|
+
return None
|
|
321
|
+
|
|
322
|
+
rowid_to_delete, value_to_return = result
|
|
323
|
+
cursor.execute("DELETE FROM beaver_lists WHERE rowid = ?", (rowid_to_delete,))
|
|
324
|
+
return json.loads(value_to_return)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
class Subscriber(AsyncIterator):
|
|
328
|
+
"""
|
|
329
|
+
An async iterator that polls a channel for new messages.
|
|
330
|
+
Designed to be used with 'async with'.
|
|
331
|
+
"""
|
|
332
|
+
|
|
333
|
+
def __init__(self, conn: sqlite3.Connection, channel_name: str, poll_interval: float = 0.1):
|
|
334
|
+
self._conn = conn
|
|
335
|
+
self._channel = channel_name
|
|
336
|
+
self._poll_interval = poll_interval
|
|
337
|
+
self._queue = asyncio.Queue()
|
|
338
|
+
self._last_seen_timestamp = time.time()
|
|
339
|
+
self._polling_task = None
|
|
340
|
+
|
|
341
|
+
async def _poll_for_messages(self):
|
|
342
|
+
"""Background task that polls the database for new messages."""
|
|
343
|
+
while True:
|
|
344
|
+
try:
|
|
345
|
+
new_messages = await asyncio.to_thread(
|
|
346
|
+
self._fetch_new_messages_from_db
|
|
347
|
+
)
|
|
348
|
+
if new_messages:
|
|
349
|
+
for msg in new_messages:
|
|
350
|
+
payload = json.loads(msg["message_payload"])
|
|
351
|
+
await self._queue.put(payload)
|
|
352
|
+
self._last_seen_timestamp = msg["timestamp"]
|
|
353
|
+
await asyncio.sleep(self._poll_interval)
|
|
354
|
+
except asyncio.CancelledError:
|
|
355
|
+
break
|
|
356
|
+
except Exception:
|
|
357
|
+
# In a real app, add more robust error logging
|
|
358
|
+
await asyncio.sleep(self._poll_interval * 5)
|
|
359
|
+
|
|
360
|
+
def _fetch_new_messages_from_db(self) -> list:
|
|
361
|
+
"""The actual synchronous database query."""
|
|
362
|
+
cursor = self._conn.cursor()
|
|
363
|
+
cursor.execute(
|
|
364
|
+
"SELECT timestamp, message_payload FROM beaver_pubsub_log WHERE channel_name = ? AND timestamp > ? ORDER BY timestamp ASC",
|
|
365
|
+
(self._channel, self._last_seen_timestamp)
|
|
366
|
+
)
|
|
367
|
+
results = cursor.fetchall()
|
|
368
|
+
cursor.close()
|
|
369
|
+
return results
|
|
370
|
+
|
|
371
|
+
async def __aenter__(self):
|
|
372
|
+
"""Starts the background task."""
|
|
373
|
+
self._polling_task = asyncio.create_task(self._poll_for_messages())
|
|
374
|
+
return self
|
|
375
|
+
|
|
376
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
377
|
+
"""Stops the background task."""
|
|
378
|
+
if self._polling_task:
|
|
379
|
+
self._polling_task.cancel()
|
|
380
|
+
await asyncio.gather(self._polling_task, return_exceptions=True)
|
|
381
|
+
|
|
382
|
+
def __aiter__(self):
|
|
383
|
+
return self
|
|
384
|
+
|
|
385
|
+
async def __anext__(self) -> Any:
|
|
386
|
+
"""Allows 'async for' to pull messages from the internal queue."""
|
|
387
|
+
return await self._queue.get()
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: beaver-db
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Asynchronous, embedded, modern DB based on SQLite.
|
|
5
|
+
Requires-Python: >=3.13
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
|
|
8
|
+
# beaver 🦫
|
|
9
|
+
|
|
10
|
+
A fast, single-file, multi-modal database for Python, built with the standard sqlite3 library.
|
|
11
|
+
|
|
12
|
+
`beaver` is the Backend for Embedded Asynchronous Vector & Event Retrieval. It's an industrious, all-in-one database designed to manage complex, modern data types without requiring a database server.
|
|
13
|
+
|
|
14
|
+
Design Philosophy
|
|
15
|
+
`beaver` is built with a minimalistic philosophy for small, local use cases where a full-blown database server would be overkill.
|
|
16
|
+
|
|
17
|
+
- **Minimalistic & Zero-Dependency**: Uses only Python's standard libraries (sqlite3, asyncio). No external packages are required, making it incredibly lightweight and portable.
|
|
18
|
+
- **Async-First (When It Matters)**: The pub/sub system is fully asynchronous for high-performance, real-time messaging. Simpler features like key-value and list operations remain synchronous for ease of use.
|
|
19
|
+
- **Built for Local Applications**: Perfect for local AI tools, chatbots (streaming tokens), task management apps, desktop utilities, and prototypes that need persistent, structured data without network overhead.
|
|
20
|
+
- **Fast by Default**: It's built on SQLite, which is famously fast, reliable, and will likely serve your needs for a long way before you need a "professional" database.
|
|
21
|
+
|
|
22
|
+
## Core Features
|
|
23
|
+
|
|
24
|
+
- **Asynchronous Pub/Sub**: A fully asynchronous, Redis-like publish-subscribe system for real-time messaging.
|
|
25
|
+
- **Persistent Key-Value Store**: A simple set/get interface for storing configuration, session data, or any other JSON-serializable object.
|
|
26
|
+
- **Pythonic List Management**: A fluent, Redis-like interface (db.list("name").push()) for managing persistent, ordered lists with support for indexing and slicing.
|
|
27
|
+
- **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.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install beaver-db
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Quickstart & API Guide
|
|
36
|
+
|
|
37
|
+
### 1. Initialization
|
|
38
|
+
|
|
39
|
+
All you need to do is import and instantiate the BeaverDB class with a file path.
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from beaver import BeaverDB
|
|
43
|
+
|
|
44
|
+
db = BeaverDB("my_application.db")
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2. Key-Value Store
|
|
48
|
+
|
|
49
|
+
Use `set()` and `get()` for simple data storage. The value can be any JSON-encodable object.
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
# Set a value
|
|
53
|
+
db.set("app_config", {"theme": "dark", "user_id": 123})
|
|
54
|
+
|
|
55
|
+
# Get a value
|
|
56
|
+
config = db.get("app_config")
|
|
57
|
+
print(f"Theme: {config['theme']}") # Output: Theme: dark
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 3. List Management
|
|
61
|
+
|
|
62
|
+
Get a list wrapper with `db.list()` and use Pythonic methods to manage it.
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
# Get a wrapper for the 'tasks' list
|
|
66
|
+
tasks = db.list("daily_tasks")
|
|
67
|
+
|
|
68
|
+
# Push items to the list
|
|
69
|
+
tasks.push("Write the project report")
|
|
70
|
+
tasks.push("Send follow-up emails")
|
|
71
|
+
tasks.prepend("Plan the day's agenda") # Push to the front
|
|
72
|
+
|
|
73
|
+
# Use len() and indexing (including slices!)
|
|
74
|
+
print(f"There are {len(tasks)} tasks.")
|
|
75
|
+
print(f"The first task is: {tasks[0]}")
|
|
76
|
+
print(f"The rest is: {tasks[1:]}")
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 4. Asynchronous Pub/Sub
|
|
80
|
+
|
|
81
|
+
Publish events from one part of your app and listen in another using asyncio.
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
import asyncio
|
|
85
|
+
|
|
86
|
+
async def listener():
|
|
87
|
+
async with db.subscribe("system_events") as sub:
|
|
88
|
+
async for message in sub:
|
|
89
|
+
print(f"LISTENER: Received event -> {message['event']}")
|
|
90
|
+
|
|
91
|
+
async def publisher():
|
|
92
|
+
await asyncio.sleep(1)
|
|
93
|
+
await db.publish("system_events", {"event": "user_login", "user": "alice"})
|
|
94
|
+
|
|
95
|
+
# To run them concurrently:
|
|
96
|
+
# asyncio.run(asyncio.gather(listener(), publisher()))
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Roadmap
|
|
100
|
+
|
|
101
|
+
`beaver` aims to be a complete, self-contained data toolkit. The following features are planned:
|
|
102
|
+
|
|
103
|
+
- **Vector Storage & Search**: Store NumPy vector embeddings and perform efficient k-nearest neighbor (k-NN) searches using `scipy.spatial.cKDTree`.
|
|
104
|
+
- **JSON Document Store with Full-Text Search**: Store flexible JSON documents and get powerful full-text search across all text fields, powered by SQLite's FTS5 extension.
|
|
105
|
+
- **Standard Relational Interface**: While `beaver` provides high-level features, you can always use the same SQLite file for normal relational tasks (e.g., managing users, products) with standard SQL.
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
This project is licensed under the MIT License.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
beaver/__init__.py,sha256=pE1JdpHVni2Hv6igs5VrKPlHkkKMik3ZiwhR23KRBkk,38
|
|
2
|
+
beaver/core.py,sha256=NQizE87jkR7DwbcZoIX61rdVe7z6bAarBUbo-oUc8SI,13720
|
|
3
|
+
beaver_db-0.2.0.dist-info/METADATA,sha256=DGdk2Il2CFRtfM2BzY7Z2dYUxy1UQeKdVKnIVa_5wzg,4295
|
|
4
|
+
beaver_db-0.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
5
|
+
beaver_db-0.2.0.dist-info/top_level.txt,sha256=FxA4XnX5Qm5VudEXCduFriqi4dQmDWpQ64d7g69VQKI,7
|
|
6
|
+
beaver_db-0.2.0.dist-info/RECORD,,
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: beaver-db
|
|
3
|
-
Version: 0.1.0
|
|
4
|
-
Summary: Asynchronous, embedded, modern DB based on SQLite.
|
|
5
|
-
Requires-Python: >=3.13
|
|
6
|
-
Description-Content-Type: text/markdown
|
|
7
|
-
|
|
8
|
-
# beaver 🦫
|
|
9
|
-
|
|
10
|
-
A single-file, multi-modal database for Python, built with the standard sqlite3 library.
|
|
11
|
-
|
|
12
|
-
`beaver` is the **B**ackend for **E**mbedded **A**synchronous **V**ector & **E**vent **R**etrieval. It's an industrious, all-in-one database designed to manage complex, modern data types without leaving the comfort of a single file.
|
|
13
|
-
|
|
14
|
-
This project is currently in its initial phase, with the core asynchronous pub/sub functionality fully implemented.
|
|
15
|
-
|
|
16
|
-
## Core Features (Current)
|
|
17
|
-
|
|
18
|
-
- **Zero Dependencies:** Built using only the standard Python `sqlite3` and `asyncio` libraries. No external packages to install.
|
|
19
|
-
- **Async Pub/Sub:** A fully asynchronous, Redis-like publish-subscribe system for real-time messaging between components of your application.
|
|
20
|
-
- **Single-File & Persistent:** All data is stored in a single SQLite file, making it incredibly portable and easy to back up. Your event log persists across application restarts.
|
|
21
|
-
- **Works with Existing Databases:** `beaver` can be pointed at an existing SQLite file and will create its tables without disturbing other data.
|
|
22
|
-
|
|
23
|
-
## Use Cases
|
|
24
|
-
|
|
25
|
-
I built `beaver` to have a local, embedded database for building small AI-powered projects without having to pay for a server-based database.
|
|
26
|
-
|
|
27
|
-
Examples include:
|
|
28
|
-
|
|
29
|
-
- Streaming messages and tokens from a local FastAPI to a local Streamlit app.
|
|
30
|
-
- Storing user files for Retrieval Augmented Generation in single-user applications.
|
|
31
|
-
|
|
32
|
-
## Installation
|
|
33
|
-
|
|
34
|
-
To use `beaver`, just run `pip install beaver-db` and import the main class.
|
|
35
|
-
|
|
36
|
-
```python
|
|
37
|
-
import asyncio
|
|
38
|
-
from beaver import BeaverDB
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
# --- Example Usage ---
|
|
42
|
-
async def listener(db: BeaverDB):
|
|
43
|
-
"""A sample task that listens for messages."""
|
|
44
|
-
print("LISTENER: Waiting for messages on the 'system_events' channel...")
|
|
45
|
-
try:
|
|
46
|
-
async with db.subscribe("system_events") as subscriber:
|
|
47
|
-
async for message in subscriber:
|
|
48
|
-
print(f"LISTENER: Received -> {message}")
|
|
49
|
-
except asyncio.CancelledError:
|
|
50
|
-
print("LISTENER: Shutting down.")
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
async def publisher(db: BeaverDB):
|
|
54
|
-
"""A sample task that publishes messages."""
|
|
55
|
-
print("PUBLISHER: Ready to send events.")
|
|
56
|
-
await asyncio.sleep(1) # Give the listener a moment to start
|
|
57
|
-
|
|
58
|
-
print("PUBLISHER: Sending user login event.")
|
|
59
|
-
await db.publish(
|
|
60
|
-
"system_events",
|
|
61
|
-
{"event": "user_login", "username": "alice", "status": "success"}
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
await asyncio.sleep(2)
|
|
65
|
-
|
|
66
|
-
print("PUBLISHER: Sending system alert.")
|
|
67
|
-
await db.publish(
|
|
68
|
-
"system_events",
|
|
69
|
-
{"event": "system_alert", "level": "warning", "detail": "CPU usage at 85%"}
|
|
70
|
-
)
|
|
71
|
-
await asyncio.sleep(1)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
async def main():
|
|
75
|
-
"""Runs the listener and publisher concurrently."""
|
|
76
|
-
db = BeaverDB("demo.db")
|
|
77
|
-
|
|
78
|
-
# Run both tasks and wait for them to complete
|
|
79
|
-
listener_task = asyncio.create_task(listener(db))
|
|
80
|
-
publisher_task = asyncio.create_task(publisher(db))
|
|
81
|
-
|
|
82
|
-
await asyncio.sleep(5) # Let them run for a bit
|
|
83
|
-
listener_task.cancel() # Cleanly shut down the listener
|
|
84
|
-
await asyncio.gather(listener_task, publisher_task, return_exceptions=True)
|
|
85
|
-
print("\nDemo finished.")
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if __name__ == "__main__":
|
|
89
|
-
# To run this demo, save the file as beaver.py and run `python beaver.py`
|
|
90
|
-
print("--- BeaverDB Pub/Sub Demo ---")
|
|
91
|
-
asyncio.run(main())
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
## Roadmap
|
|
95
|
-
|
|
96
|
-
`beaver` aims to be a complete, self-contained data toolkit for modern Python applications. The following features are planned for future releases, all accessible through a high-level API while still allowing direct SQL access:
|
|
97
|
-
|
|
98
|
-
- **Vector Storage & Search:** Store numpy vector embeddings alongside your data and perform efficient k-nearest neighbor (k-NN) searches.
|
|
99
|
-
- **Persistent Key-Value Store:** A simple get/set interface for storing configuration, session data, or any other JSON-serializable object.
|
|
100
|
-
- **JSON Document Store with Full-Text Search:** Store flexible JSON documents and get powerful full-text search across all text fields by default, powered by SQLite's FTS5 extension.
|
|
101
|
-
- **Standard Relational Interface:** While beaver provides high-level features, you will always be able to use the underlying SQLite connection for normal relational tasks, such as creating and managing users or products tables with standard SQL.
|
|
102
|
-
|
|
103
|
-
## Performance
|
|
104
|
-
|
|
105
|
-
Despite its local, embedded nature, `beaver` is highly performant by small use cases. Here are some metrics, measured on a single laptop, Intel Core i7, 7th generation.
|
|
106
|
-
|
|
107
|
-
- Process 100,000 messages (1000 messages times 100 asynchronous clients) in less than 30 seconds, giving over 3K messages per second with an average latency of only 100 ms (time elapsed between message generation and client processing).
|
|
108
|
-
|
|
109
|
-
## Why Beaver?
|
|
110
|
-
|
|
111
|
-
Beavers are nature's engineers. They build a single, robust, and complex home—the lodge—from many different materials.
|
|
112
|
-
|
|
113
|
-
Similarly, beaver builds a powerful, multi-modal database but contains it all within a single, self-contained file. It's an industrious, no-nonsense tool for building modern applications.
|
|
114
|
-
|
|
115
|
-
## License
|
|
116
|
-
|
|
117
|
-
This project is licensed under the MIT License.
|
beaver_db-0.1.0.dist-info/RECORD
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
beaver/__init__.py,sha256=pE1JdpHVni2Hv6igs5VrKPlHkkKMik3ZiwhR23KRBkk,38
|
|
2
|
-
beaver/core.py,sha256=gWuuPwpT7yWgOulv_uwhOvJniltFO8K5-dfqjfA0jNk,5888
|
|
3
|
-
beaver_db-0.1.0.dist-info/METADATA,sha256=b0gaV6IuXw2F_B4NOjKAP6Wj7dzHi7DjIYYg-0R_Q1Q,5243
|
|
4
|
-
beaver_db-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
5
|
-
beaver_db-0.1.0.dist-info/top_level.txt,sha256=FxA4XnX5Qm5VudEXCduFriqi4dQmDWpQ64d7g69VQKI,7
|
|
6
|
-
beaver_db-0.1.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|