2022-04-22 13:22:40 +00:00
|
|
|
package pilorama
|
|
|
|
|
2024-11-06 07:34:16 +00:00
|
|
|
import (
|
|
|
|
"cmp"
|
|
|
|
"slices"
|
|
|
|
)
|
2023-04-26 12:23:07 +00:00
|
|
|
|
2022-04-22 13:22:40 +00:00
|
|
|
// nodeInfo couples parent and metadata.
|
|
|
|
type nodeInfo struct {
|
2022-07-22 10:02:28 +00:00
|
|
|
Parent Node
|
|
|
|
Meta Meta
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 13:16:50 +00:00
|
|
|
type move struct {
|
|
|
|
Move
|
|
|
|
HasOld bool
|
|
|
|
Old nodeInfo
|
|
|
|
}
|
|
|
|
|
2023-04-26 14:07:51 +00:00
|
|
|
// memoryTree represents memoryTree being replicated.
|
|
|
|
type memoryTree struct {
|
2023-01-17 13:16:50 +00:00
|
|
|
operations []move
|
2022-04-22 13:22:40 +00:00
|
|
|
tree
|
|
|
|
}
|
|
|
|
|
2023-04-26 14:07:51 +00:00
|
|
|
// newMemoryTree constructs new empty tree.
|
|
|
|
func newMemoryTree() *memoryTree {
|
|
|
|
return &memoryTree{
|
|
|
|
tree: tree{
|
|
|
|
infoMap: make(map[Node]nodeInfo),
|
|
|
|
},
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// undo un-does op and changes s in-place.
|
2023-04-26 14:07:51 +00:00
|
|
|
func (s *memoryTree) undo(op *move) {
|
2022-04-22 13:22:40 +00:00
|
|
|
if op.HasOld {
|
|
|
|
s.tree.infoMap[op.Child] = op.Old
|
|
|
|
} else {
|
|
|
|
delete(s.tree.infoMap, op.Child)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply puts op in log at a proper position, re-applies all subsequent operations
|
|
|
|
// from log and changes s in-place.
|
2023-04-26 14:07:51 +00:00
|
|
|
func (s *memoryTree) Apply(op *Move) error {
|
2022-04-22 13:22:40 +00:00
|
|
|
var index int
|
|
|
|
for index = len(s.operations); index > 0; index-- {
|
|
|
|
if s.operations[index-1].Time <= op.Time {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if index == len(s.operations) {
|
|
|
|
s.operations = append(s.operations, s.do(op))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.operations = append(s.operations[:index+1], s.operations[index:]...)
|
|
|
|
for i := len(s.operations) - 1; i > index; i-- {
|
|
|
|
s.undo(&s.operations[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
s.operations[index] = s.do(op)
|
|
|
|
|
|
|
|
for i := index + 1; i < len(s.operations); i++ {
|
|
|
|
s.operations[i] = s.do(&s.operations[i].Move)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// do performs a single move operation on a tree.
|
2023-04-26 14:07:51 +00:00
|
|
|
func (s *memoryTree) do(op *Move) move {
|
2023-07-13 08:50:13 +00:00
|
|
|
m := op.Meta
|
|
|
|
if m.Items == nil {
|
|
|
|
m.Items = []KeyValue{}
|
|
|
|
}
|
2023-01-17 13:16:50 +00:00
|
|
|
lm := move{
|
2022-04-22 13:22:40 +00:00
|
|
|
Move: Move{
|
|
|
|
Parent: op.Parent,
|
2023-07-13 08:50:13 +00:00
|
|
|
Meta: m,
|
2022-04-22 13:22:40 +00:00
|
|
|
Child: op.Child,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:14:27 +00:00
|
|
|
shouldPut := !s.tree.isAncestor(op.Child, op.Parent)
|
2022-04-22 13:22:40 +00:00
|
|
|
p, ok := s.tree.infoMap[op.Child]
|
|
|
|
if ok {
|
|
|
|
lm.HasOld = true
|
|
|
|
lm.Old = p
|
|
|
|
}
|
|
|
|
|
|
|
|
if !shouldPut {
|
|
|
|
return lm
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2022-07-22 10:02:28 +00:00
|
|
|
p.Meta.Time = op.Time
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 08:50:13 +00:00
|
|
|
p.Meta = m
|
2022-04-22 13:22:40 +00:00
|
|
|
p.Parent = op.Parent
|
|
|
|
s.tree.infoMap[op.Child] = p
|
|
|
|
|
|
|
|
return lm
|
|
|
|
}
|
|
|
|
|
2023-04-26 14:07:51 +00:00
|
|
|
func (s *memoryTree) timestamp(pos, size int) Timestamp {
|
2022-04-22 13:22:40 +00:00
|
|
|
if len(s.operations) == 0 {
|
2022-05-27 12:55:02 +00:00
|
|
|
return nextTimestamp(0, uint64(pos), uint64(size))
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
2022-05-27 12:55:02 +00:00
|
|
|
return nextTimestamp(s.operations[len(s.operations)-1].Time, uint64(pos), uint64(size))
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 14:07:51 +00:00
|
|
|
func (s *memoryTree) findSpareID() Node {
|
2022-04-22 13:22:40 +00:00
|
|
|
id := uint64(1)
|
|
|
|
for _, ok := s.infoMap[id]; ok; _, ok = s.infoMap[id] {
|
|
|
|
id++
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
// tree is a mapping from the child nodes to their parent and metadata.
|
|
|
|
type tree struct {
|
2023-01-25 10:25:45 +00:00
|
|
|
syncHeight uint64
|
|
|
|
infoMap map[Node]nodeInfo
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 12:23:07 +00:00
|
|
|
func (t tree) getChildren(parent Node) []Node {
|
|
|
|
var children []Node
|
|
|
|
for c, info := range t.infoMap {
|
|
|
|
if info.Parent == parent {
|
|
|
|
children = append(children, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 07:34:16 +00:00
|
|
|
slices.SortFunc(children, func(ci, cj uint64) int {
|
|
|
|
a := t.infoMap[ci]
|
|
|
|
b := t.infoMap[cj]
|
|
|
|
return cmp.Compare(a.Meta.Time, b.Meta.Time)
|
2023-04-26 12:23:07 +00:00
|
|
|
})
|
|
|
|
return children
|
|
|
|
}
|
|
|
|
|
2022-04-22 13:22:40 +00:00
|
|
|
// isAncestor returns true if parent is an ancestor of a child.
|
|
|
|
// For convenience, also return true if parent == child.
|
|
|
|
func (t tree) isAncestor(parent, child Node) bool {
|
|
|
|
for c := child; c != parent; {
|
|
|
|
p, ok := t.infoMap[c]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c = p.Parent
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPathPrefix descends by path constructed from values of attr until
|
|
|
|
// there is no node corresponding to a path element. Returns the amount of nodes
|
|
|
|
// processed and ID of the last node.
|
|
|
|
func (t tree) getPathPrefix(attr string, path []string) (int, Node) {
|
|
|
|
var curNode Node
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for i := range path {
|
2023-04-26 12:23:07 +00:00
|
|
|
children := t.getChildren(curNode)
|
2022-04-22 13:22:40 +00:00
|
|
|
for j := range children {
|
2022-05-23 11:32:24 +00:00
|
|
|
meta := t.infoMap[children[j]].Meta
|
|
|
|
f := meta.GetAttr(attr)
|
|
|
|
if len(meta.Items) == 1 && string(f) == path[i] {
|
2022-04-22 13:22:40 +00:00
|
|
|
curNode = children[j]
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i, curNode
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(path), curNode
|
|
|
|
}
|
|
|
|
|
2023-04-26 14:07:51 +00:00
|
|
|
// getByPath returns list of nodes which have the specified path from root
|
2022-04-22 13:22:40 +00:00
|
|
|
// descending by values of attr from meta.
|
2023-04-26 14:07:51 +00:00
|
|
|
// If latest is true, only the latest node is returned.
|
|
|
|
func (t tree) getByPath(attr string, path []string, latest bool) []Node {
|
2022-04-22 13:22:40 +00:00
|
|
|
if len(path) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i, curNode := t.getPathPrefix(attr, path[:len(path)-1])
|
|
|
|
if i < len(path)-1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodes []Node
|
|
|
|
var lastTs Timestamp
|
|
|
|
|
2023-04-26 12:23:07 +00:00
|
|
|
children := t.getChildren(curNode)
|
2022-04-22 13:22:40 +00:00
|
|
|
for i := range children {
|
|
|
|
info := t.infoMap[children[i]]
|
|
|
|
fileName := string(info.Meta.GetAttr(attr))
|
|
|
|
if fileName == path[len(path)-1] {
|
|
|
|
if latest {
|
2022-07-22 10:02:28 +00:00
|
|
|
if info.Meta.Time >= lastTs {
|
2022-04-22 13:22:40 +00:00
|
|
|
nodes = append(nodes[:0], children[i])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nodes = append(nodes, children[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|