From b6c212d60ca1c16b209a23135679f44235febb97 Mon Sep 17 00:00:00 2001 From: James Mills <1290234+prologic@users.noreply.github.com> Date: Thu, 14 Mar 2019 21:14:55 +1000 Subject: [PATCH] Refactored option handling --- bitcask.go | 42 ++---------------------------------------- options.go | 42 ++++++++++++++++++++++++++++++++++++++++++ utils.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 40 deletions(-) create mode 100644 options.go create mode 100644 utils.go diff --git a/bitcask.go b/bitcask.go index 280ae87..5b2c017 100644 --- a/bitcask.go +++ b/bitcask.go @@ -2,23 +2,16 @@ package bitcask import ( "errors" - "fmt" "io" "io/ioutil" "os" "path/filepath" - "sort" - "strconv" "strings" "time" "github.com/gofrs/flock" ) -const ( - DefaultMaxDatafileSize = 1 << 20 // 1MB -) - var ( ErrKeyNotFound = errors.New("error: key not found") ErrCannotAcquireLock = errors.New("error: cannot acquire lock") @@ -27,6 +20,7 @@ var ( type Bitcask struct { *flock.Flock + opts Options path string curr *Datafile keydir *Keydir @@ -135,39 +129,6 @@ func (b *Bitcask) setMaxDatafileSize(size int64) error { return nil } -func WithMaxDatafileSize(size int64) func(*Bitcask) error { - return func(b *Bitcask) error { - return b.setMaxDatafileSize(size) - } -} - -func getDatafiles(path string) ([]string, error) { - fns, err := filepath.Glob(fmt.Sprintf("%s/*.data", path)) - if err != nil { - return nil, err - } - sort.Strings(fns) - return fns, nil -} - -func parseIds(fns []string) ([]int, error) { - var ids []int - for _, fn := range fns { - fn = filepath.Base(fn) - ext := filepath.Ext(fn) - if ext != ".data" { - continue - } - id, err := strconv.ParseInt(strings.TrimSuffix(fn, ext), 10, 32) - if err != nil { - return nil, err - } - ids = append(ids, int(id)) - } - sort.Ints(ids) - return ids, nil -} - func Merge(path string, force bool) error { fns, err := getDatafiles(path) if err != nil { @@ -353,6 +314,7 @@ func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) { bitcask := &Bitcask{ Flock: flock.New(filepath.Join(path, "lock")), + opts: NewDefaultOptions(), path: path, curr: curr, keydir: keydir, diff --git a/options.go b/options.go new file mode 100644 index 0000000..3279172 --- /dev/null +++ b/options.go @@ -0,0 +1,42 @@ +package bitcask + +const ( + DefaultMaxDatafileSize = 1 << 20 // 1MB + DefaultMaxKeySize = 64 // 64 bytes + DefaultMaxValueSize = 1 << 16 // 65KB +) + +type Options struct { + MaxDatafileSize int + MaxKeySize int + MaxValueSize int +} + +func NewDefaultOptions() Options { + return Options{ + MaxDatafileSize: DefaultMaxDatafileSize, + MaxKeySize: DefaultMaxKeySize, + MaxValueSize: DefaultMaxValueSize, + } +} + +func WithMaxDatafileSize(size int) func(*Bitcask) error { + return func(b *Bitcask) error { + b.opts.MaxDatafileSize = size + return nil + } +} + +func WithMaxKeySize(size int) func(*Bitcask) error { + return func(b *Bitcask) error { + b.opts.MaxKeySize = size + return nil + } +} + +func WithMaxValueSize(size int) func(*Bitcask) error { + return func(b *Bitcask) error { + b.opts.MaxValueSize = size + return nil + } +} diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..4e086f2 --- /dev/null +++ b/utils.go @@ -0,0 +1,36 @@ +package bitcask + +import ( + "fmt" + "path/filepath" + "sort" + "strconv" + "strings" +) + +func getDatafiles(path string) ([]string, error) { + fns, err := filepath.Glob(fmt.Sprintf("%s/*.data", path)) + if err != nil { + return nil, err + } + sort.Strings(fns) + return fns, nil +} + +func parseIds(fns []string) ([]int, error) { + var ids []int + for _, fn := range fns { + fn = filepath.Base(fn) + ext := filepath.Ext(fn) + if ext != ".data" { + continue + } + id, err := strconv.ParseInt(strings.TrimSuffix(fn, ext), 10, 32) + if err != nil { + return nil, err + } + ids = append(ids, int(id)) + } + sort.Ints(ids) + return ids, nil +}