tree.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. nodeMap := t.Root.NodeMap
  48. i := 0
  49. newDir := false
  50. // go through all the path
  51. for i = 0; i < len(nodesName)-1; i++ {
  52. // if we meet a new directory, all the directory after it must be new
  53. if newDir {
  54. tn := &treeNode{emptyNode, true, make(map[string]*treeNode)}
  55. nodeMap[nodesName[i]] = tn
  56. nodeMap = tn.NodeMap
  57. continue
  58. }
  59. // get the node from the nodeMap of the current level
  60. tn, ok := nodeMap[nodesName[i]]
  61. if !ok {
  62. // add a new directory and set newDir to true
  63. newDir = true
  64. tn := &treeNode{emptyNode, true, make(map[string]*treeNode)}
  65. nodeMap[nodesName[i]] = tn
  66. nodeMap = tn.NodeMap
  67. } else if ok && !tn.Dir {
  68. // if we meet a non-directory node, we cannot set the key
  69. return false
  70. } else {
  71. // update the nodeMap to next level
  72. nodeMap = tn.NodeMap
  73. }
  74. }
  75. // Add the last node
  76. tn, ok := nodeMap[nodesName[i]]
  77. if !ok {
  78. // we add a new treeNode
  79. tn := &treeNode{value, false, nil}
  80. nodeMap[nodesName[i]] = tn
  81. } else {
  82. if tn.Dir {
  83. return false
  84. }
  85. // we change the value of a old Treenode
  86. tn.InternalNode = value
  87. }
  88. return true
  89. }
  90. // Get the tree node of the key
  91. func (t *tree) internalGet(key string) (*treeNode, bool) {
  92. nodesName := split(key)
  93. nodeMap := t.Root.NodeMap
  94. var i int
  95. for i = 0; i < len(nodesName)-1; i++ {
  96. node, ok := nodeMap[nodesName[i]]
  97. if !ok || !node.Dir {
  98. return nil, false
  99. }
  100. nodeMap = node.NodeMap
  101. }
  102. tn, ok := nodeMap[nodesName[i]]
  103. if ok {
  104. return tn, ok
  105. } else {
  106. return nil, ok
  107. }
  108. }
  109. // get the internalNode of the key
  110. func (t *tree) get(key string) (Node, bool) {
  111. tn, ok := t.internalGet(key)
  112. if ok {
  113. if tn.Dir {
  114. return emptyNode, false
  115. }
  116. return tn.InternalNode, ok
  117. } else {
  118. return emptyNode, ok
  119. }
  120. }
  121. // get the internalNode of the key
  122. func (t *tree) list(directory string) ([]Node, []string, []bool, bool) {
  123. treeNode, ok := t.internalGet(directory)
  124. if !ok {
  125. return nil, nil, nil, ok
  126. } else {
  127. if !treeNode.Dir {
  128. nodes := make([]Node, 1)
  129. nodes[0] = treeNode.InternalNode
  130. return nodes, make([]string, 1), make([]bool, 1), true
  131. }
  132. length := len(treeNode.NodeMap)
  133. nodes := make([]Node, length)
  134. keys := make([]string, length)
  135. dirs := make([]bool, length)
  136. i := 0
  137. for key, node := range treeNode.NodeMap {
  138. nodes[i] = node.InternalNode
  139. keys[i] = key
  140. if node.Dir {
  141. dirs[i] = true
  142. } else {
  143. dirs[i] = false
  144. }
  145. i++
  146. }
  147. return nodes, keys, dirs, ok
  148. }
  149. }
  150. // delete the key, return true if success
  151. func (t *tree) delete(key string) bool {
  152. nodesName := split(key)
  153. nodeMap := t.Root.NodeMap
  154. var i int
  155. for i = 0; i < len(nodesName)-1; i++ {
  156. node, ok := nodeMap[nodesName[i]]
  157. if !ok || !node.Dir {
  158. return false
  159. }
  160. nodeMap = node.NodeMap
  161. }
  162. node, ok := nodeMap[nodesName[i]]
  163. if ok && !node.Dir {
  164. delete(nodeMap, nodesName[i])
  165. return true
  166. }
  167. return false
  168. }
  169. // traverse wrapper
  170. func (t *tree) traverse(f func(string, *Node), sort bool) {
  171. if sort {
  172. sortDfs("", t.Root, f)
  173. } else {
  174. dfs("", t.Root, f)
  175. }
  176. }
  177. // deep first search to traverse the tree
  178. // apply the func f to each internal node
  179. func dfs(key string, t *treeNode, f func(string, *Node)) {
  180. // base case
  181. if len(t.NodeMap) == 0 {
  182. f(key, &t.InternalNode)
  183. // recursion
  184. } else {
  185. for tnKey, tn := range t.NodeMap {
  186. tnKey := key + "/" + tnKey
  187. dfs(tnKey, tn, f)
  188. }
  189. }
  190. }
  191. // sort deep first search to traverse the tree
  192. // apply the func f to each internal node
  193. func sortDfs(key string, t *treeNode, f func(string, *Node)) {
  194. // base case
  195. if len(t.NodeMap) == 0 {
  196. f(key, &t.InternalNode)
  197. // recursion
  198. } else {
  199. s := make(tnWithKeySlice, len(t.NodeMap))
  200. i := 0
  201. // copy
  202. for tnKey, tn := range t.NodeMap {
  203. tnKey := key + "/" + tnKey
  204. s[i] = tnWithKey{tnKey, tn}
  205. i++
  206. }
  207. // sort
  208. sort.Sort(s)
  209. // traverse
  210. for i = 0; i < len(t.NodeMap); i++ {
  211. sortDfs(s[i].key, s[i].tn, f)
  212. }
  213. }
  214. }
  215. // split the key by '/', get the intermediate node name
  216. func split(key string) []string {
  217. key = "/" + key
  218. key = path.Clean(key)
  219. // get the intermidate nodes name
  220. nodesName := strings.Split(key, "/")
  221. // we do not need the root node, since we start with it
  222. nodesName = nodesName[1:]
  223. return nodesName
  224. }