Add docs for options

This commit is contained in:
James Mills 2019-03-21 17:20:53 +10:00
parent 34ad78efc0
commit 27eb922ba2
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
2 changed files with 27 additions and 21 deletions

@ -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
}

@ -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
}
}