scruby 0.10.1__py3-none-any.whl → 0.10.3__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
@@ -27,6 +27,9 @@ T = TypeVar("T")
27
27
  class _Meta(BaseModel):
28
28
  """Metadata of Collection."""
29
29
 
30
+ db_root: str
31
+ model_name: str
32
+ length_reduction_hash: int
30
33
  counter_documents: int
31
34
 
32
35
 
@@ -59,7 +62,8 @@ class Scruby[T]:
59
62
  msg: str = f"{unreachable} - Unacceptable value for LENGTH_REDUCTION_HASH."
60
63
  logger.critical(msg)
61
64
  assert_never(Never(unreachable))
62
- # Create metadata if absent.
65
+ # 1.Create metadata if absent.
66
+ # 2.Check metadata.
63
67
  self._create_metadata()
64
68
 
65
69
  def _create_metadata(self) -> None:
@@ -80,6 +84,9 @@ class Scruby[T]:
80
84
  if not branch_path.exists():
81
85
  branch_path.mkdir(parents=True)
82
86
  meta = _Meta(
87
+ db_root=self.__db_root,
88
+ model_name=self.__class_model.__name__,
89
+ length_reduction_hash=self.__length_reduction_hash,
83
90
  counter_documents=0,
84
91
  )
85
92
  meta_json = meta.model_dump_json()
@@ -265,7 +272,7 @@ class Scruby[T]:
265
272
  return
266
273
 
267
274
  @staticmethod
268
- def _search_task(
275
+ def _task_find(
269
276
  key: int,
270
277
  filter_fn: Callable,
271
278
  length_reduction_hash: str,
@@ -316,7 +323,7 @@ class Scruby[T]:
316
323
  If None, then there is no limit on the wait time.
317
324
  """
318
325
  keys: range = range(1, self.__max_num_keys)
319
- search_task_fn: Callable = self._search_task
326
+ search_task_fn: Callable = self._task_find
320
327
  length_reduction_hash: int = self.__length_reduction_hash
321
328
  db_root: str = self.__db_root
322
329
  class_model: T = self.__class_model
@@ -358,7 +365,7 @@ class Scruby[T]:
358
365
  If None, then there is no limit on the wait time.
359
366
  """
360
367
  keys: range = range(1, self.__max_num_keys)
361
- search_task_fn: Callable = self._search_task
368
+ search_task_fn: Callable = self._task_find
362
369
  length_reduction_hash: int = self.__length_reduction_hash
363
370
  db_root: str = self.__db_root
364
371
  class_model: T = self.__class_model
@@ -394,3 +401,43 @@ class Scruby[T]:
394
401
  """Get an estimate of the number of documents in this collection using collection metadata."""
395
402
  meta = await self._get_meta()
396
403
  return meta.counter_documents
404
+
405
+ def count_documents(
406
+ self,
407
+ filter_fn: Callable,
408
+ max_workers: int | None = None,
409
+ timeout: float | None = None,
410
+ ) -> int:
411
+ """Count the number of documents a matching the filter in this collection.
412
+
413
+ The search is based on the effect of a quantum loop.
414
+ The search effectiveness depends on the number of processor threads.
415
+ Ideally, hundreds and even thousands of threads are required.
416
+
417
+ Args:
418
+ filter_fn: A function that execute the conditions of filtering.
419
+ max_workers: The maximum number of processes that can be used to
420
+ execute the given calls. If None or not given then as many
421
+ worker processes will be created as the machine has processors.
422
+ timeout: The number of seconds to wait for the result if the future isn't done.
423
+ If None, then there is no limit on the wait time.
424
+ """
425
+ keys: range = range(1, self.__max_num_keys)
426
+ search_task_fn: Callable = self._task_find
427
+ length_reduction_hash: int = self.__length_reduction_hash
428
+ db_root: str = self.__db_root
429
+ class_model: T = self.__class_model
430
+ counter: int = 0
431
+ with concurrent.futures.ThreadPoolExecutor(max_workers) as executor:
432
+ for key in keys:
433
+ future = executor.submit(
434
+ search_task_fn,
435
+ key,
436
+ filter_fn,
437
+ length_reduction_hash,
438
+ db_root,
439
+ class_model,
440
+ )
441
+ if future.result(timeout) is not None:
442
+ counter += 1
443
+ return counter
scruby/errors.py ADDED
@@ -0,0 +1,20 @@
1
+ """XLOT Exceptions."""
2
+
3
+ from __future__ import annotations
4
+
5
+ __all__ = ("MetadataValueError",)
6
+
7
+
8
+ class ScrubyException(Exception):
9
+ """Root Custom Exception."""
10
+
11
+ def __init__(self, *args, **kwargs) -> None: # type: ignore[no-untyped-def]
12
+ super().__init__(*args, **kwargs)
13
+
14
+
15
+ class MetadataValueError(ScrubyException):
16
+ """Exception is raised if value of variable in metadata does not matching expected."""
17
+
18
+ def __init__(self, message: str) -> None: # noqa: D107
19
+ self.message = message
20
+ super().__init__(self.message)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scruby
3
- Version: 0.10.1
3
+ Version: 0.10.3
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
@@ -0,0 +1,9 @@
1
+ scruby/__init__.py,sha256=wFwUS1KcLxfIopXOVS8gPue9fNzIIU2cVj_RgK5drz4,849
2
+ scruby/constants.py,sha256=GbB-O0qaVdi5EHUp-zRAppFXLR-oHxpXUFVAOCpS0C8,1022
3
+ scruby/db.py,sha256=J14Xjyc6iyb-cwBKiH8rJuioEHoYfNkLTezzvQBsJng,16181
4
+ scruby/errors.py,sha256=4G0zNVzulBE9mM2iJLdg0EXP_W8n-L6EjZrkCCErvAU,574
5
+ scruby/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ scruby-0.10.3.dist-info/METADATA,sha256=JGgVH8QKtA-iGifWhNdSczfuglIT2RRw5njRuNKvG3M,10829
7
+ scruby-0.10.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
+ scruby-0.10.3.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
9
+ scruby-0.10.3.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- scruby/__init__.py,sha256=wFwUS1KcLxfIopXOVS8gPue9fNzIIU2cVj_RgK5drz4,849
2
- scruby/constants.py,sha256=GbB-O0qaVdi5EHUp-zRAppFXLR-oHxpXUFVAOCpS0C8,1022
3
- scruby/db.py,sha256=d4fVb4JhGaBU_YPxByT156UGHQPTwzSizBqOpCNhW8Y,14159
4
- scruby/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- scruby-0.10.1.dist-info/METADATA,sha256=k2AaJn-JijNceNIB7s13D1cB3jr4p4mW6R1z7IuXp4g,10829
6
- scruby-0.10.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
- scruby-0.10.1.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
8
- scruby-0.10.1.dist-info/RECORD,,