Refactored configuration option handling. Fixes #3

This commit is contained in:
James Mills
2019-03-17 13:53:30 +10:00
parent e83608b903
commit 6fe6fe0689
3 changed files with 400 additions and 21 deletions

View File

@@ -16,7 +16,7 @@ import (
type Bitcask struct {
*flock.Flock
opts Options
config *config
path string
curr *Datafile
keydir *Keydir
@@ -70,11 +70,11 @@ func (b *Bitcask) Get(key string) ([]byte, error) {
}
func (b *Bitcask) Put(key string, value []byte) error {
if len(key) > b.opts.MaxKeySize {
return fmt.Errorf("error: key too large %d > %d", len(key), b.opts.MaxKeySize)
if len(key) > b.config.MaxKeySize {
return fmt.Errorf("error: key too large %d > %d", len(key), b.config.MaxKeySize)
}
if len(value) > b.opts.MaxValueSize {
return fmt.Errorf("error: value too large %d > %d", len(value), b.opts.MaxValueSize)
if len(value) > b.config.MaxValueSize {
return fmt.Errorf("error: value too large %d > %d", len(value), b.config.MaxValueSize)
}
offset, err := b.put(key, value)
@@ -261,7 +261,7 @@ func Merge(path string, force bool) error {
return nil
}
func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) {
func Open(path string, options ...option) (*Bitcask, error) {
if err := os.MkdirAll(path, 0755); err != nil {
return nil, err
}
@@ -344,7 +344,7 @@ func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) {
bitcask := &Bitcask{
Flock: flock.New(filepath.Join(path, "lock")),
opts: NewDefaultOptions(),
config: NewDefaultConfig(),
path: path,
curr: curr,
keydir: keydir,
@@ -354,8 +354,8 @@ func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) {
maxDatafileSize: DefaultMaxDatafileSize,
}
for _, option := range options {
err = option(bitcask)
for _, opt := range options {
err = opt(bitcask.config)
if err != nil {
return nil, err
}