2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2022-03-21 14:15:09 +00:00
|
|
|
"encoding/binary"
|
2020-12-08 07:51:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-03-21 14:15:09 +00:00
|
|
|
gio "io"
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2021-03-22 08:10:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-10-28 14:49:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-03-15 15:28:39 +00:00
|
|
|
objectCore "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-03-02 09:17:32 +00:00
|
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
2021-04-15 07:23:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-10-28 14:49:30 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2022-08-19 12:36:37 +00:00
|
|
|
"go.uber.org/zap"
|
2020-10-28 14:49:30 +00:00
|
|
|
)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
type (
|
|
|
|
namedBucketItem struct {
|
|
|
|
name, key, val []byte
|
|
|
|
}
|
|
|
|
)
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// PutPrm groups the parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2020-12-08 09:56:14 +00:00
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
id []byte
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PutRes groups the resulting values of Put operation.
|
2020-12-08 09:56:14 +00:00
|
|
|
type PutRes struct{}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetObject is a Put option to set object to save.
|
|
|
|
func (p *PutPrm) SetObject(obj *objectSDK.Object) {
|
|
|
|
p.obj = obj
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
// SetStorageID is a Put option to set storage ID to save.
|
|
|
|
func (p *PutPrm) SetStorageID(id []byte) {
|
2022-07-12 14:59:37 +00:00
|
|
|
p.id = id
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
var (
|
2021-04-06 10:56:06 +00:00
|
|
|
ErrUnknownObjectType = errors.New("unknown object type")
|
|
|
|
ErrIncorrectSplitInfoUpdate = errors.New("updating split info on object without it")
|
|
|
|
ErrIncorrectRootObject = errors.New("invalid root object")
|
2020-10-28 14:49:30 +00:00
|
|
|
)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// Put saves object header in metabase. Object payload expected to be cut.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-03-17 13:26:17 +00:00
|
|
|
// Returns an error of type apistatus.ObjectAlreadyRemoved if object has been placed in graveyard.
|
2022-07-27 18:38:28 +00:00
|
|
|
// Returns the object.ErrObjectIsExpired if the object is presented but already expired.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (db *DB) Put(prm PutPrm) (res PutRes, err error) {
|
2022-07-21 13:26:25 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
currEpoch := db.epochState.CurrentEpoch()
|
|
|
|
|
2021-04-09 09:18:38 +00:00
|
|
|
err = db.boltDB.Batch(func(tx *bbolt.Tx) error {
|
2022-07-27 18:38:28 +00:00
|
|
|
return db.put(tx, prm.obj, prm.id, nil, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
})
|
2022-03-02 09:17:32 +00:00
|
|
|
if err == nil {
|
|
|
|
storagelog.Write(db.log,
|
2022-03-15 15:28:39 +00:00
|
|
|
storagelog.AddressField(objectCore.AddressOf(prm.obj)),
|
2022-03-02 09:17:32 +00:00
|
|
|
storagelog.OpField("metabase PUT"))
|
|
|
|
}
|
2020-12-08 09:56:14 +00:00
|
|
|
|
|
|
|
return
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
func (db *DB) put(
|
2022-07-06 14:09:50 +00:00
|
|
|
tx *bbolt.Tx, obj *objectSDK.Object, id []byte,
|
2022-07-27 18:38:28 +00:00
|
|
|
si *objectSDK.SplitInfo, currEpoch uint64) error {
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, ok := obj.ContainerID()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing container in object")
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
isParent := si != nil
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
exists, err := db.exists(tx, object.AddressOf(obj), currEpoch)
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
if errors.As(err, &splitInfoError) {
|
|
|
|
exists = true // object exists, however it is virtual
|
|
|
|
} else if err != nil {
|
|
|
|
return err // return any error besides SplitInfoError
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// most right child and split header overlap parent so we have to
|
|
|
|
// check if object exists to not overwrite it twice
|
|
|
|
if exists {
|
2022-07-06 14:09:50 +00:00
|
|
|
// When storage engine moves objects between different sub-storages,
|
|
|
|
// it calls metabase.Put method with new storage ID, thus triggering this code.
|
2020-12-08 07:51:34 +00:00
|
|
|
if !isParent && id != nil {
|
2022-07-06 14:09:50 +00:00
|
|
|
return updateStorageID(tx, object.AddressOf(obj), id)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// when storage already has last object in split hierarchy and there is
|
|
|
|
// a linking object to put (or vice versa), we should update split info
|
|
|
|
// with object ids of these objects
|
|
|
|
if isParent {
|
2022-03-03 14:19:05 +00:00
|
|
|
return updateSplitInfo(tx, object.AddressOf(obj), si)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
if par := obj.Parent(); par != nil && !isParent { // limit depth by two
|
2020-12-08 07:51:34 +00:00
|
|
|
parentSI, err := splitInfoFromObject(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
err = db.put(tx, par, id, parentSI, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err = putUniqueIndexes(tx, obj, si, id)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't put unique indexes: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err = updateListIndexes(tx, obj, putListIndexItem)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't put list indexes: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err = updateFKBTIndexes(tx, obj, putFKBTIndexItem)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't put fake bucket tree indexes: %w", err)
|
2021-01-22 11:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update container volume size estimation
|
|
|
|
if obj.Type() == objectSDK.TypeRegular && !isParent {
|
2022-05-31 17:00:41 +00:00
|
|
|
err = changeContainerSize(tx, cnr, obj.PayloadSize(), true)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:36:37 +00:00
|
|
|
if !isParent {
|
|
|
|
err = db.updateCounter(tx, 1, true)
|
|
|
|
if err != nil {
|
|
|
|
db.log.Error("could not increase object counter: %w",
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
func putUniqueIndexes(
|
|
|
|
tx *bbolt.Tx,
|
|
|
|
obj *objectSDK.Object,
|
|
|
|
si *objectSDK.SplitInfo,
|
2022-07-06 14:09:50 +00:00
|
|
|
id []byte,
|
2022-03-21 13:59:05 +00:00
|
|
|
) error {
|
2020-12-08 07:51:34 +00:00
|
|
|
isParent := si != nil
|
2022-03-03 14:19:05 +00:00
|
|
|
addr := object.AddressOf(obj)
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := addr.Container()
|
|
|
|
objKey := objectKey(addr.Object())
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// add value to primary unique bucket
|
|
|
|
if !isParent {
|
|
|
|
var bucketName []byte
|
|
|
|
|
|
|
|
switch obj.Type() {
|
|
|
|
case objectSDK.TypeRegular:
|
2022-05-31 17:00:41 +00:00
|
|
|
bucketName = primaryBucketName(cnr)
|
2020-12-08 07:51:34 +00:00
|
|
|
case objectSDK.TypeTombstone:
|
2022-05-31 17:00:41 +00:00
|
|
|
bucketName = tombstoneBucketName(cnr)
|
2020-12-08 07:51:34 +00:00
|
|
|
case objectSDK.TypeStorageGroup:
|
2022-05-31 17:00:41 +00:00
|
|
|
bucketName = storageGroupBucketName(cnr)
|
2022-02-15 09:53:35 +00:00
|
|
|
case objectSDK.TypeLock:
|
2022-05-12 16:37:46 +00:00
|
|
|
bucketName = bucketNameLockers(cnr)
|
2020-12-08 07:51:34 +00:00
|
|
|
default:
|
2022-03-21 13:59:05 +00:00
|
|
|
return ErrUnknownObjectType
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
rawObject, err := obj.CutPayload().Marshal()
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't marshal object header: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err = putUniqueIndexItem(tx, namedBucketItem{
|
2020-12-08 07:51:34 +00:00
|
|
|
name: bucketName,
|
|
|
|
key: objKey,
|
|
|
|
val: rawObject,
|
|
|
|
})
|
2022-03-21 13:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
// index storageID if it is present
|
2020-12-08 07:51:34 +00:00
|
|
|
if id != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
err = putUniqueIndexItem(tx, namedBucketItem{
|
2022-05-31 17:00:41 +00:00
|
|
|
name: smallBucketName(cnr),
|
2020-12-08 07:51:34 +00:00
|
|
|
key: objKey,
|
2022-07-06 14:09:50 +00:00
|
|
|
val: id,
|
2020-12-08 07:51:34 +00:00
|
|
|
})
|
2022-03-21 13:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// index root object
|
|
|
|
if obj.Type() == objectSDK.TypeRegular && !obj.HasParent() {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
splitInfo []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if isParent {
|
|
|
|
splitInfo, err = si.Marshal()
|
2020-10-28 14:49:30 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't marshal split info: %w", err)
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err = putUniqueIndexItem(tx, namedBucketItem{
|
2022-05-31 17:00:41 +00:00
|
|
|
name: rootBucketName(cnr),
|
2020-12-08 07:51:34 +00:00
|
|
|
key: objKey,
|
|
|
|
val: splitInfo,
|
|
|
|
})
|
2022-03-21 13:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
type updateIndexItemFunc = func(tx *bbolt.Tx, item namedBucketItem) error
|
|
|
|
|
|
|
|
func updateListIndexes(tx *bbolt.Tx, obj *objectSDK.Object, f updateIndexItemFunc) error {
|
2022-05-31 17:00:41 +00:00
|
|
|
idObj, _ := obj.ID()
|
|
|
|
cnr, _ := obj.ContainerID()
|
|
|
|
objKey := objectKey(idObj)
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2022-05-11 16:35:01 +00:00
|
|
|
cs, _ := obj.PayloadChecksum()
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// index payload hashes
|
2022-03-21 13:59:05 +00:00
|
|
|
err := f(tx, namedBucketItem{
|
2022-05-31 17:00:41 +00:00
|
|
|
name: payloadHashBucketName(cnr),
|
2022-05-11 16:35:01 +00:00
|
|
|
key: cs.Value(),
|
2020-12-08 07:51:34 +00:00
|
|
|
val: objKey,
|
|
|
|
})
|
2022-03-21 13:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
idParent, ok := obj.ParentID()
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// index parent ids
|
2022-05-12 16:37:46 +00:00
|
|
|
if ok {
|
2022-03-21 13:59:05 +00:00
|
|
|
err := f(tx, namedBucketItem{
|
2022-05-31 17:00:41 +00:00
|
|
|
name: parentBucketName(cnr),
|
|
|
|
key: objectKey(idParent),
|
2020-12-08 07:51:34 +00:00
|
|
|
val: objKey,
|
|
|
|
})
|
2022-03-21 13:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// index split ids
|
|
|
|
if obj.SplitID() != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
err := f(tx, namedBucketItem{
|
2022-05-31 17:00:41 +00:00
|
|
|
name: splitBucketName(cnr),
|
2020-12-08 07:51:34 +00:00
|
|
|
key: obj.SplitID().ToV2(),
|
|
|
|
val: objKey,
|
|
|
|
})
|
2022-03-21 13:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
func updateFKBTIndexes(tx *bbolt.Tx, obj *objectSDK.Object, f updateIndexItemFunc) error {
|
2022-05-31 17:00:41 +00:00
|
|
|
id, _ := obj.ID()
|
|
|
|
cnr, _ := obj.ContainerID()
|
2022-05-12 16:37:46 +00:00
|
|
|
objKey := []byte(id.EncodeToString())
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
attrs := obj.Attributes()
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err := f(tx, namedBucketItem{
|
2022-05-31 17:00:41 +00:00
|
|
|
name: ownerBucketName(cnr),
|
|
|
|
key: []byte(obj.OwnerID().EncodeToString()),
|
2020-12-08 07:51:34 +00:00
|
|
|
val: objKey,
|
2020-10-28 14:49:30 +00:00
|
|
|
})
|
2022-03-21 13:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// user specified attributes
|
|
|
|
for i := range attrs {
|
2022-03-21 13:59:05 +00:00
|
|
|
err := f(tx, namedBucketItem{
|
2022-05-31 17:00:41 +00:00
|
|
|
name: attributeBucketName(cnr, attrs[i].Key()),
|
2020-12-08 07:51:34 +00:00
|
|
|
key: []byte(attrs[i].Value()),
|
|
|
|
val: objKey,
|
|
|
|
})
|
2022-03-21 13:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func putUniqueIndexItem(tx *bbolt.Tx, item namedBucketItem) error {
|
|
|
|
bkt, err := tx.CreateBucketIfNotExists(item.name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create index %v: %w", item.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bkt.Put(item.key, item.val)
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func putFKBTIndexItem(tx *bbolt.Tx, item namedBucketItem) error {
|
|
|
|
bkt, err := tx.CreateBucketIfNotExists(item.name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create index %v: %w", item.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fkbtRoot, err := bkt.CreateBucketIfNotExists(item.key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create fake bucket tree index %v: %w", item.key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fkbtRoot.Put(item.val, zeroValue)
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func putListIndexItem(tx *bbolt.Tx, item namedBucketItem) error {
|
|
|
|
bkt, err := tx.CreateBucketIfNotExists(item.name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create index %v: %w", item.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lst, err := decodeList(bkt.Get(item.key))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't decode leaf list %v: %w", item.key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lst = append(lst, item.val)
|
|
|
|
|
|
|
|
encodedLst, err := encodeList(lst)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't encode leaf list %v: %w", item.key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bkt.Put(item.key, encodedLst)
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// encodeList decodes list of bytes into a single blog for list bucket indexes.
|
|
|
|
func encodeList(lst [][]byte) ([]byte, error) {
|
2021-03-22 08:10:01 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
w.WriteVarUint(uint64(len(lst)))
|
|
|
|
for i := range lst {
|
|
|
|
w.WriteVarBytes(lst[i])
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2021-03-22 08:10:01 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return nil, w.Err
|
|
|
|
}
|
|
|
|
return w.Bytes(), nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// decodeList decodes blob into the list of bytes from list bucket index.
|
|
|
|
func decodeList(data []byte) (lst [][]byte, err error) {
|
|
|
|
if len(data) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-03-21 14:15:09 +00:00
|
|
|
|
|
|
|
var offset uint64
|
|
|
|
size, n, err := getVarUint(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-03-22 08:10:01 +00:00
|
|
|
}
|
2022-03-21 14:15:09 +00:00
|
|
|
|
|
|
|
offset += uint64(n)
|
|
|
|
lst = make([][]byte, size, size+1)
|
|
|
|
for i := range lst {
|
|
|
|
sz, n, err := getVarUint(data[offset:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
offset += uint64(n)
|
|
|
|
|
|
|
|
next := offset + sz
|
|
|
|
if uint64(len(data)) < next {
|
|
|
|
return nil, gio.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
lst[i] = data[offset:next]
|
|
|
|
offset = next
|
2020-11-10 12:55:54 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
return lst, nil
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:15:09 +00:00
|
|
|
func getVarUint(data []byte) (uint64, int, error) {
|
|
|
|
if len(data) == 0 {
|
|
|
|
return 0, 0, gio.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
|
|
|
switch b := data[0]; b {
|
|
|
|
case 0xfd:
|
|
|
|
if len(data) < 3 {
|
|
|
|
return 0, 1, gio.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return uint64(binary.LittleEndian.Uint16(data[1:])), 3, nil
|
|
|
|
case 0xfe:
|
|
|
|
if len(data) < 5 {
|
|
|
|
return 0, 1, gio.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return uint64(binary.LittleEndian.Uint32(data[1:])), 5, nil
|
|
|
|
case 0xff:
|
|
|
|
if len(data) < 9 {
|
|
|
|
return 0, 1, gio.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint64(data[1:]), 9, nil
|
|
|
|
default:
|
|
|
|
return uint64(b), 1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
// updateStorageID for existing objects if they were moved from one
|
|
|
|
// storage location to another.
|
|
|
|
func updateStorageID(tx *bbolt.Tx, addr oid.Address, id []byte) error {
|
2022-05-31 17:00:41 +00:00
|
|
|
bkt, err := tx.CreateBucketIfNotExists(smallBucketName(addr.Container()))
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
return bkt.Put(objectKey(addr.Object()), id)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateSpliInfo for existing objects if storage filled with extra information
|
|
|
|
// about last object in split hierarchy or linking object.
|
2022-05-31 17:00:41 +00:00
|
|
|
func updateSplitInfo(tx *bbolt.Tx, addr oid.Address, from *objectSDK.SplitInfo) error {
|
|
|
|
bkt := tx.Bucket(rootBucketName(addr.Container()))
|
2020-12-08 07:51:34 +00:00
|
|
|
if bkt == nil {
|
|
|
|
// if object doesn't exists and we want to update split info on it
|
|
|
|
// then ignore, this should never happen
|
|
|
|
return ErrIncorrectSplitInfoUpdate
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
objectKey := objectKey(addr.Object())
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
rawSplitInfo := bkt.Get(objectKey)
|
|
|
|
if len(rawSplitInfo) == 0 {
|
|
|
|
return ErrIncorrectSplitInfoUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
to := objectSDK.NewSplitInfo()
|
|
|
|
|
|
|
|
err := to.Unmarshal(rawSplitInfo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't unmarshal split info from root index: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-04-15 07:23:52 +00:00
|
|
|
result := util.MergeSplitInfo(from, to)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
rawSplitInfo, err = result.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't marhsal merged split info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bkt.Put(objectKey, rawSplitInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitInfoFromObject returns split info based on last or linkin object.
|
|
|
|
// Otherwise returns nil, nil.
|
2022-03-03 14:19:05 +00:00
|
|
|
func splitInfoFromObject(obj *objectSDK.Object) (*objectSDK.SplitInfo, error) {
|
2020-12-08 07:51:34 +00:00
|
|
|
if obj.Parent() == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info := objectSDK.NewSplitInfo()
|
|
|
|
info.SetSplitID(obj.SplitID())
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isLinkObject(obj):
|
2022-05-12 16:37:46 +00:00
|
|
|
id, ok := obj.ID()
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("missing object ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
info.SetLink(id)
|
2020-12-08 07:51:34 +00:00
|
|
|
case isLastObject(obj):
|
2022-05-12 16:37:46 +00:00
|
|
|
id, ok := obj.ID()
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("missing object ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
info.SetLastPart(id)
|
2020-12-08 07:51:34 +00:00
|
|
|
default:
|
|
|
|
return nil, ErrIncorrectRootObject // should never happen
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isLinkObject returns true if object contains parent header and list
|
|
|
|
// of children.
|
2022-03-03 14:19:05 +00:00
|
|
|
func isLinkObject(obj *objectSDK.Object) bool {
|
2020-12-08 07:51:34 +00:00
|
|
|
return len(obj.Children()) > 0 && obj.Parent() != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isLastObject returns true if object contains only parent header without list
|
|
|
|
// of children.
|
2022-03-03 14:19:05 +00:00
|
|
|
func isLastObject(obj *objectSDK.Object) bool {
|
2020-12-08 07:51:34 +00:00
|
|
|
return len(obj.Children()) == 0 && obj.Parent() != nil
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|