-
Notifications
You must be signed in to change notification settings - Fork 2
/
object_directory.go
69 lines (63 loc) · 1.5 KB
/
object_directory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package caskin
type objectDirectory struct {
Tree map[uint64][]*Directory
Node map[uint64]*Directory
visit map[uint64]bool
}
func (o *objectDirectory) Search(prefix uint64, searchType DirectorySearchType) []*Directory {
var out []*Directory
if prefix == 0 {
for _, v := range o.Node {
if _, ok := o.Node[v.GetParentID()]; !ok {
out = append(out, v)
}
}
} else {
for _, v := range o.Tree[prefix] {
out = append(out, v)
}
}
switch searchType {
case DirectorySearchAll:
for i := 0; i < len(out); i++ {
node := out[i]
for _, v := range o.Tree[node.GetID()] {
out = append(out, v)
}
}
default:
}
return out
}
func (o *objectDirectory) dfs(current *Directory) {
o.visit[current.GetID()] = true
current.TopDirectoryCount = uint64(len(o.Tree[current.GetID()]))
current.AllDirectoryCount = current.TopDirectoryCount
current.AllItemCount = current.TopItemCount
for _, child := range o.Tree[current.GetID()] {
if !o.visit[child.GetID()] {
o.dfs(child)
}
current.AllDirectoryCount += child.AllDirectoryCount
current.AllItemCount += child.AllItemCount
}
}
func (o *objectDirectory) build() {
for _, v := range o.Node {
if _, ok := o.Node[v.GetParentID()]; !ok {
o.dfs(v)
}
}
}
func NewObjectDirectory(in []*Directory) *objectDirectory {
t := &objectDirectory{
Tree: map[uint64][]*Directory{},
Node: IDMap(in),
visit: map[uint64]bool{},
}
for _, v := range in {
t.Tree[v.GetParentID()] = append(t.Tree[v.GetParentID()], v)
}
t.build()
return t
}