Skip to content

Commit

Permalink
unify nosql and stroeddict keys (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
domenukk authored Apr 26, 2021
1 parent bfb11f3 commit 0a21ced
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/enochecker/nosqldict.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@
DB_DEFAULT_PORT = 27017


def to_keyfmt(key: Any) -> str:
"""
Convert a string to a key used in the MongoDB.
Currently this only returns the string representation of the key.
:param key: the key to format
:return: string representation of the key
"""
return str(key) # + type(key).__name__


def value_to_hash(value: Any) -> int:
"""
Create a stable hash for a value based on the json representation.
Expand Down Expand Up @@ -162,7 +150,7 @@ def __init__(
)

@_try_n_times
def __setitem__(self, key: Any, value: Any) -> None:
def __setitem__(self, key: str, value: Any) -> None:
"""
Set an entry in the dictionary.
Expand All @@ -171,20 +159,22 @@ def __setitem__(self, key: Any, value: Any) -> None:
:param key: key in the dictionary
:param value: value in the dictionary
"""
key = str(key)

self.cache[key] = value
self.hash_cache[key] = value_to_hash(value)

self._upsert(key, value)

def _upsert(self, key: Any, value: Any) -> None:
query_dict = {
"key": to_keyfmt(key),
"key": key,
"checker": self.checker_name,
"name": self.dict_name,
}

to_insert = {
"key": to_keyfmt(key),
"key": key,
"checker": self.checker_name,
"name": self.dict_name,
"value": value,
Expand All @@ -193,7 +183,7 @@ def _upsert(self, key: Any, value: Any) -> None:
self.db.replace_one(query_dict, to_insert, upsert=True)

@_try_n_times
def __getitem__(self, key: Any, print_result: bool = False) -> Any:
def __getitem__(self, key: str, print_result: bool = False) -> Any:
"""
Get an entry from the dictionary.
Expand All @@ -203,11 +193,12 @@ def __getitem__(self, key: Any, print_result: bool = False) -> Any:
:param print_result: TODO
:return: retrieved value
"""
key = str(key)
if key in self.cache.items():
return self.cache[key]

to_extract = {
"key": to_keyfmt(key),
"key": key,
"checker": self.checker_name,
"name": self.dict_name,
}
Expand All @@ -224,19 +215,20 @@ def __getitem__(self, key: Any, print_result: bool = False) -> Any:
raise KeyError("Could not find {} in {}".format(key, self))

@_try_n_times
def __delitem__(self, key: Any) -> None:
def __delitem__(self, key: str) -> None:
"""
Delete an entry from the dictionary.
Also deletes the value from the cache in addition to the MongoDB backend.
:param key: key to delete
"""
key = str(key)
if key in self.cache:
del self.cache[key]

to_extract = {
"key": to_keyfmt(key),
"key": key,
"checker": self.checker_name,
"name": self.dict_name,
}
Expand Down
7 changes: 7 additions & 0 deletions src/enochecker/storeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def release(self, locked_key: str) -> None:
:param locked_key: the key we locked
"""
locked_key = str(locked_key)
if locked_key not in self._locks:
raise KeyError("{} was not locked.".format(locked_key))
self._dir_lockname(locked_key)
Expand All @@ -166,6 +167,7 @@ def mark_dirty(self, key: str) -> Any:
:param key: the key that needs to be stored
:return: the value contained in the key
"""
key = str(key)
val = self[key]
self[key] = val
return val
Expand Down Expand Up @@ -198,6 +200,7 @@ def lock(self, key: str) -> None:
:param key: the key to lock
"""
key = str(key)
if key in self._locks:
raise KeyError("{} already locked".format(key))
self._create_lock_file(self._dir_lockname(key))
Expand All @@ -211,6 +214,7 @@ def is_locked(self, key: str) -> bool:
:param key: The key
:return: True if locked by this process, False otherwise
"""
key = str(key)
return key in self._locks

@_locked
Expand Down Expand Up @@ -263,6 +267,7 @@ def __getitem__(self, key: str) -> Any:
:param key: the key to look up
:return: the value
"""
key = str(key)
locked = self.is_locked(key) or self.ignore_locks
if not locked:
self.lock(key)
Expand All @@ -285,6 +290,7 @@ def __setitem__(self, key: str, value: Any) -> None:
:param key: Key to store
:param value: Value to store
"""
key = str(key)
self._cache[key] = value
self.persist()

Expand All @@ -295,6 +301,7 @@ def __delitem__(self, key: str) -> None:
:param key: the key to delete
"""
key = str(key)
self._to_delete.add(key)
self.persist()

Expand Down

0 comments on commit 0a21ced

Please sign in to comment.