Example (DeleteNode) in dgo is wrong?

example

(Una Chen) #1

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.

  1. Did I misunderstand any information about the above? or is a small mistake for Example (DeleteNode)?

  2. 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.


(Animesh Chandra Pathak) #2

Hi @UnaChen
I just tried deleting all predicates using dgo and it worked.

What exactly do you mean by node haven’t been deleted ? We delete all predicates corresponding to a uid(node).

It should be DeleteNquads instead of SetNquads.

I think the code snippet that you have provided above works and deletes all the predicates associated with alice


(Saurabh Garg) #3

Hello!

I am trying something similar - using deleteJSON via dgo. All predicates are deleted. But it seems the UID itself is not deleted. After deletion, if I run a query, I seem to be getting back phantom UIDs. Is that expected behavior?

Saurabh


(Animesh Chandra Pathak) #4

Hi @sgargw
Can you tell what is the query you are running after deletion?


(Saurabh Garg) #5

Hey Animesh,

Thanks for getting back. I was able to figure out what I was doing wrong. I had to delete the edges along with the node.

-Saurabh