cayley icon indicating copy to clipboard operation
cayley copied to clipboard

Nested object is not removed when the parent object is removed

Open denisvmedia opened this issue 6 years ago • 3 comments

Here is the source to illustrate the problem: https://gist.github.com/denisvmedia/1079e1f55c0eda7c2bd63368b345fd21

Description

In my code I sometimes need to remove an object that has a nested object. The problem is that the quads from the nested object remain orphaned in the database.

Steps to reproduce the issue:

  1. Compile and run this example.

Received results:

quads:
<gc:cluster1> -- <rdf:type> -> <gc:Cluster>
<gc:cluster1> -- <gc:name> -> "Cluster1"
_:n7235784696260549346 -- <ip> -> "127.0.0.1"
_:n7235784696260549346 -- <port> -> "1234"

Expected results:

quads:
<gc:cluster1> -- <rdf:type> -> <gc:Cluster>
<gc:cluster1> -- <gc:name> -> "Cluster1"

Cayley and DB backend versions:

github.com/boltdb/bolt v1.3.1
github.com/cayleygraph/cayley v0.7.5

denisvmedia avatar Dec 29 '18 23:12 denisvmedia

Thanks for reporting, but this is expected behavior. There is no direct way to know what edges the delete operation should traverse.

In your example, why do you expect the HostAddr to be removed automatically, while Cluster is not, although it's reachable from the Host as well?

But there is a way to remove both. You can get the Host structure, set its Cluster field to nil and "write" it to a quad remover, instead. Note, however, that it will remove only the fields that are mentioned in the struct, while RemoveNode removes all associated edges.

dennwc avatar Dec 29 '18 23:12 dennwc

In your example, why do you expect the HostAddr to be removed automatically, while Cluster is not, although it's reachable from the Host as well?

Well, just because, Cluster is addressable independently, I can delete it by its ID the same way, as I deleted the Host. But to delete the HostAddr I in fact have to go through all the quads and then find and delete the "orphans" (at least if there is no better way).

But ok, if it's how it's expected to work, I will think, how I can work around this issue.

denisvmedia avatar Dec 30 '18 02:12 denisvmedia

It actually has an implicit ID field. If you add it to the struct definition you will be able to remove it directly. You may leave the field empty on insert and it will generate an ID as it does right now.

dennwc avatar Dec 30 '18 10:12 dennwc