-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_host_data.go
112 lines (105 loc) · 2.63 KB
/
app_host_data.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
package fsearch
import (
"sort"
"sync"
)
type appHostData struct {
mux sync.RWMutex
data map[string]map[uint64]*NodeInfo // appName: NodeId:*NodeInfo
}
type NodeInfo struct {
NodeId uint64 `json:"nodeId"`
HostName string `json:"hostName"`
Files []string `json:"files"`
}
type ClusterNode struct {
AppName string `json:"appName,omitempty"`
AllFiles []string `json:"allFiles"` // all files after deduplication
NodeInfos []NodeInfo `json:"nodeInfos"`
}
func (a *appHostData) Set(appName string, nodeId uint64, hostName string, files []string) {
a.mux.Lock()
defer a.mux.Unlock()
if _, ok := a.data[appName]; !ok {
a.data[appName] = make(map[uint64]*NodeInfo)
}
a.data[appName][nodeId] = &NodeInfo{HostName: hostName, NodeId: nodeId, Files: files}
}
func (a *appHostData) Del(appName string, id uint64) {
a.mux.Lock()
defer a.mux.Unlock()
if _, ok := a.data[appName]; !ok {
return
}
delete(a.data[appName], id)
if len(a.data[appName]) == 0 {
delete(a.data, appName)
}
}
func (a *appHostData) ExistsBy(appName string, uid uint64) bool {
a.mux.RLock()
defer a.mux.RUnlock()
if _, ok := a.data[appName]; !ok {
return false
}
_, ok := a.data[appName][uid]
return ok
}
func (a *appHostData) GetHostNodeInfoMap(appName string) map[uint64]NodeInfo {
a.mux.RLock()
defer a.mux.RUnlock()
if _, ok := a.data[appName]; !ok {
return nil
}
var result = make(map[uint64]NodeInfo, len(a.data[appName]))
for k, v := range a.data[appName] {
result[k] = *v
}
return result
}
func (a *appHostData) GetHostNodeUniqueIds(appName string) []uint64 {
a.mux.RLock()
defer a.mux.RUnlock()
if _, ok := a.data[appName]; !ok {
return nil
}
var result = make([]uint64, 0, len(a.data[appName]))
for k := range a.data[appName] {
result = append(result, k)
}
return result
}
func (a *appHostData) GetClusterNodes() []ClusterNode {
a.mux.RLock()
defer a.mux.RUnlock()
if len(a.data) == 0 {
return nil
}
items := make([]ClusterNode, 0, len(a.data))
for appName, hostsNode := range a.data {
fileMap := make(map[string]struct{}, len(hostsNode))
var nodeInfos []NodeInfo
for _, info := range hostsNode {
nodeInfos = append(nodeInfos, *info)
for _, file := range info.Files {
fileMap[file] = struct{}{}
}
}
allFiles := make([]string, 0, len(fileMap))
for file := range fileMap {
allFiles = append(allFiles, file)
}
sort.Strings(allFiles)
clusterNode := ClusterNode{
AppName: appName,
NodeInfos: nodeInfos,
AllFiles: allFiles,
}
items = append(items, clusterNode)
}
// sort by appName
sort.Slice(items, func(i, j int) bool {
return items[i].AppName < items[j].AppName
})
return items
}