From 8c2bee605de946419b08d6b6adaecb481d11a27a Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Wed, 22 Jan 2025 21:59:24 +0530 Subject: [PATCH] fixed count bug --- posting/index.go | 5 +++-- posting/list.go | 11 +++-------- posting/mvcc.go | 4 ++++ schema/parse.go | 3 +++ 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/posting/index.go b/posting/index.go index 739b45898a1..aea6dccbb8f 100644 --- a/posting/index.go +++ b/posting/index.go @@ -522,8 +522,8 @@ func (txn *Txn) addMutationHelper(ctx context.Context, l *List, doUpdateIndex bo switch { case hasCountIndex: countBefore, found, currPost = l.getPostingAndLengthNoSort(txn.StartTs, 0, getUID(t)) - if countBefore == -1 { - return val, false, emptyCountParams, errors.Wrapf(ErrTsTooOld, "Add mutation count index") + if countBefore < 0 { + return val, false, emptyCountParams, errors.Wrapf(ErrTsTooOld, "Count less than. Add mutation count index") } case doUpdateIndex || delNonListPredicate: found, currPost, err = l.findPosting(txn.StartTs, fingerprintEdge(t)) @@ -692,6 +692,7 @@ func (r *rebuilder) RunWithoutTemp(ctx context.Context) error { l := new(List) l.key = key + l.pk = pk l.plist = new(pb.PostingList) found := false diff --git a/posting/list.go b/posting/list.go index c0b97e0cea3..ea1c2982496 100644 --- a/posting/list.go +++ b/posting/list.go @@ -77,6 +77,7 @@ const ( type List struct { x.SafeMutex key []byte + pk x.ParsedKey plist *pb.PostingList mutationMap *MutableLayer minTs uint64 // commit timestamp of immutable layer, reject reads before this ts. @@ -751,14 +752,8 @@ func (l *List) updateMutationLayer(mpost *pb.Posting, singleUidUpdate bool) erro newPlist := &pb.PostingList{} newPlist.Postings = append(newPlist.Postings, mpost) - // Add the deletions in the existing plist because those postings are not picked - // up by iterating. Not doing so would result in delete operations that are not - // applied when the transaction is committed. - for _, post := range l.mutationMap.currentEntries.Postings { - if post.Op == Del && post.Uid != mpost.Uid { - newPlist.Postings = append(newPlist.Postings, post) - } - } + // Set current entries as nil so that they don't show up in the iterate. + l.mutationMap.setCurrentEntries(mpost.StartTs, &pb.PostingList{}) err := l.iterate(mpost.StartTs, 0, func(obj *pb.Posting) error { // Ignore values which have the same uid as they will get replaced diff --git a/posting/mvcc.go b/posting/mvcc.go index f006f516cab..dea4b388255 100644 --- a/posting/mvcc.go +++ b/posting/mvcc.go @@ -20,6 +20,7 @@ import ( "bytes" "context" "encoding/hex" + "fmt" "math" "strconv" "sync" @@ -446,6 +447,7 @@ func initMemoryLayer(cacheSize int64, deleteOnUpdates bool) *MemoryLayer { defer ticker.Stop() for range ticker.C { // Record the posting list cache hit ratio + fmt.Println(ml.cache.numCacheRead, ml.cache.numCacheReadFails, ml.cache.numCacheSave, ml.numDisksRead) ostats.Record(context.Background(), x.PLCacheHitRatio.M(m.Ratio())) } }() @@ -550,6 +552,7 @@ func ReadPostingList(key []byte, it *badger.Iterator) (*List, error) { } l := new(List) + l.pk = pk l.key = key l.plist = new(pb.PostingList) l.minTs = 0 @@ -627,6 +630,7 @@ func copyList(l *List) *List { l.AssertRLock() // No need to clone the immutable layer or the key since mutations will not modify it. lCopy := &List{ + pk: l.pk, minTs: l.minTs, maxTs: l.maxTs, key: l.key, diff --git a/schema/parse.go b/schema/parse.go index 983fd2408f9..869161cb745 100644 --- a/schema/parse.go +++ b/schema/parse.go @@ -71,6 +71,9 @@ func parseDirective(it *lex.ItemIterator, schema *pb.SchemaUpdate, t types.TypeI schema.Tokenizer = tokenizer schema.IndexSpecs = vectorSpecs case "count": + if !schema.List { + return next.Errorf("Cannot count for not list") + } schema.Count = true case "upsert": schema.Upsert = true