2020-09-28 13:10:50 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-12-17 15:12:31 +00:00
|
|
|
"errors"
|
2020-09-28 13:10:50 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-09-28 13:10:50 +00:00
|
|
|
)
|
|
|
|
|
2020-12-17 15:12:31 +00:00
|
|
|
// HeadReceiver is an interface of entity that can receive
|
|
|
|
// object header or the information about the object relations.
|
|
|
|
type HeadReceiver interface {
|
|
|
|
// Head must return one of:
|
|
|
|
// * object header (*object.Object);
|
2022-03-03 14:19:05 +00:00
|
|
|
// * structured information about split-chain (*object.SplitInfo).
|
2023-02-21 11:42:45 +00:00
|
|
|
Head(id oid.Address) (any, error)
|
2020-12-17 15:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SplitMemberHandler is a handler of next split-chain element.
|
|
|
|
//
|
|
|
|
// If reverseDirection arg is true, then the traversal is done in reverse order.
|
|
|
|
// Stop boolean result provides the ability to interrupt the traversal.
|
|
|
|
type SplitMemberHandler func(member *object.Object, reverseDirection bool) (stop bool)
|
|
|
|
|
|
|
|
// IterateAllSplitLeaves is an iterator over all object split-tree leaves in direct order.
|
2022-05-31 17:00:41 +00:00
|
|
|
func IterateAllSplitLeaves(r HeadReceiver, addr oid.Address, h func(*object.Object)) error {
|
2020-12-17 15:12:31 +00:00
|
|
|
return IterateSplitLeaves(r, addr, func(leaf *object.Object) bool {
|
|
|
|
h(leaf)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterateSplitLeaves is an iterator over object split-tree leaves in direct order.
|
|
|
|
//
|
|
|
|
// If member handler returns true, then the iterator aborts without error.
|
2022-05-31 17:00:41 +00:00
|
|
|
func IterateSplitLeaves(r HeadReceiver, addr oid.Address, h func(*object.Object) bool) error {
|
2020-12-17 15:12:31 +00:00
|
|
|
var (
|
|
|
|
reverse bool
|
|
|
|
leaves []*object.Object
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := TraverseSplitChain(r, addr, func(member *object.Object, reverseDirection bool) (stop bool) {
|
|
|
|
reverse = reverseDirection
|
|
|
|
|
|
|
|
if reverse {
|
|
|
|
leaves = append(leaves, member)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return h(member)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(leaves) - 1; i >= 0; i-- {
|
|
|
|
if h(leaves[i]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraverseSplitChain is an iterator over object split-tree leaves.
|
|
|
|
//
|
|
|
|
// Traversal occurs in one of two directions, which depends on what pslit info was received:
|
|
|
|
// * in direct order for link part;
|
|
|
|
// * in reverse order for last part.
|
2022-05-31 17:00:41 +00:00
|
|
|
func TraverseSplitChain(r HeadReceiver, addr oid.Address, h SplitMemberHandler) error {
|
2020-12-17 15:12:31 +00:00
|
|
|
_, err := traverseSplitChain(r, addr, h)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func traverseSplitChain(r HeadReceiver, addr oid.Address, h SplitMemberHandler) (bool, error) {
|
2020-12-17 15:12:31 +00:00
|
|
|
v, err := r.Head(addr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := addr.Container()
|
2020-12-17 15:12:31 +00:00
|
|
|
|
|
|
|
switch res := v.(type) {
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected result of %T: %T", r, v))
|
|
|
|
case *object.Object:
|
|
|
|
return h(res, false), nil
|
2022-03-03 14:19:05 +00:00
|
|
|
case *object.SplitInfo:
|
2022-05-12 16:37:46 +00:00
|
|
|
link, withLink := res.Link()
|
|
|
|
last, withLast := res.LastPart()
|
|
|
|
|
2020-12-17 15:12:31 +00:00
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
return false, errors.New("lack of split information")
|
2022-05-12 16:37:46 +00:00
|
|
|
case withLink:
|
2023-04-06 13:03:00 +00:00
|
|
|
return traverseByLink(cnr, link, r, h)
|
|
|
|
case withLast:
|
|
|
|
return traverseByLast(cnr, last, withLast, res, r, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
func traverseByLink(cnr cid.ID, link oid.ID, r HeadReceiver, h SplitMemberHandler) (bool, error) {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(link)
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
chain := make([]oid.ID, 0)
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
if _, err := traverseSplitChain(r, addr, func(member *object.Object, reverseDirection bool) (stop bool) {
|
|
|
|
children := member.Children()
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
if reverseDirection {
|
|
|
|
chain = append(children, chain...)
|
|
|
|
} else {
|
|
|
|
chain = append(chain, children...)
|
|
|
|
}
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
return false
|
|
|
|
}); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
var reverseChain []*object.Object
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
for i := range chain {
|
|
|
|
addr.SetObject(chain[i])
|
2021-01-11 15:21:06 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
if stop, err := traverseSplitChain(r, addr, func(member *object.Object, reverseDirection bool) (stop bool) {
|
|
|
|
if !reverseDirection {
|
|
|
|
return h(member, false)
|
2020-12-17 15:12:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
reverseChain = append(reverseChain, member)
|
|
|
|
return false
|
|
|
|
}); err != nil || stop {
|
|
|
|
return stop, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(reverseChain) - 1; i >= 0; i-- {
|
|
|
|
if h(reverseChain[i], false) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
func traverseByLast(cnr cid.ID, last oid.ID, withLast bool, res *object.SplitInfo, r HeadReceiver, h SplitMemberHandler) (bool, error) {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
for last, withLast = res.LastPart(); withLast; {
|
|
|
|
addr.SetObject(last)
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
var directChain []*object.Object
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
if _, err := traverseSplitChain(r, addr, func(member *object.Object, reverseDirection bool) (stop bool) {
|
|
|
|
if reverseDirection {
|
|
|
|
last, withLast = member.PreviousID()
|
|
|
|
return h(member, true)
|
|
|
|
}
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
directChain = append(directChain, member)
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
return false
|
|
|
|
}); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-12-17 15:12:31 +00:00
|
|
|
|
2023-04-06 13:03:00 +00:00
|
|
|
for i := len(directChain) - 1; i >= 0; i-- {
|
|
|
|
if h(directChain[i], true) {
|
|
|
|
return true, nil
|
2020-12-17 15:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-06 13:03:00 +00:00
|
|
|
|
|
|
|
if len(directChain) > 0 {
|
|
|
|
last, withLast = directChain[len(directChain)-1].PreviousID()
|
|
|
|
}
|
2020-12-17 15:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|