Mercurial > libervia-backend
comparison libervia/backend/memory/persistent.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 4b842c1fb686 |
children |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
32 | 32 |
33 class PersistentDict: | 33 class PersistentDict: |
34 r"""A dictionary which save persistently each value assigned | 34 r"""A dictionary which save persistently each value assigned |
35 | 35 |
36 /!\ be careful, each assignment means a database write | 36 /!\ be careful, each assignment means a database write |
37 /!\ Memory must be initialised before loading/setting value with instances of this class""" | 37 /!\ Memory must be initialised before loading/setting value with instances of this class |
38 """ | |
39 | |
38 storage = None | 40 storage = None |
39 binary = False | 41 binary = False |
40 | 42 |
41 def __init__(self, namespace, profile=None): | 43 def __init__(self, namespace, profile=None): |
42 """ | 44 """ |
58 """Load persistent data from storage. | 60 """Load persistent data from storage. |
59 | 61 |
60 need to be called before any other operation | 62 need to be called before any other operation |
61 @return: defers the PersistentDict instance itself | 63 @return: defers the PersistentDict instance itself |
62 """ | 64 """ |
63 d = defer.ensureDeferred(self.storage.get_privates( | 65 d = defer.ensureDeferred( |
64 self.namespace, binary=self.binary, profile=self.profile | 66 self.storage.get_privates( |
65 )) | 67 self.namespace, binary=self.binary, profile=self.profile |
68 ) | |
69 ) | |
66 d.addCallback(self._set_cache) | 70 d.addCallback(self._set_cache) |
67 d.addCallback(lambda __: self) | 71 d.addCallback(lambda __: self) |
68 return d | 72 return d |
69 | 73 |
70 def iteritems(self): | 74 def iteritems(self): |
128 return self._cache.__delitem__(key) | 132 return self._cache.__delitem__(key) |
129 | 133 |
130 def clear(self): | 134 def clear(self): |
131 """Delete all values from this namespace""" | 135 """Delete all values from this namespace""" |
132 self._cache.clear() | 136 self._cache.clear() |
133 return self.storage.del_private_namespace(self.namespace, self.binary, self.profile) | 137 return self.storage.del_private_namespace( |
138 self.namespace, self.binary, self.profile | |
139 ) | |
134 | 140 |
135 def get(self, key, default=None): | 141 def get(self, key, default=None): |
136 return self._cache.get(key, default) | 142 return self._cache.get(key, default) |
137 | 143 |
138 def aset(self, key, value): | 144 def aset(self, key, value): |
146 | 152 |
147 def adel(self, key): | 153 def adel(self, key): |
148 """Async del, return a Deferred fired when value is actually deleted""" | 154 """Async del, return a Deferred fired when value is actually deleted""" |
149 self._cache.__delitem__(key) | 155 self._cache.__delitem__(key) |
150 return self.storage.del_private_value( | 156 return self.storage.del_private_value( |
151 self.namespace, key, self.binary, self.profile) | 157 self.namespace, key, self.binary, self.profile |
158 ) | |
152 | 159 |
153 def setdefault(self, key, default): | 160 def setdefault(self, key, default): |
154 try: | 161 try: |
155 return self._cache[key] | 162 return self._cache[key] |
156 except: | 163 except: |
169 ) | 176 ) |
170 | 177 |
171 | 178 |
172 class PersistentBinaryDict(PersistentDict): | 179 class PersistentBinaryDict(PersistentDict): |
173 """Persistent dict where value can be any python data (instead of string only)""" | 180 """Persistent dict where value can be any python data (instead of string only)""" |
181 | |
174 binary = True | 182 binary = True |
175 | 183 |
176 | 184 |
177 class LazyPersistentBinaryDict(PersistentBinaryDict): | 185 class LazyPersistentBinaryDict(PersistentBinaryDict): |
178 r"""PersistentBinaryDict which get key/value when needed | 186 r"""PersistentBinaryDict which get key/value when needed |
179 | 187 |
180 This Persistent need more database access, it is suitable for largest data, | 188 This Persistent need more database access, it is suitable for largest data, |
181 to save memory. | 189 to save memory. |
182 /!\ most of methods return a Deferred | 190 /!\ most of methods return a Deferred |
183 """ | 191 """ |
192 | |
184 # TODO: missing methods should be implemented using database access | 193 # TODO: missing methods should be implemented using database access |
185 # TODO: a cache would be useful (which is deleted after a timeout) | 194 # TODO: a cache would be useful (which is deleted after a timeout) |
186 | 195 |
187 def load(self): | 196 def load(self): |
188 # we show a warning as calling load on LazyPersistentBinaryDict sounds like a code mistake | 197 # we show a warning as calling load on LazyPersistentBinaryDict sounds like a code mistake |
190 | 199 |
191 def iteritems(self): | 200 def iteritems(self): |
192 raise NotImplementedError | 201 raise NotImplementedError |
193 | 202 |
194 def items(self): | 203 def items(self): |
195 d = defer.ensureDeferred(self.storage.get_privates( | 204 d = defer.ensureDeferred( |
196 self.namespace, binary=self.binary, profile=self.profile | 205 self.storage.get_privates( |
197 )) | 206 self.namespace, binary=self.binary, profile=self.profile |
207 ) | |
208 ) | |
198 d.addCallback(lambda data_dict: data_dict.items()) | 209 d.addCallback(lambda data_dict: data_dict.items()) |
199 return d | 210 return d |
200 | 211 |
201 def all(self): | 212 def all(self): |
202 return defer.ensureDeferred(self.storage.get_privates( | 213 return defer.ensureDeferred( |
203 self.namespace, binary=self.binary, profile=self.profile | 214 self.storage.get_privates( |
204 )) | 215 self.namespace, binary=self.binary, profile=self.profile |
216 ) | |
217 ) | |
205 | 218 |
206 def __repr__(self): | 219 def __repr__(self): |
207 return self.__str__() | 220 return self.__str__() |
208 | 221 |
209 def __str__(self): | 222 def __str__(self): |
229 | 242 |
230 def __cmp__(self, other): | 243 def __cmp__(self, other): |
231 raise NotImplementedError | 244 raise NotImplementedError |
232 | 245 |
233 def __hash__(self): | 246 def __hash__(self): |
234 return hash(str(self.__class__) + self.namespace + (self.profile or '')) | 247 return hash(str(self.__class__) + self.namespace + (self.profile or "")) |
235 | 248 |
236 def __bool__(self): | 249 def __bool__(self): |
237 raise NotImplementedError | 250 raise NotImplementedError |
238 | 251 |
239 def __contains__(self, key): | 252 def __contains__(self, key): |
250 # into debugger in debug mode. | 263 # into debugger in debug mode. |
251 raise failure.Failure(e) | 264 raise failure.Failure(e) |
252 | 265 |
253 def __getitem__(self, key): | 266 def __getitem__(self, key): |
254 """get the value as a Deferred""" | 267 """get the value as a Deferred""" |
255 d = defer.ensureDeferred(self.storage.get_privates( | 268 d = defer.ensureDeferred( |
256 self.namespace, keys=[key], binary=self.binary, profile=self.profile | 269 self.storage.get_privates( |
257 )) | 270 self.namespace, keys=[key], binary=self.binary, profile=self.profile |
271 ) | |
272 ) | |
258 d.addCallback(self._data2value, key) | 273 d.addCallback(self._data2value, key) |
259 return d | 274 return d |
260 | 275 |
261 def __setitem__(self, key, value): | 276 def __setitem__(self, key, value): |
262 defer.ensureDeferred( | 277 defer.ensureDeferred( |
289 | 304 |
290 def adel(self, key): | 305 def adel(self, key): |
291 """Async del, return a Deferred fired when value is actually deleted""" | 306 """Async del, return a Deferred fired when value is actually deleted""" |
292 # XXX: similar as PersistentDict.adel, but doesn't use cache | 307 # XXX: similar as PersistentDict.adel, but doesn't use cache |
293 return self.storage.del_private_value( | 308 return self.storage.del_private_value( |
294 self.namespace, key, self.binary, self.profile) | 309 self.namespace, key, self.binary, self.profile |
310 ) | |
295 | 311 |
296 def setdefault(self, key, default): | 312 def setdefault(self, key, default): |
297 raise NotImplementedError | 313 raise NotImplementedError |
298 | 314 |
299 def force(self, name, value): | 315 def force(self, name, value): |
312 """Delete a key from sotrage, and return a deferred called when it's done | 328 """Delete a key from sotrage, and return a deferred called when it's done |
313 | 329 |
314 @param key(unicode): key to delete | 330 @param key(unicode): key to delete |
315 @return (D): A deferred fired when delete is done | 331 @return (D): A deferred fired when delete is done |
316 """ | 332 """ |
317 return self.storage.del_private_value(self.namespace, key, self.binary, self.profile) | 333 return self.storage.del_private_value( |
334 self.namespace, key, self.binary, self.profile | |
335 ) |