Skip to content

Commit

Permalink
db: Expose logic to calculate the table cache size
Browse files Browse the repository at this point in the history
This will be used by cockroach when calculating the table cache
size for a shared cache, instead of duplicating the logic.
  • Loading branch information
bananabrick committed Oct 13, 2021
1 parent 0ee4290 commit cdbcfe9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
10 changes: 5 additions & 5 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
)

const (
// MinTableCacheSize is the minimum size of the table cache, for a given store.
MinTableCacheSize = 64
// minTableCacheSize is the minimum size of the table cache, for a single db.
minTableCacheSize = 64

// NumNonTableCacheFiles is an approximation for the number of MaxOpenFiles
// that we don't use for table caches, for a given store.
NumNonTableCacheFiles = 10
// numNonTableCacheFiles is an approximation for the number of files
// that we don't use for table caches, for a given db.
numNonTableCacheFiles = 10
)

var (
Expand Down
17 changes: 13 additions & 4 deletions open.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ const (
maxMemTableSize = 4 << 30 // 4 GB
)

// TableCacheSize can be used to determine the table
// cache size for a single db, given the maximum open
// files which can be used by a table cache which is
// only used by a single db.
func TableCacheSize(maxOpenFiles int) int {
tableCacheSize := maxOpenFiles - numNonTableCacheFiles
if tableCacheSize < minTableCacheSize {
tableCacheSize = minTableCacheSize
}
return tableCacheSize
}

// Open opens a DB whose files live in the given directory.
func Open(dirname string, opts *Options) (db *DB, _ error) {
// Make a copy of the options so that we don't mutate the passed in options.
Expand Down Expand Up @@ -105,10 +117,7 @@ func Open(dirname string, opts *Options) (db *DB, _ error) {
d.equal = bytes.Equal
}

tableCacheSize := opts.MaxOpenFiles - NumNonTableCacheFiles
if tableCacheSize < MinTableCacheSize {
tableCacheSize = MinTableCacheSize
}
tableCacheSize := TableCacheSize(opts.MaxOpenFiles)
d.tableCache = newTableCacheContainer(opts.TableCache, d.cacheID, dirname, opts.FS, d.opts, tableCacheSize)
d.newIters = d.tableCache.newIters

Expand Down

0 comments on commit cdbcfe9

Please sign in to comment.