2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
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"
|
2024-08-20 08:59:42 +00:00
|
|
|
"strconv"
|
2023-06-06 09:27:19 +00:00
|
|
|
"time"
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2024-08-20 08:59:42 +00:00
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
storagelog "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/log"
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-30 10:42:10 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-03-07 13:38:26 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-03-22 08:10:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-10-28 14:49:30 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
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.
|
2024-01-15 06:04:25 +00:00
|
|
|
type PutRes struct {
|
|
|
|
Inserted bool
|
|
|
|
}
|
2020-12-08 09:56:14 +00:00
|
|
|
|
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.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (db *DB) Put(ctx context.Context, prm PutPrm) (res PutRes, err error) {
|
2023-06-06 09:27:19 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("Put", time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.Put",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("address", objectCore.AddressOf(prm.obj).EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-07-21 13:26:25 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-11-15 12:46:32 +00:00
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return res, ErrDegradedMode
|
2022-11-15 14:53:23 +00:00
|
|
|
} else if db.mode.ReadOnly() {
|
|
|
|
return res, ErrReadOnlyMode
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-01-15 06:04:25 +00:00
|
|
|
var e error
|
|
|
|
res, e = db.put(tx, prm.obj, prm.id, nil, currEpoch)
|
|
|
|
return e
|
2020-12-08 07:51:34 +00:00
|
|
|
})
|
2022-03-02 09:17:32 +00:00
|
|
|
if err == nil {
|
2023-06-06 09:27:19 +00:00
|
|
|
success = true
|
2022-03-02 09:17:32 +00:00
|
|
|
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
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return res, metaerr.Wrap(err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2023-03-30 10:42:10 +00:00
|
|
|
func (db *DB) put(tx *bbolt.Tx,
|
|
|
|
obj *objectSDK.Object,
|
|
|
|
id []byte,
|
|
|
|
si *objectSDK.SplitInfo,
|
2023-10-31 11:56:55 +00:00
|
|
|
currEpoch uint64,
|
2024-01-15 06:04:25 +00:00
|
|
|
) (PutRes, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, ok := obj.ContainerID()
|
|
|
|
if !ok {
|
2024-01-15 06:04:25 +00:00
|
|
|
return PutRes{}, errors.New("missing container in object")
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
isParent := si != nil
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2024-05-27 19:07:43 +00:00
|
|
|
exists, _, err := db.exists(tx, objectCore.AddressOf(obj), oid.Address{}, currEpoch)
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2023-07-26 13:02:08 +00:00
|
|
|
var splitInfoError *objectSDK.SplitInfoError
|
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 {
|
2024-01-15 06:04:25 +00:00
|
|
|
return PutRes{}, err // return any error besides SplitInfoError
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2023-03-30 10:42:10 +00:00
|
|
|
if exists {
|
2024-01-15 06:04:25 +00:00
|
|
|
return PutRes{}, db.updateObj(tx, obj, id, si, isParent)
|
2023-03-30 10:42:10 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 06:04:25 +00:00
|
|
|
return PutRes{Inserted: true}, db.insertObject(tx, obj, id, si, isParent, cnr, currEpoch)
|
2023-03-30 10:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) updateObj(tx *bbolt.Tx, obj *objectSDK.Object, id []byte, si *objectSDK.SplitInfo, isParent bool) error {
|
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
|
|
|
|
|
2023-03-30 10:42:10 +00:00
|
|
|
// When storage engine moves objects between different sub-storages,
|
|
|
|
// it calls metabase.Put method with new storage ID, thus triggering this code.
|
|
|
|
if !isParent && id != nil {
|
2024-02-26 13:30:03 +00:00
|
|
|
return setStorageID(tx, objectCore.AddressOf(obj), id, true)
|
2023-03-30 10:42:10 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2023-03-30 10:42:10 +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 {
|
2023-04-03 13:11:56 +00:00
|
|
|
return updateSplitInfo(tx, objectCore.AddressOf(obj), si)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 10:42:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) insertObject(tx *bbolt.Tx, obj *objectSDK.Object, id []byte, si *objectSDK.SplitInfo, isParent bool, cnr cid.ID, currEpoch uint64) error {
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-15 06:04:25 +00:00
|
|
|
_, err = db.put(tx, par, id, parentSI, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:42:10 +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 {
|
2023-12-05 14:00:16 +00:00
|
|
|
if err = db.incCounters(tx, cnr, IsUserObject(obj)); err != nil {
|
2023-11-01 17:53:24 +00:00
|
|
|
return err
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 09:21:39 +00:00
|
|
|
func putUniqueIndexes(tx *bbolt.Tx, obj *objectSDK.Object, si *objectSDK.SplitInfo, id []byte) error {
|
2020-12-08 07:51:34 +00:00
|
|
|
isParent := si != nil
|
2023-04-03 13:11:56 +00:00
|
|
|
addr := objectCore.AddressOf(obj)
|
2022-09-08 11:54:21 +00:00
|
|
|
objKey := objectKey(addr.Object(), make([]byte, objectKeySize))
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
2020-12-08 07:51:34 +00:00
|
|
|
if !isParent {
|
2024-08-20 09:21:39 +00:00
|
|
|
err := putRawObjectData(tx, obj, bucketName, addr, objKey)
|
2022-03-21 13:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
if id != nil {
|
2024-02-26 13:30:03 +00:00
|
|
|
if err = setStorageID(tx, objectCore.AddressOf(obj), id, false); err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 09:21:39 +00:00
|
|
|
if err := putExpirationEpoch(tx, obj, addr, objKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return putSplitInfo(tx, obj, bucketName, addr, si, objKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putRawObjectData(tx *bbolt.Tx, obj *objectSDK.Object, bucketName []byte, addr oid.Address, objKey []byte) error {
|
|
|
|
switch obj.Type() {
|
|
|
|
case objectSDK.TypeRegular:
|
|
|
|
bucketName = primaryBucketName(addr.Container(), bucketName)
|
|
|
|
case objectSDK.TypeTombstone:
|
|
|
|
bucketName = tombstoneBucketName(addr.Container(), bucketName)
|
|
|
|
case objectSDK.TypeLock:
|
|
|
|
bucketName = bucketNameLockers(addr.Container(), bucketName)
|
|
|
|
default:
|
|
|
|
return ErrUnknownObjectType
|
|
|
|
}
|
|
|
|
rawObject, err := obj.CutPayload().Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't marshal object header: %w", err)
|
|
|
|
}
|
|
|
|
return putUniqueIndexItem(tx, namedBucketItem{
|
|
|
|
name: bucketName,
|
|
|
|
key: objKey,
|
|
|
|
val: rawObject,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func putExpirationEpoch(tx *bbolt.Tx, obj *objectSDK.Object, addr oid.Address, objKey []byte) error {
|
2024-08-20 08:59:42 +00:00
|
|
|
if expEpoch, ok := hasExpirationEpoch(obj); ok {
|
|
|
|
err := putUniqueIndexItem(tx, namedBucketItem{
|
|
|
|
name: expEpochToObjectBucketName,
|
2024-08-20 09:21:39 +00:00
|
|
|
key: expirationEpochKey(expEpoch, addr.Container(), addr.Object()),
|
2024-08-20 08:59:42 +00:00
|
|
|
val: zeroValue,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
val := make([]byte, epochSize)
|
|
|
|
binary.LittleEndian.PutUint64(val, expEpoch)
|
|
|
|
err = putUniqueIndexItem(tx, namedBucketItem{
|
2024-08-20 09:21:39 +00:00
|
|
|
name: objectToExpirationEpochBucketName(addr.Container(), make([]byte, bucketKeySize)),
|
2024-08-20 08:59:42 +00:00
|
|
|
key: objKey,
|
|
|
|
val: val,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-08-20 09:21:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-08-20 08:59:42 +00:00
|
|
|
|
2024-08-20 09:21:39 +00:00
|
|
|
func putSplitInfo(tx *bbolt.Tx, obj *objectSDK.Object, bucketName []byte, addr oid.Address, si *objectSDK.SplitInfo, objKey []byte) error {
|
2020-12-08 07:51:34 +00:00
|
|
|
if obj.Type() == objectSDK.TypeRegular && !obj.HasParent() {
|
2024-05-20 10:14:27 +00:00
|
|
|
if ecHead := obj.ECHeader(); ecHead != nil {
|
2024-06-06 07:52:17 +00:00
|
|
|
parentID := ecHead.Parent()
|
|
|
|
if ecHead.ParentSplitID() != nil {
|
|
|
|
parentSplitParentID := ecHead.ParentSplitParentID()
|
|
|
|
if parentSplitParentID == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
si = objectSDK.NewSplitInfo()
|
|
|
|
si.SetSplitID(ecHead.ParentSplitID())
|
|
|
|
si.SetLastPart(ecHead.Parent())
|
|
|
|
|
|
|
|
parentID = *parentSplitParentID
|
2024-04-22 06:43:42 +00:00
|
|
|
}
|
2024-06-06 07:52:17 +00:00
|
|
|
objKey = objectKey(parentID, objKey)
|
2024-04-22 06:43:42 +00:00
|
|
|
}
|
2024-08-20 09:21:39 +00:00
|
|
|
return updateSplitInfoIndex(tx, objKey, addr.Container(), bucketName, si)
|
2020-12-08 07:51:34 +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
|
|
|
|
2024-06-06 07:52:17 +00:00
|
|
|
func updateSplitInfoIndex(tx *bbolt.Tx, objKey []byte, cnr cid.ID, bucketName []byte, si *objectSDK.SplitInfo) error {
|
|
|
|
return updateUniqueIndexItem(tx, namedBucketItem{
|
|
|
|
name: rootBucketName(cnr, bucketName),
|
|
|
|
key: objKey,
|
|
|
|
}, func(old, _ []byte) ([]byte, error) {
|
|
|
|
switch {
|
|
|
|
case si == nil && old == nil:
|
|
|
|
return []byte{}, nil
|
|
|
|
case si == nil:
|
|
|
|
return old, nil
|
|
|
|
case old == nil:
|
|
|
|
return si.Marshal()
|
|
|
|
default:
|
|
|
|
oldSI := objectSDK.NewSplitInfo()
|
|
|
|
if err := oldSI.Unmarshal(old); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
si = util.MergeSplitInfo(si, oldSI)
|
|
|
|
return si.Marshal()
|
2024-05-21 11:25:15 +00:00
|
|
|
}
|
2024-06-06 07:52:17 +00:00
|
|
|
})
|
2024-05-21 11:25:15 +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()
|
2022-09-08 11:54:21 +00:00
|
|
|
objKey := objectKey(idObj, make([]byte, objectKeySize))
|
|
|
|
bucketName := make([]byte, bucketKeySize)
|
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-09-08 11:54:21 +00:00
|
|
|
name: parentBucketName(cnr, bucketName),
|
|
|
|
key: objectKey(idParent, make([]byte, objectKeySize)),
|
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-09-08 11:54:21 +00:00
|
|
|
name: splitBucketName(cnr, bucketName),
|
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
|
|
|
|
2024-05-21 11:19:22 +00:00
|
|
|
if ech := obj.ECHeader(); ech != nil {
|
|
|
|
err := f(tx, namedBucketItem{
|
2024-06-04 10:30:30 +00:00
|
|
|
name: ecInfoBucketName(cnr, bucketName),
|
2024-05-21 11:19:22 +00:00
|
|
|
key: objectKey(ech.Parent(), make([]byte, objectKeySize)),
|
|
|
|
val: objKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-05 08:14:06 +00:00
|
|
|
|
|
|
|
if ech.ParentSplitID() != nil {
|
|
|
|
objKey := objectKey(ech.Parent(), make([]byte, objectKeySize))
|
|
|
|
err := f(tx, namedBucketItem{
|
|
|
|
name: splitBucketName(cnr, bucketName),
|
|
|
|
key: ech.ParentSplitID().ToV2(),
|
|
|
|
val: objKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-07-17 13:48:10 +00:00
|
|
|
|
|
|
|
if parentSplitParentID := ech.ParentSplitParentID(); parentSplitParentID != nil {
|
|
|
|
objKey := objectKey(ech.Parent(), make([]byte, objectKeySize))
|
|
|
|
err := f(tx, namedBucketItem{
|
|
|
|
name: parentBucketName(cnr, bucketName),
|
|
|
|
key: objectKey(*parentSplitParentID, make([]byte, objectKeySize)),
|
|
|
|
val: objKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-05-21 11:19:22 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 08:59:42 +00:00
|
|
|
func hasExpirationEpoch(obj *objectSDK.Object) (uint64, bool) {
|
|
|
|
attributes := obj.Attributes()
|
|
|
|
if ech := obj.ECHeader(); ech != nil {
|
|
|
|
attributes = ech.ParentAttributes()
|
|
|
|
}
|
|
|
|
for _, attr := range attributes {
|
|
|
|
if attr.Key() == objectV2.SysAttributeExpEpochNeoFS {
|
|
|
|
expEpoch, err := strconv.ParseUint(attr.Value(), 10, 64)
|
|
|
|
return expEpoch, err == nil
|
|
|
|
}
|
|
|
|
if attr.Key() == objectV2.SysAttributeExpEpoch {
|
|
|
|
expEpoch, err := strconv.ParseUint(attr.Value(), 10, 64)
|
|
|
|
return expEpoch, err == nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
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-09-08 11:54:21 +00:00
|
|
|
objKey := objectKey(id, make([]byte, objectKeySize))
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, bucketKeySize)
|
2024-05-27 12:52:08 +00:00
|
|
|
var attrs []objectSDK.Attribute
|
|
|
|
if obj.ECHeader() != nil {
|
|
|
|
attrs = obj.ECHeader().ParentAttributes()
|
|
|
|
objKey = objectKey(obj.ECHeader().Parent(), make([]byte, objectKeySize))
|
|
|
|
} else {
|
|
|
|
attrs = obj.Attributes()
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// user specified attributes
|
|
|
|
for i := range attrs {
|
2022-09-08 11:54:21 +00:00
|
|
|
key = attributeBucketName(cnr, attrs[i].Key(), key)
|
2022-03-21 13:59:05 +00:00
|
|
|
err := f(tx, namedBucketItem{
|
2022-09-08 11:54:21 +00:00
|
|
|
name: 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
|
|
|
}
|
|
|
|
|
2024-02-16 19:49:53 +00:00
|
|
|
type bucketContainer interface {
|
|
|
|
Bucket([]byte) *bbolt.Bucket
|
|
|
|
CreateBucket([]byte) (*bbolt.Bucket, error)
|
|
|
|
CreateBucketIfNotExists([]byte) (*bbolt.Bucket, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createBucketLikelyExists[T bucketContainer](tx T, name []byte) (*bbolt.Bucket, error) {
|
|
|
|
if bkt := tx.Bucket(name); bkt != nil {
|
|
|
|
return bkt, nil
|
|
|
|
}
|
|
|
|
return tx.CreateBucket(name)
|
|
|
|
}
|
|
|
|
|
2024-06-06 07:52:17 +00:00
|
|
|
func updateUniqueIndexItem(tx *bbolt.Tx, item namedBucketItem, update func(oldData, newData []byte) ([]byte, error)) error {
|
2024-02-16 19:49:53 +00:00
|
|
|
bkt, err := createBucketLikelyExists(tx, item.name)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create index %v: %w", item.name, err)
|
|
|
|
}
|
|
|
|
|
2024-06-06 07:52:17 +00:00
|
|
|
data, err := update(bkt.Get(item.key), item.val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bkt.Put(item.key, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putUniqueIndexItem(tx *bbolt.Tx, item namedBucketItem) error {
|
|
|
|
return updateUniqueIndexItem(tx, item, func(_, val []byte) ([]byte, error) { return val, nil })
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func putFKBTIndexItem(tx *bbolt.Tx, item namedBucketItem) error {
|
2024-02-16 19:49:53 +00:00
|
|
|
bkt, err := createBucketLikelyExists(tx, item.name)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create index %v: %w", item.name, err)
|
|
|
|
}
|
|
|
|
|
2024-02-16 19:49:53 +00:00
|
|
|
fkbtRoot, err := createBucketLikelyExists(bkt, item.key)
|
2020-12-08 07:51:34 +00:00
|
|
|
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 {
|
2024-02-16 19:49:53 +00:00
|
|
|
bkt, err := createBucketLikelyExists(tx, item.name)
|
2020-12-08 07:51:34 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:30:03 +00:00
|
|
|
// setStorageID for existing objects if they were moved from one
|
2022-07-06 14:09:50 +00:00
|
|
|
// storage location to another.
|
2024-02-26 13:30:03 +00:00
|
|
|
func setStorageID(tx *bbolt.Tx, addr oid.Address, id []byte, override bool) error {
|
2022-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, bucketKeySize)
|
2024-02-16 19:49:53 +00:00
|
|
|
bkt, err := createBucketLikelyExists(tx, smallBucketName(addr.Container(), key))
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2024-02-26 13:30:03 +00:00
|
|
|
key = objectKey(addr.Object(), key)
|
|
|
|
if override || bkt.Get(key) == nil {
|
|
|
|
return bkt.Put(key, id)
|
|
|
|
}
|
|
|
|
return nil
|
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 {
|
2024-06-06 07:52:17 +00:00
|
|
|
objKey := objectKey(addr.Object(), make([]byte, bucketKeySize))
|
|
|
|
return updateSplitInfoIndex(tx, objKey, addr.Container(), make([]byte, bucketKeySize), from)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|