mirror of
https://github.com/senju1337/senju.git
synced 2025-12-24 07:39:29 +00:00
refactor: update store to use the document ids instead of uuids
Refs: OPS-21 OPS-16
This commit is contained in:
parent
d7dece3424
commit
6b6f442408
2 changed files with 19 additions and 36 deletions
|
|
@ -1,8 +1,7 @@
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from tinydb import TinyDB, Query
|
from tinydb import TinyDB
|
||||||
from uuid import UUID, uuid1
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import time
|
from logging import Logger
|
||||||
|
|
||||||
from tinydb.queries import QueryImpl
|
from tinydb.queries import QueryImpl
|
||||||
|
|
||||||
|
|
@ -10,41 +9,23 @@ DEFAULT_DB_PATH: Path = Path("/var/lib/senju.json")
|
||||||
|
|
||||||
|
|
||||||
class StoreManager:
|
class StoreManager:
|
||||||
__slots__ = "_db"
|
__slots__ = "_db", "logger"
|
||||||
_db: TinyDB
|
_db: TinyDB
|
||||||
|
logger: Logger
|
||||||
|
|
||||||
def __init__(self, path_to_db: Path = DEFAULT_DB_PATH) -> None:
|
def __init__(self, path_to_db: Path = DEFAULT_DB_PATH) -> None:
|
||||||
self._db = TinyDB(path_to_db)
|
self._db = TinyDB(path_to_db)
|
||||||
|
self.logger = Logger(__name__)
|
||||||
def new_id(self) -> UUID:
|
|
||||||
_guard: int = 0
|
|
||||||
while True:
|
|
||||||
unix_timestamp: int = int(time.time())
|
|
||||||
id = uuid1(node=None, clock_seq=unix_timestamp)
|
|
||||||
if len(self._query(Query().id == id)) < 1:
|
|
||||||
break
|
|
||||||
_guard += 1
|
|
||||||
if _guard > 100:
|
|
||||||
raise Exception(
|
|
||||||
"tried 100 random UUIDs but found no unused one")
|
|
||||||
return id
|
|
||||||
|
|
||||||
def _query(self, query: QueryImpl) -> list[dict]:
|
def _query(self, query: QueryImpl) -> list[dict]:
|
||||||
return self._db.search(query)
|
return self._db.search(query)
|
||||||
|
|
||||||
def load(self, key: UUID) -> Optional[dict]:
|
def _load(self, id: int) -> Optional[dict]:
|
||||||
results = self._query(Query().id == str(key))
|
try:
|
||||||
if len(results) < 1:
|
return self._db.get(doc_id=id)
|
||||||
raise Exception("foobar")
|
except IndexError as e:
|
||||||
elif len(results) > 2:
|
self.logger.warning(f"could not get item with id {id}: {e}")
|
||||||
raise KeyError("The requested item did not exist in our database")
|
return None
|
||||||
else:
|
|
||||||
return results[0]["data"]
|
|
||||||
|
|
||||||
def save(self, data: dict) -> UUID:
|
def _save(self, data: dict) -> int:
|
||||||
id = self.new_id()
|
return self._db.insert(data)
|
||||||
self._db.insert({
|
|
||||||
"id": str(id),
|
|
||||||
"data": data
|
|
||||||
})
|
|
||||||
return id
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import pytest # noqa: F401
|
||||||
from senju.store_manager import StoreManager # noqa: F401
|
from senju.store_manager import StoreManager # noqa: F401
|
||||||
|
|
||||||
|
|
||||||
def test_temp_data_dir(store_manager: StoreManager):
|
def test_save_and_load_any(store_manager: StoreManager):
|
||||||
thing = {
|
thing = {
|
||||||
"color": "blue",
|
"color": "blue",
|
||||||
"number": 19,
|
"number": 19,
|
||||||
|
|
@ -13,9 +13,11 @@ def test_temp_data_dir(store_manager: StoreManager):
|
||||||
"no": "yes"
|
"no": "yes"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
thing_id = store_manager.save(thing)
|
thing_id = store_manager._save(thing)
|
||||||
thing_loaded = store_manager.load(thing_id)
|
thing_loaded = store_manager._load(thing_id)
|
||||||
|
|
||||||
if thing_loaded is None:
|
if thing_loaded is None:
|
||||||
assert False, "the store manager load did not return anything"
|
assert False, "store manager load did not return anything but \
|
||||||
|
should have"
|
||||||
for key in thing.keys():
|
for key in thing.keys():
|
||||||
assert thing[key] == thing_loaded[key]
|
assert thing[key] == thing_loaded[key]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue