2022-04-22 13:22:40 +00:00
|
|
|
package pilorama
|
|
|
|
|
|
|
|
import (
|
2022-05-11 13:29:04 +00:00
|
|
|
"sort"
|
2022-10-18 11:59:32 +00:00
|
|
|
"strings"
|
2022-05-11 13:29:04 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
2023-03-21 12:43:12 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2022-04-22 13:22:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// memoryForest represents multiple replicating trees sharing a single storage.
|
|
|
|
type memoryForest struct {
|
|
|
|
// treeMap maps tree identifier (container ID + name) to the replicated log.
|
|
|
|
treeMap map[string]*state
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Forest = (*memoryForest)(nil)
|
|
|
|
|
|
|
|
// NewMemoryForest creates new empty forest.
|
|
|
|
// TODO: this function will eventually be removed and is here for debugging.
|
|
|
|
func NewMemoryForest() ForestStorage {
|
|
|
|
return &memoryForest{
|
|
|
|
treeMap: make(map[string]*state),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TreeMove implements the Forest interface.
|
2023-01-25 11:12:02 +00:00
|
|
|
func (f *memoryForest) TreeMove(d CIDDescriptor, treeID string, op *Move) (*Move, error) {
|
2022-05-27 12:55:02 +00:00
|
|
|
if !d.checkValid() {
|
|
|
|
return nil, ErrInvalidCIDDescriptor
|
|
|
|
}
|
|
|
|
|
|
|
|
fullID := d.CID.String() + "/" + treeID
|
2022-04-22 13:22:40 +00:00
|
|
|
s, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
s = newState()
|
|
|
|
f.treeMap[fullID] = s
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:55:02 +00:00
|
|
|
op.Time = s.timestamp(d.Position, d.Size)
|
2022-04-22 13:22:40 +00:00
|
|
|
if op.Child == RootID {
|
|
|
|
op.Child = s.findSpareID()
|
|
|
|
}
|
|
|
|
|
|
|
|
lm := s.do(op)
|
|
|
|
s.operations = append(s.operations, lm)
|
2023-01-17 13:16:50 +00:00
|
|
|
return &lm.Move, nil
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TreeAddByPath implements the Forest interface.
|
2023-01-25 11:12:02 +00:00
|
|
|
func (f *memoryForest) TreeAddByPath(d CIDDescriptor, treeID string, attr string, path []string, m []KeyValue) ([]Move, error) {
|
2022-05-27 12:55:02 +00:00
|
|
|
if !d.checkValid() {
|
|
|
|
return nil, ErrInvalidCIDDescriptor
|
|
|
|
}
|
2022-05-24 13:12:50 +00:00
|
|
|
if !isAttributeInternal(attr) {
|
|
|
|
return nil, ErrNotPathAttribute
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:55:02 +00:00
|
|
|
fullID := d.CID.String() + "/" + treeID
|
2022-04-22 13:22:40 +00:00
|
|
|
s, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
s = newState()
|
|
|
|
f.treeMap[fullID] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
i, node := s.getPathPrefix(attr, path)
|
2023-01-25 11:12:02 +00:00
|
|
|
lm := make([]Move, len(path)-i+1)
|
2022-04-22 13:22:40 +00:00
|
|
|
for j := i; j < len(path); j++ {
|
2023-01-17 13:16:50 +00:00
|
|
|
op := s.do(&Move{
|
2022-04-22 13:22:40 +00:00
|
|
|
Parent: node,
|
2022-05-27 12:55:02 +00:00
|
|
|
Meta: Meta{
|
|
|
|
Time: s.timestamp(d.Position, d.Size),
|
|
|
|
Items: []KeyValue{{Key: attr, Value: []byte(path[j])}}},
|
|
|
|
Child: s.findSpareID(),
|
2022-04-22 13:22:40 +00:00
|
|
|
})
|
2023-01-17 13:16:50 +00:00
|
|
|
lm[j-i] = op.Move
|
|
|
|
node = op.Child
|
|
|
|
s.operations = append(s.operations, op)
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
2022-05-23 11:32:24 +00:00
|
|
|
|
|
|
|
mCopy := make([]KeyValue, len(m))
|
|
|
|
copy(mCopy, m)
|
2023-01-17 13:16:50 +00:00
|
|
|
op := s.do(&Move{
|
2022-04-22 13:22:40 +00:00
|
|
|
Parent: node,
|
2022-05-27 12:55:02 +00:00
|
|
|
Meta: Meta{
|
|
|
|
Time: s.timestamp(d.Position, d.Size),
|
|
|
|
Items: mCopy,
|
|
|
|
},
|
|
|
|
Child: s.findSpareID(),
|
2022-04-22 13:22:40 +00:00
|
|
|
})
|
2023-01-17 13:16:50 +00:00
|
|
|
lm[len(lm)-1] = op.Move
|
2022-04-22 13:22:40 +00:00
|
|
|
return lm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TreeApply implements the Forest interface.
|
2023-03-21 12:43:12 +00:00
|
|
|
func (f *memoryForest) TreeApply(cnr cid.ID, treeID string, op *Move, _ bool) error {
|
|
|
|
fullID := cnr.String() + "/" + treeID
|
2022-04-22 13:22:40 +00:00
|
|
|
s, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
s = newState()
|
|
|
|
f.treeMap[fullID] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Apply(op)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *memoryForest) Init() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-28 13:42:50 +00:00
|
|
|
func (f *memoryForest) Open(bool) error {
|
2022-04-22 13:22:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-07-05 04:55:46 +00:00
|
|
|
func (f *memoryForest) SetMode(mode.Mode) error {
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-22 13:22:40 +00:00
|
|
|
func (f *memoryForest) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TreeGetByPath implements the Forest interface.
|
2023-04-03 13:11:56 +00:00
|
|
|
func (f *memoryForest) TreeGetByPath(cid cid.ID, treeID string, attr string, path []string, latest bool) ([]Node, error) {
|
2022-05-24 13:12:50 +00:00
|
|
|
if !isAttributeInternal(attr) {
|
|
|
|
return nil, ErrNotPathAttribute
|
|
|
|
}
|
|
|
|
|
2022-04-22 13:22:40 +00:00
|
|
|
fullID := cid.String() + "/" + treeID
|
|
|
|
s, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrTreeNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.get(attr, path, latest), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TreeGetMeta implements the Forest interface.
|
2023-04-03 13:11:56 +00:00
|
|
|
func (f *memoryForest) TreeGetMeta(cid cid.ID, treeID string, nodeID Node) (Meta, Node, error) {
|
2022-04-22 13:22:40 +00:00
|
|
|
fullID := cid.String() + "/" + treeID
|
|
|
|
s, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
2022-05-20 08:41:37 +00:00
|
|
|
return Meta{}, 0, ErrTreeNotFound
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 08:41:37 +00:00
|
|
|
return s.getMeta(nodeID), s.infoMap[nodeID].Parent, nil
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
2022-04-29 10:06:10 +00:00
|
|
|
|
|
|
|
// TreeGetChildren implements the Forest interface.
|
2023-04-03 13:11:56 +00:00
|
|
|
func (f *memoryForest) TreeGetChildren(cid cid.ID, treeID string, nodeID Node) ([]uint64, error) {
|
2022-04-29 10:06:10 +00:00
|
|
|
fullID := cid.String() + "/" + treeID
|
|
|
|
s, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrTreeNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
children, ok := s.childMap[nodeID]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]Node, len(children))
|
|
|
|
copy(res, children)
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-05-11 13:29:04 +00:00
|
|
|
|
|
|
|
// TreeGetOpLog implements the pilorama.Forest interface.
|
2023-04-03 13:11:56 +00:00
|
|
|
func (f *memoryForest) TreeGetOpLog(cid cid.ID, treeID string, height uint64) (Move, error) {
|
2022-05-11 13:29:04 +00:00
|
|
|
fullID := cid.String() + "/" + treeID
|
|
|
|
s, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
return Move{}, ErrTreeNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
n := sort.Search(len(s.operations), func(i int) bool {
|
|
|
|
return s.operations[i].Time >= height
|
|
|
|
})
|
|
|
|
if n == len(s.operations) {
|
|
|
|
return Move{}, nil
|
|
|
|
}
|
|
|
|
return s.operations[n].Move, nil
|
|
|
|
}
|
2022-09-07 08:46:13 +00:00
|
|
|
|
|
|
|
// TreeDrop implements the pilorama.Forest interface.
|
2023-04-03 13:11:56 +00:00
|
|
|
func (f *memoryForest) TreeDrop(cid cid.ID, treeID string) error {
|
2022-11-08 12:32:38 +00:00
|
|
|
cidStr := cid.String()
|
|
|
|
if treeID == "" {
|
|
|
|
for k := range f.treeMap {
|
|
|
|
if strings.HasPrefix(k, cidStr) {
|
|
|
|
delete(f.treeMap, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fullID := cidStr + "/" + treeID
|
|
|
|
_, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
return ErrTreeNotFound
|
|
|
|
}
|
|
|
|
delete(f.treeMap, fullID)
|
2022-09-07 08:46:13 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-18 11:59:32 +00:00
|
|
|
|
2022-10-18 12:49:40 +00:00
|
|
|
// TreeList implements the pilorama.Forest interface.
|
2023-04-03 13:11:56 +00:00
|
|
|
func (f *memoryForest) TreeList(cid cid.ID) ([]string, error) {
|
2022-10-18 11:59:32 +00:00
|
|
|
var res []string
|
|
|
|
cidStr := cid.EncodeToString()
|
|
|
|
|
|
|
|
for k := range f.treeMap {
|
|
|
|
cidAndTree := strings.Split(k, "/")
|
|
|
|
if cidAndTree[0] != cidStr {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, cidAndTree[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-10-06 16:06:19 +00:00
|
|
|
|
2023-04-07 10:26:54 +00:00
|
|
|
func (f *memoryForest) TreeHeight(cid cid.ID, treeID string) (uint64, error) {
|
|
|
|
fullID := cid.EncodeToString() + "/" + treeID
|
|
|
|
tree, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
return 0, ErrTreeNotFound
|
|
|
|
}
|
|
|
|
return tree.operations[len(tree.operations)-1].Time, nil
|
|
|
|
}
|
|
|
|
|
2022-10-06 16:06:19 +00:00
|
|
|
// TreeExists implements the pilorama.Forest interface.
|
2023-04-03 13:11:56 +00:00
|
|
|
func (f *memoryForest) TreeExists(cid cid.ID, treeID string) (bool, error) {
|
2022-10-06 16:06:19 +00:00
|
|
|
fullID := cid.EncodeToString() + "/" + treeID
|
|
|
|
_, ok := f.treeMap[fullID]
|
|
|
|
return ok, nil
|
|
|
|
}
|
2023-01-25 10:25:45 +00:00
|
|
|
|
|
|
|
// TreeUpdateLastSyncHeight implements the pilorama.Forest interface.
|
2023-04-03 13:11:56 +00:00
|
|
|
func (f *memoryForest) TreeUpdateLastSyncHeight(cid cid.ID, treeID string, height uint64) error {
|
2023-01-25 10:25:45 +00:00
|
|
|
fullID := cid.EncodeToString() + "/" + treeID
|
|
|
|
t, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
return ErrTreeNotFound
|
|
|
|
}
|
|
|
|
t.syncHeight = height
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TreeLastSyncHeight implements the pilorama.Forest interface.
|
2023-04-03 13:11:56 +00:00
|
|
|
func (f *memoryForest) TreeLastSyncHeight(cid cid.ID, treeID string) (uint64, error) {
|
2023-01-25 10:25:45 +00:00
|
|
|
fullID := cid.EncodeToString() + "/" + treeID
|
|
|
|
t, ok := f.treeMap[fullID]
|
|
|
|
if !ok {
|
|
|
|
return 0, ErrTreeNotFound
|
|
|
|
}
|
|
|
|
return t.syncHeight, nil
|
|
|
|
}
|