Skip to content

Commit

Permalink
add method to retrieve count of external files
Browse files Browse the repository at this point in the history
This allows callers to operate differently if the database contains
external files that might be slower to read than expected.
  • Loading branch information
stevendanna committed Feb 19, 2024
1 parent 5a2d779 commit dbc9896
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,43 @@ func (d *DB) Download(ctx context.Context, spans []DownloadSpan) error {
return nil
}

// ExternalFileCount returns the number of external files present in
// the range `[start, end]`.
func (d *DB) ExternalFileCount(ctx context.Context, start, end []byte) (int, error) {
if d.opts.Comparer.Compare(start, end) > 0 {
return 0, errors.New("invalid key-range specified (start > end)")
}

rs := d.loadReadState()
defer rs.unref()

cmp := d.opts.Comparer
overlaps := func(startA, endA, startB, endB []byte) bool {
return !(cmp.Compare(endB, startA) < 0 || cmp.Compare(endA, startB) < 0)
}

count := 0
for _level, files := range rs.current.Levels {

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-bsds

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / code-cover-gen

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-crossversion

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-crossversion

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-cgo

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-cgo

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-cgo

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-cgo

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-cgo

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-cgo

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-cgo

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-cgo

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-invariants

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-invariants

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-invariants

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-invariants

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-invariants

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-invariants

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-invariants

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-no-invariants

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-32bit

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-32bit

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-32bit

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-32bit

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-32bit

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-32bit

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-32bit

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-32bit

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-macos

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-macos

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-macos

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-macos

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-macos

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-macos

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-macos

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-macos

_level declared and not used (compile)

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-race

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-linux-race

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-windows

_level declared and not used

Check failure on line 2040 in db.go

View workflow job for this annotation

GitHub Actions / go-windows

_level declared and not used
iter := files.Iter()
for m := iter.First(); m != nil; m = iter.Next() {
if err := ctx.Err(); err != nil {
return 0, err
}

if overlaps(start, end, m.Smallest.UserKey, m.Largest.UserKey) {
objMeta, err := d.objProvider.Lookup(fileTypeTable, m.FileBacking.DiskFileNum)
if err != nil {
return 0, err
}
if objMeta.IsExternal() {
count++
}
}
}
}
return count, nil
}

// Flush the memtable to stable storage.
func (d *DB) Flush() error {
flushDone, err := d.AsyncFlush()
Expand Down

0 comments on commit dbc9896

Please sign in to comment.