Skip to content

Commit fc1d8b7

Browse files
Merge pull request #2 from UmbrellaMalware/refactoring
Refactoring
2 parents 8512ce9 + c8918e5 commit fc1d8b7

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

dictdatabase/dataclasses.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import dataclasses
2+
3+
4+
@dataclasses.dataclass(frozen=True)
5+
class SearchResult:
6+
start_byte: int
7+
end_byte: int
8+
found: bool

dictdatabase/io_unsafe.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,16 @@ def get_partial_file_handle(db_name: str, key: str) -> PartialFileHandle:
185185
return partial_handle
186186

187187
# Not found in index file, search for key in the entire file
188-
key_start, key_end, found = searching.search_key_position_in_db(all_file_bytes, key)
188+
position = searching.search_key_position_in_db(all_file_bytes, key)
189189

190-
if not found:
190+
if not position.found:
191191
raise KeyError(f"Key \"{key}\" not found in db \"{db_name}\"")
192192

193193
# 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)
194+
start = position.end_byte + (1 if all_file_bytes[position.end_byte] == byte_codes.SPACE else 0)
195195
end = utils.seek_index_through_value_bytes(all_file_bytes, start)
196196

197-
indent_level, indent_with = utils.detect_indentation_in_json_bytes(all_file_bytes, key_start)
197+
indent_level, indent_with = utils.detect_indentation_in_json_bytes(all_file_bytes, position.start_byte)
198198

199199
partial_value = orjson.loads(all_file_bytes[start:end])
200200
prefix_bytes = all_file_bytes[:start] if config.use_compression else None

dictdatabase/searching.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
from dictdatabase import byte_codes
66
from dictdatabase import utils
7+
from dictdatabase.dataclasses import SearchResult
78

89

9-
def find_key_position_in_bytes(file: bytes, key: str) -> Tuple[int, int, bool]:
10+
def find_key_position_in_bytes(file: bytes, key: str) -> SearchResult:
1011
"""
1112
It finds the start and end indices of the value of a key in a JSON file
1213
@@ -19,28 +20,30 @@ def find_key_position_in_bytes(file: bytes, key: str) -> Tuple[int, int, bool]:
1920
"""
2021
key_start, key_end = utils.find_outermost_key_in_json_bytes(file, key)
2122
if key_end == -1:
22-
return -1, -1, False
23+
return SearchResult(start_byte=-1, end_byte=-1, found=False)
2324
start = key_end + (1 if file[key_end] == byte_codes.SPACE else 0)
2425
end = utils.seek_index_through_value_bytes(file, start)
25-
return start, end, True
26+
return SearchResult(start_byte=start, end_byte=end, found=True)
2627

2728

28-
def search_key_position_in_db(file: bytes, key: str, glom_searching=True) -> Tuple[int, int, bool]:
29+
def search_key_position_in_db(
30+
file: bytes, key: str, glom_searching=True
31+
) -> SearchResult:
2932
original_value_start = 0
3033
original_value_end = len(file)
3134
original_key_start = 0
3235
original_key_end = len(file)
3336
for k in key.split(".") if glom_searching else [key]:
3437
key_start, key_end = utils.find_outermost_key_in_json_bytes(file, k)
3538
if key_end == -1:
36-
return -1, -1, False
39+
return SearchResult(start_byte=-1, end_byte=-1, found=False)
3740
original_key_end = original_value_start + key_end
3841
original_key_start = original_value_start + key_start
39-
value_start, value_end, found = find_key_position_in_bytes(file, k)
42+
position = find_key_position_in_bytes(file, k)
4043
original_value_end = original_value_start + original_value_end
41-
original_value_start += value_start
44+
original_value_start += position.start_byte
4245
file = file[original_value_start:original_value_end]
43-
return original_key_start, original_key_end, True
46+
return SearchResult(start_byte=original_key_start, end_byte=original_key_end, found=True)
4447

4548

4649
def search_value_position_in_db(
@@ -61,11 +64,11 @@ def search_value_position_in_db(
6164
original_start = 0
6265
original_end = len(all_file_bytes)
6366
for k in key.split(".") if glom_searching else [key]:
64-
start, end, found = find_key_position_in_bytes(
67+
position = find_key_position_in_bytes(
6568
all_file_bytes[original_start:original_end], k
6669
)
67-
if not found:
70+
if not position.found:
6871
return -1, -1, False
69-
original_end = original_start + end
70-
original_start += start
72+
original_end = original_start + position.end_byte
73+
original_start += position.start_byte
7174
return original_start, original_end, True

tests/test_glom_writing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
import dictdatabase as DDB
24

35
data = {
@@ -15,3 +17,11 @@ def test_glom_writing():
1517
purchase["status"] = "cancelled"
1618
session.write()
1719
assert DDB.at("users", key="users.Ben.status").read() == "cancelled"
20+
21+
22+
def test_glom_writing_sub_key_not_exists():
23+
DDB.at("users").create(data, force_overwrite=True)
24+
with pytest.raises(KeyError):
25+
with DDB.at("users", key="users.SUBKEY").session() as (session, purchase):
26+
purchase["status"] = "cancelled"
27+
session.write()

0 commit comments

Comments
 (0)