tree.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. // should be able to get root
  99. if len(nodesName) == 1 && nodesName[0] == "" {
  100. return t.Root, true
  101. }
  102. nodeMap := t.Root.NodeMap
  103. var i int
  104. for i = 0; i < len(nodesName)-1; i++ {
  105. node, ok := nodeMap[nodesName[i]]
  106. if !ok || !node.Dir {
  107. return nil, false
  108. }
  109. nodeMap = node.NodeMap
  110. }
  111. tn, ok := nodeMap[nodesName[i]]
  112. if ok {
  113. return tn, ok
  114. } else {
  115. return nil, ok
  116. }
  117. }
  118. // get the internalNode of the key
  119. func (t *tree) get(key string) (Node, bool) {
  120. tn, ok := t.internalGet(key)
  121. if ok {
  122. if tn.Dir {
  123. return emptyNode, false
  124. }
  125. return tn.InternalNode, ok
  126. } else {
  127. return emptyNode, ok
  128. }
  129. }
  130. // get the internalNode of the key
  131. func (t *tree) list(directory string) (interface{}, []string, bool) {
  132. treeNode, ok := t.internalGet(directory)
  133. if !ok {
  134. return nil, nil, ok
  135. } else {
  136. if !treeNode.Dir {
  137. return &treeNode.InternalNode, nil, ok
  138. }
  139. length := len(treeNode.NodeMap)
  140. nodes := make([]*Node, length)
  141. keys := make([]string, length)
  142. i := 0
  143. for key, node := range treeNode.NodeMap {
  144. nodes[i] = &node.InternalNode
  145. keys[i] = key
  146. i++
  147. }
  148. return nodes, keys, ok
  149. }
  150. }
  151. // delete the key, return true if success
  152. func (t *tree) delete(key string) bool {
  153. nodesName := split(key)
  154. nodeMap := t.Root.NodeMap
  155. var i int
  156. for i = 0; i < len(nodesName)-1; i++ {
  157. node, ok := nodeMap[nodesName[i]]
  158. if !ok || !node.Dir {
  159. return false
  160. }
  161. nodeMap = node.NodeMap
  162. }
  163. node, ok := nodeMap[nodesName[i]]
  164. if ok && !node.Dir {
  165. delete(nodeMap, nodesName[i])
  166. return true
  167. }
  168. return false
  169. }
  170. // traverse wrapper
  171. func (t *tree) traverse(f func(string, *Node), sort bool) {
  172. if sort {
  173. sortDfs("", t.Root, f)
  174. } else {
  175. dfs("", t.Root, f)
  176. }
  177. }
  178. // clone() will return a deep cloned tree
  179. func (t *tree) clone() *tree {
  180. newTree := new(tree)
  181. newTree.Root = &treeNode{
  182. Node{
  183. "/",
  184. time.Unix(0, 0),
  185. nil,
  186. },
  187. true,
  188. make(map[string]*treeNode),
  189. }
  190. recursiveClone(t.Root, newTree.Root)
  191. return newTree
  192. }
  193. // recursiveClone is a helper function for clone()
  194. func recursiveClone(tnSrc *treeNode, tnDes *treeNode) {
  195. if !tnSrc.Dir {
  196. tnDes.InternalNode = tnSrc.InternalNode
  197. return
  198. } else {
  199. tnDes.InternalNode = tnSrc.InternalNode
  200. tnDes.Dir = true
  201. tnDes.NodeMap = make(map[string]*treeNode)
  202. for key, tn := range tnSrc.NodeMap {
  203. newTn := new(treeNode)
  204. recursiveClone(tn, newTn)
  205. tnDes.NodeMap[key] = newTn
  206. }
  207. }
  208. }
  209. // deep first search to traverse the tree
  210. // apply the func f to each internal node
  211. func dfs(key string, t *treeNode, f func(string, *Node)) {
  212. // base case
  213. if len(t.NodeMap) == 0 {
  214. f(key, &t.InternalNode)
  215. // recursion
  216. } else {
  217. for tnKey, tn := range t.NodeMap {
  218. tnKey := key + "/" + tnKey
  219. dfs(tnKey, tn, f)
  220. }
  221. }
  222. }
  223. // sort deep first search to traverse the tree
  224. // apply the func f to each internal node
  225. func sortDfs(key string, t *treeNode, f func(string, *Node)) {
  226. // base case
  227. if len(t.NodeMap) == 0 {
  228. f(key, &t.InternalNode)
  229. // recursion
  230. } else {
  231. s := make(tnWithKeySlice, len(t.NodeMap))
  232. i := 0
  233. // copy
  234. for tnKey, tn := range t.NodeMap {
  235. tnKey := key + "/" + tnKey
  236. s[i] = tnWithKey{tnKey, tn}
  237. i++
  238. }
  239. // sort
  240. sort.Sort(s)
  241. // traverse
  242. for i = 0; i < len(t.NodeMap); i++ {
  243. sortDfs(s[i].key, s[i].tn, f)
  244. }
  245. }
  246. }
  247. // split the key by '/', get the intermediate node name
  248. func split(key string) []string {
  249. key = "/" + key
  250. key = path.Clean(key)
  251. // get the intermidate nodes name
  252. nodesName := strings.Split(key, "/")
  253. // we do not need the root node, since we start with it
  254. nodesName = nodesName[1:]
  255. return nodesName
  256. }