tree.go 6.3 KB

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