Skip to content

Commit 92dc439

Browse files
committed
improve code clarity
1 parent ebcbe53 commit 92dc439

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

dictdatabase/io_unsafe.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,17 @@ def read(db_name: str) -> dict:
4646
########################################################################################
4747

4848

49-
def try_read_bytes_by_index(indexer: indexing.Indexer, db_name, key):
49+
def try_read_bytes_using_indexer(indexer: indexing.Indexer, db_name: str, key: str) -> bytes | None:
50+
"""
51+
Check if the key info is saved in the file's index file.
52+
If it is and the value has not changed, return the value bytes.
53+
Otherwise return None.
54+
"""
55+
5056
if (index := indexer.get(key)) is None:
5157
return None
52-
start_index, end_index, _, _, value_hash = index
53-
partial_bytes = io_bytes.read(db_name, start_index, end_index)
58+
start, end, _, _, value_hash = index
59+
partial_bytes = io_bytes.read(db_name, start, end)
5460
if value_hash != hashlib.sha256(partial_bytes).hexdigest():
5561
return None
5662
return partial_bytes
@@ -68,27 +74,26 @@ def partial_read_only(db_name: str, key: str) -> dict | None:
6874

6975
# Search for key in the index file
7076
indexer = indexing.Indexer(db_name)
71-
if (value_bytes := try_read_bytes_by_index(indexer, db_name, key)) is not None:
77+
if (value_bytes := try_read_bytes_using_indexer(indexer, db_name, key)) is not None:
7278
return orjson.loads(value_bytes)
7379

7480
# Not found in index file, search for key in the entire file
75-
file_bytes = io_bytes.read(db_name)
76-
key_start, key_end = utils.find_outermost_key_in_json_bytes(file_bytes, key)
81+
all_file_bytes = io_bytes.read(db_name)
82+
key_start, key_end = utils.find_outermost_key_in_json_bytes(all_file_bytes, key)
7783

7884
if key_end == -1:
7985
return None
8086

81-
# Key found, now determine the bounds of the value
82-
value_start = key_end + (1 if file_bytes[key_end] == byte_codes.SPACE else 0)
83-
value_end = utils.seek_index_through_value_bytes(file_bytes, value_start)
87+
# Key found, now determine the bounding byte indices of the value
88+
start = key_end + (1 if all_file_bytes[key_end] == byte_codes.SPACE else 0)
89+
end = utils.seek_index_through_value_bytes(all_file_bytes, start)
8490

85-
indent_level, indent_with = utils.detect_indentation_in_json_bytes(file_bytes, key_start)
86-
value_bytes = file_bytes[value_start:value_end]
91+
indent_level, indent_with = utils.detect_indentation_in_json_bytes(all_file_bytes, key_start)
92+
value_bytes = all_file_bytes[start:end]
8793
value_hash = hashlib.sha256(value_bytes).hexdigest()
8894

8995
# Write key info to index file
90-
91-
indexer.write(key, value_start, value_end, indent_level, indent_with, value_hash, value_end)
96+
indexer.write(key, start, end, indent_level, indent_with, value_hash, end)
9297
return orjson.loads(value_bytes)
9398

9499

@@ -134,36 +139,35 @@ def try_get_parial_file_handle_by_index(indexer: indexing.Indexer, db_name, key)
134139
If the data could not be read from the index file, a tuple of None and the file
135140
bytes is returned, so that the file bytes can be searched for the key.
136141
"""
142+
137143
if (index := indexer.get(key)) is None:
138144
return None, io_bytes.read(db_name)
139-
value_start, value_end, indent_level, indent_with, value_hash = index
145+
start, end, indent_level, indent_with, value_hash = index
140146

141147
# If compression is enabled, all data has to be read from the file
142148
if config.use_compression:
143-
data_bytes = io_bytes.read(db_name)
144-
value_bytes = data_bytes[value_start:value_end]
149+
all_file_bytes = io_bytes.read(db_name)
150+
value_bytes = all_file_bytes[start:end]
145151
if value_hash != hashlib.sha256(value_bytes).hexdigest():
146-
return None, data_bytes
152+
return None, all_file_bytes
147153
value_data = orjson.loads(value_bytes)
148-
partial_dict = PartialDict(data_bytes[:value_start], key, value_data, value_start, value_end, data_bytes[value_end:])
154+
partial_dict = PartialDict(all_file_bytes[:start], key, value_data, start, end, all_file_bytes[end:])
149155

150156
# If compression is disabled, only the value and suffix have to be read
151157
else:
152-
value_and_suffix_bytes = io_bytes.read(db_name, value_start)
153-
value_length = value_end - value_start
158+
value_and_suffix_bytes = io_bytes.read(db_name, start)
159+
value_length = end - start
154160
value_bytes = value_and_suffix_bytes[:value_length]
155161
if value_hash != hashlib.sha256(value_bytes).hexdigest():
156162
# If the hashes don't match, read the prefix to concat the full file bytes
157-
prefix_bytes = io_bytes.read(db_name, 0, value_start)
163+
prefix_bytes = io_bytes.read(db_name, 0, start)
158164
return None, prefix_bytes + value_and_suffix_bytes
159165
value_data = orjson.loads(value_bytes)
160-
partial_dict = PartialDict(None, key, value_data, value_start, value_end, value_and_suffix_bytes[value_length:])
166+
partial_dict = PartialDict(None, key, value_data, start, end, value_and_suffix_bytes[value_length:])
161167

162168
return PartialFileHandle(db_name, partial_dict, indent_level, indent_with, indexer), None
163169

164170

165-
166-
167171
def get_partial_file_handle(db_name: str, key: str) -> PartialFileHandle:
168172
"""
169173
Partially read a key from a db.
@@ -176,25 +180,25 @@ def get_partial_file_handle(db_name: str, key: str) -> PartialFileHandle:
176180

177181
# Search for key in the index file
178182
indexer = indexing.Indexer(db_name)
179-
partial_handle, data_bytes = try_get_parial_file_handle_by_index(indexer, db_name, key)
183+
partial_handle, all_file_bytes = try_get_parial_file_handle_by_index(indexer, db_name, key)
180184
if partial_handle is not None:
181185
return partial_handle
182186

183187
# Not found in index file, search for key in the entire file
184-
key_start, key_end = utils.find_outermost_key_in_json_bytes(data_bytes, key)
188+
key_start, key_end = utils.find_outermost_key_in_json_bytes(all_file_bytes, key)
185189

186190
if key_end == -1:
187191
raise KeyError(f"Key \"{key}\" not found in db \"{db_name}\"")
188192

189-
# Key found, now determine the bounds of the value
190-
value_start = key_end + (1 if data_bytes[key_end] == byte_codes.SPACE else 0)
191-
value_end = utils.seek_index_through_value_bytes(data_bytes, value_start)
193+
# Key found, now determine the bounding byte indices of the value
194+
start = key_end + (1 if all_file_bytes[key_end] == byte_codes.SPACE else 0)
195+
end = utils.seek_index_through_value_bytes(all_file_bytes, start)
192196

193-
indent_level, indent_with = utils.detect_indentation_in_json_bytes(data_bytes, key_start)
197+
indent_level, indent_with = utils.detect_indentation_in_json_bytes(all_file_bytes, key_start)
194198

195-
partial_value = orjson.loads(data_bytes[value_start:value_end])
196-
prefix_bytes = data_bytes[:value_start] if config.use_compression else None
197-
partial_dict = PartialDict(prefix_bytes, key, partial_value, value_start, value_end, data_bytes[value_end:])
199+
partial_value = orjson.loads(all_file_bytes[start:end])
200+
prefix_bytes = all_file_bytes[:start] if config.use_compression else None
201+
partial_dict = PartialDict(prefix_bytes, key, partial_value, start, end, all_file_bytes[end:])
198202
return PartialFileHandle(db_name, partial_dict, indent_level, indent_with, indexer)
199203

200204

dictdatabase/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def find_outermost_key_in_json_bytes(json_bytes: bytes, key: str):
124124
represented as bytes.
125125
126126
Returns:
127-
- A tuple of the key start and end index, or (-1, -1) if the key is not found.
127+
- A tuple of the key start and end index, or `(-1, -1)` if the key is not found.
128128
"""
129129
key = f"\"{key}\":".encode()
130130

0 commit comments

Comments
 (0)