tree.go 6.9 KB

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