After executing this Example (DeleteNode), the target node haven’t been deleted and no error occurred.
I checked the related source code about the following part in Example (DeleteNode).
d := map[string]string{"uid": alice}
pb, err = json.Marshal(d)
if err != nil {
log.Fatal(err)
}
mu = &api.Mutation{
CommitNow: true,
DeleteJson: pb,
}
_, err = dg.NewTxn().Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
Firstly, func (txn *Txn) Mutate would trigger func (txn *Txn) Do. The following code will be executed to call func (s *Server) Query server side.
resp, err := txn.dc.Query(ctx, req)
Secondly, func (s *Server) Query would trigger func (s *Server) doMutate which would execute func parseMutationObject(mu *api.Mutation). Then, the import library “chunker” would be called.
if len(mu.DeleteJson) > 0 {
nqs, err := chunker.ParseJSON(mu.DeleteJson, chunker.DeleteNquads)
if err != nil {
return nil, err
}
res.Del = append(res.Del, nqs...)
}
The func ParseJSON(b []byte, op int) in chunker/json_parser.go is to parse JSON into []*api.NQuad.
Therefore, I tried to add the following test in json_parser_test.go to check what format d := map[string]string{"uid": alice}
in Example (DeleteNode) would be converted into.
However, by referring to the following output, the JSON failed to be parsed.
func Parse(b []byte, op int) ([]*api.NQuad, error) {
nqs := NewNQuadBuffer(1000)
err := nqs.ParseJSON(b, op)
fmt.Println("\n---- original JSON\n", string(b)) // add for checking json
fmt.Println("---- Nquads\n", nqs.nquads) // add for checking the parsed result
return nqs.nquads, err
}
func TestNquadsFromJson5(t *testing.T) { // add for check what format the json in Example (DeleteNode) would be converted into.
d := map[string]string{"uid": "1"}
pb, err := json.Marshal(d)
require.NoError(t, err)
nq, err := Parse([]byte(pb), SetNquads)
require.NoError(t, err)
require.Equal(t, 0, len(nq))
}
// output:
// ---- original JSON
// {"uid":"1"}
// ---- Nquads
// []
As mentioned above, I would like to ask 3 questions.
-
Did I misunderstand any information about the above? or is a small mistake for Example (DeleteNode)?
-
If I would like to delete all related data for a UID with GO client, what’s the best practice?
– After tracing code, I found the DeleteJson in
mu = &api.Mutation {CommitNow:true,DeleteJson: pb,}
should be assigned with UID and predicate at the same time (e.g,{"uid":$UID,"$predicate": $predicate_val}
). However, I would like to directly delete all info. for a node sometimes.
Thank you for your help.