scruby 0.5.1__py3-none-any.whl → 0.6.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.
scruby/__init__.py CHANGED
@@ -1,13 +1,17 @@
1
1
  """A fast key-value storage library.
2
2
 
3
- Scruby is a fast key-value storage library that provides an ordered mapping from string keys to string values.
3
+ Scruby is a fast key-value storage asynchronous library that provides an
4
+ ordered mapping from string keys to string values.
4
5
  The library uses fractal-tree addressing.
5
6
 
6
- The maximum size of the database is 16**32=340282366920938463463374607431768211456 branches,
7
+ The database consists of collections.
8
+ The maximum size of the one collection is 16**8=4294967296 branches,
7
9
  each branch can store one or more keys.
8
10
 
9
- The value of any key can be obtained in 32 steps, thereby achieving high performance.
10
- There is no need to iterate through all the keys in search of the desired value.
11
+ The value of any key in collection can be obtained in 8 steps,
12
+ thereby achieving high performance.
13
+
14
+ In the future, to search by value of key, the use of a quantum loop is supposed.
11
15
  """
12
16
 
13
17
  from __future__ import annotations
scruby/db.py CHANGED
@@ -4,7 +4,8 @@ from __future__ import annotations
4
4
 
5
5
  __all__ = ("Scruby",)
6
6
 
7
- import hashlib
7
+ import contextlib
8
+ import zlib
8
9
  from shutil import rmtree
9
10
  from typing import TypeVar
10
11
 
@@ -35,13 +36,21 @@ class Scruby[T]:
35
36
  Args:
36
37
  key: Key name.
37
38
  """
38
- # Key to md5 sum.
39
- key_md5: str = hashlib.md5(key.encode("utf-8")).hexdigest() # noqa: S324
40
- # Convert md5 sum in the segment of path.
41
- separated_md5: str = "/".join(list(key_md5))
39
+ if not isinstance(key, str):
40
+ raise KeyError("The key is not a type of `str`.")
41
+ if len(key) == 0:
42
+ raise KeyError("The key should not be empty.")
43
+ # Key to adler32 sum.
44
+ key_adler32: str = f"{zlib.adler32(key.encode('utf-8')):08x}"
45
+ # Convert adler32 sum in the segment of path.
46
+ separated_adler32: str = "/".join(list(key_adler32))
42
47
  # The path of the branch to the database.
43
48
  branch_path: Path = Path(
44
- *(constants.DB_ROOT, self.__class_model.__name__, separated_md5),
49
+ *(
50
+ constants.DB_ROOT,
51
+ self.__class_model.__name__,
52
+ separated_adler32,
53
+ ),
45
54
  )
46
55
  # If the branch does not exist, need to create it.
47
56
  if not await branch_path.exists():
@@ -127,7 +136,8 @@ class Scruby[T]:
127
136
  return
128
137
  raise KeyError()
129
138
 
130
- async def napalm(self) -> None:
139
+ @classmethod
140
+ async def napalm(cls) -> None:
131
141
  """Asynchronous method for full database deletion (Arg: db_name).
132
142
 
133
143
  The main purpose is tests.
@@ -135,5 +145,6 @@ class Scruby[T]:
135
145
  Warning:
136
146
  - `Be careful, this will remove all keys.`
137
147
  """
138
- await to_thread.run_sync(rmtree, constants.DB_ROOT)
148
+ with contextlib.suppress(FileNotFoundError):
149
+ await to_thread.run_sync(rmtree, constants.DB_ROOT)
139
150
  return
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scruby
3
- Version: 0.5.1
3
+ Version: 0.6.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
@@ -65,16 +65,18 @@ Description-Content-Type: text/markdown
65
65
  <a href="https://github.com/kebasyaty/scruby/releases/" alt="GitHub release"><img src="https://img.shields.io/github/release/kebasyaty/scruby" alt="GitHub release"></a>
66
66
  </p>
67
67
  <p align="center">
68
- Scruby is a fast key-value storage library that provides an ordered mapping from string keys to string values.
68
+ Scruby is a fast key-value storage asynchronous library that provides an ordered mapping from string keys to string values.
69
69
  <br>
70
70
  The library uses fractal-tree addressing.
71
71
  <br>
72
- The maximum size of the database is 16**32=340282366920938463463374607431768211456 branches,
72
+ The database consists of collections.
73
+ <br>
74
+ The maximum size of the one collection is 16**8=4294967296 branches,
73
75
  each branch can store one or more keys.
74
76
  <br>
75
- The value of any key can be obtained in 32 steps, thereby achieving high performance.
77
+ The value of any key in collection can be obtained in 8 steps, thereby achieving high performance.
76
78
  <br>
77
- There is no need to iterate through all the keys in search of the desired value.
79
+ In the future, to search by value of key, the use of a quantum loop is supposed.
78
80
  </p>
79
81
  </p>
80
82
  </div>
@@ -106,7 +108,6 @@ constants.DB_ROOT = "ScrubyDB" # By default = "ScrubyDB"
106
108
 
107
109
  class User(BaseModel):
108
110
  """Model of User."""
109
-
110
111
  first_name: str
111
112
  last_name: str
112
113
  birthday: datetime.datetime
@@ -115,7 +116,8 @@ class User(BaseModel):
115
116
 
116
117
  async def main() -> None:
117
118
  """Example."""
118
- db = Scruby(User)
119
+ # Get collection of `User`.
120
+ user_coll = Scruby(User)
119
121
 
120
122
  user = User(
121
123
  first_name="John",
@@ -125,23 +127,21 @@ async def main() -> None:
125
127
  phone="+447986123456",
126
128
  )
127
129
 
128
- await db.set_key("+447986123456", user)
130
+ await user_coll.set_key("+447986123456", user)
129
131
 
130
- await db.get_key("+447986123456") # => user
131
- await db.get_key("key missing") # => KeyError
132
+ await user_coll.get_key("+447986123456") # => user
133
+ await user_coll.get_key("key missing") # => KeyError
132
134
 
133
- await db.has_key("+447986123456") # => True
134
- await db.has_key("key missing") # => False
135
+ await user_coll.has_key("+447986123456") # => True
136
+ await user_coll.has_key("key missing") # => False
135
137
 
136
- await db.delete_key("+447986123456")
137
- await db.delete_key("+447986123456") # => KeyError
138
- await db.delete_key("key missing") # => KeyError
138
+ await user_coll.delete_key("+447986123456")
139
+ await user_coll.delete_key("+447986123456") # => KeyError
140
+ await user_coll.delete_key("key missing") # => KeyError
139
141
 
140
142
  # Full database deletion.
141
143
  # Hint: The main purpose is tests.
142
- await db.napalm()
143
- await db.napalm() # => FileNotFoundError
144
-
144
+ await Scruby.napalm()
145
145
 
146
146
  if __name__ == "__main__":
147
147
  anyio.run(main)
@@ -0,0 +1,8 @@
1
+ scruby/__init__.py,sha256=Bhg7pIV6u0YM7Dipr7vH7ka931bVuBoJugiYNd73HQ4,704
2
+ scruby/constants.py,sha256=kwF0FIbeChBxsNxOCQhMsDEn1lakD7MIQKJ-PHYeSAo,328
3
+ scruby/db.py,sha256=qhVoSMu-MDiIdPK4M3ruEZDXxcvKBPu-AS-J_xSNZE4,4763
4
+ scruby/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ scruby-0.6.1.dist-info/METADATA,sha256=qzdPamjqOufrJ7hibG37MaiUV27RAIcPikn9idMIYzg,6786
6
+ scruby-0.6.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ scruby-0.6.1.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
8
+ scruby-0.6.1.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- scruby/__init__.py,sha256=LnHBN1pIOtT89baSQoknQwYI1cy-hmN1Lo0k8o1Ms48,659
2
- scruby/constants.py,sha256=kwF0FIbeChBxsNxOCQhMsDEn1lakD7MIQKJ-PHYeSAo,328
3
- scruby/db.py,sha256=iG1D4-ncVrVysp7OXH-eZksnNacjNna4_8nUbMkWnSE,4409
4
- scruby/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- scruby-0.5.1.dist-info/METADATA,sha256=47mNNYczCbwwTVMyJRoNIn03fQ2bnVNh8ynuVCm-P00,6678
6
- scruby-0.5.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
- scruby-0.5.1.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
8
- scruby-0.5.1.dist-info/RECORD,,
File without changes