-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathpickledb.py
180 lines (147 loc) · 5.52 KB
/
pickledb.py
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
"""
Copyright Harrison Erd
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import os
import orjson
class PickleDB:
"""
A barebones orjson-based key-value store with essential methods:
set, get, save, remove, purge, and all.
"""
def __init__(self, location):
"""
Initialize the PickleDB object.
Args:
location (str): Path to the JSON file.
"""
self.location = os.path.expanduser(location)
self._load()
def __setitem__(self, key, value):
"""
Wraps the `set` method to allow `db[key] = value`. See `set`
method for details.
"""
return self.set(key, value)
def __getitem__(self, key):
"""
Wraps the `get` method to allow `value = db[key]`. See `get`
method for details.
"""
return self.get(key)
def _load(self):
"""
Load data from the JSON file if it exists, or initialize an empty
database.
"""
if (os.path.exists(self.location) and
os.path.getsize(self.location) > 0):
try:
with open(self.location, "rb") as f:
self.db = orjson.loads(f.read())
except Exception as e:
raise RuntimeError(f"{e}\nFailed to load database.")
else:
self.db = {}
def save(self, option=0):
"""
Save the database to the file using an atomic save.
Args:
options (int): `orjson.OPT_*` flags to configure
serialization behavior.
Behavior:
- Writes to a temporary file and replaces the
original file only after the write is successful,
ensuring data integrity.
Returns:
bool: True if save was successful, False if not.
"""
temp_location = f"{self.location}.tmp"
try:
with open(temp_location, "wb") as temp_file:
temp_file.write(orjson.dumps(self.db, option=option))
os.replace(temp_location, self.location)
return True
except Exception as e:
print(f"Failed to save database: {e}")
return False
def set(self, key, value):
"""
Add or update a key-value pair in the database.
Args:
key (any): The key to set. If the key is not a string, it
will be converted to a string.
value (any): The value to associate with the key.
Behavior:
- If the key already exists, its value will be updated.
- If the key does not exist, it will be added to the
database.
Returns:
bool: True if the operation succeeds.
"""
key = str(key) if not isinstance(key, str) else key
self.db[key] = value
return True
def remove(self, key):
"""
Remove a key and its value from the database.
Args:
key (any): The key to delete. If the key is not a string,
it will be converted to a string.
Returns:
bool: True if the key was deleted, False if the key does
not exist.
"""
key = str(key) if not isinstance(key, str) else key
if key in self.db:
del self.db[key]
return True
return False
def purge(self):
"""
Clear all keys from the database.
Returns:
bool: True if the operation succeeds.
"""
self.db.clear()
return True
def get(self, key):
"""
Get the value associated with a key.
Args:
key (any): The key to retrieve. If the key is not a
string, it will be converted to a string.
Returns:
any: The value associated with the key, or None if the
key does not exist.
"""
key = str(key) if not isinstance(key, str) else key
return self.db.get(key)
def all(self):
"""
Get a list of all keys in the database.
Returns:
list: A list of all keys.
"""
return list(self.db.keys())