scruby 0.15.1__py3-none-any.whl → 0.16.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 scruby might be problematic. Click here for more details.

scruby/db.py CHANGED
@@ -18,6 +18,10 @@ from anyio import Path, to_thread
18
18
  from pydantic import BaseModel
19
19
 
20
20
  from scruby import constants
21
+ from scruby.errors import (
22
+ KeyAlreadyExistsError,
23
+ KeyNotExistsError,
24
+ )
21
25
 
22
26
  logger = logging.getLogger(__name__)
23
27
 
@@ -175,36 +179,75 @@ class Scruby[T]:
175
179
  leaf_path: Path = Path(*(branch_path, "leaf.json"))
176
180
  return leaf_path
177
181
 
178
- async def set_key(
182
+ async def add_key(
179
183
  self,
180
184
  key: str,
181
185
  value: T,
182
186
  ) -> None:
183
- """Asynchronous method for adding and updating keys to collection.
187
+ """Asynchronous method for adding key to collection.
184
188
 
185
189
  Args:
186
- key: Key name.
187
- value: Value of key.
190
+ key: Key name. Type `str`.
191
+ value: Value of key. Type `BaseModel`.
188
192
 
189
193
  Returns:
190
194
  None.
191
195
  """
192
- # The path to the database cell.
196
+ # The path to cell of collection.
193
197
  leaf_path: Path = await self._get_leaf_path(key)
194
198
  value_json: str = value.model_dump_json()
195
- # Write key-value to the database.
199
+ # Write key-value to collection.
196
200
  if await leaf_path.exists():
197
- # Add new key or update existing.
201
+ # Add new key.
198
202
  data_json: bytes = await leaf_path.read_bytes()
199
203
  data: dict = orjson.loads(data_json) or {}
200
- if data.get(key) is None:
201
- await self._counter_documents(1)
202
- data[key] = value_json
203
- await leaf_path.write_bytes(orjson.dumps(data))
204
+ try:
205
+ data[key]
206
+ except KeyError:
207
+ data[key] = value_json
208
+ await leaf_path.write_bytes(orjson.dumps(data))
209
+ else:
210
+ err = KeyAlreadyExistsError()
211
+ logger.error(err.message)
212
+ raise err
204
213
  else:
205
214
  # Add new key to a blank leaf.
206
215
  await leaf_path.write_bytes(orjson.dumps({key: value_json}))
207
- await self._counter_documents(1)
216
+ await self._counter_documents(1)
217
+
218
+ async def update_key(
219
+ self,
220
+ key: str,
221
+ value: T,
222
+ ) -> None:
223
+ """Asynchronous method for updating key to collection.
224
+
225
+ Args:
226
+ key: Key name. Type `str`.
227
+ value: Value of key. Type `BaseModel`.
228
+
229
+ Returns:
230
+ None.
231
+ """
232
+ # The path to cell of collection.
233
+ leaf_path: Path = await self._get_leaf_path(key)
234
+ value_json: str = value.model_dump_json()
235
+ # Update the existing key.
236
+ if await leaf_path.exists():
237
+ # Update the existing key.
238
+ data_json: bytes = await leaf_path.read_bytes()
239
+ data: dict = orjson.loads(data_json) or {}
240
+ try:
241
+ data[key]
242
+ data[key] = value_json
243
+ await leaf_path.write_bytes(orjson.dumps(data))
244
+ except KeyError:
245
+ err = KeyNotExistsError()
246
+ logger.error(err.message)
247
+ raise err from None
248
+ else:
249
+ logger.error("The key not exists.")
250
+ raise KeyError()
208
251
 
209
252
  async def get_key(self, key: str) -> T:
210
253
  """Asynchronous method for getting value of key from collection.
@@ -236,7 +279,7 @@ class Scruby[T]:
236
279
  Returns:
237
280
  True, if the key is present.
238
281
  """
239
- # The path to the database cell.
282
+ # Get path to cell of collection.
240
283
  leaf_path: Path = await self._get_leaf_path(key)
241
284
  # Checking whether there is a key.
242
285
  if await leaf_path.exists():
scruby/errors.py CHANGED
@@ -21,3 +21,19 @@ class MetadataValueError(ScrubyException):
21
21
  def __init__(self, message: str) -> None: # noqa: D107
22
22
  self.message = message
23
23
  super().__init__(self.message)
24
+
25
+
26
+ class KeyAlreadyExistsError(ScrubyException):
27
+ """Exception is raised if the key already exists."""
28
+
29
+ def __init__(self) -> None:
30
+ self.message = "The key already exists."
31
+ super().__init__(self.message)
32
+
33
+
34
+ class KeyNotExistsError(ScrubyException):
35
+ """Exception is raised If the key is not exists."""
36
+
37
+ def __init__(self) -> None:
38
+ self.message = "The key not exists."
39
+ super().__init__(self.message)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scruby
3
- Version: 0.15.1
3
+ Version: 0.16.1
4
4
  Summary: A fast key-value storage library.
5
5
  Project-URL: Homepage, https://github.com/kebasyaty/scruby
6
6
  Project-URL: Repository, https://github.com/kebasyaty/scruby
@@ -21,6 +21,7 @@ Classifier: Programming Language :: Python :: 3
21
21
  Classifier: Programming Language :: Python :: 3 :: Only
22
22
  Classifier: Programming Language :: Python :: 3.12
23
23
  Classifier: Programming Language :: Python :: 3.13
24
+ Classifier: Programming Language :: Python :: 3.14
24
25
  Classifier: Programming Language :: Python :: Implementation :: CPython
25
26
  Classifier: Topic :: Database
26
27
  Classifier: Typing :: Typed
@@ -143,7 +144,9 @@ async def main() -> None:
143
144
  phone="+447986123456",
144
145
  )
145
146
 
146
- await user_coll.set_key("+447986123456", user)
147
+ await user_coll.add_key(user.phone, user)
148
+
149
+ await user_coll.update_key(user.phone, user)
147
150
 
148
151
  await user_coll.get_key("+447986123456") # => user
149
152
  await user_coll.get_key("key missing") # => KeyError
@@ -209,7 +212,7 @@ async def main() -> None:
209
212
  )
210
213
 
211
214
  # Add user to collection.
212
- await user_coll.set_key("+447986123456", user)
215
+ await user_coll.add_key(user.phone, user)
213
216
 
214
217
  # Find user by email.
215
218
  user_details: User | None = user_coll.find_one(
@@ -282,7 +285,7 @@ async def main() -> None:
282
285
  email=f"John_Smith_{num}@gmail.com",
283
286
  phone=f"+44798612345{num}",
284
287
  )
285
- await db.set_key(f"+44798612345{num}", user)
288
+ await user_coll.add_key(user.phone, user)
286
289
 
287
290
  # Find users by email.
288
291
  users: list[User] | None = user_coll.find_many(
@@ -0,0 +1,10 @@
1
+ scruby/__init__.py,sha256=GOVcjXmcOEDBbJQJDJlQq-x3M-VGJaMSN278EXsl2po,884
2
+ scruby/aggregation.py,sha256=_SUo9gL9yrtr94F-BNaBpuMkEfxtxtcvuOUqw7Ryygs,3466
3
+ scruby/constants.py,sha256=3LZfcxcuRqwzoB0-iogLMjKBZRdxfWJmTbyPwVRhQgY,1007
4
+ scruby/db.py,sha256=Oanc8M5JR33ffMS_MF5QebMZ8npQODeDgIttvFNcK8w,27079
5
+ scruby/errors.py,sha256=enLprEqfj_B2HcuOiU2oObZ5wUKyjY1VhGifXz84wew,1081
6
+ scruby/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ scruby-0.16.1.dist-info/METADATA,sha256=th9EmHhTNv6fndLgqVZsevTke0iTUpRN5HrOsLqzg7w,11013
8
+ scruby-0.16.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
+ scruby-0.16.1.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
10
+ scruby-0.16.1.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- scruby/__init__.py,sha256=GOVcjXmcOEDBbJQJDJlQq-x3M-VGJaMSN278EXsl2po,884
2
- scruby/aggregation.py,sha256=_SUo9gL9yrtr94F-BNaBpuMkEfxtxtcvuOUqw7Ryygs,3466
3
- scruby/constants.py,sha256=3LZfcxcuRqwzoB0-iogLMjKBZRdxfWJmTbyPwVRhQgY,1007
4
- scruby/db.py,sha256=FNW_o2JDd_RnGpOdsEfSubMH8kcO7CSoej52y9vJwnc,25769
5
- scruby/errors.py,sha256=aHQri4LNcFVQrSHwjyzb1fL8O49SwjYEU4QgMOo4uyA,622
6
- scruby/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
- scruby-0.15.1.dist-info/METADATA,sha256=shka5n94xxvbJ8FHWvMryCvzpEmrWyJ6Z2xzS4A0o9g,10925
8
- scruby-0.15.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
- scruby-0.15.1.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
10
- scruby-0.15.1.dist-info/RECORD,,