Skip to content

Commit

Permalink
MB-60662: revert doc id cache (#222)
Browse files Browse the repository at this point in the history
- reverts the cache to store the id dictionary and the max docID seen in segment
- with this revert you would always create a fresh dictionary object and traverse the fst to get the max doc id per segment base
  • Loading branch information
CascadingRadium authored Feb 23, 2024
1 parent 4006929 commit 0c7027f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 53 deletions.
8 changes: 1 addition & 7 deletions dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package zap

import (
"fmt"
"sync"

"github.com/RoaringBitmap/roaring"
index "github.com/blevesearch/bleve_index_api"
Expand All @@ -31,10 +30,6 @@ type Dictionary struct {
fieldID uint16
fst *vellum.FST

// if the dictionary is shared across multiple threads
// we need to protect the fstReader with a mutex
// since it is not thread safe
m sync.Mutex
fstReader *vellum.Reader

bytesRead uint64
Expand Down Expand Up @@ -62,9 +57,8 @@ func (d *Dictionary) postingsList(term []byte, except *roaring.Bitmap, rv *Posti
return d.postingsListInit(rv, except), nil
}

d.m.Lock()
postingsOffset, exists, err := d.fstReader.Get(term)
d.m.Unlock()

if err != nil {
return nil, fmt.Errorf("vellum err: %v", err)
}
Expand Down
55 changes: 9 additions & 46 deletions segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,6 @@ type SegmentBase struct {

m sync.Mutex
fieldFSTs map[uint16]*vellum.FST

docIDMutex sync.RWMutex
// cache the maximum docID seen in this segment
cachedMaxDocID string
//cache the dictionary for the _id field
idDict *Dictionary
}

func (sb *SegmentBase) Size() int {
Expand Down Expand Up @@ -587,57 +581,26 @@ func (s *SegmentBase) Count() uint64 {
return s.numDocs
}

func (s *SegmentBase) getDocIDinfo() (*Dictionary, string, error) {
// obtain a read lock to check if the max doc ID and dict for _id is cached
s.docIDMutex.RLock()
cachedDocID := s.cachedMaxDocID
cachedDict := s.idDict
if cachedDocID != "" && cachedDict != nil {
s.docIDMutex.RUnlock()
// max doc ID and the id dict is cached, return it
return cachedDict, cachedDocID, nil
}
s.docIDMutex.RUnlock()
// not cached so obtain a write lock
// to create the _id dict and read the FST
// to get the max doc id and cache them
s.docIDMutex.Lock()
defer s.docIDMutex.Unlock()
// check if the info is cached again
// by some other thread to avoid unnecessary
// ops.
if s.idDict != nil && s.cachedMaxDocID != "" {
return s.idDict, s.cachedMaxDocID, nil
}
// create the _id dict
idDict, err := s.dictionary("_id")
if err != nil {
return nil, "", err
}
s.idDict = idDict
// max doc ID is not cached, get it from the FST
sMax, err := idDict.fst.GetMaxKey()
if err != nil {
return nil, "", err
}
// cache it
s.cachedMaxDocID = string(sMax)
return s.idDict, s.cachedMaxDocID, nil
}

// DocNumbers returns a bitset corresponding to the doc numbers of all the
// provided _id strings
func (s *SegmentBase) DocNumbers(ids []string) (*roaring.Bitmap, error) {
rv := roaring.New()

if len(s.fieldsMap) > 0 {
idDict, err := s.dictionary("_id")
if err != nil {
return nil, err
}

postingsList := emptyPostingsList
idDict, maxDocID, err := s.getDocIDinfo()

sMax, err := idDict.fst.GetMaxKey()
if err != nil {
return nil, err
}
sMaxStr := string(sMax)
for _, id := range ids {
if id <= maxDocID {
if id <= sMaxStr {
postingsList, err = idDict.postingsList([]byte(id), nil, postingsList)
if err != nil {
return nil, err
Expand Down

0 comments on commit 0c7027f

Please sign in to comment.