diff --git a/bitcask.go b/bitcask.go index 02a6ef5..cae9b02 100644 --- a/bitcask.go +++ b/bitcask.go @@ -85,10 +85,10 @@ func (b *Bitcask) Has(key string) bool { } func (b *Bitcask) Put(key string, value []byte) error { - if len(key) > b.config.MaxKeySize { + if len(key) > b.config.maxKeySize { return ErrKeyTooLarge } - if len(value) > b.config.MaxValueSize { + if len(value) > b.config.maxValueSize { return ErrValueTooLarge } @@ -284,7 +284,7 @@ func Merge(path string, force bool) error { return nil } -func Open(path string, options ...option) (*Bitcask, error) { +func Open(path string, options ...Option) (*Bitcask, error) { if err := os.MkdirAll(path, 0755); err != nil { return nil, err } diff --git a/options.go b/options.go index 0153676..a9c3eff 100644 --- a/options.go +++ b/options.go @@ -1,47 +1,53 @@ package bitcask const ( + // DefaultMaxDatafileSize is the default maximum datafile size in bytes DefaultMaxDatafileSize = 1 << 20 // 1MB - DefaultMaxKeySize = 64 // 64 bytes - DefaultMaxValueSize = 1 << 16 // 65KB + + // DefaultMaxKeySize is the default maximum key size in bytes + DefaultMaxKeySize = 64 // 64 bytes + + // DefaultMaxValueSize is the default value size in bytes + DefaultMaxValueSize = 1 << 16 // 65KB ) -// Option ... -type Option option - -type option func(*config) error +// Option is a function that takes a config struct and modifies it +type Option func(*config) error type config struct { - MaxDatafileSize int - MaxKeySize int - MaxValueSize int + maxDatafileSize int + maxKeySize int + maxValueSize int } func newDefaultConfig() *config { return &config{ - MaxDatafileSize: DefaultMaxDatafileSize, - MaxKeySize: DefaultMaxKeySize, - MaxValueSize: DefaultMaxValueSize, + maxDatafileSize: DefaultMaxDatafileSize, + maxKeySize: DefaultMaxKeySize, + maxValueSize: DefaultMaxValueSize, } } -func WithMaxDatafileSize(size int) option { +// WithMaxDatafileSize sets the maximum datafile size option +func WithMaxDatafileSize(size int) Option { return func(cfg *config) error { - cfg.MaxDatafileSize = size + cfg.maxDatafileSize = size return nil } } -func WithMaxKeySize(size int) option { +// WithMaxKeySize sets the maximum key size option +func WithMaxKeySize(size int) Option { return func(cfg *config) error { - cfg.MaxKeySize = size + cfg.maxKeySize = size return nil } } -func WithMaxValueSize(size int) option { +// WithMaxValueSize sets the maximum value size option +func WithMaxValueSize(size int) Option { return func(cfg *config) error { - cfg.MaxValueSize = size + cfg.maxValueSize = size return nil } }