From d6b8724586e4a3522848a28a226c2a0f595bec6c Mon Sep 17 00:00:00 2001 From: Li Date: Thu, 8 May 2025 17:42:38 +0800 Subject: [PATCH 1/2] fix(core): fix value loss when reverse predicate is updated and deleted multiple times in one DQL txn --- posting/list.go | 2 +- worker/mutation_unit_test.go | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/posting/list.go b/posting/list.go index c015c58553e..6e9a37e8b93 100644 --- a/posting/list.go +++ b/posting/list.go @@ -372,7 +372,7 @@ func (mm *MutableLayer) insertPosting(mpost *pb.Posting, hasCountIndex bool) { } res := mm.currentEntries.Postings[:postIndex] if postIndex+1 <= len(mm.currentEntries.Postings) { - mm.currentEntries.Postings = append(res, + res = append(res, mm.currentEntries.Postings[(postIndex+1):]...) } mm.currentUids = nil diff --git a/worker/mutation_unit_test.go b/worker/mutation_unit_test.go index 5d66ae042b2..73d52e09836 100644 --- a/worker/mutation_unit_test.go +++ b/worker/mutation_unit_test.go @@ -58,6 +58,68 @@ func TestReverseEdge(t *testing.T) { require.Equal(t, c, 0) } +func TestReverseEdgeSetDel(t *testing.T) { + dir, err := os.MkdirTemp("", "storetest_") + x.Check(err) + defer os.RemoveAll(dir) + + opt := badger.DefaultOptions(dir) + ps, err := badger.OpenManaged(opt) + x.Check(err) + pstore = ps + // Not using posting list cache + posting.Init(ps, 0, false) + Init(ps) + err = schema.ParseBytes([]byte("revc: [uid] @reverse @count ."), 1) + require.NoError(t, err) + + ctx := context.Background() + txn := posting.Oracle().RegisterStartTs(5) + attr := x.AttrInRootNamespace("revc") + + edgeDel := &pb.DirectedEdge{ + ValueId: 2, + Attr: attr, + Entity: 3, + Op: pb.DirectedEdge_DEL, + } + + edgeSet1 := &pb.DirectedEdge{ + ValueId: 2, + Attr: attr, + Entity: 1, + Op: pb.DirectedEdge_SET, + } + + edgeSet2 := &pb.DirectedEdge{ + ValueId: 2, + Attr: attr, + Entity: 3, + Op: pb.DirectedEdge_SET, + } + + + edgeSet3 := &pb.DirectedEdge{ + ValueId: 2, + Attr: attr, + Entity: 4, + Op: pb.DirectedEdge_SET, + } + + + x.Check(runMutation(ctx, edgeSet1, txn)) + x.Check(runMutation(ctx, edgeSet2, txn)) + x.Check(runMutation(ctx, edgeSet3, txn)) + x.Check(runMutation(ctx, edgeDel, txn)) + + pl, err := txn.Get(x.ReverseKey(attr, 2)) + require.NoError(t, err) + pl.RLock() + c := pl.GetLength(5) + pl.RUnlock() + require.Equal(t, 2, c) +} + func TestConvertEdgeType(t *testing.T) { var testEdges = []struct { input *pb.DirectedEdge From d79a7e3c725eee7d822031f3b0b5596ffcf7ffbe Mon Sep 17 00:00:00 2001 From: Li Date: Thu, 8 May 2025 17:52:48 +0800 Subject: [PATCH 2/2] fix incorrect require.Equal arg order in reverse edge test --- worker/mutation_unit_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/worker/mutation_unit_test.go b/worker/mutation_unit_test.go index 73d52e09836..d734290857b 100644 --- a/worker/mutation_unit_test.go +++ b/worker/mutation_unit_test.go @@ -55,7 +55,7 @@ func TestReverseEdge(t *testing.T) { pl.RLock() c := pl.GetLength(5) pl.RUnlock() - require.Equal(t, c, 0) + require.Equal(t, 0, c) } func TestReverseEdgeSetDel(t *testing.T) {