Skip to content

Commit

Permalink
fix(django): only parse tiles when accessed
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Apr 25, 2024
1 parent 868ca01 commit 1008bc7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
4 changes: 3 additions & 1 deletion arches_orm/arches_django/datatypes/resource_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def resource_instance(
else:
return None

if not isinstance(resource_instance, WKRI):
if not resource_instance:
return None
elif not isinstance(resource_instance, WKRI):
wkrm = get_well_known_resource_model_by_graph_id(
resource_instance.graph_id, default=None
)
Expand Down
8 changes: 8 additions & 0 deletions arches_orm/arches_django/pseudo_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def __init__(self, node, tile=None, value=None, parent=None, child_nodes=None, p
self._parent_node = None
self._child_nodes = child_nodes
self._value = value
self._accessed = False
self._original_tile = tile

def __str__(self):
return f"{{{self.value}}}"
Expand Down Expand Up @@ -147,7 +149,13 @@ def clear(self):
if self.tile and self.tile.data and str(self.node.nodeid) in self.tile.data:
del self.tile.data[str(self.node.nodeid)]

@property
def accessed(self) -> bool:
return self._accessed

def _update_value(self):
self._accessed = True

if not self.tile:
if not self.node:
raise RuntimeError("Empty tile")
Expand Down
24 changes: 16 additions & 8 deletions arches_orm/arches_django/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,22 @@ def _update_tiles(
if not isinstance(root, PseudoNodeList):
parent = root
for pseudo_node in root.get_children():
if len(pseudo_node):
subrelationships = self._update_tiles(
tiles, root=pseudo_node, parent=parent
)
relationships += subrelationships
if not isinstance(pseudo_node, PseudoNodeList):
t_and_r = pseudo_node.get_tile()
combined_tiles.append(t_and_r)
if pseudo_node.accessed:
if len(pseudo_node):
subrelationships = self._update_tiles(
tiles, root=pseudo_node, parent=parent
)
relationships += subrelationships
if not isinstance(pseudo_node, PseudoNodeList):
t_and_r = pseudo_node.get_tile()
combined_tiles.append(t_and_r)
# This avoids loading a tile as a set of view models, simply to re-save it.
elif not isinstance(pseudo_node, PseudoNodeList) and pseudo_node._original_tile:
# TODO: NOTE THAT THIS DOES NOT CAPTURE RELATIONSHIPS THAT HAVE NOT BEEN ACCESSED
combined_tiles.append((
pseudo_node._original_tile,
[]
))

for tile, subrelationships in combined_tiles:
if tile:
Expand Down
3 changes: 3 additions & 0 deletions arches_orm/view_models/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def append(self, item: str | uuid.UUID | WKRI):
resource_instance_id = item["resourceId"]

value, _, __, ___ = self._make_ri_cb(resource_instance or resource_instance_id)

if not value:
raise RuntimeError(f"Could not append {item} to resource list within {self._parent_wkri}")
if str(value._cross_record["wkriFrom"].id) != str(self._parent_wkri.id):
raise NotImplementedError("Cannot currently reparent related resources")

Expand Down

0 comments on commit 1008bc7

Please sign in to comment.