Current Situation
The _object_cache of the LocalFileObjectStore is not initialised when a new instance of the class is created. This means that the Identifiables from the local .json files are not loaded initially:
|
def __init__(self, directory_path: str): |
|
""" |
|
Initializer of class LocalFileObjectStore |
|
|
|
:param directory_path: Path to the local file backend (the path where you want to store your AAS JSON files) |
|
""" |
|
self.directory_path: str = directory_path.rstrip("/") |
|
|
|
# A dictionary of weak references to local replications of stored objects. Objects are kept in this cache as |
|
# long as there is any other reference in the Python application to them. We use this to make sure that only one |
|
# local replication of each object is kept in the application and retrieving an object from the store always |
|
# returns the **same** (not only equal) object. Still, objects are forgotten, when they are not referenced |
|
# anywhere else to save memory. |
|
self._object_cache: weakref.WeakValueDictionary[model.Identifier, model.Identifiable] \ |
|
= weakref.WeakValueDictionary() |
|
self._object_cache_lock = threading.Lock() |
The cache is designed to operate efficiently, i.e. it is only filled with a specific Identifiable from a local .json file when the user searches for or adds it. Consequently, calling discard() on a newly initialised LocalFileObjectStore raises a KeyError whenever the Identifiable to delete exists in a local .json file, but is not present in the initially empty cache:
|
def discard(self, x: model.Identifiable) -> None: |
|
""" |
|
Delete an :class:`~basyx.aas.model.base.Identifiable` AAS object from the local file store |
|
|
|
:param x: The object to be deleted |
|
:raises KeyError: If the object does not exist in the database |
|
""" |
|
logger.debug("Deleting object %s from Local File Store database ...", repr(x)) |
|
try: |
|
os.remove("{}/{}.json".format(self.directory_path, self._transform_id(x.id))) |
|
except FileNotFoundError as e: |
|
raise KeyError("No AAS object with id {} exists in local file database".format(x.id)) from e |
|
with self._object_cache_lock: |
|
del self._object_cache[x.id] |
Proposed Change
Ensure that the discard() method can handle an empty _object_cache by catching potential KeyErrors.
Current Situation
The
_object_cacheof theLocalFileObjectStoreis not initialised when a new instance of the class is created. This means that the Identifiables from the local.jsonfiles are not loaded initially:basyx-python-sdk/sdk/basyx/aas/backend/local_file.py
Lines 34 to 49 in e622edc
The cache is designed to operate efficiently, i.e. it is only filled with a specific Identifiable from a local
.jsonfile when the user searches for or adds it. Consequently, callingdiscard()on a newly initialisedLocalFileObjectStoreraises aKeyErrorwhenever the Identifiable to delete exists in a local.jsonfile, but is not present in the initially empty cache:basyx-python-sdk/sdk/basyx/aas/backend/local_file.py
Lines 112 to 125 in e622edc
Proposed Change
Ensure that the
discard()method can handle an empty_object_cacheby catching potentialKeyErrors.