[#288] pilorama: Use more descriptive names for memory tree

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-04-26 17:07:51 +03:00 committed by Evgenii Stratonikov
parent f856ad7480
commit 0045f1bcd4
2 changed files with 22 additions and 25 deletions

View file

@ -12,7 +12,7 @@ import (
// memoryForest represents multiple replicating trees sharing a single storage. // memoryForest represents multiple replicating trees sharing a single storage.
type memoryForest struct { type memoryForest struct {
// treeMap maps tree identifier (container ID + name) to the replicated log. // treeMap maps tree identifier (container ID + name) to the replicated log.
treeMap map[string]*state treeMap map[string]*memoryTree
} }
var _ Forest = (*memoryForest)(nil) var _ Forest = (*memoryForest)(nil)
@ -21,7 +21,7 @@ var _ Forest = (*memoryForest)(nil)
// TODO: this function will eventually be removed and is here for debugging. // TODO: this function will eventually be removed and is here for debugging.
func NewMemoryForest() ForestStorage { func NewMemoryForest() ForestStorage {
return &memoryForest{ return &memoryForest{
treeMap: make(map[string]*state), treeMap: make(map[string]*memoryTree),
} }
} }
@ -34,7 +34,7 @@ func (f *memoryForest) TreeMove(_ context.Context, d CIDDescriptor, treeID strin
fullID := d.CID.String() + "/" + treeID fullID := d.CID.String() + "/" + treeID
s, ok := f.treeMap[fullID] s, ok := f.treeMap[fullID]
if !ok { if !ok {
s = newState() s = newMemoryTree()
f.treeMap[fullID] = s f.treeMap[fullID] = s
} }
@ -60,7 +60,7 @@ func (f *memoryForest) TreeAddByPath(_ context.Context, d CIDDescriptor, treeID
fullID := d.CID.String() + "/" + treeID fullID := d.CID.String() + "/" + treeID
s, ok := f.treeMap[fullID] s, ok := f.treeMap[fullID]
if !ok { if !ok {
s = newState() s = newMemoryTree()
f.treeMap[fullID] = s f.treeMap[fullID] = s
} }
@ -98,7 +98,7 @@ func (f *memoryForest) TreeApply(_ context.Context, cnr cid.ID, treeID string, o
fullID := cnr.String() + "/" + treeID fullID := cnr.String() + "/" + treeID
s, ok := f.treeMap[fullID] s, ok := f.treeMap[fullID]
if !ok { if !ok {
s = newState() s = newMemoryTree()
f.treeMap[fullID] = s f.treeMap[fullID] = s
} }
@ -131,7 +131,7 @@ func (f *memoryForest) TreeGetByPath(_ context.Context, cid cid.ID, treeID strin
return nil, ErrTreeNotFound return nil, ErrTreeNotFound
} }
return s.get(attr, path, latest), nil return s.getByPath(attr, path, latest), nil
} }
// TreeGetMeta implements the Forest interface. // TreeGetMeta implements the Forest interface.

View file

@ -14,21 +14,23 @@ type move struct {
Old nodeInfo Old nodeInfo
} }
// state represents state being replicated. // memoryTree represents memoryTree being replicated.
type state struct { type memoryTree struct {
operations []move operations []move
tree tree
} }
// newState constructs new empty tree. // newMemoryTree constructs new empty tree.
func newState() *state { func newMemoryTree() *memoryTree {
return &state{ return &memoryTree{
tree: *newTree(), tree: tree{
infoMap: make(map[Node]nodeInfo),
},
} }
} }
// undo un-does op and changes s in-place. // undo un-does op and changes s in-place.
func (s *state) undo(op *move) { func (s *memoryTree) undo(op *move) {
if op.HasOld { if op.HasOld {
s.tree.infoMap[op.Child] = op.Old s.tree.infoMap[op.Child] = op.Old
} else { } else {
@ -38,7 +40,7 @@ func (s *state) undo(op *move) {
// Apply puts op in log at a proper position, re-applies all subsequent operations // Apply puts op in log at a proper position, re-applies all subsequent operations
// from log and changes s in-place. // from log and changes s in-place.
func (s *state) Apply(op *Move) error { func (s *memoryTree) Apply(op *Move) error {
var index int var index int
for index = len(s.operations); index > 0; index-- { for index = len(s.operations); index > 0; index-- {
if s.operations[index-1].Time <= op.Time { if s.operations[index-1].Time <= op.Time {
@ -65,7 +67,7 @@ func (s *state) Apply(op *Move) error {
} }
// do performs a single move operation on a tree. // do performs a single move operation on a tree.
func (s *state) do(op *Move) move { func (s *memoryTree) do(op *Move) move {
lm := move{ lm := move{
Move: Move{ Move: Move{
Parent: op.Parent, Parent: op.Parent,
@ -96,14 +98,14 @@ func (s *state) do(op *Move) move {
return lm return lm
} }
func (s *state) timestamp(pos, size int) Timestamp { func (s *memoryTree) timestamp(pos, size int) Timestamp {
if len(s.operations) == 0 { if len(s.operations) == 0 {
return nextTimestamp(0, uint64(pos), uint64(size)) return nextTimestamp(0, uint64(pos), uint64(size))
} }
return nextTimestamp(s.operations[len(s.operations)-1].Time, uint64(pos), uint64(size)) return nextTimestamp(s.operations[len(s.operations)-1].Time, uint64(pos), uint64(size))
} }
func (s *state) findSpareID() Node { func (s *memoryTree) findSpareID() Node {
id := uint64(1) id := uint64(1)
for _, ok := s.infoMap[id]; ok; _, ok = s.infoMap[id] { for _, ok := s.infoMap[id]; ok; _, ok = s.infoMap[id] {
id++ id++
@ -117,12 +119,6 @@ type tree struct {
infoMap map[Node]nodeInfo infoMap map[Node]nodeInfo
} }
func newTree() *tree {
return &tree{
infoMap: make(map[Node]nodeInfo),
}
}
func (t tree) getChildren(parent Node) []Node { func (t tree) getChildren(parent Node) []Node {
var children []Node var children []Node
for c, info := range t.infoMap { for c, info := range t.infoMap {
@ -175,9 +171,10 @@ loop:
return len(path), curNode return len(path), curNode
} }
// get returns list of nodes which have the specified path from root // getByPath returns list of nodes which have the specified path from root
// descending by values of attr from meta. // descending by values of attr from meta.
func (t tree) get(attr string, path []string, latest bool) []Node { // If latest is true, only the latest node is returned.
func (t tree) getByPath(attr string, path []string, latest bool) []Node {
if len(path) == 0 { if len(path) == 0 {
return nil return nil
} }