2020-11-23 13:30:56 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2020-11-23 13:30:56 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// ToMoveItPrm groups the parameters of ToMoveIt operation.
|
|
|
|
type ToMoveItPrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToMoveItRes groups resulting values of ToMoveIt operation.
|
|
|
|
type ToMoveItRes struct{}
|
|
|
|
|
|
|
|
// WithAddress sets address of the object to move into another shard.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *ToMoveItPrm) WithAddress(addr *addressSDK.Address) *ToMoveItPrm {
|
2020-12-08 09:56:14 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoNotMovePrm groups the parameters of DoNotMove operation.
|
|
|
|
type DoNotMovePrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DoNotMoveRes groups resulting values of DoNotMove operation.
|
|
|
|
type DoNotMoveRes struct{}
|
|
|
|
|
|
|
|
// WithAddress sets address of the object to prevent moving into another shard.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *DoNotMovePrm) WithAddress(addr *addressSDK.Address) *DoNotMovePrm {
|
2020-12-08 09:56:14 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// MovablePrm groups the parameters of Movable operation.
|
|
|
|
type MovablePrm struct{}
|
|
|
|
|
|
|
|
// MovableRes groups resulting values of Movable operation.
|
|
|
|
type MovableRes struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addrList []*addressSDK.Address
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// AddressList returns resulting addresses of Movable operation.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *MovableRes) AddressList() []*addressSDK.Address {
|
2020-12-08 09:56:14 +00:00
|
|
|
return p.addrList
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMoveIt marks object to move it into another shard.
|
2022-01-26 12:11:13 +00:00
|
|
|
func ToMoveIt(db *DB, addr *addressSDK.Address) error {
|
2020-12-08 09:56:14 +00:00
|
|
|
_, err := db.ToMoveIt(new(ToMoveItPrm).WithAddress(addr))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:30:56 +00:00
|
|
|
// ToMoveIt marks objects to move it into another shard. This useful for
|
|
|
|
// faster HRW fetching.
|
2020-12-08 09:56:14 +00:00
|
|
|
func (db *DB) ToMoveIt(prm *ToMoveItPrm) (res *ToMoveItRes, err error) {
|
|
|
|
err = db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2020-11-23 13:30:56 +00:00
|
|
|
toMoveIt, err := tx.CreateBucketIfNotExists(toMoveItBucketName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
return toMoveIt.Put(addressKey(prm.addr), zeroValue)
|
2020-11-23 13:30:56 +00:00
|
|
|
})
|
2020-12-08 09:56:14 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoNotMove prevents the object to be moved into another shard.
|
2022-01-26 12:11:13 +00:00
|
|
|
func DoNotMove(db *DB, addr *addressSDK.Address) error {
|
2020-12-08 09:56:14 +00:00
|
|
|
_, err := db.DoNotMove(new(DoNotMovePrm).WithAddress(addr))
|
|
|
|
return err
|
2020-11-23 13:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DoNotMove removes `MoveIt` mark from the object.
|
2020-12-08 09:56:14 +00:00
|
|
|
func (db *DB) DoNotMove(prm *DoNotMovePrm) (res *DoNotMoveRes, err error) {
|
|
|
|
err = db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2020-11-23 13:30:56 +00:00
|
|
|
toMoveIt := tx.Bucket(toMoveItBucketName)
|
|
|
|
if toMoveIt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
return toMoveIt.Delete(addressKey(prm.addr))
|
2020-11-23 13:30:56 +00:00
|
|
|
})
|
2020-12-08 09:56:14 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Movable returns all movable objects of DB.
|
2022-01-26 12:11:13 +00:00
|
|
|
func Movable(db *DB) ([]*addressSDK.Address, error) {
|
2020-12-08 09:56:14 +00:00
|
|
|
r, err := db.Movable(new(MovablePrm))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.AddressList(), nil
|
2020-11-23 13:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Movable returns list of marked objects to move into other shard.
|
2020-12-08 09:56:14 +00:00
|
|
|
func (db *DB) Movable(prm *MovablePrm) (*MovableRes, error) {
|
2020-11-23 13:30:56 +00:00
|
|
|
var strAddrs []string
|
|
|
|
|
|
|
|
err := db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
toMoveIt := tx.Bucket(toMoveItBucketName)
|
|
|
|
if toMoveIt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return toMoveIt.ForEach(func(k, v []byte) error {
|
|
|
|
strAddrs = append(strAddrs, string(k))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can parse strings to structures in-place, but probably it seems
|
|
|
|
// more efficient to keep bolt db TX code smaller because it might be
|
|
|
|
// bottleneck.
|
2022-01-26 12:11:13 +00:00
|
|
|
addrs := make([]*addressSDK.Address, 0, len(strAddrs))
|
2020-11-23 13:30:56 +00:00
|
|
|
|
|
|
|
for i := range strAddrs {
|
2021-02-19 08:31:25 +00:00
|
|
|
addr, err := addressFromKey([]byte(strAddrs[i]))
|
2020-11-23 13:30:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse object address %v: %w",
|
|
|
|
strAddrs[i], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
return &MovableRes{
|
|
|
|
addrList: addrs,
|
|
|
|
}, nil
|
2020-11-23 13:30:56 +00:00
|
|
|
}
|