/
hier.go
231 lines (181 loc) · 4.38 KB
/
hier.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
// "fmt"
"fmt"
"strings"
lls "github.com/emirpasic/gods/stacks/linkedliststack"
)
/*
fields
TREE (n,t,p,content)
update Operations
add(p,n,t)
remove(p,t)
update(p,n,t,u)
for each particular policy must be adopted
obtains from the upper layer
Map (p,t) -> content
for all (p,t) that are in the set
returns to upper layer (assume hier is upper layer)
Tree with elements
(name,type,path,content)
*/
//struct definitions
//an element of the tree
type DfsTreeElement struct {
name string
fileType string
path string
content string
parent *DfsTreeElement
children []*DfsTreeElement
}
func (d DfsTreeElement) getPath() string {
if d.name == "/" {
return d.name
}
if d.fileType == "dir" {
return d.path+d.name + "/"
}
return d.path + d.name
}
//slice referring to children nodes
type children []*DfsTreeElement
//store the root
type DfsTree DfsTreeElement
//hier layer
type hierLayer struct {
dfs *Dfs
root *DfsTreeElement
contentMap map[string]string
}
//initalisation
func newhierLayer() *hierLayer {
ro := DfsTreeElement{name: "/", fileType: "dir", path: "", content: "",parent:nil,}
l := hierLayer{root: &ro,
contentMap: make(map[string]string),
}
return &l
}
func (l *hierLayer) setDfs(dfs *Dfs) {
l.dfs = dfs
}
//Update Interface
//add element
// func (l *hierLayer) add(path string, name string, typ string) {
// l.dfs.UpdateAddReplication(path+"/"+name, typ)
// }
// //remove element
// func (l *hierLayer) remove(path string, typ string) {
// l.dfs.UpdateRemoveReplication(path, typ)
// }
// func (tree DfsTree) update(path string,name string,typ string){
// fmt.Println("Element has been updated")
// }
//update lower layer
func updateReplation() {
//communicate it to the Dfs instance
}
//modify the state based on new info from replication(Defualt implement skip )
func (l *hierLayer) updateState(cmap map[*replicationElement]string) {
//go through the map and build the tree
l.root = &DfsTreeElement{name: "/", fileType: "dir", path: "", content: "",parent:nil,}
stack := lls.New()
//policy used here is skip
stack.Push(l.root)
// untill stack empty
for !stack.Empty() {
// pop stack call el
ra, _ := stack.Pop()
el := ra.(*DfsTreeElement)
if el.fileType == "dir" {
for _, i := range getChildren(el, cmap) {
ii := i
stack.Push(&ii)
el.children = append(el.children, &ii)
}
// fmt.Println(el.getPath(), el.children)
}
}
//last step is to send the interface layer with update state
l.updateInterface()
}
func (l *hierLayer) reappear() {
//rreappear policy
}
func skip(map[*replicationElement]string){
//iterate thro
}
//pass to interfac
func (l *hierLayer) updateInterface(){
l.dfs.updateInterface(l.root)
}
//return to interface
//user interface will be looking it up
//axulariy functions
func (l *hierLayer) printCurrentState() {
l.printElement(*l.root, 0)
}
func (l *hierLayer) printElement(root DfsTreeElement, nt int) {
for i := 0; i < nt; i++ {
fmt.Printf("\t") //print tabs
}
// fmt.Println(root.name)
val := (root.children)
isDir :=""
if(root.fileType=="dir"){
isDir="+"
}
fmt.Println(isDir+root.name)
for i := 0; i < len(val); i++ {
l.printElement(*val[i], nt+1)
}
}
func (l *hierLayer) runDown(ui chan UiToHier,rep chan HierToRep){
for{
msgu := <- ui //receiving from ui layer
msgR :=HierToRep{
path: msgu.path+msgu.name,
fileType: msgu.fileType,
op: msgu.op,
}
rep <-msgR //sending message to replication layer
}
}
func (l* hierLayer) runUp(rep chan map[*replicationElement]string ,ui chan *DfsTreeElement){
for{
msgr:=<-rep
//apply the policies
l.updateState(msgr)
ui <-l.root //send the root to ui
}
}
func findRoot(cmap map[*replicationElement]string) string {
for k := range cmap {
if !strings.Contains(k.Name, "/") {
return k.Name //root found
}
}
return ""
}
func pathAndName(str string) (string, string) {
li := strings.LastIndex(str, "/")
return str[:li+1], str[li+1:]
}
func getChildren(root *DfsTreeElement, cmap map[*replicationElement]string) []DfsTreeElement {
path:=root.getPath()
temp := []DfsTreeElement{}
for k := range cmap {
p, n := pathAndName(k.Name)
if p == path && k.Name != "/" {
el := DfsTreeElement{name: n,
fileType: k.ElementType,
path: p,
children: []*DfsTreeElement{},
parent: root,
}
temp = append(temp, el)
}
}
return temp
}