Given a tree of objects in Drive Realtime API with a concurrent operation in two or more clients where existing children are moved into a new parent (think of a grouping operation in a graphical editor), what is the best datastructure to avoid a) duplicate existing children and b) empty new parents?
Asked
Active
Viewed 111 times
1 Answers
2
Can each child only have a single parent?
If that is the case, I would suggest having a "parent" field on the child objects. This guarantees that each child only has one parent, and the non-empty parent set is implicitly created by looking finding all the parents.
You could easily maintain this parent set by doing a one-time scan through the children on startup, and then listening for change events on the child and updating the set accordingly.1111

Cheryl Simon
- 46,552
- 15
- 93
- 82
-
This is in the context of draw.io where we have the problem that a concurrent grouping of existing cells (nodes in a tree) results in one group having the children and the other groups being empty. The group operation adds a new node in the tree and moves the existing children into that new node. (Children are ordered so in addition to the parent reference we also have a children list in the parent.) With the suggested solution where we scan the tree for empty parents, does this mean we'd also delete all leaves in the tree (as they have no children)? – user1084282 May 31 '13 at 11:28
-
You'd need to be a little bit careful about when you removed such empty parents. The safest thing to do would be to just ignore them at render time. This does leave some cruft in the data model, but I bet this concurrent grouping doesn't happen too often where it would be a big deal. Otherwise, with the careful use of compound operations you could probably ensure that an empty node would never eventually get children, and safely delete it. – Cheryl Simon May 31 '13 at 21:03