-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathpickledb.py
More file actions
400 lines (328 loc) · 12.5 KB
/
Copy pathpickledb.py
File metadata and controls
400 lines (328 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""
pickleDB - https://patx.github.io/pickledb
Harrison Erd - https://harrisonerd.com/
Licensed - BSD 3 Clause (see LICENSE)
"""
import asyncio
import os
import re
from typing import Any
import uuid
import orjson
import aiofiles
try:
import sqlite3
import aiosqlite
sqlite_enable = True
except ImportError:
sqlite_enable = False
MISSING = object()
_SQLITE_TABLE_NAME_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
def in_async() -> bool:
"""Return True if we're currently running inside an event loop."""
try:
asyncio.get_running_loop()
return True
except RuntimeError:
return False
def dualmethod(func):
"""
Decorator that lets an async method be called in both sync and async code.
- In async code: returns the coroutine (you must `await` it).
- In sync code: runs the coroutine with asyncio.run() and returns the result.
"""
def wrapper(self, *args, **kwargs):
coro = func(self, *args, **kwargs)
if in_async():
return coro
return asyncio.run(coro)
return wrapper
class PickleDB:
"""
A unified async/sync key-value store using orjson + aiofiles.
All data is kept in-memory in `self.db` and serialized to disk as a single
orjson-encoded file at `self.location`.
"""
def __init__(self, location: str):
self.location = os.path.expanduser(location)
self.db: dict[str, Any] = {}
self._lock = asyncio.Lock()
def __enter__(self):
self.load()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
self.save()
async def __aenter__(self):
await self.load()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
await self.save()
@dualmethod
async def load(self) -> bool:
"""
Load JSON database from disk into memory.
Returns `self` to allow chaining:
db = PickleDB("Example.json").load()
"""
if os.path.exists(self.location) and os.path.getsize(self.location) > 0:
async with aiofiles.open(self.location, "rb") as f:
data = await f.read()
new_db = orjson.loads(data)
else:
new_db = {}
async with self._lock:
self.db = new_db
return self
@dualmethod
async def save(self) -> bool:
"""
Atomically save database to disk.
Writes to `<location>.tmp` and then os.replace() over the original file.
Returns True on success.
"""
temp = f"{self.location}.tmp"
async with self._lock:
async with aiofiles.open(temp, "wb") as f:
await f.write(orjson.dumps(self.db))
await asyncio.to_thread(os.replace, temp, self.location)
return True
@dualmethod
async def set(self, key, value) -> bool:
"""Set a key-value pair. Always returns True."""
async with self._lock:
self.db[str(key)] = value
return True
@dualmethod
async def get(self, key, default=None):
"""Get a key's value, or `default` if missing."""
async with self._lock:
return self.db.get(str(key), default)
@dualmethod
async def remove(self, key) -> bool:
"""Remove a key-value pair. Returns True if it existed, False otherwise."""
async with self._lock:
return self.db.pop(str(key), None) is not None
@dualmethod
async def all(self):
"""Return a list of all keys."""
async with self._lock:
return list(self.db.keys())
@dualmethod
async def purge(self) -> bool:
"""Remove all key-value pairs from the database. Always returns True."""
async with self._lock:
self.db.clear()
return True
if sqlite_enable:
class PickleDBSQLite:
"""
A unified async/sync key-value store backed by SQLite.
Each key is stored as a row:
CREATE TABLE kv (
key TEXT PRIMARY KEY,
value BLOB NOT NULL
)
Values are stored as JSON-encoded bytes via orjson.
"""
def __init__(
self,
sqlite_path: str = "pickledb.sqlite3",
table_name: str = "kv",
) -> None:
if (
not isinstance(table_name, str)
or not _SQLITE_TABLE_NAME_RE.fullmatch(table_name)
):
raise ValueError(
"Invalid table name. Use only ASCII letters, numbers, and "
"underscores, and start with a letter or underscore."
)
self.sqlite_path = sqlite_path
self.table_name = table_name
self._conn = sqlite3.connect(self.sqlite_path, check_same_thread=False)
self._conn.row_factory = sqlite3.Row
self._conn.execute(
f"""
CREATE TABLE IF NOT EXISTS {self.table_name} (
key TEXT PRIMARY KEY,
value BLOB NOT NULL
)
"""
)
self._conn.commit()
def _dumps(self, value: Any) -> bytes:
"""Serialize a Python object to orjson-encoded bytes."""
return orjson.dumps(value)
def _loads(self, data: bytes) -> Any:
"""Deserialize orjson-encoded bytes back into a Python object."""
return orjson.loads(data)
def set(self, key: str | None, value: Any) -> str:
"""
Set a key-value pair.
If key is None, generate a new random UUID key and return it.
In async code, returns a coroutine you must `await`.
In sync code, returns the key string directly.
"""
if in_async():
async def _aset() -> str:
async with aiosqlite.connect(self.sqlite_path) as db:
db.row_factory = sqlite3.Row
payload = self._dumps(value)
if key is None:
new_key = str(uuid.uuid4())
await db.execute(
f"INSERT INTO {self.table_name} (key, value) VALUES (?, ?)",
(new_key, payload),
)
await db.commit()
return new_key
await db.execute(
f"""
INSERT INTO {self.table_name} (key, value)
VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value=excluded.value
""",
(str(key), payload),
)
await db.commit()
return str(key)
return _aset()
payload = self._dumps(value)
if key is None:
new_key = str(uuid.uuid4())
self._conn.execute(
f"INSERT INTO {self.table_name} (key, value) VALUES (?, ?)",
(new_key, payload),
)
self._conn.commit()
return new_key
self._conn.execute(
f"""
INSERT INTO {self.table_name} (key, value)
VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value=excluded.value
""",
(str(key), payload),
)
self._conn.commit()
return str(key)
def get(self, key: str, default: Any = MISSING) -> Any:
"""
Get the value for a key.
If the key does not exist:
- If default is MISSING, raises KeyError.
- Otherwise returns default.
In async code, returns a coroutine you must `await`.
In sync code, returns the value directly.
"""
if in_async():
async def _aget() -> Any:
async with aiosqlite.connect(self.sqlite_path) as db:
db.row_factory = sqlite3.Row
cursor = await db.execute(
f"SELECT value FROM {self.table_name} WHERE key = ?",
(str(key),),
)
row = await cursor.fetchone()
await cursor.close()
if row is None:
if default is MISSING:
raise KeyError(key)
return default
return self._loads(row["value"])
return _aget()
cursor = self._conn.execute(
f"SELECT value FROM {self.table_name} WHERE key = ?",
(str(key),),
)
row = cursor.fetchone()
if row is None:
if default is MISSING:
raise KeyError(key)
return default
return self._loads(row["value"])
def remove(self, key: str) -> bool:
"""
Remove a key-value pair.
Returns True if a row was deleted, False otherwise.
In async code, returns a coroutine you must `await`.
In sync code, returns a bool directly.
"""
if in_async():
async def _aremove() -> bool:
async with aiosqlite.connect(self.sqlite_path) as db:
cursor = await db.execute(
f"DELETE FROM {self.table_name} WHERE key = ?",
(str(key),),
)
await db.commit()
return cursor.rowcount > 0
return _aremove()
cursor = self._conn.execute(
f"DELETE FROM {self.table_name} WHERE key = ?",
(str(key),),
)
self._conn.commit()
return cursor.rowcount > 0
def all(self) -> list[str]:
"""
Return a list of all keys in the database.
In async code, returns a coroutine you must `await`.
In sync code, returns the list directly.
"""
if in_async():
async def _aall() -> list[str]:
async with aiosqlite.connect(self.sqlite_path) as db:
db.row_factory = sqlite3.Row
cursor = await db.execute(
f"SELECT key FROM {self.table_name} ORDER BY key"
)
rows = await cursor.fetchall()
await cursor.close()
return [row["key"] for row in rows]
return _aall()
cursor = self._conn.execute(
f"SELECT key FROM {self.table_name} ORDER BY key"
)
return [row["key"] for row in cursor.fetchall()]
def purge(self) -> bool:
"""
Remove all key-value pairs from the database.
Always returns True.
In async code, returns a coroutine you must `await`.
In sync code, returns True directly.
"""
if in_async():
async def _apurge() -> bool:
async with aiosqlite.connect(self.sqlite_path) as db:
await db.execute(f"DELETE FROM {self.table_name}")
await db.commit()
return True
return _apurge()
self._conn.execute(f"DELETE FROM {self.table_name}")
self._conn.commit()
return True
def close(self) -> None:
"""
Close the underlying sync SQLite connection.
In async code, returns a coroutine you must `await`.
In sync code, closes immediately.
"""
if in_async():
async def _aclose() -> None:
self._conn.close()
return _aclose()
self._conn.close()
else:
class PickleDBSQLite:
"""
This class is only usable if `aiosqlite` is installed, e.g.:
pip install "pickledb[sqlite]"
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
raise RuntimeError(
"PickleDBSQLite requires `aiosqlite`. "
"Install it via `pip install \"pickledb[sqlite]\"`."
)