beaver-db 0.18.6__py3-none-any.whl → 0.19.1__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 CHANGED
@@ -2,4 +2,4 @@ from .core import BeaverDB
2
2
  from .types import Model
3
3
  from .collections import Document, WalkDirection
4
4
 
5
- __version__ = "0.18.6"
5
+ __version__ = "0.19.1"
beaver/core.py CHANGED
@@ -9,6 +9,7 @@ from .channels import ChannelManager
9
9
  from .collections import CollectionManager, Document
10
10
  from .dicts import DictManager
11
11
  from .lists import ListManager
12
+ from .locks import LockManager
12
13
  from .logs import LogManager
13
14
  from .queues import QueueManager
14
15
 
@@ -99,6 +100,35 @@ class BeaverDB:
99
100
  self._create_pubsub_table()
100
101
  self._create_trigrams_table()
101
102
  self._create_versions_table()
103
+ self._create_locks_table()
104
+
105
+ def _create_locks_table(self): # <-- Add this new method
106
+ """Creates the table for managing inter-process lock waiters."""
107
+ self.connection.execute(
108
+ """
109
+ CREATE TABLE IF NOT EXISTS beaver_lock_waiters (
110
+ lock_name TEXT NOT NULL,
111
+ waiter_id TEXT NOT NULL,
112
+ requested_at REAL NOT NULL,
113
+ expires_at REAL NOT NULL,
114
+ PRIMARY KEY (lock_name, requested_at)
115
+ )
116
+ """
117
+ )
118
+ # Index for fast cleanup of expired locks
119
+ self.connection.execute(
120
+ """
121
+ CREATE INDEX IF NOT EXISTS idx_lock_expires
122
+ ON beaver_lock_waiters (lock_name, expires_at)
123
+ """
124
+ )
125
+ # Index for fast deletion by the lock holder
126
+ self.connection.execute(
127
+ """
128
+ CREATE INDEX IF NOT EXISTS idx_lock_waiter_id
129
+ ON beaver_lock_waiters (lock_name, waiter_id)
130
+ """
131
+ )
102
132
 
103
133
  def _create_logs_table(self):
104
134
  """Creates the table for time-indexed logs."""
@@ -421,3 +451,24 @@ class BeaverDB:
421
451
  raise TypeError("The model parameter must be a JsonSerializable class.")
422
452
 
423
453
  return LogManager(name, self, self._db_path, model)
454
+
455
+ def lock(
456
+ self,
457
+ name: str,
458
+ timeout: float | None = None,
459
+ lock_ttl: float = 60.0,
460
+ poll_interval: float = 0.1,
461
+ ) -> LockManager:
462
+ """
463
+ Returns an inter-process lock manager for a given lock name.
464
+
465
+ Args:
466
+ name: The unique name of the lock (e.g., "run_compaction").
467
+ timeout: Max seconds to wait to acquire the lock.
468
+ If None, it will wait forever.
469
+ lock_ttl: Max seconds the lock can be held. If the process crashes,
470
+ the lock will auto-expire after this time.
471
+ poll_interval: Seconds to wait between polls. Shorter intervals
472
+ are more responsive but create more DB I/O.
473
+ """
474
+ return LockManager(self, name, timeout, lock_ttl, poll_interval)
beaver/locks.py ADDED
@@ -0,0 +1,173 @@
1
+ import random
2
+ import time
3
+ import os
4
+ import uuid
5
+ from typing import Optional
6
+ from .types import IDatabase
7
+
8
+
9
+ class LockManager:
10
+ """
11
+ An inter-process, deadlock-proof, and fair (FIFO) lock built on SQLite.
12
+
13
+ This class provides a context manager (`with` statement) to ensure that
14
+ only one process (among many) can enter a critical section of code at a
15
+ time.
16
+
17
+ It is "fair" because it uses a FIFO queue (based on insertion time).
18
+ It is "deadlock-proof" because locks have a Time-To-Live (TTL); if a
19
+ process crashes, its lock will eventually expire and be cleaned up.
20
+ """
21
+
22
+ def __init__(
23
+ self,
24
+ db: IDatabase,
25
+ name: str,
26
+ timeout: Optional[float] = None,
27
+ lock_ttl: float = 60.0,
28
+ poll_interval: float = 0.1,
29
+ ):
30
+ """
31
+ Initializes the lock manager.
32
+
33
+ Args:
34
+ db: The BeaverDB instance.
35
+ name: The unique name of the lock (e.g., "run_compaction").
36
+ timeout: Max seconds to wait to acquire the lock. If None,
37
+ it will wait forever.
38
+ lock_ttl: Max seconds the lock can be held. If the process crashes,
39
+ the lock will auto-expire after this time.
40
+ poll_interval: Seconds to wait between polls.
41
+ """
42
+ if not isinstance(name, str) or not name:
43
+ raise ValueError("Lock name must be a non-empty string.")
44
+ if lock_ttl <= 0:
45
+ raise ValueError("lock_ttl must be positive.")
46
+ if poll_interval <= 0:
47
+ raise ValueError("poll_interval must be positive.")
48
+
49
+ self._db = db
50
+ self._lock_name = name
51
+ self._timeout = timeout
52
+ self._lock_ttl = lock_ttl
53
+ self._poll_interval = poll_interval
54
+ # A unique ID for this specific lock instance across all processes
55
+ self._waiter_id = f"pid:{os.getpid()}:id:{uuid.uuid4()}"
56
+ self._acquired = False # State to track if this instance holds the lock
57
+
58
+ def acquire(self) -> "LockManager":
59
+ """
60
+ Blocks until the lock is acquired or the timeout expires.
61
+
62
+ Raises:
63
+ TimeoutError: If the lock cannot be acquired within the specified timeout.
64
+ """
65
+ if self._acquired:
66
+ # This instance already holds the lock
67
+ return self
68
+
69
+ start_time = time.time()
70
+ requested_at = time.time()
71
+ expires_at = requested_at + self._lock_ttl
72
+
73
+ conn = self._db.connection
74
+
75
+ try:
76
+ # 1. Add self to the FIFO queue (atomic)
77
+ with conn:
78
+ conn.execute(
79
+ """
80
+ INSERT INTO beaver_lock_waiters (lock_name, waiter_id, requested_at, expires_at)
81
+ VALUES (?, ?, ?, ?)
82
+ """,
83
+ (self._lock_name, self._waiter_id, requested_at, expires_at),
84
+ )
85
+
86
+ # 2. Start polling loop
87
+ while True:
88
+ with conn:
89
+ # 3. Clean up expired locks from crashed processes
90
+ now = time.time()
91
+ conn.execute(
92
+ "DELETE FROM beaver_lock_waiters WHERE lock_name = ? AND expires_at < ?",
93
+ (self._lock_name, now),
94
+ )
95
+
96
+ # 4. Check who is at the front of the queue
97
+ cursor = conn.cursor()
98
+ cursor.execute(
99
+ """
100
+ SELECT waiter_id FROM beaver_lock_waiters
101
+ WHERE lock_name = ?
102
+ ORDER BY requested_at ASC
103
+ LIMIT 1
104
+ """,
105
+ (self._lock_name,),
106
+ )
107
+ result = cursor.fetchone()
108
+ cursor.close()
109
+
110
+ if result and result["waiter_id"] == self._waiter_id:
111
+ # We are at the front. We own the lock.
112
+ self._acquired = True
113
+ return self
114
+
115
+ # 5. Check for timeout
116
+ if self._timeout is not None:
117
+ if (time.time() - start_time) > self._timeout:
118
+ # We timed out. Remove ourselves from the queue and raise.
119
+ self._release_from_queue()
120
+ raise TimeoutError(
121
+ f"Failed to acquire lock '{self._lock_name}' within {self._timeout}s."
122
+ )
123
+
124
+ # 6. Wait politely before polling again
125
+ # Add +/- 10% jitter to the poll interval to avoid thundering herd
126
+ jitter = self._poll_interval * 0.1
127
+ sleep_time = random.uniform(
128
+ self._poll_interval - jitter, self._poll_interval + jitter
129
+ )
130
+ time.sleep(sleep_time)
131
+
132
+ except Exception:
133
+ # If anything goes wrong, try to clean up our waiter entry
134
+ self._release_from_queue()
135
+ raise
136
+
137
+ def _release_from_queue(self):
138
+ """
139
+ Atomically removes this instance's entry from the waiter queue.
140
+ This is a best-effort, fire-and-forget operation.
141
+ """
142
+ try:
143
+ with self._db.connection:
144
+ self._db.connection.execute(
145
+ "DELETE FROM beaver_lock_waiters WHERE lock_name = ? AND waiter_id = ?",
146
+ (self._lock_name, self._waiter_id),
147
+ )
148
+ except Exception:
149
+ # Don't raise errors during release/cleanup
150
+ pass
151
+
152
+ def release(self):
153
+ """
154
+ Releases the lock, allowing the next process in the queue to acquire it.
155
+ This is safe to call multiple times.
156
+ """
157
+ if not self._acquired:
158
+ # We don't hold the lock, so nothing to do.
159
+ return
160
+
161
+ self._release_from_queue()
162
+ self._acquired = False
163
+
164
+ def __enter__(self) -> "LockManager":
165
+ """Acquires the lock when entering a 'with' statement."""
166
+ return self.acquire()
167
+
168
+ def __exit__(self, exc_type, exc_val, exc_tb):
169
+ """Releases the lock when exiting a 'with' statement."""
170
+ self.release()
171
+
172
+ def __repr__(self) -> str:
173
+ return f"LockManager(name='{self._lock_name}', acquired={self._acquired})"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: beaver-db
3
- Version: 0.18.6
3
+ Version: 0.19.1
4
4
  Summary: Fast, embedded, and multi-modal DB based on SQLite for AI-powered applications.
5
5
  License-File: LICENSE
6
6
  Classifier: License :: OSI Approved :: MIT License
@@ -36,7 +36,7 @@ A fast, single-file, multi-modal database for Python, built with the standard `s
36
36
 
37
37
  `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.
38
38
 
39
- > If you like beaver's minimalist, no-bullshit philosophy, check out [castor](https://github.com/apiad/castor) for an equally minimalistic approach to task orchestration.
39
+ > If you like beaver's minimalist, no-bullshit philosophy, check out [castor](https://github.com/apiad/castor "null") for an equally minimalistic approach to task orchestration.
40
40
 
41
41
  ## Design Philosophy
42
42
 
@@ -55,6 +55,7 @@ A fast, single-file, multi-modal database for Python, built with the standard `s
55
55
  - **Namespaced Key-Value Dictionaries**: A Pythonic, dictionary-like interface for storing any JSON-serializable object within separate namespaces with optional TTL for cache implementations.
56
56
  - **Pythonic List Management**: A fluent, Redis-like interface for managing persistent, ordered lists.
57
57
  - **Persistent Priority Queue**: A high-performance, persistent priority queue perfect for task orchestration across multiple processes. Also with optional async support.
58
+ - **Inter-Process Locking**: A robust, deadlock-proof, and fair (FIFO) distributed lock (`db.lock()`) to coordinate multiple processes and prevent race conditions.
58
59
  - **Time-Indexed Log for Monitoring**: A specialized data structure for structured, time-series logs. Query historical data by time range or create a live, aggregated view of the most recent events for real-time dashboards.
59
60
  - **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.
60
61
  - **High-Performance Vector Storage & Search (Optional)**: Store vector embeddings and perform fast approximate nearest neighbor searches using a `faiss`-based hybrid index.
@@ -103,14 +104,14 @@ pip install "beaver-db[full]"
103
104
  ```
104
105
 
105
106
  ### Running with Docker
107
+
106
108
  For a fully embedded and lightweight solution, you can run the BeaverDB REST API server using Docker. This is the easiest way to get a self-hosted instance up and running.
107
109
 
108
110
  ```bash
109
111
  docker run -p 8000:8000 -v $(pwd)/data:/app apiad/beaverdb
110
112
  ```
111
113
 
112
- This command will start the BeaverDB server, and your database file will be stored in the data directory on your host machine. You can access the API at <http://localhost:8000>.
113
-
114
+ This command will start the BeaverDB server, and your database file will be stored in the data directory on your host machine. You can access the API at [http://localhost:8000](http://localhost:8000").
114
115
 
115
116
  ## Quickstart
116
117
 
@@ -172,12 +173,12 @@ Here are a couple of examples using `curl`:
172
173
 
173
174
  ```bash
174
175
  # Set a value in the 'app_config' dictionary
175
- curl -X PUT http://127.0.0.1:8000/dicts/app_config/api_key
176
+ curl -X PUT [http://127.0.0.1:8000/dicts/app_config/api_key](http://127.0.0.1:8000/dicts/app_config/api_key)
176
177
  -H "Content-Type: application/json"
177
178
  -d '"your-secret-api-key"'
178
179
 
179
180
  # Get the value back
180
- curl http://127.0.0.1:8000/dicts/app_config/api_key
181
+ curl [http://127.0.0.1:8000/dicts/app_config/api_key](http://127.0.0.1:8000/dicts/app_config/api_key)
181
182
  # Output: "your-secret-api-key"
182
183
  ```
183
184
 
@@ -341,6 +342,34 @@ for summary in live_summary:
341
342
  print(f"Live Stats (10s window): Count={summary['count']}, Mean={summary['mean']:.2f}")
342
343
  ```
343
344
 
345
+ ### 9. Coordinate Distributed Web Scrapers
346
+
347
+ Run multiple scraper processes in parallel and use `db.lock()` to coordinate them. You can ensure only one process refreshes a shared API token or sitemap, preventing race conditions and rate-limiting.
348
+
349
+ ```python
350
+ import time
351
+
352
+ scrapers_state = db.dict("scraper_state")
353
+
354
+ last_refresh = scrapers_state.get("last_sitemap_refresh", 0)
355
+ if time.time() - last_refresh > 3600: # Only refresh once per hour
356
+ try:
357
+ # Try to get a lock to refresh the shared sitemap, but don't wait long
358
+ with db.lock("refresh_sitemap", timeout=1):
359
+ # We got the lock. Check if it's time to refresh.
360
+ print(f"PID {os.getpid()} is refreshing the sitemap...")
361
+ scrapers_state["sitemap"] = ["/page1", "/page2"] # Your fetch_sitemap()
362
+ scrapers_state["last_sitemap_refresh"] = time.time()
363
+
364
+ except TimeoutError:
365
+ # Another process is already refreshing, so we can skip
366
+ print(f"PID {os.getpid()} letting other process handle refresh.")
367
+
368
+ # All processes can now safely use the shared sitemap
369
+ sitemap = scrapers_state.get("sitemap")
370
+ # ... proceed with scraping ...
371
+ ```
372
+
344
373
  ## Type-Safe Data Models
345
374
 
346
375
  For enhanced data integrity and a better developer experience, BeaverDB supports type-safe operations for all modalities. By associating a model with these data structures, you get automatic serialization and deserialization, complete with autocompletion in your editor.
@@ -348,7 +377,6 @@ For enhanced data integrity and a better developer experience, BeaverDB supports
348
377
  This feature is designed to be flexible and works seamlessly with two kinds of models:
349
378
 
350
379
  - **Pydantic Models**: If you're already using Pydantic, your `BaseModel` classes will work out of the box.
351
-
352
380
  - **Lightweight `beaver.Model`**: For a zero-dependency solution, you can inherit from the built-in `beaver.Model` class, which is a standard Python class with serialization methods automatically included.
353
381
 
354
382
 
@@ -393,10 +421,11 @@ For more in-depth examples, check out the scripts in the `examples/` directory:
393
421
  - [`graph.py`](examples/graph.py): Shows how to create relationships between documents and perform multi-hop graph traversals.
394
422
  - [`kvstore.py`](examples/kvstore.py): A comprehensive demo of the namespaced dictionary feature.
395
423
  - [`list.py`](examples/list.py): Shows the full capabilities of the persistent list, including slicing and in-place updates.
424
+ - [`locks.py`](examples/lock_test.py): Demonstrates how to use the inter-process lock to create critical sections.
396
425
  - [`logs.py`](examples/logs.py): A short example showing how to build a realtime dashboard with the logging feature.
397
426
  - [`pqueue.py`](examples/pqueue.py): A practical example of using the persistent priority queue for task management.
398
427
  - [`producer_consumer.py`](examples/producer_consumer.py): A demonstration of the distributed task queue system in a multi-process environment.
399
- - [`publisher.py`](examples/publisher.p) and [`subscriber.py`](examples/subscriber.py): A pair of examples demonstrating inter-process message passing with the publish/subscribe system.
428
+ - [`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.
400
429
  - [`pubsub.py`](examples/pubsub.py): A demonstration of the synchronous, thread-safe publish/subscribe system in a single process.
401
430
  - [`rerank.py`](examples/rerank.py): Shows how to combine results from vector and text search for more refined results.
402
431
  - [`stress_vectors.py`](examples/stress_vectors.py): A stress test for the vector search functionality.
@@ -410,14 +439,13 @@ For more in-depth examples, check out the scripts in the `examples/` directory:
410
439
 
411
440
  These are some of the features and improvements planned for future releases:
412
441
 
413
- - **Async API**: Extend the async support with on-demand wrappers for all features besides channels.
414
- - **Type-Safe Models**: Enhance built-in `Model` to handle recursive and embedded types.
415
- - **Drop-in REST Client**: Implement a `BeaverClient` class that acts as a drop-in replacement for `BeaverDB` but instead of a local database file, it works against a REST API server.
442
+ - **[Issue #2](https://github.com/syalia-srl/beaver/issues/2) Comprehensive async wrappers**: Extend the async support with on-demand wrappers for all data structures, not just channels.
443
+ - **[Issue #9](https://github.com/syalia-srl/beaver/issues/2) Type-safe wrappers based on Pydantic-compatible models**: Enhance the built-in `Model` to handle recursive and embedded types and provide Pydantic compatibility.
444
+ - **[Issue #6](https://github.com/syalia-srl/beaver/issues/2) Drop-in replacement for Beaver REST server client**: Implement a `BeaverClient` class that acts as a drop-in replacement for `BeaverDB` but works against the REST API server.
445
+ - **[Issue #7](https://github.com/syalia-srl/beaver/issues/2) Replace `faiss` with simpler, linear `numpy` vectorial search**: Investigate removing the heavy `faiss` dependency in favor of a pure `numpy` implementation to improve installation simplicity, accepting a trade-off in search performance for O(1) installation.
416
446
 
417
- Check out the [roadmap](roadmap.md) for a detailed list of upcoming features and design ideas.
418
447
 
419
448
  If you think of something that would make `beaver` more useful for your use case, please open an issue and/or submit a pull request.
420
-
421
449
  ## License
422
450
 
423
451
  This project is licensed under the MIT License.
@@ -1,18 +1,19 @@
1
- beaver/__init__.py,sha256=jeTzeVn_M64ZYQyO3f62tBlLi427gv_8jwVxDGbbzvg,125
1
+ beaver/__init__.py,sha256=dMWanhrt58thAIt9Qj4jzxy5Dg5KzvNQenZ_cNcz_Tk,125
2
2
  beaver/blobs.py,sha256=YkIEskHD6oHRaJTF0P25HrTT8LqM-REyV_UBPVQxeqQ,4055
3
3
  beaver/channels.py,sha256=kIuwKMDBdDQObaKT23znsMXzfpKfE7pXSxvf-u4LlpY,9554
4
4
  beaver/cli.py,sha256=Sxm-mYU3LGd4tIqw-5LHb0ektWebjV9vn51hm-CMJD0,2232
5
5
  beaver/collections.py,sha256=UAQAuRxJRCqY5PHfxJNm3CdKqMNuyY8DOLdodvY6jpk,26107
6
- beaver/core.py,sha256=d37hnD1xaYdZFKf1z0sDAoGwYwuPU8ETrOotso7aHfk,15209
6
+ beaver/core.py,sha256=JRkRvc0Sb3FT9KlR3YbmiPcqCQ686dFKmHSNZ_UJ_aE,17100
7
7
  beaver/dicts.py,sha256=Xp8lPfQt08O8zCbptQLWQLO79OxG6uAVER6ryj3SScQ,5495
8
8
  beaver/lists.py,sha256=rfJ8uTNLkMREYc0uGx0z1VKt2m3eR9hvbdvDD58EbmQ,10140
9
+ beaver/locks.py,sha256=GWDSRkPw2lrAQfXIRqvkc5PK9zZ2eLYWKTuzHTs9j_A,6321
9
10
  beaver/logs.py,sha256=a5xenwl5NZeegIU0dWVEs67lvaHzzw-JRAZtEzNNO3E,9529
10
11
  beaver/queues.py,sha256=Fr3oie63EtceSoiC8EOEDSLu1tDI8q2MYLXd8MEeC3g,6476
11
12
  beaver/server.py,sha256=WoNcPXU9oh6hcHtb60IbEk5DfZT5J4Fb-yubJE3YLIc,13642
12
13
  beaver/types.py,sha256=m0ohT7A8r0Y1a7bJEx4VanLaOUWU2VYxaLHPsVPjrIw,1651
13
14
  beaver/vectors.py,sha256=EGZf1s364-rMubxkYoTcjBl72lRRxM1cUwypjsoC6ec,18499
14
- beaver_db-0.18.6.dist-info/METADATA,sha256=jwuZKs1NM14M9Q6aMYoKutgTUCkBKlc8OmAZeUaZvQs,21232
15
- beaver_db-0.18.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
- beaver_db-0.18.6.dist-info/entry_points.txt,sha256=bd5E2s45PoBdtdR9-ToKSdLNhmHp8naV1lWP5mOzlrc,42
17
- beaver_db-0.18.6.dist-info/licenses/LICENSE,sha256=1xrIY5JnMk_QDQzsqmVzPIIyCgZAkWCC8kF2Ddo1UT0,1071
18
- beaver_db-0.18.6.dist-info/RECORD,,
15
+ beaver_db-0.19.1.dist-info/METADATA,sha256=Nt_3RlU4UraPp-zJusitwvwE2yza_zUyhRvecRcEvrI,23299
16
+ beaver_db-0.19.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
17
+ beaver_db-0.19.1.dist-info/entry_points.txt,sha256=bd5E2s45PoBdtdR9-ToKSdLNhmHp8naV1lWP5mOzlrc,42
18
+ beaver_db-0.19.1.dist-info/licenses/LICENSE,sha256=1xrIY5JnMk_QDQzsqmVzPIIyCgZAkWCC8kF2Ddo1UT0,1071
19
+ beaver_db-0.19.1.dist-info/RECORD,,