tree.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package store
  2. import (
  3. "path"
  4. "sort"
  5. "strings"
  6. )
  7. //------------------------------------------------------------------------------
  8. //
  9. // Typedefs
  10. //
  11. //------------------------------------------------------------------------------
  12. // A file system like tree structure. Each non-leaf node of the tree has a hashmap to
  13. // store its children nodes. Leaf nodes has no hashmap (a nil pointer)
  14. type tree struct {
  15. Root *treeNode
  16. }
  17. // A treeNode wraps a Node. It has a hashmap to keep records of its children treeNodes.
  18. type treeNode struct {
  19. InternalNode Node
  20. Dir bool
  21. NodeMap map[string]*treeNode
  22. }
  23. // TreeNode with its key. We use it when we need to sort the treeNodes.
  24. type tnWithKey struct {
  25. key string
  26. tn *treeNode
  27. }
  28. // Define type and functions to match sort interface
  29. type tnWithKeySlice []tnWithKey
  30. func (s tnWithKeySlice) Len() int { return len(s) }
  31. func (s tnWithKeySlice) Less(i, j int) bool { return s[i].key < s[j].key }
  32. func (s tnWithKeySlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  33. // CONSTANT VARIABLE
  34. // Represent an empty node
  35. var emptyNode = Node{".", PERMANENT, nil}
  36. //------------------------------------------------------------------------------
  37. //
  38. // Methods
  39. //
  40. //------------------------------------------------------------------------------
  41. // Set the key to the given value, return true if success
  42. // If any intermidate path of the key is not a directory type, it will fail
  43. // For example if the /foo = Node(bar) exists, set /foo/foo = Node(barbar)
  44. // will fail.
  45. func (t *tree) set(key string, value Node) bool {
  46. nodesName := split(key)
  47. // avoid set value to "/"
  48. if len(nodesName) == 1 && len(nodesName[0]) == 0 {
  49. return false
  50. }
  51. nodeMap := t.Root.NodeMap
  52. i := 0
  53. newDir := false
  54. // go through all the path
  55. for i = 0; i < len(nodesName)-1; i++ {
  56. // if we meet a new directory, all the directory after it must be new
  57. if newDir {
  58. tn := &treeNode{emptyNode, true, make(map[string]*treeNode)}
  59. nodeMap[nodesName[i]] = tn
  60. nodeMap = tn.NodeMap
  61. continue
  62. }
  63. // get the node from the nodeMap of the current level
  64. tn, ok := nodeMap[nodesName[i]]
  65. if !ok {
  66. // add a new directory and set newDir to true
  67. newDir = true
  68. tn := &treeNode{emptyNode, true, make(map[string]*treeNode)}
  69. nodeMap[nodesName[i]] = tn
  70. nodeMap = tn.NodeMap
  71. } else if ok && !tn.Dir {
  72. // if we meet a non-directory node, we cannot set the key
  73. return false
  74. } else {
  75. // update the nodeMap to next level
  76. nodeMap = tn.NodeMap
  77. }
  78. }
  79. // Add the last node
  80. tn, ok := nodeMap[nodesName[i]]
  81. if !ok {
  82. // we add a new treeNode
  83. tn := &treeNode{value, false, nil}
  84. nodeMap[nodesName[i]] = tn
  85. } else {
  86. if tn.Dir {
  87. return false
  88. }
  89. // we change the value of a old Treenode
  90. tn.InternalNode = value
  91. }
  92. return true
  93. }
  94. // Get the tree node of the key
  95. func (t *tree) internalGet(key string) (*treeNode, bool) {
  96. nodesName := split(key)
  97. nodeMap := t.Root.NodeMap
  98. var i int
  99. for i = 0; i < len(nodesName)-1; i++ {
  100. node, ok := nodeMap[nodesName[i]]
  101. if !ok || !node.Dir {
  102. return nil, false
  103. }
  104. nodeMap = node.NodeMap
  105. }
  106. tn, ok := nodeMap[nodesName[i]]
  107. if ok {
  108. return tn, ok
  109. } else {
  110. return nil, ok
  111. }
  112. }
  113. // get the internalNode of the key
  114. func (t *tree) get(key string) (Node, bool) {
  115. tn, ok := t.internalGet(key)
  116. if ok {
  117. if tn.Dir {
  118. return emptyNode, false
  119. }
  120. return tn.InternalNode, ok
  121. } else {
  122. return emptyNode, ok
  123. }
  124. }
  125. // get the internalNode of the key
  126. func (t *tree) list(directory string) ([]Node, []string, []bool, bool) {
  127. treeNode, ok := t.internalGet(directory)
  128. if !ok {
  129. return nil, nil, nil, ok
  130. } else {
  131. if !treeNode.Dir {
  132. nodes := make([]Node, 1)
  133. nodes[0] = treeNode.InternalNode
  134. return nodes, make([]string, 1), make([]bool, 1), true
  135. }
  136. length := len(treeNode.NodeMap)
  137. nodes := make([]Node, length)
  138. keys := make([]string, length)
  139. dirs := make([]bool, length)
  140. i := 0
  141. for key, node := range treeNode.NodeMap {
  142. nodes[i] = node.InternalNode
  143. keys[i] = key
  144. if node.Dir {
  145. dirs[i] = true
  146. } else {
  147. dirs[i] = false
  148. }
  149. i++
  150. }
  151. return nodes, keys, dirs, ok
  152. }
  153. }
  154. // delete the key, return true if success
  155. func (t *tree) delete(key string) bool {
  156. nodesName := split(key)
  157. nodeMap := t.Root.NodeMap
  158. var i int
  159. for i = 0; i < len(nodesName)-1; i++ {
  160. node, ok := nodeMap[nodesName[i]]
  161. if !ok || !node.Dir {
  162. return false
  163. }
  164. nodeMap = node.NodeMap
  165. }
  166. node, ok := nodeMap[nodesName[i]]
  167. if ok && !node.Dir {
  168. delete(nodeMap, nodesName[i])
  169. return true
  170. }
  171. return false
  172. }
  173. // traverse wrapper
  174. func (t *tree) traverse(f func(string, *Node), sort bool) {
  175. if sort {
  176. sortDfs("", t.Root, f)
  177. } else {
  178. dfs("", t.Root, f)
  179. }
  180. }
  181. // deep first search to traverse the tree
  182. // apply the func f to each internal node
  183. func dfs(key string, t *treeNode, f func(string, *Node)) {
  184. // base case
  185. if len(t.NodeMap) == 0 {
  186. f(key, &t.InternalNode)
  187. // recursion
  188. } else {
  189. for tnKey, tn := range t.NodeMap {
  190. tnKey := key + "/" + tnKey
  191. dfs(tnKey, tn, f)
  192. }
  193. }
  194. }
  195. // sort deep first search to traverse the tree
  196. // apply the func f to each internal node
  197. func sortDfs(key string, t *treeNode, f func(string, *Node)) {
  198. // base case
  199. if len(t.NodeMap) == 0 {
  200. f(key, &t.InternalNode)
  201. // recursion
  202. } else {
  203. s := make(tnWithKeySlice, len(t.NodeMap))
  204. i := 0
  205. // copy
  206. for tnKey, tn := range t.NodeMap {
  207. tnKey := key + "/" + tnKey
  208. s[i] = tnWithKey{tnKey, tn}
  209. i++
  210. }
  211. // sort
  212. sort.Sort(s)
  213. // traverse
  214. for i = 0; i < len(t.NodeMap); i++ {
  215. sortDfs(s[i].key, s[i].tn, f)
  216. }
  217. }
  218. }
  219. // split the key by '/', get the intermediate node name
  220. func split(key string) []string {
  221. key = "/" + key
  222. key = path.Clean(key)
  223. // get the intermidate nodes name
  224. nodesName := strings.Split(key, "/")
  225. // we do not need the root node, since we start with it
  226. nodesName = nodesName[1:]
  227. return nodesName
  228. }