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

100 lines
1.7 KiB
Go
Raw Normal View History

package fsbucket
import (
"os"
"github.com/mr-tron/base58"
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.uber.org/atomic"
)
type (
2020-07-24 13:54:03 +00:00
Bucket struct {
dir string
perm os.FileMode
}
treeBucket struct {
dir string
perm os.FileMode
depth int
prefixLength int
sz *atomic.Int64
}
)
2020-07-24 13:54:03 +00:00
const name = "fsbucket"
const (
defaultDirectory = "fsbucket"
defaultPermissions = 0755
defaultDepth = 2
defaultPrefixLen = 2
)
2020-07-24 13:54:03 +00:00
var errShortKey = errors.New("key is too short for tree fs bucket")
func stringifyKey(key []byte) string {
return base58.Encode(key)
}
func decodeKey(key string) []byte {
k, err := base58.Decode(key)
if err != nil {
panic(err) // it can fail only for not base58 strings
}
return k
}
2020-07-24 13:54:03 +00:00
// NewBucket creates new file system bucket instance.
func NewBucket(v *viper.Viper) (bucket.Bucket, error) {
var (
dir string
perm os.FileMode
prefixLen int
depth int
)
2020-07-24 13:54:03 +00:00
if dir = v.GetString(name + ".directory"); dir == "" {
dir = defaultDirectory
}
2020-07-24 13:54:03 +00:00
if perm = os.FileMode(v.GetInt(name + ".permissions")); perm == 0 {
perm = defaultPermissions
}
2020-07-24 13:54:03 +00:00
if depth = v.GetInt(name + ".depth"); depth <= 0 {
depth = defaultDepth
}
2020-07-24 13:54:03 +00:00
if prefixLen = v.GetInt(name + ".prefix_len"); prefixLen <= 0 {
prefixLen = defaultPrefixLen
}
if err := os.MkdirAll(dir, perm); err != nil {
2020-07-24 13:54:03 +00:00
return nil, errors.Wrapf(err, "could not create bucket %s", name)
}
2020-07-24 13:54:03 +00:00
if v.GetBool(name + ".tree_enabled") {
b := &treeBucket{
dir: dir,
perm: perm,
depth: depth,
prefixLength: prefixLen,
}
b.sz = atomic.NewInt64(b.size())
return b, nil
}
2020-07-24 13:54:03 +00:00
return &Bucket{
dir: dir,
perm: perm,
}, nil
}