memory (sqla): Add with_parent and with_link to get_pubsub_nodes():
Added `with_parent` and `with_link` parameters for eager loading of parent and linked_node relationships. Fixed CTE to include all children (not just base nodes) and use `selectinload` for parent to avoid cartesian products. rel 476
This commit is contained in:
parent
20bf3dffa1
commit
f1ceb7db8e
1 changed files with 14 additions and 10 deletions
|
|
@ -1283,6 +1283,8 @@ class Storage:
|
||||||
with_items: bool = False,
|
with_items: bool = False,
|
||||||
with_subscriptions: bool = False,
|
with_subscriptions: bool = False,
|
||||||
with_affiliations: bool = False,
|
with_affiliations: bool = False,
|
||||||
|
with_parent: bool = False,
|
||||||
|
with_link: bool = False,
|
||||||
) -> list[PubsubNode]:
|
) -> list[PubsubNode]:
|
||||||
"""Retrieve pubsub nodes with their hierarchy and linking nodes.
|
"""Retrieve pubsub nodes with their hierarchy and linking nodes.
|
||||||
|
|
||||||
|
|
@ -1300,6 +1302,8 @@ class Storage:
|
||||||
@param with_items: If True, eagerly load items for each node.
|
@param with_items: If True, eagerly load items for each node.
|
||||||
@param with_subscriptions: If True, eagerly load subscriptions for each node.
|
@param with_subscriptions: If True, eagerly load subscriptions for each node.
|
||||||
@param with_affiliations: If True, eagerly load affiliations for each node.
|
@param with_affiliations: If True, eagerly load affiliations for each node.
|
||||||
|
@param with_parent: If True, eagerly load parent_node for each node.
|
||||||
|
@param with_link: If True, eagerly load linked_node for each node.
|
||||||
@return: List of base nodes matching criteria (root nodes of the request).
|
@return: List of base nodes matching criteria (root nodes of the request).
|
||||||
"""
|
"""
|
||||||
# Base criteria that always apply
|
# Base criteria that always apply
|
||||||
|
|
@ -1313,13 +1317,15 @@ class Storage:
|
||||||
criteria.append(PubsubNode.linked_node_id.is_(None))
|
criteria.append(PubsubNode.linked_node_id.is_(None))
|
||||||
|
|
||||||
# Prepare query options
|
# Prepare query options
|
||||||
|
# Always include parent_node and linked_node for all nodes when requested
|
||||||
|
options = []
|
||||||
if depth > 0:
|
if depth > 0:
|
||||||
options = [
|
# Load child_nodes when depth > 0 so hierarchy can be traversed
|
||||||
selectinload(PubsubNode.child_nodes),
|
options.append(selectinload(PubsubNode.child_nodes))
|
||||||
joinedload(PubsubNode.parent_node),
|
if with_parent:
|
||||||
]
|
options.append(selectinload(PubsubNode.parent_node))
|
||||||
else:
|
if with_link:
|
||||||
options = []
|
options.append(joinedload(PubsubNode.linked_node))
|
||||||
if with_items:
|
if with_items:
|
||||||
options.append(selectinload(PubsubNode.items))
|
options.append(selectinload(PubsubNode.items))
|
||||||
if linked_node:
|
if linked_node:
|
||||||
|
|
@ -1369,13 +1375,11 @@ class Storage:
|
||||||
.cte(name="hierarchy", recursive=True)
|
.cte(name="hierarchy", recursive=True)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Recursive part: get direct children that are also base nodes.
|
# Recursive part: get direct children (including nodes with linked_node_id).
|
||||||
children = (
|
children = (
|
||||||
select(PubsubNode.id, hierarchy.c.depth + 1)
|
select(PubsubNode.id, hierarchy.c.depth + 1)
|
||||||
.join(hierarchy, PubsubNode.parent_node_id == hierarchy.c.id)
|
.join(hierarchy, PubsubNode.parent_node_id == hierarchy.c.id)
|
||||||
.filter(
|
.filter(hierarchy.c.depth < depth)
|
||||||
hierarchy.c.depth < depth, PubsubNode.linked_node_id.is_(None)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Combine and create the full hierarchy.
|
# Combine and create the full hierarchy.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue