-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from mkrd/improve-partial
Improve partial
- Loading branch information
Showing
8 changed files
with
171 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import orjson | ||
import os | ||
from . import config | ||
|
||
|
||
class Indexer: | ||
|
||
def __init__(self, db_name: str): | ||
db_name = db_name.replace("/", "___") | ||
path = os.path.join(config.storage_directory, ".ddb", f"{db_name}.index") | ||
self.path = path | ||
os.makedirs(os.path.dirname(path), exist_ok=True) | ||
if not os.path.exists(self.path): | ||
self.data = {} | ||
else: | ||
with open(self.path, "rb") as f: | ||
self.data = orjson.loads(f.read()) | ||
|
||
def get(self, key): | ||
return self.data.get(key, None) | ||
|
||
def write(self, key, start_index, end_index, indent_level, indent_with, value_hash): | ||
self.data[key] = [start_index, end_index, indent_level, indent_with, value_hash] | ||
with open(self.path, "wb") as f: | ||
f.write(orjson.dumps(self.data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import zlib | ||
import os | ||
from . import config, utils | ||
|
||
|
||
def read(db_name: str, start=None, end=None) -> bytes: | ||
""" | ||
Read the content of a db as as bytes. Reading works even when the config | ||
changes, so a compressed ddb file can also be read if compression is | ||
disabled, and vice versa. | ||
""" | ||
json_path, json_exists, ddb_path, ddb_exists = utils.db_paths(db_name) | ||
|
||
if json_exists: | ||
if ddb_exists: | ||
raise FileExistsError(f"DB Inconsistency: \"{db_name}\" exists as .json and .ddb") | ||
with open(json_path, "rb") as f: | ||
if start is not None and end is not None: | ||
f.seek(start) | ||
return f.read(end - start) | ||
return f.read() | ||
if not ddb_exists: | ||
raise FileNotFoundError(f"DB does not exist: \"{db_name}\"") | ||
with open(ddb_path, "rb") as f: | ||
json_bytes = zlib.decompress(f.read()) | ||
if start is not None and end is not None: | ||
return json_bytes[start:end] | ||
return json_bytes | ||
|
||
|
||
def write(db_name: str, dump: bytes): | ||
""" | ||
Write the bytes to the file of the db_path. | ||
If the db was compressed but now config.use_compression is False, | ||
remove the compressed file, and vice versa. | ||
""" | ||
json_path, json_exists, ddb_path, ddb_exists = utils.db_paths(db_name) | ||
# Write bytes or string to file | ||
remove_this = None | ||
if config.use_compression: | ||
write_path = ddb_path | ||
if json_exists: | ||
remove_this = json_path | ||
dump = zlib.compress(dump, 1) | ||
else: | ||
write_path = json_path | ||
if ddb_exists: | ||
remove_this = ddb_path | ||
|
||
# Write bytes or string to file | ||
with open(write_path, "wb") as f: | ||
f.write(dump) | ||
|
||
# Remove the other file if it exists | ||
# This is done after writing to avoid data loss | ||
if remove_this is not None: | ||
os.remove(remove_this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.