beaver-db 0.5.3__tar.gz → 0.6.2__tar.gz
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_db-0.6.2/LICENSE +21 -0
- {beaver_db-0.5.3 → beaver_db-0.6.2}/PKG-INFO +19 -9
- {beaver_db-0.5.3 → beaver_db-0.6.2}/README.md +16 -8
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver/core.py +15 -39
- beaver_db-0.6.2/beaver/dicts.py +119 -0
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver/lists.py +84 -4
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver_db.egg-info/PKG-INFO +19 -9
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver_db.egg-info/SOURCES.txt +2 -0
- {beaver_db-0.5.3 → beaver_db-0.6.2}/pyproject.toml +1 -1
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver/__init__.py +0 -0
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver/collections.py +0 -0
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver/subscribers.py +0 -0
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver_db.egg-info/dependency_links.txt +0 -0
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver_db.egg-info/requires.txt +0 -0
- {beaver_db-0.5.3 → beaver_db-0.6.2}/beaver_db.egg-info/top_level.txt +0 -0
- {beaver_db-0.5.3 → beaver_db-0.6.2}/setup.cfg +0 -0
beaver_db-0.6.2/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Alejandro Piad
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: beaver-db
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.2
|
|
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
|
|
7
|
+
License-File: LICENSE
|
|
7
8
|
Requires-Dist: numpy>=2.3.3
|
|
8
9
|
Requires-Dist: scipy>=1.16.2
|
|
10
|
+
Dynamic: license-file
|
|
9
11
|
|
|
10
12
|
# beaver 🦫
|
|
11
13
|
|
|
14
|
+

|
|
15
|
+

|
|
16
|
+

|
|
17
|
+
|
|
12
18
|
A fast, single-file, multi-modal database for Python, built with the standard `sqlite3` library.
|
|
13
19
|
|
|
14
20
|
`beaver` is the **B**ackend for **E**mbedded, **A**ll-in-one **V**ector, **E**ntity, and **R**elationship storage. It's a simple, local, and embedded database designed to manage complex, modern data types without requiring a database server, built on top of SQLite.
|
|
@@ -26,8 +32,8 @@ A fast, single-file, multi-modal database for Python, built with the standard `s
|
|
|
26
32
|
## Core Features
|
|
27
33
|
|
|
28
34
|
- **Synchronous Pub/Sub**: A simple, thread-safe, Redis-like publish-subscribe system for real-time messaging.
|
|
29
|
-
- **
|
|
30
|
-
- **Pythonic List Management**: A fluent, Redis-like interface for managing persistent, ordered lists.
|
|
35
|
+
- **Namespaced Key-Value Dictionaries**: A Pythonic, dictionary-like interface for storing any JSON-serializable object within separate namespaces with optional TTL for cache implementations.
|
|
36
|
+
- **Pythonic List Management**: A fluent, Redis-like interface for managing persistent, ordered lists, with all operations in constant time.
|
|
31
37
|
- **Efficient Vector Storage & Search**: Store vector embeddings and perform fast approximate nearest neighbor searches using an in-memory k-d tree.
|
|
32
38
|
- **Full-Text Search**: Automatically index and search through document metadata using SQLite's powerful FTS5 engine.
|
|
33
39
|
- **Graph Traversal**: Create relationships between documents and traverse the graph to find neighbors or perform multi-hop walks.
|
|
@@ -51,17 +57,21 @@ from beaver import BeaverDB, Document
|
|
|
51
57
|
db = BeaverDB("my_application.db")
|
|
52
58
|
```
|
|
53
59
|
|
|
54
|
-
###
|
|
60
|
+
### Namespaced Dictionaries
|
|
55
61
|
|
|
56
|
-
Use `
|
|
62
|
+
Use `db.dict()` to get a dictionary-like object for a specific namespace. The value can be any JSON-encodable object.
|
|
57
63
|
|
|
58
64
|
```python
|
|
59
|
-
#
|
|
60
|
-
db.
|
|
65
|
+
# Get a handle to the 'app_config' namespace
|
|
66
|
+
config = db.dict("app_config")
|
|
67
|
+
|
|
68
|
+
# Set values using standard dictionary syntax
|
|
69
|
+
config["theme"] = "dark"
|
|
70
|
+
config["user_id"] = 123
|
|
61
71
|
|
|
62
72
|
# Get a value
|
|
63
|
-
|
|
64
|
-
print(f"Theme: {
|
|
73
|
+
theme = config.get("theme")
|
|
74
|
+
print(f"Theme: {theme}") # Output: Theme: dark
|
|
65
75
|
```
|
|
66
76
|
|
|
67
77
|
### List Management
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# beaver 🦫
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
3
7
|
A fast, single-file, multi-modal database for Python, built with the standard `sqlite3` library.
|
|
4
8
|
|
|
5
9
|
`beaver` is the **B**ackend for **E**mbedded, **A**ll-in-one **V**ector, **E**ntity, and **R**elationship storage. It's a simple, local, and embedded database designed to manage complex, modern data types without requiring a database server, built on top of SQLite.
|
|
@@ -17,8 +21,8 @@ A fast, single-file, multi-modal database for Python, built with the standard `s
|
|
|
17
21
|
## Core Features
|
|
18
22
|
|
|
19
23
|
- **Synchronous Pub/Sub**: A simple, thread-safe, Redis-like publish-subscribe system for real-time messaging.
|
|
20
|
-
- **
|
|
21
|
-
- **Pythonic List Management**: A fluent, Redis-like interface for managing persistent, ordered lists.
|
|
24
|
+
- **Namespaced Key-Value Dictionaries**: A Pythonic, dictionary-like interface for storing any JSON-serializable object within separate namespaces with optional TTL for cache implementations.
|
|
25
|
+
- **Pythonic List Management**: A fluent, Redis-like interface for managing persistent, ordered lists, with all operations in constant time.
|
|
22
26
|
- **Efficient Vector Storage & Search**: Store vector embeddings and perform fast approximate nearest neighbor searches using an in-memory k-d tree.
|
|
23
27
|
- **Full-Text Search**: Automatically index and search through document metadata using SQLite's powerful FTS5 engine.
|
|
24
28
|
- **Graph Traversal**: Create relationships between documents and traverse the graph to find neighbors or perform multi-hop walks.
|
|
@@ -42,17 +46,21 @@ from beaver import BeaverDB, Document
|
|
|
42
46
|
db = BeaverDB("my_application.db")
|
|
43
47
|
```
|
|
44
48
|
|
|
45
|
-
###
|
|
49
|
+
### Namespaced Dictionaries
|
|
46
50
|
|
|
47
|
-
Use `
|
|
51
|
+
Use `db.dict()` to get a dictionary-like object for a specific namespace. The value can be any JSON-encodable object.
|
|
48
52
|
|
|
49
53
|
```python
|
|
50
|
-
#
|
|
51
|
-
db.
|
|
54
|
+
# Get a handle to the 'app_config' namespace
|
|
55
|
+
config = db.dict("app_config")
|
|
56
|
+
|
|
57
|
+
# Set values using standard dictionary syntax
|
|
58
|
+
config["theme"] = "dark"
|
|
59
|
+
config["user_id"] = 123
|
|
52
60
|
|
|
53
61
|
# Get a value
|
|
54
|
-
|
|
55
|
-
print(f"Theme: {
|
|
62
|
+
theme = config.get("theme")
|
|
63
|
+
print(f"Theme: {theme}") # Output: Theme: dark
|
|
56
64
|
```
|
|
57
65
|
|
|
58
66
|
### List Management
|
|
@@ -3,6 +3,7 @@ import sqlite3
|
|
|
3
3
|
import time
|
|
4
4
|
from typing import Any
|
|
5
5
|
|
|
6
|
+
from .dicts import DictWrapper
|
|
6
7
|
from .lists import ListWrapper
|
|
7
8
|
from .subscribers import SubWrapper
|
|
8
9
|
from .collections import CollectionWrapper
|
|
@@ -30,22 +31,25 @@ class BeaverDB:
|
|
|
30
31
|
|
|
31
32
|
def _create_all_tables(self):
|
|
32
33
|
"""Initializes all required tables in the database file."""
|
|
33
|
-
self._create_kv_table()
|
|
34
34
|
self._create_pubsub_table()
|
|
35
35
|
self._create_list_table()
|
|
36
36
|
self._create_collections_table()
|
|
37
37
|
self._create_fts_table()
|
|
38
38
|
self._create_edges_table()
|
|
39
39
|
self._create_versions_table()
|
|
40
|
+
self._create_dict_table()
|
|
40
41
|
|
|
41
|
-
def
|
|
42
|
-
"""Creates the
|
|
42
|
+
def _create_dict_table(self):
|
|
43
|
+
"""Creates the namespaced dictionary table."""
|
|
43
44
|
with self._conn:
|
|
44
45
|
self._conn.execute(
|
|
45
46
|
"""
|
|
46
|
-
CREATE TABLE IF NOT EXISTS
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
CREATE TABLE IF NOT EXISTS beaver_dicts (
|
|
48
|
+
dict_name TEXT NOT NULL,
|
|
49
|
+
key TEXT NOT NULL,
|
|
50
|
+
value TEXT NOT NULL,
|
|
51
|
+
expires_at REAL,
|
|
52
|
+
PRIMARY KEY (dict_name, key)
|
|
49
53
|
)
|
|
50
54
|
"""
|
|
51
55
|
)
|
|
@@ -148,39 +152,11 @@ class BeaverDB:
|
|
|
148
152
|
|
|
149
153
|
# --- Factory and Passthrough Methods ---
|
|
150
154
|
|
|
151
|
-
def
|
|
152
|
-
"""
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if not isinstance(key, str):
|
|
157
|
-
raise TypeError("Key must be a string.")
|
|
158
|
-
|
|
159
|
-
try:
|
|
160
|
-
json_value = json.dumps(value)
|
|
161
|
-
except TypeError as e:
|
|
162
|
-
raise TypeError("Value must be JSON-serializable.") from e
|
|
163
|
-
|
|
164
|
-
with self._conn:
|
|
165
|
-
self._conn.execute(
|
|
166
|
-
"INSERT OR REPLACE INTO _beaver_kv_store (key, value) VALUES (?, ?)",
|
|
167
|
-
(key, json_value),
|
|
168
|
-
)
|
|
169
|
-
|
|
170
|
-
def get(self, key: str) -> Any:
|
|
171
|
-
"""
|
|
172
|
-
Retrieves a value for a given key.
|
|
173
|
-
This operation is synchronous.
|
|
174
|
-
"""
|
|
175
|
-
if not isinstance(key, str):
|
|
176
|
-
raise TypeError("Key must be a string.")
|
|
177
|
-
|
|
178
|
-
cursor = self._conn.cursor()
|
|
179
|
-
cursor.execute("SELECT value FROM _beaver_kv_store WHERE key = ?", (key,))
|
|
180
|
-
result = cursor.fetchone()
|
|
181
|
-
cursor.close()
|
|
182
|
-
|
|
183
|
-
return json.loads(result["value"]) if result else None
|
|
155
|
+
def dict(self, name: str) -> DictWrapper:
|
|
156
|
+
"""Returns a wrapper object for interacting with a named dictionary."""
|
|
157
|
+
if not isinstance(name, str) or not name:
|
|
158
|
+
raise TypeError("Dictionary name must be a non-empty string.")
|
|
159
|
+
return DictWrapper(name, self._conn)
|
|
184
160
|
|
|
185
161
|
def list(self, name: str) -> ListWrapper:
|
|
186
162
|
"""Returns a wrapper object for interacting with a named list."""
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sqlite3
|
|
3
|
+
import time # Add this import
|
|
4
|
+
from typing import Any, Iterator, Tuple
|
|
5
|
+
|
|
6
|
+
class DictWrapper:
|
|
7
|
+
"""A wrapper providing a Pythonic interface to a dictionary in the database."""
|
|
8
|
+
|
|
9
|
+
def __init__(self, name: str, conn: sqlite3.Connection):
|
|
10
|
+
self._name = name
|
|
11
|
+
self._conn = conn
|
|
12
|
+
|
|
13
|
+
def set(self, key: str, value: Any, ttl_seconds: int | None = None):
|
|
14
|
+
"""Sets a value for a key, with an optional TTL."""
|
|
15
|
+
self.__setitem__(key, value, ttl_seconds=ttl_seconds)
|
|
16
|
+
|
|
17
|
+
def __setitem__(self, key: str, value: Any, ttl_seconds: int | None = None):
|
|
18
|
+
"""Sets a value for a key (e.g., `my_dict[key] = value`)."""
|
|
19
|
+
expires_at = None
|
|
20
|
+
if ttl_seconds is not None:
|
|
21
|
+
if not isinstance(ttl_seconds, int) or ttl_seconds <= 0:
|
|
22
|
+
raise ValueError("ttl_seconds must be a positive integer.")
|
|
23
|
+
expires_at = time.time() + ttl_seconds
|
|
24
|
+
|
|
25
|
+
with self._conn:
|
|
26
|
+
self._conn.execute(
|
|
27
|
+
"INSERT OR REPLACE INTO beaver_dicts (dict_name, key, value, expires_at) VALUES (?, ?, ?, ?)",
|
|
28
|
+
(self._name, key, json.dumps(value), expires_at),
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
def get(self, key: str, default: Any = None) -> Any:
|
|
32
|
+
"""Gets a value for a key, with a default if it doesn't exist or is expired."""
|
|
33
|
+
try:
|
|
34
|
+
return self[key]
|
|
35
|
+
except KeyError:
|
|
36
|
+
return default
|
|
37
|
+
|
|
38
|
+
def __getitem__(self, key: str) -> Any:
|
|
39
|
+
"""Retrieves a value for a given key, raising KeyError if expired."""
|
|
40
|
+
cursor = self._conn.cursor()
|
|
41
|
+
cursor.execute(
|
|
42
|
+
"SELECT value, expires_at FROM beaver_dicts WHERE dict_name = ? AND key = ?",
|
|
43
|
+
(self._name, key),
|
|
44
|
+
)
|
|
45
|
+
result = cursor.fetchone()
|
|
46
|
+
|
|
47
|
+
if result is None:
|
|
48
|
+
cursor.close()
|
|
49
|
+
raise KeyError(f"Key '{key}' not found in dictionary '{self._name}'")
|
|
50
|
+
|
|
51
|
+
value, expires_at = result["value"], result["expires_at"]
|
|
52
|
+
|
|
53
|
+
if expires_at is not None and time.time() > expires_at:
|
|
54
|
+
# Expired: delete the key and raise KeyError
|
|
55
|
+
cursor.execute("DELETE FROM beaver_dicts WHERE dict_name = ? AND key = ?", (self._name, key))
|
|
56
|
+
self._conn.commit()
|
|
57
|
+
cursor.close()
|
|
58
|
+
raise KeyError(f"Key '{key}' not found in dictionary '{self._name}' (expired)")
|
|
59
|
+
|
|
60
|
+
cursor.close()
|
|
61
|
+
return json.loads(value)
|
|
62
|
+
|
|
63
|
+
def __delitem__(self, key: str):
|
|
64
|
+
"""Deletes a key-value pair (e.g., `del my_dict[key]`)."""
|
|
65
|
+
with self._conn:
|
|
66
|
+
cursor = self._conn.cursor()
|
|
67
|
+
cursor.execute(
|
|
68
|
+
"DELETE FROM beaver_dicts WHERE dict_name = ? AND key = ?",
|
|
69
|
+
(self._name, key),
|
|
70
|
+
)
|
|
71
|
+
if cursor.rowcount == 0:
|
|
72
|
+
raise KeyError(f"Key '{key}' not found in dictionary '{self._name}'")
|
|
73
|
+
|
|
74
|
+
def __len__(self) -> int:
|
|
75
|
+
"""Returns the number of items in the dictionary."""
|
|
76
|
+
cursor = self._conn.cursor()
|
|
77
|
+
cursor.execute(
|
|
78
|
+
"SELECT COUNT(*) FROM beaver_dicts WHERE dict_name = ?", (self._name,)
|
|
79
|
+
)
|
|
80
|
+
count = cursor.fetchone()[0]
|
|
81
|
+
cursor.close()
|
|
82
|
+
return count
|
|
83
|
+
|
|
84
|
+
def __iter__(self) -> Iterator[str]:
|
|
85
|
+
"""Returns an iterator over the keys of the dictionary."""
|
|
86
|
+
return self.keys()
|
|
87
|
+
|
|
88
|
+
def keys(self) -> Iterator[str]:
|
|
89
|
+
"""Returns an iterator over the dictionary's keys."""
|
|
90
|
+
cursor = self._conn.cursor()
|
|
91
|
+
cursor.execute(
|
|
92
|
+
"SELECT key FROM beaver_dicts WHERE dict_name = ?", (self._name,)
|
|
93
|
+
)
|
|
94
|
+
for row in cursor:
|
|
95
|
+
yield row["key"]
|
|
96
|
+
cursor.close()
|
|
97
|
+
|
|
98
|
+
def values(self) -> Iterator[Any]:
|
|
99
|
+
"""Returns an iterator over the dictionary's values."""
|
|
100
|
+
cursor = self._conn.cursor()
|
|
101
|
+
cursor.execute(
|
|
102
|
+
"SELECT value FROM beaver_dicts WHERE dict_name = ?", (self._name,)
|
|
103
|
+
)
|
|
104
|
+
for row in cursor:
|
|
105
|
+
yield json.loads(row["value"])
|
|
106
|
+
cursor.close()
|
|
107
|
+
|
|
108
|
+
def items(self) -> Iterator[Tuple[str, Any]]:
|
|
109
|
+
"""Returns an iterator over the dictionary's items (key-value pairs)."""
|
|
110
|
+
cursor = self._conn.cursor()
|
|
111
|
+
cursor.execute(
|
|
112
|
+
"SELECT key, value FROM beaver_dicts WHERE dict_name = ?", (self._name,)
|
|
113
|
+
)
|
|
114
|
+
for row in cursor:
|
|
115
|
+
yield (row["key"], json.loads(row["value"]))
|
|
116
|
+
cursor.close()
|
|
117
|
+
|
|
118
|
+
def __repr__(self) -> str:
|
|
119
|
+
return f"DictWrapper(name='{self._name}')"
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import sqlite3
|
|
3
|
-
from typing import Any, Union
|
|
4
|
-
|
|
3
|
+
from typing import Any, Union, Iterator
|
|
5
4
|
|
|
6
5
|
class ListWrapper:
|
|
7
|
-
"""A wrapper providing a Pythonic interface to a list in the database."""
|
|
6
|
+
"""A wrapper providing a Pythonic, full-featured interface to a list in the database."""
|
|
8
7
|
|
|
9
8
|
def __init__(self, name: str, conn: sqlite3.Connection):
|
|
10
9
|
self._name = name
|
|
@@ -61,6 +60,87 @@ class ListWrapper:
|
|
|
61
60
|
else:
|
|
62
61
|
raise TypeError("List indices must be integers or slices.")
|
|
63
62
|
|
|
63
|
+
def __setitem__(self, key: int, value: Any):
|
|
64
|
+
"""Sets the value of an item at a specific index (e.g., `my_list[0] = 'new'`)."""
|
|
65
|
+
if not isinstance(key, int):
|
|
66
|
+
raise TypeError("List indices must be integers.")
|
|
67
|
+
|
|
68
|
+
list_len = len(self)
|
|
69
|
+
if key < -list_len or key >= list_len:
|
|
70
|
+
raise IndexError("List index out of range.")
|
|
71
|
+
|
|
72
|
+
offset = key if key >= 0 else list_len + key
|
|
73
|
+
|
|
74
|
+
with self._conn:
|
|
75
|
+
cursor = self._conn.cursor()
|
|
76
|
+
# Find the rowid of the item to update
|
|
77
|
+
cursor.execute(
|
|
78
|
+
"SELECT rowid FROM beaver_lists WHERE list_name = ? ORDER BY item_order ASC LIMIT 1 OFFSET ?",
|
|
79
|
+
(self._name, offset)
|
|
80
|
+
)
|
|
81
|
+
result = cursor.fetchone()
|
|
82
|
+
if not result:
|
|
83
|
+
raise IndexError("List index out of range during update.")
|
|
84
|
+
|
|
85
|
+
rowid_to_update = result['rowid']
|
|
86
|
+
# Update the value for that specific row
|
|
87
|
+
cursor.execute(
|
|
88
|
+
"UPDATE beaver_lists SET item_value = ? WHERE rowid = ?",
|
|
89
|
+
(json.dumps(value), rowid_to_update)
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
def __delitem__(self, key: int):
|
|
93
|
+
"""Deletes an item at a specific index (e.g., `del my_list[0]`)."""
|
|
94
|
+
if not isinstance(key, int):
|
|
95
|
+
raise TypeError("List indices must be integers.")
|
|
96
|
+
|
|
97
|
+
list_len = len(self)
|
|
98
|
+
if key < -list_len or key >= list_len:
|
|
99
|
+
raise IndexError("List index out of range.")
|
|
100
|
+
|
|
101
|
+
offset = key if key >= 0 else list_len + key
|
|
102
|
+
|
|
103
|
+
with self._conn:
|
|
104
|
+
cursor = self._conn.cursor()
|
|
105
|
+
# Find the rowid of the item to delete
|
|
106
|
+
cursor.execute(
|
|
107
|
+
"SELECT rowid FROM beaver_lists WHERE list_name = ? ORDER BY item_order ASC LIMIT 1 OFFSET ?",
|
|
108
|
+
(self._name, offset)
|
|
109
|
+
)
|
|
110
|
+
result = cursor.fetchone()
|
|
111
|
+
if not result:
|
|
112
|
+
raise IndexError("List index out of range during delete.")
|
|
113
|
+
|
|
114
|
+
rowid_to_delete = result['rowid']
|
|
115
|
+
# Delete that specific row
|
|
116
|
+
cursor.execute("DELETE FROM beaver_lists WHERE rowid = ?", (rowid_to_delete,))
|
|
117
|
+
|
|
118
|
+
def __iter__(self) -> Iterator[Any]:
|
|
119
|
+
"""Returns an iterator for the list."""
|
|
120
|
+
cursor = self._conn.cursor()
|
|
121
|
+
cursor.execute(
|
|
122
|
+
"SELECT item_value FROM beaver_lists WHERE list_name = ? ORDER BY item_order ASC",
|
|
123
|
+
(self._name,)
|
|
124
|
+
)
|
|
125
|
+
for row in cursor:
|
|
126
|
+
yield json.loads(row['item_value'])
|
|
127
|
+
cursor.close()
|
|
128
|
+
|
|
129
|
+
def __contains__(self, value: Any) -> bool:
|
|
130
|
+
"""Checks for the existence of an item in the list (e.g., `'item' in my_list`)."""
|
|
131
|
+
cursor = self._conn.cursor()
|
|
132
|
+
cursor.execute(
|
|
133
|
+
"SELECT 1 FROM beaver_lists WHERE list_name = ? AND item_value = ? LIMIT 1",
|
|
134
|
+
(self._name, json.dumps(value))
|
|
135
|
+
)
|
|
136
|
+
result = cursor.fetchone()
|
|
137
|
+
cursor.close()
|
|
138
|
+
return result is not None
|
|
139
|
+
|
|
140
|
+
def __repr__(self) -> str:
|
|
141
|
+
"""Returns a developer-friendly representation of the object."""
|
|
142
|
+
return f"ListWrapper(name='{self._name}')"
|
|
143
|
+
|
|
64
144
|
def _get_order_at_index(self, index: int) -> float:
|
|
65
145
|
"""Helper to get the float `item_order` at a specific index."""
|
|
66
146
|
cursor = self._conn.cursor()
|
|
@@ -163,4 +243,4 @@ class ListWrapper:
|
|
|
163
243
|
cursor.execute(
|
|
164
244
|
"DELETE FROM beaver_lists WHERE rowid = ?", (rowid_to_delete,)
|
|
165
245
|
)
|
|
166
|
-
return json.loads(value_to_return)
|
|
246
|
+
return json.loads(value_to_return)
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: beaver-db
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.2
|
|
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
|
|
7
|
+
License-File: LICENSE
|
|
7
8
|
Requires-Dist: numpy>=2.3.3
|
|
8
9
|
Requires-Dist: scipy>=1.16.2
|
|
10
|
+
Dynamic: license-file
|
|
9
11
|
|
|
10
12
|
# beaver 🦫
|
|
11
13
|
|
|
14
|
+

|
|
15
|
+

|
|
16
|
+

|
|
17
|
+
|
|
12
18
|
A fast, single-file, multi-modal database for Python, built with the standard `sqlite3` library.
|
|
13
19
|
|
|
14
20
|
`beaver` is the **B**ackend for **E**mbedded, **A**ll-in-one **V**ector, **E**ntity, and **R**elationship storage. It's a simple, local, and embedded database designed to manage complex, modern data types without requiring a database server, built on top of SQLite.
|
|
@@ -26,8 +32,8 @@ A fast, single-file, multi-modal database for Python, built with the standard `s
|
|
|
26
32
|
## Core Features
|
|
27
33
|
|
|
28
34
|
- **Synchronous Pub/Sub**: A simple, thread-safe, Redis-like publish-subscribe system for real-time messaging.
|
|
29
|
-
- **
|
|
30
|
-
- **Pythonic List Management**: A fluent, Redis-like interface for managing persistent, ordered lists.
|
|
35
|
+
- **Namespaced Key-Value Dictionaries**: A Pythonic, dictionary-like interface for storing any JSON-serializable object within separate namespaces with optional TTL for cache implementations.
|
|
36
|
+
- **Pythonic List Management**: A fluent, Redis-like interface for managing persistent, ordered lists, with all operations in constant time.
|
|
31
37
|
- **Efficient Vector Storage & Search**: Store vector embeddings and perform fast approximate nearest neighbor searches using an in-memory k-d tree.
|
|
32
38
|
- **Full-Text Search**: Automatically index and search through document metadata using SQLite's powerful FTS5 engine.
|
|
33
39
|
- **Graph Traversal**: Create relationships between documents and traverse the graph to find neighbors or perform multi-hop walks.
|
|
@@ -51,17 +57,21 @@ from beaver import BeaverDB, Document
|
|
|
51
57
|
db = BeaverDB("my_application.db")
|
|
52
58
|
```
|
|
53
59
|
|
|
54
|
-
###
|
|
60
|
+
### Namespaced Dictionaries
|
|
55
61
|
|
|
56
|
-
Use `
|
|
62
|
+
Use `db.dict()` to get a dictionary-like object for a specific namespace. The value can be any JSON-encodable object.
|
|
57
63
|
|
|
58
64
|
```python
|
|
59
|
-
#
|
|
60
|
-
db.
|
|
65
|
+
# Get a handle to the 'app_config' namespace
|
|
66
|
+
config = db.dict("app_config")
|
|
67
|
+
|
|
68
|
+
# Set values using standard dictionary syntax
|
|
69
|
+
config["theme"] = "dark"
|
|
70
|
+
config["user_id"] = 123
|
|
61
71
|
|
|
62
72
|
# Get a value
|
|
63
|
-
|
|
64
|
-
print(f"Theme: {
|
|
73
|
+
theme = config.get("theme")
|
|
74
|
+
print(f"Theme: {theme}") # Output: Theme: dark
|
|
65
75
|
```
|
|
66
76
|
|
|
67
77
|
### List Management
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|