mirror of
https://github.com/taigrr/bitcask
synced 2025-01-18 04:03:17 -08:00
Refactored option handling
This commit is contained in:
parent
3f1b90eb23
commit
b6c212d60c
42
bitcask.go
42
bitcask.go
@ -2,23 +2,16 @@ package bitcask
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofrs/flock"
|
"github.com/gofrs/flock"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
DefaultMaxDatafileSize = 1 << 20 // 1MB
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrKeyNotFound = errors.New("error: key not found")
|
ErrKeyNotFound = errors.New("error: key not found")
|
||||||
ErrCannotAcquireLock = errors.New("error: cannot acquire lock")
|
ErrCannotAcquireLock = errors.New("error: cannot acquire lock")
|
||||||
@ -27,6 +20,7 @@ var (
|
|||||||
type Bitcask struct {
|
type Bitcask struct {
|
||||||
*flock.Flock
|
*flock.Flock
|
||||||
|
|
||||||
|
opts Options
|
||||||
path string
|
path string
|
||||||
curr *Datafile
|
curr *Datafile
|
||||||
keydir *Keydir
|
keydir *Keydir
|
||||||
@ -135,39 +129,6 @@ func (b *Bitcask) setMaxDatafileSize(size int64) error {
|
|||||||
return nil
|
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 {
|
func Merge(path string, force bool) error {
|
||||||
fns, err := getDatafiles(path)
|
fns, err := getDatafiles(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -353,6 +314,7 @@ func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) {
|
|||||||
|
|
||||||
bitcask := &Bitcask{
|
bitcask := &Bitcask{
|
||||||
Flock: flock.New(filepath.Join(path, "lock")),
|
Flock: flock.New(filepath.Join(path, "lock")),
|
||||||
|
opts: NewDefaultOptions(),
|
||||||
path: path,
|
path: path,
|
||||||
curr: curr,
|
curr: curr,
|
||||||
keydir: keydir,
|
keydir: keydir,
|
||||||
|
42
options.go
Normal file
42
options.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
36
utils.go
Normal file
36
utils.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user