Skip to content

Commit e3c23b2

Browse files
committed
WIP
1 parent da9874a commit e3c23b2

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

tiled/catalog/adapter.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ def __init__(self, metadata, specs, top_level_access_blob):
145145
self.ancestors = []
146146
self.key = None
147147
self.data_sources = None
148-
self.id = None
149-
self.parent_id = None
148+
# self.id = None
149+
# self.parent = None
150150
self.access_blob = top_level_access_blob or {}
151151

152152

@@ -369,7 +369,8 @@ def __repr__(self):
369369
return f"<{type(self).__name__} /{'/'.join(self.segments)}>"
370370

371371
async def __aiter__(self):
372-
statement = select(orm.Node.key).filter(orm.Node.parent_id == self.node.id)
372+
# statement = select(orm.Node.key).filter(orm.Node.parent == self.node.id)
373+
statement = select(orm.Node.key).filter(orm.Node.ancestors == self.segments)
373374
for condition in self.conditions:
374375
statement = statement.filter(condition)
375376
async with self.context.session() as db:
@@ -378,7 +379,8 @@ async def __aiter__(self):
378379
.scalars()
379380
.all()
380381
)
381-
statement = select(orm.Node.key).filter(orm.Node.parent_id == self.node.id)
382+
# statement = select(orm.Node.key).filter(orm.Node.parent == self.node.id)
383+
statement = select(orm.Node.key).filter(orm.Node.ancestors == self.segments)
382384
async with self.context.session() as db:
383385
return (await db.execute(statement)).scalar().all()
384386

@@ -427,7 +429,8 @@ def apply_conditions(self, statement):
427429

428430
async def async_len(self):
429431
statement = select(func.count(orm.Node.key)).filter(
430-
orm.Node.parent_id == self.node.id
432+
# orm.Node.parent == self.node.id
433+
orm.Node.ancestors == self.segments
431434
)
432435
statement = self.apply_conditions(statement)
433436
async with self.context.session() as db:
@@ -650,7 +653,7 @@ async def create_node(
650653

651654
node = orm.Node(
652655
key=key,
653-
parent_id=self.node.id,
656+
# parent=self.node.id,
654657
ancestors=self.segments,
655658
metadata_=metadata,
656659
structure_family=structure_family,
@@ -1026,7 +1029,8 @@ async def keys_range(self, offset, limit):
10261029
offset,
10271030
(offset + limit) if limit is not None else None, # noqa: E203
10281031
)
1029-
statement = select(orm.Node.key).filter(orm.Node.parent_id == self.node.id)
1032+
# statement = select(orm.Node.key).filter(orm.Node.parent == self.node.id)
1033+
statement = select(orm.Node.key).filter(orm.Node.ancestors == self.segments)
10301034
statement = self.apply_conditions(statement)
10311035
async with self.context.session() as db:
10321036
return (
@@ -1048,7 +1052,8 @@ async def items_range(self, offset, limit):
10481052
offset,
10491053
(offset + limit) if limit is not None else None, # noqa: E203
10501054
)
1051-
statement = select(orm.Node).filter(orm.Node.parent_id == self.node.id)
1055+
# statement = select(orm.Node).filter(orm.Node.parent == self.node.id)
1056+
statement = select(orm.Node).filter(orm.Node.ancestors == self.segments)
10521057
statement = self.apply_conditions(statement)
10531058
async with self.context.session() as db:
10541059
nodes = (

tiled/catalog/orm.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Node(Timestamped, Base):
6464

6565
# This id is internal, never exposed to the client.
6666
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
67-
parent_id = Column(Integer, ForeignKey("nodes.id"), nullable=True)
67+
parent = Column(Integer, ForeignKey("nodes.id"), nullable=True)
6868

6969
key = Column(Unicode(1023), nullable=False)
7070
ancestors = Column(JSONVariant, nullable=True)
@@ -86,17 +86,16 @@ class Node(Timestamped, Base):
8686
passive_deletes=True,
8787
)
8888

89-
# This a self-referencing relationship between parent and children
90-
parent = relationship("Node", remote_side=[id], back_populates="children")
91-
children = relationship("Node", back_populates="parent")
89+
# This is a self-referencing relationship between parent and children
90+
prnt_rel = relationship("Node", remote_side=[id], back_populates="chld_rel")
91+
chld_rel = relationship("Node", back_populates="prnt_rel")
9292

9393
__table_args__ = (
94-
UniqueConstraint("key", "parent_id", name="key_parent_id_unique_constraint"),
94+
UniqueConstraint("key", "parent", name="key_parent_unique_constraint"),
9595
# This index supports comparison operations (==, <, ...).
9696
# For key-existence operations we will need a GIN index additionally.
9797
Index(
9898
"top_level_metadata",
99-
"parent_id",
10099
# include the keys of the default sorting ('time_created', 'id'),
101100
# used to avoid creating a temp sort index
102101
"time_created",
@@ -115,11 +114,11 @@ class NodesClosure(Base):
115114

116115
__tablename__ = "nodes_closure"
117116

118-
parent = Column(Integer, ForeignKey("nodes.id"), primary_key=True)
119-
child = Column(Integer, ForeignKey("nodes.id"), primary_key=True)
117+
ancestor = Column(Integer, ForeignKey("nodes.id"), primary_key=True)
118+
descendant = Column(Integer, ForeignKey("nodes.id"), primary_key=True)
120119
depth = Column(Integer, nullable=False)
121120

122-
__table_args__ = (UniqueConstraint("parent", "child", name="parent_child_unique_constraint"),
121+
__table_args__ = (UniqueConstraint("ancestor", "descendant", name="ancestor_descendant_unique_constraint"),
123122
)
124123

125124
class DataSourceAssetAssociation(Base):
@@ -294,12 +293,12 @@ def update_closure_table(target, connection, **kw):
294293
CREATE TRIGGER update_closure_table_when_inserting
295294
AFTER INSERT ON nodes
296295
BEGIN
297-
INSERT INTO nodes_closure(parent, child, depth)
296+
INSERT INTO nodes_closure(ancestor, descendant, depth)
298297
SELECT NEW.id, NEW.id, 0;
299-
INSERT INTO nodes_closure(parent, child, depth)
300-
SELECT p.parent, c.child, p.depth+c.depth+1
298+
INSERT INTO nodes_closure(ancestor, descendant, depth)
299+
SELECT p.ancestor, c.descendant, p.depth+c.depth+1
301300
FROM nodes_closure p, nodes_closure c
302-
WHERE p.child=NEW.parent_id and c.parent=NEW.id;
301+
WHERE p.descendant=NEW.parent and c.ancestor=NEW.id;
303302
END"""
304303
)
305304
)
@@ -310,10 +309,10 @@ def update_closure_table(target, connection, **kw):
310309
BEFORE DELETE ON nodes
311310
BEGIN
312311
DELETE FROM nodes_closure
313-
WHERE (parent, child) IN (
314-
SELECT p.parent, c.child
312+
WHERE (ancestor, descendant) IN (
313+
SELECT p.ancestor, c.descendant
315314
FROM nodes_closure p, nodes_closure c
316-
WHERE (p.child=OLD.parent_id OR p.child=OLD.id) AND c.parent=OLD.id);
315+
WHERE (p.descendant=OLD.parent OR p.descendant=OLD.id) AND c.ancestor=OLD.id);
317316
END"""
318317
)
319318
)

0 commit comments

Comments
 (0)