feat: create backend skeleton
This commit is contained in:
1 parent
e08d26dd10
commit
62aac0bf32
37 files changed
+1591
-3
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class ContentAddressedStorage:
|
||||
def __init__(self, root: str | Path, shard_depth: int = 2) -> None:
|
||||
self._root = Path(root)
|
||||
self._shard_depth = shard_depth
|
||||
|
||||
def _path(self, key: str) -> Path:
|
||||
shards = [key[i * 2 : i * 2 + 2] for i in range(self._shard_depth)]
|
||||
return self._root.joinpath(*shards, key)
|
||||
|
||||
def put(self, data: bytes) -> str:
|
||||
key = hashlib.sha256(data).hexdigest()
|
||||
path = self._path(key)
|
||||
if not path.exists():
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes(data)
|
||||
return key
|
||||
|
||||
def get(self, key: str) -> bytes:
|
||||
return self._path(key).read_bytes()
|
||||
|
||||
def exists(self, key: str) -> bool:
|
||||
return self._path(key).exists()
|
||||
|
||||
def url(self, key: str) -> str:
|
||||
return str(self._path(key))
|
||||
Reference in new issue
Block a user