Skip to content

Commit

Permalink
added cache for len
Browse files Browse the repository at this point in the history
  • Loading branch information
harshil-goel committed Oct 19, 2024
1 parent 4ea547e commit fd2ef39
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
58 changes: 42 additions & 16 deletions posting/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ type MutableMap struct {
deleteMarker uint64
uidMap map[uint64]int
uidsH map[uint64]*pb.Posting
length int
}

func newMutableMap() *MutableMap {
return &MutableMap{
oldList: make(map[uint64]*pb.PostingList),
curTime: 0,
deleteMarker: math.MaxUint64,
length: math.MaxInt,
uidsH: make(map[uint64]*pb.Posting),
}
}
Expand All @@ -116,6 +118,7 @@ func (mm *MutableMap) clone() *MutableMap {
curTime: 0,
deleteMarker: mm.deleteMarker,
uidsH: mm.uidsH,
length: mm.length,
}
}

Expand Down Expand Up @@ -165,16 +168,30 @@ func (mm *MutableMap) listLen(readTs uint64) int {
return 0
}

count := 0
mm.iterate(func(ts uint64, pl *pb.PostingList) {
for _, mpost := range pl.Postings {
if mm.length == math.MaxInt {
count := 0
mm.iterate(func(ts uint64, pl *pb.PostingList) {
for _, mpost := range pl.Postings {
if mpost.Op == Del {
count -= 1
} else {
count += 1
}
}
}, readTs)
return count
}

count := mm.length
if mm.curList != nil {
for _, mpost := range mm.curList.Postings {
if mpost.Op == Del {
count -= 1
} else {
count += 1
}
}
}, readTs)
}
return count
}

Expand Down Expand Up @@ -252,6 +269,11 @@ func (mm *MutableMap) insertOldPosting(pl *pb.PostingList) {
if _, ok := mm.uidsH[mpost.Uid]; !ok {
mm.uidsH[mpost.Uid] = mpost
}
if mpost.Op == Del {
mm.length -= 1
} else {
mm.length += 1
}
}
mm.oldList[pl.CommitTs] = pl
}
Expand Down Expand Up @@ -824,6 +846,11 @@ func (l *List) setMutationAfterCommit(startTs, commitTs uint64, pl *pb.PostingLi
}

l.mutationMap.uidsH[mpost.Uid] = mpost
if mpost.Op == Del {
l.mutationMap.length -= 1
} else {
l.mutationMap.length += 1
}
}

l.mutationMap.curList = nil
Expand Down Expand Up @@ -1046,19 +1073,18 @@ func (l *List) IsEmpty(readTs, afterUid uint64) (bool, error) {
func (l *List) getPostingAndLengthNoSort(readTs, afterUid, uid uint64) (int, bool, *pb.Posting) {
l.AssertRLock()

dec := codec.Decoder{Pack: l.plist.Pack}
uids := dec.Seek(uid, codec.SeekStart)
found := len(uids) > 0 && uids[0] == uid
length := l.mutationMap.listLen(readTs)
if !found && l.mutationMap != nil {
_, ok := l.mutationMap.uidsH[uid]
if ok {
found = true
}
_, ok = l.mutationMap.uidMap[uid]
if ok {
found = true
}

found := false

searchFurther, pos := l.mutationMap.findPosting(readTs, uid)
if pos != nil {
found = true
}
if !found && searchFurther {
dec := codec.Decoder{Pack: l.plist.Pack}
uids := dec.Seek(uid, codec.SeekStart)
found = len(uids) > 0 && uids[0] == uid
}

if l.mutationMap.populateDeleteAll(readTs) == 0 {
Expand Down
1 change: 1 addition & 0 deletions posting/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ func ReadPostingList(key []byte, it *badger.Iterator) (*List, error) {
pl.CommitTs = item.Version()
if l.mutationMap == nil {
l.mutationMap = newMutableMap()
l.mutationMap.length = 0
}
l.mutationMap.insertOldPosting(pl)
return nil
Expand Down

0 comments on commit fd2ef39

Please sign in to comment.