-
Notifications
You must be signed in to change notification settings - Fork 1
Store: add persistent dependency registry and notify hook #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f2d0be8
dc0124a
d7ea97a
9de4aa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| # | ||
|
|
||
|
|
||
| import uuid | ||
| import atexit | ||
|
|
||
| from sqlitedict import SqliteDict | ||
|
|
@@ -54,6 +55,13 @@ def __init__( | |
| self.file_name, tablename=attribute_function_space, autocommit=True | ||
| ) | ||
|
|
||
| # dependency registry (persistent) | ||
| self._registry_key = "__dependency_registry__" | ||
|
|
||
| if self._registry_key not in self.sqlite_dict: | ||
| self.sqlite_dict[self._registry_key] = {} | ||
| self.sqlite_dict.commit() | ||
|
|
||
| # register to be called at exit: | ||
| atexit.register(self.close) | ||
|
|
||
|
|
@@ -76,27 +84,72 @@ def register(self, af: AttributeFunction): | |
| (i.e. persisted). | ||
| @param af: The AttributeFunction instance to register. | ||
| """ | ||
| uuid_str: str = str(af.uuid) | ||
|
|
||
| self.sqlite_dict[af.uuid] = af | ||
| self.sqlite_dict[uuid_str] = af | ||
| self.sqlite_dict.commit() | ||
| self.attribute_function_buffer[af.uuid] = af | ||
|
|
||
| def load(self, afid: int) -> None: | ||
| """Load an afid from the persistent store into the buffer. | ||
| @param afid: The ID of the item to load. | ||
| """ | ||
|
|
||
| try: | ||
| af: AttributeFunction = self.sqlite_dict[afid] | ||
| if self.add_reference_to_store_on_read: | ||
| af.__dict__["store"] = self | ||
|
|
||
| self.attribute_function_buffer[afid] = af | ||
| except KeyError as e: | ||
| raise KeyError(f"ID '{afid}' not found in the store.") from e | ||
|
|
||
| def _get_registry(self) -> dict[str, list[str]]: | ||
| return self.sqlite_dict.get(self._registry_key, {}) | ||
|
|
||
| def register_dependency(self, parent_uuid: uuid.UUID, child_uuid: uuid.UUID): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so the idea here is to have a separte dependency registry, i.e. all subscriptions are additionally subscribed here, correct?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the idea is to maintain a separate persistent dependency registry that mirrors the subscription relationships. The reason for not storing dependencies directly within the AttributeFunctions is to keep the dependency mechanism at the Store level and independent of the in-memory state of AFs. This ensures that: Dependencies persist even when AFs are not loaded in memory So the registry is not meant to be redundant, but rather a persistence layer that enables subscriptions to work transparently across sessions. |
||
| """ | ||
| Register a persistent dependency between two AttributeFunctions. | ||
|
|
||
| @param parent_uuid: The UUID of the AF being observed. | ||
| @param child_uuid: The UUID of the AF that depends on the parent. | ||
| """ | ||
| registry: dict[str, list[str]] = self._get_registry() | ||
|
|
||
| p_uuid_str: str = str(parent_uuid) | ||
| c_uuid_str: str = str(child_uuid) | ||
|
|
||
| if p_uuid_str not in registry: | ||
| registry[p_uuid_str] = [] | ||
|
|
||
| if c_uuid_str not in registry[p_uuid_str]: | ||
| registry[p_uuid_str].append(c_uuid_str) | ||
|
|
||
| self.sqlite_dict[self._registry_key] = registry | ||
| self.sqlite_dict.commit() | ||
|
|
||
| def _notify(self, parent_uuid: uuid.UUID): | ||
| registry = self._get_registry() | ||
| p_uuid_str = str(parent_uuid) | ||
|
|
||
| if p_uuid_str not in registry: | ||
| return | ||
|
|
||
| parent_af: AttributeFunction = self.get(parent_uuid) | ||
|
|
||
| dependent_id: str | ||
| for dependent_id in registry[p_uuid_str]: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typehint missing |
||
| try: | ||
| dependent_af: AttributeFunction = self.get(dependent_id) | ||
| if dependent_af and hasattr(dependent_af, "update"): | ||
| dependent_af.update(other=parent_af) | ||
| dependent_af.was_updated_in_test = True | ||
| self.put(dependent_af) | ||
| except KeyError: | ||
| continue | ||
|
|
||
| def __len__(self) -> int: | ||
| """Return the number of items in the store. | ||
| @return: The number of items in the store. | ||
| """ | ||
| return len(self.sqlite_dict) | ||
| size = len(self.sqlite_dict) | ||
|
|
||
| if self._registry_key in self.sqlite_dict: | ||
| size -= 1 | ||
|
|
||
| return size | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the purpose of this string?