frostfs-node/pkg/local_object_storage/bucket/boltdb/boltdb.go

106 lines
2.2 KiB
Go
Raw Normal View History

package boltdb
import (
"io/ioutil"
"log"
"os"
"path"
2020-07-24 13:54:03 +00:00
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/bucket"
"github.com/pkg/errors"
"github.com/spf13/viper"
"go.etcd.io/bbolt"
)
type (
2020-07-24 13:54:03 +00:00
boltBucket struct {
db *bbolt.DB
name []byte
}
// Options groups the BoltDB bucket's options.
Options struct {
bbolt.Options
Name []byte
Path string
Perm os.FileMode
}
)
2020-07-24 13:54:03 +00:00
const defaultFilePermission = 0777
2020-07-24 13:54:03 +00:00
var errEmptyPath = errors.New("database empty path")
2020-07-24 13:54:03 +00:00
const name = "boltbucket"
func makeCopy(val []byte) []byte {
tmp := make([]byte, len(val))
copy(tmp, val)
return tmp
}
// NewOptions prepares options for badger instance.
2020-07-24 13:54:03 +00:00
func NewOptions(v *viper.Viper) (opts Options, err error) {
opts = Options{
Options: bbolt.Options{
// set defaults:
Timeout: bbolt.DefaultOptions.Timeout,
FreelistType: bbolt.DefaultOptions.FreelistType,
// set config options:
2020-07-24 13:54:03 +00:00
NoSync: v.GetBool(name + ".no_sync"),
ReadOnly: v.GetBool(name + ".read_only"),
NoGrowSync: v.GetBool(name + ".no_grow_sync"),
NoFreelistSync: v.GetBool(name + ".no_freelist_sync"),
PageSize: v.GetInt(name + ".page_size"),
MmapFlags: v.GetInt(name + ".mmap_flags"),
InitialMmapSize: v.GetInt(name + ".initial_mmap_size"),
},
Name: []byte(name),
Perm: defaultFilePermission,
2020-07-24 13:54:03 +00:00
Path: v.GetString(name + ".path"),
}
if opts.Path == "" {
return opts, errEmptyPath
}
2020-07-24 13:54:03 +00:00
if tmp := v.GetDuration(name + ".lock_timeout"); tmp > 0 {
opts.Timeout = tmp
}
2020-07-24 13:54:03 +00:00
if perm := v.GetUint32(name + ".perm"); perm != 0 {
opts.Perm = os.FileMode(perm)
}
base := path.Dir(opts.Path)
if err := os.MkdirAll(base, opts.Perm); err != nil {
return opts, errors.Wrapf(err, "could not use `%s` dir", base)
}
return opts, nil
}
// NewBucket creates badger-bucket instance.
2020-07-24 13:54:03 +00:00
func NewBucket(opts *Options) (bucket.Bucket, error) {
log.SetOutput(ioutil.Discard) // disable default logger
db, err := bbolt.Open(opts.Path, opts.Perm, &opts.Options)
if err != nil {
return nil, err
}
err = db.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(opts.Name)
return err
})
if err != nil {
return nil, err
}
2020-07-24 13:54:03 +00:00
return &boltBucket{db: db, name: opts.Name}, nil
}