From adc172bc53145cc906203c700689a79e402d1326 Mon Sep 17 00:00:00 2001 From: animesh Date: Tue, 27 Aug 2019 17:19:37 +0530 Subject: [PATCH 1/2] Use txn.Get in addReverseMutation We cannot use txn.cache.GetFromDeltas for reverse mutations becasue cache will not have deltas corresponding to reverse key. Fixes #3840 --- dgraph/cmd/alpha/upsert_test.go | 49 +++++++++++++++++++++++++++++++++ posting/index.go | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/dgraph/cmd/alpha/upsert_test.go b/dgraph/cmd/alpha/upsert_test.go index 7030d21d7c0..b0472be292b 100644 --- a/dgraph/cmd/alpha/upsert_test.go +++ b/dgraph/cmd/alpha/upsert_test.go @@ -1422,3 +1422,52 @@ upsert { require.NotContains(t, res, "San Francisco") require.NotContains(t, res, "Fuller Street, SF") } + +func TestDeleteCountIndex(t *testing.T) { + require.NoError(t, dropAll()) + require.NoError(t, alterSchema(` +: uid @count @reverse . +: int @index(int) .`)) + + m1 := ` +{ +set { + _:1 _:2 . + _:1 "1" . + _:2 _:3 . + _:2 "2" . + _:4 _:2 . + _:3 "3" . + _:4 "4" . +} +}` + + _, _, _, err := mutationWithTs(m1, "application/rdf", false, true, 0) + require.NoError(t, err) + + m2 := ` +upsert { +query { + u3 as var(func: eq(name, "3")) + u2 as var(func: eq(name, "2")) +} +mutation { + delete { + uid(u2) uid(u3) . + } +} +}` + _, _, _, err = mutationWithTs(m2, "application/rdf", false, true, 0) + require.NoError(t, err) + + q1 := ` +{ + me(func: eq(count(~game_answer), 1)) { + name + count(~game_answer) + } + }` + res, _, err := queryWithTs(q1, "application/graphql+-", "", 0) + require.NoError(t, err) + require.NotContains(t, res, "count(~game_answer)") +} diff --git a/posting/index.go b/posting/index.go index b785b446cc0..328d6a0335f 100644 --- a/posting/index.go +++ b/posting/index.go @@ -169,7 +169,7 @@ func (txn *Txn) addReverseMutationHelper(ctx context.Context, plist *List, func (txn *Txn) addReverseMutation(ctx context.Context, t *pb.DirectedEdge) error { key := x.ReverseKey(t.Attr, t.ValueId) - plist, err := txn.cache.GetFromDelta(key) + plist, err := txn.Get(key) if err != nil { return err } From a68a3b8c4cf1073cf288068caf5eaf667a68762a Mon Sep 17 00:00:00 2001 From: Manish R Jain Date: Tue, 27 Aug 2019 19:03:17 -0700 Subject: [PATCH 2/2] Only retrieve the full PL if needed for reverse count. If no count needed, don't read the PL from disk. --- posting/index.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/posting/index.go b/posting/index.go index 328d6a0335f..ec30ece18e5 100644 --- a/posting/index.go +++ b/posting/index.go @@ -169,7 +169,18 @@ func (txn *Txn) addReverseMutationHelper(ctx context.Context, plist *List, func (txn *Txn) addReverseMutation(ctx context.Context, t *pb.DirectedEdge) error { key := x.ReverseKey(t.Attr, t.ValueId) - plist, err := txn.Get(key) + hasCountIndex := schema.State().HasCount(t.Attr) + + var getFn func(key []byte) (*List, error) + if hasCountIndex { + // We need to retrieve the full posting list from disk, to allow us to get the length of the + // posting list for the counts. + getFn = txn.Get + } else { + // We are just adding a reverse edge. No need to read the list from disk. + getFn = txn.GetFromDelta + } + plist, err := getFn(key) if err != nil { return err } @@ -184,7 +195,6 @@ func (txn *Txn) addReverseMutation(ctx context.Context, t *pb.DirectedEdge) erro Facets: t.Facets, } - hasCountIndex := schema.State().HasCount(t.Attr) cp, err := txn.addReverseMutationHelper(ctx, plist, hasCountIndex, edge) if err != nil { return err