-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
131 lines (105 loc) · 2.44 KB
/
util.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"sort"
"strconv"
)
const (
FILE = iota
DIRECTORY = iota
SYMLINK = iota
)
type InodeId uint64
type ChildId uint64
type HDFSFileName string
type NameCount uint32
type ChildrenCount int
type ParentId uint64
type INodeTree struct {
INode
Children []*INodeTree
}
type EntityCount struct {
Files uint32
Directories uint32
Symlinks uint32
}
type INode struct {
Name []byte
Id InodeId
Type int
}
func (i ChildrenCount) String() string {
return strconv.Itoa(int(i))
}
func (i ParentId) String() string {
return strconv.FormatUint(uint64(i), 10)
}
type NameCountPair struct {
Name HDFSFileName
Count NameCount
}
func (p NameCountPair) String() string {
return string(p.Name) + " " + strconv.FormatUint(uint64(p.Count), 10)
}
type NameCountPairList []NameCountPair
func (p NameCountPairList) Len() int { return len(p) }
func (p NameCountPairList) Less(i, j int) bool { return p[i].Count < p[j].Count }
func (p NameCountPairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func SortByNameCount(m map[HDFSFileName]NameCount) NameCountPairList {
pl := make(NameCountPairList, len(m))
i := 0
for k, v := range m {
pl[i] = NameCountPair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
type ChildrenCountPair struct {
Parid ParentId
ChildCount ChildrenCount
}
type ChildrenCountPairList []ChildrenCountPair
func (p ChildrenCountPair) String() string {
return p.Parid.String() + " " + p.ChildCount.String()
}
func (p ChildrenCountPairList) Len() int { return len(p) }
func (p ChildrenCountPairList) Less(i, j int) bool { return p[i].ChildCount < p[j].ChildCount }
func (p ChildrenCountPairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func SortByChildCount(data map[ParentId]ChildrenCount) ChildrenCountPairList {
pl := make(ChildrenCountPairList, len(data))
i := 0
for k, v := range data {
pl[i] = ChildrenCountPair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
func MinINodeID(inodeIds []InodeId) InodeId {
min := inodeIds[0]
for _, v := range inodeIds {
if v < min {
min = v
}
}
return min
}
func CountTreeNodes(rootNode INodeTree) {
var i uint32 = 1
for _, child := range rootNode.Children {
i++
countInSubTree(*child, &i)
}
}
func countInSubTree(node INodeTree, counter *uint32) {
children := node.Children
if children == nil || len(children) == 0 {
(*counter)++
return
}
for _, c := range node.Children {
(*counter)++
countInSubTree(*c, counter)
}
}