Skip to content

Commit 1b1a884

Browse files
authored
sdk: Fix empty-cache handling in LocalFileIdentifiableStore (#463)
Previously, the `_object_cache` of the `LocalFileIdentifiableStore` was initialised empty. Calling `discard()` immediately after store initialisation raised a `KeyError` if the Identifiable to delete existed on disk but had not yet been loaded into the in-memory cache. This adds explicit handling of missing cache entries in `discard()`, preventing errors when a key is not present in the in-memory cache. Moreover, a corresponding regression test is added. Finally, the interim empty-cache workaround in `sync()` is removed. Fixes #438
1 parent 10ff4e4 commit 1b1a884

3 files changed

Lines changed: 11 additions & 15 deletions

File tree

sdk/basyx/aas/backend/local_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def discard(self, x: model.Identifiable) -> None:
123123
except FileNotFoundError as e:
124124
raise KeyError("No AAS object with id {} exists in local file database".format(x.id)) from e
125125
with self._object_cache_lock:
126-
del self._object_cache[x.id]
126+
self._object_cache.pop(x.id, None)
127127

128128
def __contains__(self, x: object) -> bool:
129129
"""

sdk/basyx/aas/model/provider.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,6 @@ def sync(self, other: Iterable[_VALUE], overwrite: bool) -> Tuple[int, int, int]
7878
for value in other:
7979
if value in self:
8080
if overwrite:
81-
82-
# TODO: This is a quick fix. Yes it works. The underlying problem with the subclass
83-
# `LocalFileIdentifiableStore` will be solved in a separate issue
84-
# (https://github.com/eclipse-basyx/basyx-python-sdk/issues/438).
85-
# Think of this as pythonic duct tape.
86-
#
87-
# The problem is that the `_object_cache` isn't initialised together with the
88-
# `LocalFileIdentifiableStore`, leading to an error when `discard()` is called on the empty cache.
89-
# The for-loop calls `__iter__` calls `get_identifiable_by_hash()` calls
90-
# `self._object_cache[obj.id] = obj`, adding all identifiables to the cache and therefore avoiding
91-
# the error.
92-
for element in self:
93-
pass
94-
9581
self.discard(value)
9682
self.add(value)
9783
overwritten += 1

sdk/test/backend/test_local_file.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,13 @@ def test_key_errors(self) -> None:
106106
self.identifiable_store.discard(retrieved_submodel)
107107
self.assertEqual("'No AAS object with id https://acplt.org/Test_Submodel exists in "
108108
"local file database'", str(cm.exception))
109+
110+
def test_reload_discard(self) -> None:
111+
# Load example submodel
112+
example_submodel = create_example_submodel()
113+
self.identifiable_store.add(example_submodel)
114+
115+
# Reload the DictIdentifiableStore and discard the example submodel
116+
self.identifiable_store = local_file.LocalFileIdentifiableStore(store_path)
117+
self.identifiable_store.discard(example_submodel)
118+
self.assertNotIn(example_submodel, self.identifiable_store)

0 commit comments

Comments
 (0)