Implemented ConceptManager with concept creation, modification and deletion

This commit is contained in:
2020-12-08 15:36:21 +01:00
parent d364878ddb
commit 4b6e1dd55b
40 changed files with 1847 additions and 979 deletions
+1
View File
@@ -27,5 +27,6 @@ class Cache(BaseCache):
def _delete(self, key, value):
del(self._cache[key])
self._current_size -= 1
self._add_to_remove(key)
+33 -2
View File
@@ -6,14 +6,22 @@ from cache.BaseCache import BaseCache
from core.concept import Concept
@dataclass
class MultipleEntryError(Exception):
"""
Exception raised when trying to alter an entry with multiple element
without giving the origin of the element
"""
def __init__(self, key):
self.key = key
key: str
@dataclass
class ConceptNotFound(Exception):
"""
Thrown when you try to remove a concept that is not found
"""
concept: object
@dataclass
@@ -127,6 +135,29 @@ class CacheManager:
# pass
# self.is_dirty = True
def remove_concept(self, concept):
"""
Remove a concept from all caches
:param concept:
:return:
"""
with self._lock:
# the first concept cache must the one where all concept are unique
# eg it has to be the concept by id
ref_cache_def = self.caches[self.concept_caches[0]]
concept_id = ref_cache_def.get_key(concept)
ref_concept = ref_cache_def.cache.get(concept_id)
if ref_concept is None:
raise ConceptNotFound(concept)
for cache_name in self.concept_caches:
cache_def = self.caches[cache_name]
key = cache_def.get_key(ref_concept)
cache_def.cache.delete(key, ref_concept)
self.is_dirty = True
def get(self, cache_name, key):
"""
From concept cache, get an entry
+20
View File
@@ -54,3 +54,23 @@ class ListIfNeededCache(BaseCache):
else:
self._cache[new_key] = new_value
self._add_to_add(new_key)
def _delete(self, key, value):
if value is None:
self._current_size -= len(self._cache[key])
del self._cache[key]
self._add_to_remove(key)
else:
previous = self._cache[key]
if isinstance(previous, list):
previous.remove(value)
if len(previous) == 1:
self._cache[key] = previous[0]
self._current_size -= 1
self.to_add.add(key)
else:
if previous == value:
del self._cache[key]
self._current_size -= 1
self.to_remove.add(key)
+14
View File
@@ -49,3 +49,17 @@ class SetCache(BaseCache):
self._cache[new_key].remove(old_value)
self._put(new_key, new_value)
self._add_to_add(new_key)
def _delete(self, key, value):
if value is None:
self._current_size -= len(self._cache[key])
del self._cache[key]
self._add_to_remove(key)
else:
self._cache[key].remove(value)
self._current_size -= 1
if len(self._cache[key]) == 0:
del self._cache[key]
self._add_to_remove(key)
else:
self._add_to_add(key)