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