Skip to content

Commit

Permalink
Use SeekPrefixGE to avoid unnecessary disk read in Get function
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Aug 24, 2023
1 parent 8638640 commit 4eb0200
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ func (d *DB) getInternal(key []byte, b *Batch, s *Snapshot) ([]byte, io.Closer,
logger: d.opts.Logger,
cmp: d.cmp,
equal: d.equal,
split: d.split,
newIters: d.newIters,
snapshot: seqNum,
key: key,
Expand Down
23 changes: 19 additions & 4 deletions get_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type getIter struct {
logger Logger
cmp Compare
equal Equal
split Split
newIters tableNewIters
snapshot uint64
key []byte
Expand Down Expand Up @@ -159,13 +160,20 @@ func (g *getIter) Next() (*InternalKey, base.LazyValue) {
files := g.l0[n-1].Iter()
g.l0 = g.l0[:n-1]
iterOpts := IterOptions{logger: g.logger, snapshotForHideObsoletePoints: g.snapshot}
g.levelIter.init(context.Background(), iterOpts, g.cmp, nil /* split */, g.newIters,
g.levelIter.init(context.Background(), iterOpts, g.cmp, g.split, g.newIters,
files, manifest.L0Sublevel(n), internalIterOpts{})
g.levelIter.initRangeDel(&g.rangeDelIter)
bc := levelIterBoundaryContext{}
g.levelIter.initBoundaryContext(&bc)
g.iter = &g.levelIter
g.iterKey, g.iterValue = g.iter.SeekGE(g.key, base.SeekGEFlagsNone)

// Compute the key prefix for bloom filtering if split function is
// specified, or use the user key as default.
prefix := g.key
if g.split != nil {
prefix = g.key[:g.split(g.key)]
}
g.iterKey, g.iterValue = g.iter.SeekPrefixGE(prefix, g.key, base.SeekGEFlagsNone)
if bc.isSyntheticIterBoundsKey || bc.isIgnorableBoundaryKey {
g.iterKey = nil
g.iterValue = base.LazyValue{}
Expand All @@ -184,14 +192,21 @@ func (g *getIter) Next() (*InternalKey, base.LazyValue) {
}

iterOpts := IterOptions{logger: g.logger, snapshotForHideObsoletePoints: g.snapshot}
g.levelIter.init(context.Background(), iterOpts, g.cmp, nil /* split */, g.newIters,
g.levelIter.init(context.Background(), iterOpts, g.cmp, g.split, g.newIters,
g.version.Levels[g.level].Iter(), manifest.Level(g.level), internalIterOpts{})
g.levelIter.initRangeDel(&g.rangeDelIter)
bc := levelIterBoundaryContext{}
g.levelIter.initBoundaryContext(&bc)
g.level++
g.iter = &g.levelIter
g.iterKey, g.iterValue = g.iter.SeekGE(g.key, base.SeekGEFlagsNone)

// Compute the key prefix for bloom filtering if split function is
// specified, or use the user key as default.
prefix := g.key
if g.split != nil {
prefix = g.key[:g.split(g.key)]
}
g.iterKey, g.iterValue = g.iter.SeekPrefixGE(prefix, g.key, base.SeekGEFlagsNone)
if bc.isSyntheticIterBoundsKey || bc.isIgnorableBoundaryKey {
g.iterKey = nil
g.iterValue = base.LazyValue{}
Expand Down
6 changes: 5 additions & 1 deletion level_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,11 @@ func (l *levelIter) SeekPrefixGE(
// next file will defeat the optimization for the next SeekPrefixGE that is
// called with flags.TrySeekUsingNext(), since for sparse key spaces it is
// likely that the next key will also be contained in the current file.
if n := l.split(l.iterFile.LargestPointKey.UserKey); l.cmp(prefix, l.iterFile.LargestPointKey.UserKey[:n]) < 0 {
n := len(l.iterFile.LargestPointKey.UserKey)
if l.split != nil {
n = l.split(l.iterFile.LargestPointKey.UserKey)
}
if l.cmp(prefix, l.iterFile.LargestPointKey.UserKey[:n]) < 0 {
return nil, base.LazyValue{}
}
return l.verify(l.skipEmptyFileForward())
Expand Down

0 comments on commit 4eb0200

Please sign in to comment.