Skip to content
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

DM-43671: Implement atomic collection prepend #989

Merged
merged 16 commits into from
Apr 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 20 additions & 13 deletions python/lsst/daf/butler/registry/collections/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,22 +426,29 @@ def update_chain(
record.name for record in self.resolve_wildcard(children_as_wildcard, flatten_chains=True)
)

rows = []
position = itertools.count()
names = []
for child in self.resolve_wildcard(CollectionWildcard.from_names(children), flatten_chains=False):
rows.append(
{
"parent": chain.key,
"child": child.key,
"position": next(position),
}
)
names.append(child.name)
child_records = self.resolve_wildcard(CollectionWildcard.from_names(children), flatten_chains=False)
names = [child.name for child in child_records]
with self._db.transaction():
self._db.delete(self._tables.collection_chain, ["parent"], {"parent": chain.key})
self._db.insert(self._tables.collection_chain, *rows)
self._insert_collection_chain_rows(chain.key, 0, [child.key for child in child_records])

record = ChainedCollectionRecord[K](chain.key, chain.name, children=tuple(names))
self._addCachedRecord(record)
return record

def _insert_collection_chain_rows(
self,
parent_key: K,
starting_position: int,
child_keys: Iterable[K],
) -> None:
position = itertools.count(starting_position)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using enumerate inside the list comprehension plus an offset seems a little less error-prone. I don't know why that wasn't the case before.

rows = [
{
"parent": parent_key,
"child": child,
"position": next(position),
}
for child in child_keys
]
self._db.insert(self._tables.collection_chain, *rows)