beaver-db 0.1.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/__init__.py +1 -0
- beaver/core.py +163 -0
- beaver_db-0.1.0.dist-info/METADATA +117 -0
- beaver_db-0.1.0.dist-info/RECORD +6 -0
- beaver_db-0.1.0.dist-info/WHEEL +5 -0
- beaver_db-0.1.0.dist-info/top_level.txt +1 -0
beaver/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .core import BeaverDB, Subscriber
|
beaver/core.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import json
|
|
3
|
+
import sqlite3
|
|
4
|
+
import time
|
|
5
|
+
from typing import Any, AsyncIterator
|
|
6
|
+
|
|
7
|
+
# --- SQL Schema ---
|
|
8
|
+
# These statements are executed once to set up the database.
|
|
9
|
+
|
|
10
|
+
PUBSUB_TABLE_SQL = """
|
|
11
|
+
CREATE TABLE IF NOT EXISTS _beaver_pubsub_log (
|
|
12
|
+
timestamp REAL PRIMARY KEY,
|
|
13
|
+
channel_name TEXT NOT NULL,
|
|
14
|
+
message_payload TEXT NOT NULL
|
|
15
|
+
);
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
PUBSUB_INDEX_SQL = """
|
|
19
|
+
CREATE INDEX IF NOT EXISTS _beaver_idx_pubsub_channel_timestamp
|
|
20
|
+
ON _beaver_pubsub_log (channel_name, timestamp);
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Subscriber(AsyncIterator):
|
|
25
|
+
"""
|
|
26
|
+
A stateful, async context manager for receiving messages from a channel.
|
|
27
|
+
|
|
28
|
+
This object is returned by `BeaverDB.subscribe()` and is designed to
|
|
29
|
+
be used with `async with` and `async for`.
|
|
30
|
+
"""
|
|
31
|
+
def __init__(self, db_path: str, channel_name: str, poll_interval: float = 0.1):
|
|
32
|
+
self._db_path = db_path
|
|
33
|
+
self._channel = channel_name
|
|
34
|
+
self._poll_interval = poll_interval
|
|
35
|
+
self._queue = asyncio.Queue()
|
|
36
|
+
self._last_seen_timestamp = time.time()
|
|
37
|
+
self._polling_task = None
|
|
38
|
+
|
|
39
|
+
async def _poll_for_messages(self):
|
|
40
|
+
"""A background task that polls SQLite for new messages."""
|
|
41
|
+
while True:
|
|
42
|
+
try:
|
|
43
|
+
# Run the synchronous DB query in a thread to avoid blocking asyncio
|
|
44
|
+
new_messages = await asyncio.to_thread(
|
|
45
|
+
self._fetch_new_messages_from_db
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
if new_messages:
|
|
49
|
+
for timestamp, payload_str in new_messages:
|
|
50
|
+
payload = json.loads(payload_str)
|
|
51
|
+
await self._queue.put(payload)
|
|
52
|
+
self._last_seen_timestamp = timestamp
|
|
53
|
+
|
|
54
|
+
await asyncio.sleep(self._poll_interval)
|
|
55
|
+
except asyncio.CancelledError:
|
|
56
|
+
# Gracefully exit when the task is cancelled
|
|
57
|
+
break
|
|
58
|
+
except Exception as e:
|
|
59
|
+
# In a real app, you'd add more robust logging/error handling
|
|
60
|
+
print(f"ERROR: Pub/sub polling task failed: {e}")
|
|
61
|
+
await asyncio.sleep(self._poll_interval * 5) # Back off on error
|
|
62
|
+
|
|
63
|
+
def _fetch_new_messages_from_db(self) -> list[tuple[float, str]]:
|
|
64
|
+
"""The actual synchronous database query using sqlite3."""
|
|
65
|
+
# Each call in a separate thread gets its own connection object.
|
|
66
|
+
with sqlite3.connect(self._db_path) as conn:
|
|
67
|
+
cursor = conn.cursor()
|
|
68
|
+
cursor.execute(
|
|
69
|
+
"SELECT timestamp, message_payload FROM _beaver_pubsub_log "
|
|
70
|
+
"WHERE channel_name = ? AND timestamp > ? "
|
|
71
|
+
"ORDER BY timestamp ASC",
|
|
72
|
+
(self._channel, self._last_seen_timestamp)
|
|
73
|
+
)
|
|
74
|
+
return cursor.fetchall()
|
|
75
|
+
|
|
76
|
+
async def __aenter__(self):
|
|
77
|
+
"""Starts the background task when entering an 'async with' block."""
|
|
78
|
+
if not self._polling_task:
|
|
79
|
+
self._polling_task = asyncio.create_task(self._poll_for_messages())
|
|
80
|
+
return self
|
|
81
|
+
|
|
82
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
83
|
+
"""Stops the background task when exiting the 'async with' block."""
|
|
84
|
+
if self._polling_task:
|
|
85
|
+
self._polling_task.cancel()
|
|
86
|
+
await asyncio.gather(self._polling_task, return_exceptions=True)
|
|
87
|
+
|
|
88
|
+
def __aiter__(self):
|
|
89
|
+
return self
|
|
90
|
+
|
|
91
|
+
async def __anext__(self) -> Any:
|
|
92
|
+
"""Allows 'async for' to pull messages from the internal queue."""
|
|
93
|
+
return await self._queue.get()
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class BeaverDB:
|
|
97
|
+
"""
|
|
98
|
+
A single-file, multi-modal database for Python.
|
|
99
|
+
|
|
100
|
+
Currently provides asynchronous pub/sub functionality using the standard
|
|
101
|
+
sqlite3 library.
|
|
102
|
+
"""
|
|
103
|
+
def __init__(self, db_path: str = "beaver.db"):
|
|
104
|
+
"""
|
|
105
|
+
Initializes the database.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
db_path: The path to the SQLite database file.
|
|
109
|
+
"""
|
|
110
|
+
self.db_path = db_path
|
|
111
|
+
self._setup_database()
|
|
112
|
+
|
|
113
|
+
def _setup_database(self):
|
|
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()
|
|
120
|
+
|
|
121
|
+
async def publish(self, channel_name: str, payload: Any):
|
|
122
|
+
"""
|
|
123
|
+
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).
|
|
128
|
+
"""
|
|
129
|
+
if not isinstance(channel_name, str) or not channel_name:
|
|
130
|
+
raise ValueError("Channel name must be a non-empty string.")
|
|
131
|
+
|
|
132
|
+
try:
|
|
133
|
+
json_payload = json.dumps(payload)
|
|
134
|
+
except TypeError as e:
|
|
135
|
+
raise TypeError("Message payload must be JSON-serializable.") from e
|
|
136
|
+
|
|
137
|
+
# Run the blocking DB write in a thread to keep this method non-blocking
|
|
138
|
+
await asyncio.to_thread(
|
|
139
|
+
self._write_publish_to_db, channel_name, json_payload
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
def _write_publish_to_db(self, channel_name: str, json_payload: str):
|
|
143
|
+
"""The synchronous part of the publish operation using sqlite3."""
|
|
144
|
+
with sqlite3.connect(self.db_path) as conn:
|
|
145
|
+
cursor = conn.cursor()
|
|
146
|
+
cursor.execute(
|
|
147
|
+
"INSERT INTO _beaver_pubsub_log (timestamp, channel_name, message_payload) "
|
|
148
|
+
"VALUES (?, ?, ?)",
|
|
149
|
+
(time.time(), channel_name, json_payload)
|
|
150
|
+
)
|
|
151
|
+
conn.commit()
|
|
152
|
+
|
|
153
|
+
def subscribe(self, channel_name: str) -> Subscriber:
|
|
154
|
+
"""
|
|
155
|
+
Subscribes to a channel.
|
|
156
|
+
|
|
157
|
+
Returns a 'Subscriber' object that can be used in an 'async with'
|
|
158
|
+
block and `async for` loop to receive messages.
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
channel_name: The name of the channel to subscribe to.
|
|
162
|
+
"""
|
|
163
|
+
return Subscriber(self.db_path, channel_name)
|
|
@@ -0,0 +1,117 @@
|
|
|
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.
|
|
@@ -0,0 +1,6 @@
|
|
|
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,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
beaver
|