Implemented SheerkaOntology
This commit is contained in:
Vendored
+81
-66
@@ -4,6 +4,7 @@ from typing import Callable
|
||||
|
||||
from cache.BaseCache import BaseCache
|
||||
from core.concept import Concept
|
||||
from core.global_symbols import NotFound
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -37,8 +38,9 @@ class CacheManager:
|
||||
Single class to manage all the caches
|
||||
"""
|
||||
|
||||
def __init__(self, cache_only):
|
||||
def __init__(self, cache_only, sdp=None):
|
||||
self.cache_only = cache_only # if true disable all remote access when key not found
|
||||
self.sdp = sdp
|
||||
self.caches = {}
|
||||
self.concept_caches = []
|
||||
self.is_dirty = False # to indicate that the value of a cache has changed
|
||||
@@ -57,6 +59,8 @@ class CacheManager:
|
||||
with self._lock:
|
||||
if self.cache_only:
|
||||
cache.disable_default()
|
||||
if self.sdp:
|
||||
cache.configure(sdp=self.sdp)
|
||||
self.caches[name] = CacheDefinition(cache, use_ref, get_key)
|
||||
self.concept_caches.append(name)
|
||||
|
||||
@@ -70,8 +74,13 @@ class CacheManager:
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
if self.sdp:
|
||||
cache.configure(sdp=self.sdp)
|
||||
|
||||
if self.cache_only:
|
||||
cache.disable_default()
|
||||
persist = False
|
||||
|
||||
self.caches[name] = CacheDefinition(cache, use_ref, None, persist)
|
||||
|
||||
def add_concept(self, concept):
|
||||
@@ -89,11 +98,12 @@ class CacheManager:
|
||||
|
||||
self.is_dirty = True
|
||||
|
||||
def update_concept(self, old, new):
|
||||
def update_concept(self, old, new, alt_sdp=None):
|
||||
"""
|
||||
Update a concept.
|
||||
:param old: old version of the concept
|
||||
:param new: new version of the concept
|
||||
:param alt_sdp: if not found in self.sdp, look in other repositories
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
@@ -103,42 +113,15 @@ class CacheManager:
|
||||
old_key = cache_def.get_key(old)
|
||||
new_key = cache_def.get_key(new)
|
||||
|
||||
cache_def.cache.update(old_key, old, new_key, new)
|
||||
cache_def.cache.update(old_key, old, new_key, new, alt_sdp=alt_sdp)
|
||||
|
||||
self.is_dirty = True
|
||||
|
||||
# how can you update an entry it the key may have changed ?
|
||||
# You need to have an invariant. By convention the keys in the first cache cannot change
|
||||
# with self._lock:
|
||||
# iter_cache_def = iter(self.caches)
|
||||
#
|
||||
# cache_def = next(iter_cache_def)
|
||||
# old_key = cache_def.get_key(concept)
|
||||
#
|
||||
# try:
|
||||
# while True:
|
||||
# items = cache_def.cache[old_key]
|
||||
# if isinstance(items, (list, set)):
|
||||
# for item in items:
|
||||
# if item.id == concept.id:
|
||||
# break
|
||||
# else:
|
||||
# raise IndexError(f"{old_key=}, id={concept.id}")
|
||||
#
|
||||
# cache_def.cache.update(old_key, item, cache_def.get_key(concept), concept)
|
||||
#
|
||||
# else:
|
||||
# cache_def.cache.update(old_key, items, cache_def.get_key(concept), concept)
|
||||
#
|
||||
# cache_def = next(iter_cache_def)
|
||||
# except StopIteration:
|
||||
# pass
|
||||
# self.is_dirty = True
|
||||
|
||||
def remove_concept(self, concept):
|
||||
def remove_concept(self, concept, alt_sdp=None):
|
||||
"""
|
||||
Remove a concept from all caches
|
||||
:param concept:
|
||||
:param alt_sdp: if not found in self.sdp, look in other repositories
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
@@ -148,25 +131,66 @@ class CacheManager:
|
||||
concept_id = ref_cache_def.get_key(concept)
|
||||
ref_concept = ref_cache_def.cache.get(concept_id)
|
||||
|
||||
if ref_concept is None:
|
||||
if ref_concept is NotFound and alt_sdp:
|
||||
ref_concept = alt_sdp.get(self.concept_caches[0], concept_id)
|
||||
|
||||
if ref_concept is NotFound:
|
||||
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)
|
||||
cache_def.cache.delete(key, ref_concept, alt_sdp=alt_sdp)
|
||||
|
||||
self.is_dirty = True
|
||||
|
||||
def get(self, cache_name, key):
|
||||
def get(self, cache_name, key, alt_sdp=None):
|
||||
"""
|
||||
From concept cache, get an entry
|
||||
:param cache_name:
|
||||
:param key:
|
||||
:param alt_sdp: if not found in self.sdp, look in other repositories
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
return self.caches[cache_name].cache.get(key, alt_sdp)
|
||||
|
||||
def alt_get(self, cache_name, key):
|
||||
"""
|
||||
Alternate way to get an entry, from concept cache
|
||||
This is mainly used for IncCache, in order to get the value without increasing it
|
||||
:param cache_name:
|
||||
:param key:
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
return self.caches[cache_name].cache.get(key)
|
||||
return self.caches[cache_name].cache.alt_get(key)
|
||||
|
||||
def put(self, cache_name, key, value, alt_sdp=None):
|
||||
"""
|
||||
Add to a cache
|
||||
:param cache_name:
|
||||
:param key:
|
||||
:param value:
|
||||
:param alt_sdp: if not found in self.sdp, look in other repositories
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
self.caches[cache_name].cache.put(key, value, alt_sdp)
|
||||
self.is_dirty = True
|
||||
|
||||
def delete(self, cache_name, key, value=None, alt_sdp=None):
|
||||
"""
|
||||
Delete an entry from the cache
|
||||
:param cache_name:
|
||||
:param key:
|
||||
:param value:
|
||||
:param alt_sdp: if not found in self.sdp, look in other repositories
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
if self.caches[cache_name].cache.delete(key, value, alt_sdp):
|
||||
self.is_dirty = True
|
||||
|
||||
def get_cache(self, cache_name):
|
||||
"""
|
||||
@@ -186,40 +210,31 @@ class CacheManager:
|
||||
"""
|
||||
return self.caches[cache_name].cache.copy()
|
||||
|
||||
def put(self, cache_name, key, value):
|
||||
"""
|
||||
Add to a cache
|
||||
:param cache_name:
|
||||
:param key:
|
||||
:param value:
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
self.caches[cache_name].cache.put(key, value)
|
||||
self.is_dirty = True
|
||||
|
||||
def delete(self, cache_name, key, value=None):
|
||||
"""
|
||||
Delete an entry from the cache
|
||||
:param cache_name:
|
||||
:param key:
|
||||
:param value:
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
self.caches[cache_name].cache.delete(key, value)
|
||||
self.is_dirty = True
|
||||
|
||||
def populate(self, cache_name, populate_function, get_key_function):
|
||||
def populate(self, cache_name, populate_function, get_key_function, reset_events=False):
|
||||
"""
|
||||
Populate a specific cache with a bunch of items
|
||||
:param cache_name:
|
||||
:param populate_function: how to get the items
|
||||
:param get_key_function: how to get the key, out of an item
|
||||
:param reset_events: reset to_add and to_remove events after populate
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
self.caches[cache_name].cache.init(populate_function, get_key_function)
|
||||
self.caches[cache_name].cache.populate(populate_function, get_key_function, reset_events)
|
||||
|
||||
def force_value(self, cache_name, key, value):
|
||||
"""
|
||||
Update the content of the cache, but does not raise any event
|
||||
"""
|
||||
with self._lock:
|
||||
self.caches[cache_name].cache.force_value(key, value)
|
||||
|
||||
def remove_initialized_key(self, cache_name, key):
|
||||
"""
|
||||
|
||||
"""
|
||||
with self._lock:
|
||||
self.caches[cache_name].cache.remove_initialized_key(key)
|
||||
|
||||
def has(self, cache_name, key):
|
||||
"""
|
||||
@@ -267,7 +282,7 @@ class CacheManager:
|
||||
return
|
||||
|
||||
with self._lock:
|
||||
with context.sheerka.sdp.get_transaction(context.event.get_digest()) as transaction:
|
||||
with self.sdp.get_transaction(context.event.get_digest()) as transaction:
|
||||
for cache_name, cache_def in self.caches.items():
|
||||
if not cache_def.persist:
|
||||
continue
|
||||
@@ -287,13 +302,13 @@ class CacheManager:
|
||||
cache_def.cache.reset_events()
|
||||
self.is_dirty = False
|
||||
|
||||
def clear(self, cache_name=None):
|
||||
def clear(self, cache_name=None, set_is_cleared=True):
|
||||
with self._lock:
|
||||
if cache_name:
|
||||
self.caches[cache_name].cache.clear()
|
||||
self.caches[cache_name].cache.clear(set_is_cleared)
|
||||
else:
|
||||
for cache_def in self.caches.values():
|
||||
cache_def.cache.clear()
|
||||
cache_def.cache.clear(set_is_cleared)
|
||||
|
||||
def dump(self):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user