|
3 | 3 | import orjson
|
4 | 4 |
|
5 | 5 | from . import config
|
| 6 | +from .dataclasses import Index |
6 | 7 |
|
7 | 8 |
|
8 | 9 | # Problem: Multiple read processes will concurrently read and write the same file
|
|
25 | 26 |
|
26 | 27 |
|
27 | 28 | class Indexer:
|
28 |
| - """ |
29 |
| - The Indexer takes the name of a database file, and tries to load the .index file |
30 |
| - of the corresponding database file. |
31 |
| -
|
32 |
| - The name of the index file is the name of the database file, with the extension |
33 |
| - .index and all "/" replaced with "___" |
34 |
| -
|
35 |
| - The content of the index file is a json object, where the keys are keys inside |
36 |
| - the database json file, and the values are lists of 5 elements: |
37 |
| - - start_index: The index of the first byte of the value of the key in the database file |
38 |
| - - end_index: The index of the last byte of the value of the key in the database file |
39 |
| - - indent_level: The indent level of the key in the database file |
40 |
| - - indent_with: The indent string used. |
41 |
| - - value_hash: The hash of the value bytes |
42 |
| - """ |
43 |
| - |
44 |
| - __slots__ = ("data", "path") |
45 |
| - |
46 |
| - def __init__(self, db_name: str): |
47 |
| - # Make path of index file |
48 |
| - db_name = db_name.replace("/", "___") |
49 |
| - self.path = os.path.join(config.storage_directory, ".ddb", f"{db_name}.index") |
50 |
| - |
51 |
| - os.makedirs(os.path.dirname(self.path), exist_ok=True) |
52 |
| - if not os.path.exists(self.path): |
53 |
| - self.data = {} |
54 |
| - return |
55 |
| - |
56 |
| - try: |
57 |
| - with open(self.path, "rb") as f: |
58 |
| - self.data = orjson.loads(f.read()) |
59 |
| - except orjson.JSONDecodeError: |
60 |
| - self.data = {} |
61 |
| - |
62 |
| - |
63 |
| - def get(self, key): |
64 |
| - """ |
65 |
| - Returns a list of 5 elements for a key if it exists, otherwise None |
66 |
| - Elements:[start_index, end_index, indent_level, indent_with, value_hash] |
67 |
| - """ |
68 |
| - return self.data.get(key, None) |
69 |
| - |
70 |
| - |
71 |
| - def write(self, key, start_index, end_index, indent_level, indent_with, value_hash, old_value_end): |
72 |
| - """ |
73 |
| - Write index information for a key to the index file |
74 |
| - """ |
75 |
| - |
76 |
| - if self.data.get(key, None) is not None: |
77 |
| - delta = end_index - old_value_end |
78 |
| - for entry in self.data.values(): |
79 |
| - if entry[0] > old_value_end: |
80 |
| - entry[0] += delta |
81 |
| - entry[1] += delta |
82 |
| - |
83 |
| - self.data[key] = [start_index, end_index, indent_level, indent_with, value_hash] |
84 |
| - with open(self.path, "wb") as f: |
85 |
| - f.write(orjson.dumps(self.data)) |
| 29 | + """ |
| 30 | + The Indexer takes the name of a database file, and tries to load the .index file |
| 31 | + of the corresponding database file. |
| 32 | +
|
| 33 | + The name of the index file is the name of the database file, with the extension |
| 34 | + .index and all "/" replaced with "___" |
| 35 | +
|
| 36 | + The content of the index file is a json object, where the keys are keys inside |
| 37 | + the database json file, and the values are lists of 5 elements: |
| 38 | + - start_index: The index of the first byte of the value of the key in the database file |
| 39 | + - end_index: The index of the last byte of the value of the key in the database file |
| 40 | + - indent_level: The indent level of the key in the database file |
| 41 | + - indent_with: The indent string used. |
| 42 | + - value_hash: The hash of the value bytes |
| 43 | + """ |
| 44 | + |
| 45 | + __slots__ = ("data", "path") |
| 46 | + |
| 47 | + def __init__(self, db_name: str): |
| 48 | + # Make path of index file |
| 49 | + db_name = db_name.replace("/", "___") |
| 50 | + self.path = os.path.join(config.storage_directory, ".ddb", f"{db_name}.index") |
| 51 | + |
| 52 | + os.makedirs(os.path.dirname(self.path), exist_ok=True) |
| 53 | + if not os.path.exists(self.path): |
| 54 | + self.data = {} |
| 55 | + return |
| 56 | + |
| 57 | + try: |
| 58 | + with open(self.path, "rb") as f: |
| 59 | + self.data = orjson.loads(f.read()) |
| 60 | + except orjson.JSONDecodeError: |
| 61 | + self.data = {} |
| 62 | + |
| 63 | + def get(self, key): |
| 64 | + """ |
| 65 | + Returns a list of 5 elements for a key if it exists, otherwise None |
| 66 | + Elements:[start_index, end_index, indent_level, indent_with, value_hash] |
| 67 | + """ |
| 68 | + return self.data.get(key, None) |
| 69 | + |
| 70 | + def write(self, index: Index): |
| 71 | + """ |
| 72 | + Write index information for a key to the index file |
| 73 | + """ |
| 74 | + |
| 75 | + if self.data.get(index.key, None) is not None: |
| 76 | + delta = index.key_end - index.old_value_end |
| 77 | + for entry in self.data.values(): |
| 78 | + if entry[0] > index.old_value_end: |
| 79 | + entry[0] += delta |
| 80 | + entry[1] += delta |
| 81 | + |
| 82 | + self.data[index.key] = [ |
| 83 | + index.key_start, |
| 84 | + index.key_end, |
| 85 | + index.indent_level, |
| 86 | + index.indent_with, |
| 87 | + index.value_hash, |
| 88 | + ] |
| 89 | + with open(self.path, "wb") as f: |
| 90 | + f.write(orjson.dumps(self.data)) |
0 commit comments