2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2022-05-25 12:39:51 +00:00
|
|
|
"bytes"
|
2020-12-08 07:51:34 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
2021-06-28 14:01:31 +00:00
|
|
|
"io/fs"
|
2020-12-08 07:51:34 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2021-08-18 07:26:14 +00:00
|
|
|
"strings"
|
2022-07-05 04:55:46 +00:00
|
|
|
"sync"
|
2022-03-21 13:34:31 +00:00
|
|
|
"time"
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2020-10-29 16:12:38 +00:00
|
|
|
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2022-07-05 04:55:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode"
|
2020-11-09 10:22:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2020-10-28 14:49:30 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2020-11-09 10:22:36 +00:00
|
|
|
"go.uber.org/zap"
|
2020-10-28 14:49:30 +00:00
|
|
|
)
|
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
type matcher struct {
|
|
|
|
matchSlow func(string, []byte, string) bool
|
|
|
|
matchBucket func(*bbolt.Bucket, string, string, func([]byte, []byte) error) error
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:34:25 +00:00
|
|
|
// EpochState is an interface that provides access to the
|
|
|
|
// current epoch number.
|
|
|
|
type EpochState interface {
|
|
|
|
// CurrentEpoch must return current epoch height.
|
|
|
|
CurrentEpoch() uint64
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
// DB represents local metabase of storage node.
|
|
|
|
type DB struct {
|
2020-11-09 10:14:52 +00:00
|
|
|
*cfg
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-07-05 04:55:46 +00:00
|
|
|
modeMtx sync.RWMutex
|
|
|
|
mode mode.Mode
|
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
matchers map[object.SearchMatchType]matcher
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
boltDB *bbolt.DB
|
2022-06-15 09:50:59 +00:00
|
|
|
|
|
|
|
initialized bool
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 10:14:52 +00:00
|
|
|
// Option is an option of DB constructor.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
type cfg struct {
|
2020-12-08 07:51:34 +00:00
|
|
|
boltOptions *bbolt.Options // optional
|
|
|
|
|
2022-03-21 13:34:31 +00:00
|
|
|
boltBatchSize int
|
|
|
|
boltBatchDelay time.Duration
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
info Info
|
2020-11-09 10:22:36 +00:00
|
|
|
|
|
|
|
log *logger.Logger
|
2022-07-27 18:34:25 +00:00
|
|
|
|
|
|
|
epochState EpochState
|
2020-11-09 10:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
2020-11-09 10:22:36 +00:00
|
|
|
return &cfg{
|
2020-12-08 07:51:34 +00:00
|
|
|
info: Info{
|
|
|
|
Permission: os.ModePerm, // 0777
|
|
|
|
},
|
2022-03-21 13:34:31 +00:00
|
|
|
boltBatchDelay: bbolt.DefaultMaxBatchDelay,
|
|
|
|
boltBatchSize: bbolt.DefaultMaxBatchSize,
|
2022-09-28 07:41:01 +00:00
|
|
|
log: &logger.Logger{Logger: zap.L()},
|
2020-11-09 10:22:36 +00:00
|
|
|
}
|
2020-11-09 10:14:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// New creates and returns new Metabase instance.
|
|
|
|
func New(opts ...Option) *DB {
|
2020-11-09 10:14:52 +00:00
|
|
|
c := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](c)
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:34:25 +00:00
|
|
|
if c.epochState == nil {
|
|
|
|
panic("metabase: epoch state is not specified")
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
return &DB{
|
2020-11-19 13:53:22 +00:00
|
|
|
cfg: c,
|
2022-05-25 12:39:51 +00:00
|
|
|
matchers: map[object.SearchMatchType]matcher{
|
|
|
|
object.MatchUnknown: {
|
|
|
|
matchSlow: unknownMatcher,
|
|
|
|
matchBucket: unknownMatcherBucket,
|
|
|
|
},
|
|
|
|
object.MatchStringEqual: {
|
|
|
|
matchSlow: stringEqualMatcher,
|
|
|
|
matchBucket: stringEqualMatcherBucket,
|
|
|
|
},
|
|
|
|
object.MatchStringNotEqual: {
|
|
|
|
matchSlow: stringNotEqualMatcher,
|
|
|
|
matchBucket: stringNotEqualMatcherBucket,
|
|
|
|
},
|
|
|
|
object.MatchCommonPrefix: {
|
|
|
|
matchSlow: stringCommonPrefixMatcher,
|
|
|
|
matchBucket: stringCommonPrefixMatcherBucket,
|
|
|
|
},
|
2020-10-28 14:49:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2021-02-01 21:00:40 +00:00
|
|
|
func stringifyValue(key string, objVal []byte) string {
|
2020-10-29 16:12:38 +00:00
|
|
|
switch key {
|
|
|
|
default:
|
2021-02-01 21:00:40 +00:00
|
|
|
return string(objVal)
|
2022-09-08 11:54:21 +00:00
|
|
|
case v2object.FilterHeaderObjectID, v2object.FilterHeaderContainerID, v2object.FilterHeaderParent:
|
|
|
|
return base58.Encode(objVal)
|
2020-12-08 07:51:34 +00:00
|
|
|
case v2object.FilterHeaderPayloadHash, v2object.FilterHeaderHomomorphicHash:
|
2021-02-01 21:00:40 +00:00
|
|
|
return hex.EncodeToString(objVal)
|
2020-12-08 07:51:34 +00:00
|
|
|
case v2object.FilterHeaderCreationEpoch, v2object.FilterHeaderPayloadLength:
|
2021-02-01 21:00:40 +00:00
|
|
|
return strconv.FormatUint(binary.LittleEndian.Uint64(objVal), 10)
|
2020-11-10 12:55:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
// fromHexChar converts a hex character into its value and a success flag.
|
|
|
|
func fromHexChar(c byte) (byte, bool) {
|
|
|
|
switch {
|
|
|
|
case '0' <= c && c <= '9':
|
|
|
|
return c - '0', true
|
|
|
|
case 'a' <= c && c <= 'f':
|
|
|
|
return c - 'a' + 10, true
|
|
|
|
case 'A' <= c && c <= 'F':
|
|
|
|
return c - 'A' + 10, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// destringifyValue is the reverse operation for stringify value.
|
|
|
|
// The last return value returns true if the filter CAN match any value.
|
|
|
|
// The second return value is true iff prefix is true and the filter value is considered
|
|
|
|
// a hex-encoded string. In this case only the first (highest) bits of the last byte should be checked.
|
|
|
|
func destringifyValue(key, value string, prefix bool) ([]byte, bool, bool) {
|
|
|
|
switch key {
|
|
|
|
default:
|
|
|
|
return []byte(value), false, true
|
2022-09-08 11:54:21 +00:00
|
|
|
case v2object.FilterHeaderObjectID, v2object.FilterHeaderContainerID, v2object.FilterHeaderParent:
|
|
|
|
v, err := base58.Decode(value)
|
|
|
|
return v, false, err == nil
|
2022-05-25 12:39:51 +00:00
|
|
|
case v2object.FilterHeaderPayloadHash, v2object.FilterHeaderHomomorphicHash:
|
|
|
|
v, err := hex.DecodeString(value)
|
|
|
|
if err != nil {
|
|
|
|
if !prefix || len(value)%2 == 0 {
|
|
|
|
return v, false, false
|
|
|
|
}
|
|
|
|
// To match the old behaviour we need to process odd length hex strings, such as 'abc'
|
|
|
|
last, ok := fromHexChar(value[len(value)-1])
|
|
|
|
if !ok {
|
|
|
|
return v, false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
v := make([]byte, hex.DecodedLen(len(value)-1)+1)
|
|
|
|
_, err := hex.Decode(v, []byte(value[:len(value)-1]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, false
|
|
|
|
}
|
|
|
|
v[len(v)-1] = last
|
|
|
|
|
|
|
|
return v, true, true
|
|
|
|
}
|
|
|
|
return v, false, err == nil
|
|
|
|
case v2object.FilterHeaderCreationEpoch, v2object.FilterHeaderPayloadLength:
|
|
|
|
u, err := strconv.ParseUint(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, false
|
|
|
|
}
|
|
|
|
raw := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(raw, u)
|
|
|
|
return raw, false, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:00:40 +00:00
|
|
|
func stringEqualMatcher(key string, objVal []byte, filterVal string) bool {
|
|
|
|
return stringifyValue(key, objVal) == filterVal
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
func stringEqualMatcherBucket(b *bbolt.Bucket, fKey string, fValue string, f func([]byte, []byte) error) error {
|
|
|
|
// Ignore the second return value because we check for strict equality.
|
|
|
|
val, _, ok := destringifyValue(fKey, fValue, false)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if data := b.Get(val); data != nil {
|
|
|
|
return f(val, data)
|
|
|
|
}
|
|
|
|
if b.Bucket(val) != nil {
|
|
|
|
return f(val, nil)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:00:40 +00:00
|
|
|
func stringNotEqualMatcher(key string, objVal []byte, filterVal string) bool {
|
|
|
|
return stringifyValue(key, objVal) != filterVal
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
func stringNotEqualMatcherBucket(b *bbolt.Bucket, fKey string, fValue string, f func([]byte, []byte) error) error {
|
|
|
|
// Ignore the second return value because we check for strict inequality.
|
|
|
|
val, _, ok := destringifyValue(fKey, fValue, false)
|
|
|
|
return b.ForEach(func(k, v []byte) error {
|
|
|
|
if !ok || !bytes.Equal(val, k) {
|
|
|
|
return f(k, v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-18 07:26:14 +00:00
|
|
|
func stringCommonPrefixMatcher(key string, objVal []byte, filterVal string) bool {
|
|
|
|
return strings.HasPrefix(stringifyValue(key, objVal), filterVal)
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
func stringCommonPrefixMatcherBucket(b *bbolt.Bucket, fKey string, fVal string, f func([]byte, []byte) error) error {
|
|
|
|
val, checkLast, ok := destringifyValue(fKey, fVal, true)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := val
|
|
|
|
if checkLast {
|
|
|
|
prefix = val[:len(val)-1]
|
|
|
|
}
|
|
|
|
|
2022-08-04 11:59:30 +00:00
|
|
|
if len(val) == 0 {
|
|
|
|
// empty common prefix, all the objects
|
|
|
|
// satisfy that filter
|
|
|
|
return b.ForEach(f)
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
c := b.Cursor()
|
|
|
|
for k, v := c.Seek(val); bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
|
|
|
if checkLast && (len(k) == len(prefix) || k[len(prefix)]>>4 != val[len(val)-1]) {
|
|
|
|
// If the last byte doesn't match, this means the prefix does no longer match,
|
|
|
|
// so we need to break here.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := f(k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func unknownMatcher(_ string, _ []byte, _ string) bool {
|
|
|
|
return false
|
2020-10-29 16:12:38 +00:00
|
|
|
}
|
2020-11-09 10:14:52 +00:00
|
|
|
|
2022-05-25 12:39:51 +00:00
|
|
|
func unknownMatcherBucket(_ *bbolt.Bucket, _ string, _ string, _ func([]byte, []byte) error) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// bucketKeyHelper returns byte representation of val that is used as a key
|
|
|
|
// in boltDB. Useful for getting filter values from unique and list indexes.
|
|
|
|
func bucketKeyHelper(hdr string, val string) []byte {
|
|
|
|
switch hdr {
|
2022-09-08 11:54:21 +00:00
|
|
|
case v2object.FilterHeaderParent:
|
|
|
|
v, err := base58.Decode(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v
|
2020-12-08 07:51:34 +00:00
|
|
|
case v2object.FilterHeaderPayloadHash:
|
|
|
|
v, err := hex.DecodeString(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
case v2object.FilterHeaderSplitID:
|
|
|
|
s := object.NewSplitID()
|
|
|
|
|
|
|
|
err := s.Parse(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.ToV2()
|
|
|
|
default:
|
|
|
|
return []byte(val)
|
2020-11-09 10:14:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-09 10:22:36 +00:00
|
|
|
|
2022-07-19 11:27:12 +00:00
|
|
|
// SetLogger sets logger. It is used after the shard ID was generated to use it in logs.
|
2022-09-28 07:41:01 +00:00
|
|
|
func (db *DB) SetLogger(l *logger.Logger) {
|
2022-07-19 11:27:12 +00:00
|
|
|
db.log = l
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:22:36 +00:00
|
|
|
// WithLogger returns option to set logger of DB.
|
|
|
|
func WithLogger(l *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.log = l
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// WithBoltDBOptions returns option to specify BoltDB options.
|
|
|
|
func WithBoltDBOptions(opts *bbolt.Options) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.boltOptions = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPath returns option to set system path to Metabase.
|
|
|
|
func WithPath(path string) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.info.Path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPermissions returns option to specify permission bits
|
|
|
|
// of Metabase system path.
|
2021-06-28 14:01:31 +00:00
|
|
|
func WithPermissions(perm fs.FileMode) Option {
|
2020-12-08 07:51:34 +00:00
|
|
|
return func(c *cfg) {
|
|
|
|
c.info.Permission = perm
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 13:34:31 +00:00
|
|
|
|
2022-06-15 06:59:51 +00:00
|
|
|
// WithMaxBatchSize returns option to specify maximum concurrent operations
|
2022-03-21 13:34:31 +00:00
|
|
|
// to be processed in a single transactions.
|
|
|
|
// This option is missing from `bbolt.Options` but is set right after DB is open.
|
2022-06-15 06:59:51 +00:00
|
|
|
func WithMaxBatchSize(s int) Option {
|
2022-03-21 13:34:31 +00:00
|
|
|
return func(c *cfg) {
|
2022-06-15 06:59:51 +00:00
|
|
|
if s != 0 {
|
|
|
|
c.boltBatchSize = s
|
|
|
|
}
|
2022-03-21 13:34:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 06:59:51 +00:00
|
|
|
// WithMaxBatchDelay returns option to specify maximum time to wait before
|
2022-03-21 13:34:31 +00:00
|
|
|
// the batch of concurrent transactions is processed.
|
|
|
|
// This option is missing from `bbolt.Options` but is set right after DB is open.
|
2022-06-15 06:59:51 +00:00
|
|
|
func WithMaxBatchDelay(d time.Duration) Option {
|
2022-03-21 13:34:31 +00:00
|
|
|
return func(c *cfg) {
|
2022-06-15 06:59:51 +00:00
|
|
|
if d != 0 {
|
|
|
|
c.boltBatchDelay = d
|
|
|
|
}
|
2022-03-21 13:34:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-27 18:34:25 +00:00
|
|
|
|
|
|
|
// WithEpochState return option to specify a source of current epoch height.
|
|
|
|
func WithEpochState(s EpochState) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.epochState = s
|
|
|
|
}
|
|
|
|
}
|