|
1 | 1 | from typing import Tuple |
2 | 2 |
|
| 3 | +import orjson |
| 4 | + |
3 | 5 | from dictdatabase import byte_codes |
4 | 6 | from dictdatabase import utils |
5 | 7 |
|
6 | 8 |
|
7 | | -class KeySearcher: |
8 | | - @staticmethod |
9 | | - def find_start_end_in_bytes(file: bytes, key: str) -> Tuple[int, int, bool]: |
10 | | - """ |
11 | | - It finds the start and end indices of the value of a key in a JSON file |
| 9 | +def find_start_end_in_bytes(file: bytes, key: str) -> Tuple[int, int, bool]: |
| 10 | + """ |
| 11 | + It finds the start and end indices of the value of a key in a JSON file |
| 12 | +
|
| 13 | + Args: |
| 14 | + file (bytes): bytes |
| 15 | + key (str): The key to find in the JSON file. |
12 | 16 |
|
13 | | - Args: |
14 | | - file (bytes): bytes |
15 | | - key (str): The key to find in the JSON file. |
| 17 | + Returns: |
| 18 | + A tuple of the start and end index of the key, and a boolean value indicating whether the key was found. |
| 19 | + """ |
| 20 | + key_start, key_end = utils.find_outermost_key_in_json_bytes(file, key) |
| 21 | + if key_end == -1: |
| 22 | + return -1, -1, False |
| 23 | + start = key_end + (1 if file[key_end] == byte_codes.SPACE else 0) |
| 24 | + end = utils.seek_index_through_value_bytes(file, start) |
| 25 | + return start, end, True |
16 | 26 |
|
17 | | - Returns: |
18 | | - A tuple of the start and end index of the key, and a boolean value indicating whether the key was found. |
19 | | - """ |
20 | | - key_start, key_end = utils.find_outermost_key_in_json_bytes(file, key) |
| 27 | + |
| 28 | +def search_key(file: bytes, key: str, glom_searching=True) -> Tuple[int, int, bool]: |
| 29 | + original_value_start = 0 |
| 30 | + original_value_end = len(file) |
| 31 | + original_key_start = 0 |
| 32 | + original_key_end = len(file) |
| 33 | + for k in key.split(".") if glom_searching else [key]: |
| 34 | + key_start, key_end = utils.find_outermost_key_in_json_bytes(file, k) |
21 | 35 | if key_end == -1: |
22 | 36 | return -1, -1, False |
23 | | - start = key_end + (1 if file[key_end] == byte_codes.SPACE else 0) |
24 | | - end = utils.seek_index_through_value_bytes(file, start) |
25 | | - return start, end, True |
26 | | - |
27 | | - def search( |
28 | | - self, all_file_bytes: bytes, key: str, glom_searching=True |
29 | | - ) -> Tuple[int, int, bool]: |
30 | | - """ |
31 | | - It takes a byte string, a key, and a boolean, and returns a tuple of three integers |
32 | | -
|
33 | | - Args: |
34 | | - all_file_bytes (bytes): The bytes of the file you're searching in. |
35 | | - key (str): The key to search for. |
36 | | - glom_searching: If True, then the key is a glom path, and we need to search for each part of the path. Defaults to |
37 | | - True |
38 | | -
|
39 | | - Returns: |
40 | | - The start and end of the key in the file. |
41 | | - """ |
42 | | - original_start = 0 |
43 | | - original_end = len(all_file_bytes) |
44 | | - for k in key.split(".") if glom_searching else [key]: |
45 | | - start, end, found = self.find_start_end_in_bytes( |
46 | | - all_file_bytes[original_start:original_end], k |
47 | | - ) |
48 | | - if not found: |
49 | | - return -1, -1, False |
50 | | - original_end = original_start + end |
51 | | - original_start += start |
52 | | - return original_start, original_end, True |
| 37 | + original_key_end = original_value_start + key_end |
| 38 | + original_key_start = original_value_start + key_start |
| 39 | + value_start, value_end, found = find_start_end_in_bytes(file, k) |
| 40 | + original_value_end = original_value_start + original_value_end |
| 41 | + original_value_start += value_start |
| 42 | + file = file[original_value_start:original_value_end] |
| 43 | + return original_key_start, original_key_end, True |
| 44 | + |
| 45 | + |
| 46 | +def search_value_by_key( |
| 47 | + all_file_bytes: bytes, key: str, glom_searching=True |
| 48 | +) -> Tuple[int, int, bool]: |
| 49 | + """ |
| 50 | + It takes a byte string, a key, and a boolean, and returns a tuple of three integers |
| 51 | +
|
| 52 | + Args: |
| 53 | + all_file_bytes (bytes): The bytes of the file you're searching in. |
| 54 | + key (str): The key to search for. |
| 55 | + glom_searching: If True, then the key is a glom path, and we need to search for each part of the path. Defaults to |
| 56 | + True |
| 57 | +
|
| 58 | + Returns: |
| 59 | + The start and end of the key in the file. |
| 60 | + """ |
| 61 | + original_start = 0 |
| 62 | + original_end = len(all_file_bytes) |
| 63 | + for k in key.split(".") if glom_searching else [key]: |
| 64 | + start, end, found = find_start_end_in_bytes( |
| 65 | + all_file_bytes[original_start:original_end], k |
| 66 | + ) |
| 67 | + if not found: |
| 68 | + return -1, -1, False |
| 69 | + original_end = original_start + end |
| 70 | + original_start += start |
| 71 | + return original_start, original_end, True |
0 commit comments