Skip to content

Commit

Permalink
config of buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
sebheitzmann committed Jan 1, 2025
1 parent e625db2 commit 82054b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions simpledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const MemStoreMaxSizeBytes uint64 = 1024 * 1024 * 1024 // 1gb
const NumSSTablesToTriggerCompaction int = 10
const DefaultCompactionMaxSizeBytes uint64 = 5 * 1024 * 1024 * 1024 // 5gb
const DefaultCompactionInterval = 5 * time.Second
const DefaultWriteBufferSizeBytes uint64 = 4 * 1024 * 1024 // 4Mb
const DefaultReadBufferSizeBytes uint64 = 4 * 1024 * 1024 // 4Mb

var ErrNotFound = errors.New("ErrNotFound")
var ErrNotOpenedYet = errors.New("database has not been opened yet, please call Open() first")
Expand Down Expand Up @@ -90,6 +92,9 @@ type DB struct {
compactionTicker *time.Ticker
compactionTickerStopChannel chan interface{}
doneCompactionChannel chan bool

writeBufferSizeBytes uint64
readBufferSizeBytes uint64
}

func (db *DB) Open() error {
Expand Down Expand Up @@ -327,6 +332,8 @@ func NewSimpleDB(basePath string, extraOptions ...ExtraOption) (*DB, error) {
NumSSTablesToTriggerCompaction,
DefaultCompactionMaxSizeBytes,
DefaultCompactionInterval,
DefaultWriteBufferSizeBytes,
DefaultReadBufferSizeBytes,
}

for _, extraOption := range extraOptions {
Expand Down Expand Up @@ -377,6 +384,8 @@ type ExtraOptions struct {
compactionFileThreshold int
compactionMaxSizeBytes uint64
compactionRunInterval time.Duration
writeBufferSizeBytes uint64
readBufferSizeBytes uint64
}

type ExtraOption func(options *ExtraOptions)
Expand Down Expand Up @@ -433,3 +442,17 @@ func CompactionMaxSizeBytes(n uint64) ExtraOption {
args.compactionMaxSizeBytes = n
}
}

// WriteBufferSizeBytes is the write buffer size for all buffer used by simple db.
func WriteBufferSizeBytes(n uint64) ExtraOption {
return func(args *ExtraOptions) {
args.writeBufferSizeBytes = n
}
}

// ReadBufferSizeBytes is the read buffer size for all buffer used by simple db.
func ReadBufferSizeBytes(n uint64) ExtraOption {
return func(args *ExtraOptions) {
args.readBufferSizeBytes = n
}
}
4 changes: 2 additions & 2 deletions simpledb/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func executeFlush(db *DB, flushAction memStoreFlushAction) error {
err = memStoreToFlush.FlushWithTombstones(
sstables.WriteBasePath(writePath),
sstables.WithKeyComparator(db.cmp),
sstables.WriteBufferSizeBytes(16*1024),
sstables.WriteBufferSizeBytes(int(db.writeBufferSizeBytes)),
sstables.BloomExpectedNumberOfElements(numElements))
if err != nil {
return err
Expand All @@ -67,7 +67,7 @@ func executeFlush(db *DB, flushAction memStoreFlushAction) error {
reader, err := sstables.NewSSTableReader(
sstables.ReadBasePath(writePath),
sstables.ReadWithKeyComparator(db.cmp),
sstables.ReadBufferSizeBytes(4*1024),
sstables.ReadBufferSizeBytes(int(db.readBufferSizeBytes)),
)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion simpledb/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (db *DB) reconstructSSTables() error {
reader, err := sstables.NewSSTableReader(
sstables.ReadBasePath(p),
sstables.ReadWithKeyComparator(db.cmp),
sstables.ReadBufferSizeBytes(16*1024),
sstables.ReadBufferSizeBytes(int(db.readBufferSizeBytes)),
)
if err != nil {
return err
Expand Down

0 comments on commit 82054b4

Please sign in to comment.