threatgrid / asami

A graph store for Clojure and ClojureScript
Eclipse Public License 1.0
634 stars 29 forks source link

retractEntity #230

Open lilactown opened 2 years ago

lilactown commented 2 years ago

Provide the capability to retract all the attributes of an entity and any references to it.

https://docs.datomic.com/cloud/transactions/transaction-functions.html

quoll commented 2 years ago

Since this is generating the triples to be removed, it belongs in asami.entities.writer

There are 2 conditions here: top-level-entities and sub-entities. The latter may not be necessary, but I'm going to assume that they are useful.

For a top-level entity:

  1. Get the :db/id (I'll call this the node)
  2. Find all statements that match: [node :tg/owns ?s]. This returns the sub-entity node list.
  3. Delete the node, using the sub-entity node list for recursion...
    • Find all statements that match: [?e ?a node]. Add these to the list of statements to remove.
    • Find all statements that match: [node ?a ?v]. Add these to the list of statements to remove. The set of ?v values contains the referenced data.
    • For each item in the referenced data that matches the sub-entity node list, go back to 3 and repeat.

For an entity that isn't top-level:

  1. Get the :db/id
  2. Get the statement that matches: [?p :tg/owns node]. This is the parent.
  3. Find all statements that match [parent :tg/owns ?s]. This returns the sub-entity node list of the parent.
  4. Perform step 3 from the top-level-entity operations above.

This second process should recurse down, matching the things owned by the same parent, but should not affect siblings/cousins/etc.