tree.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package store
  2. import (
  3. "path"
  4. "strings"
  5. "sort"
  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. // we change the value of a old Treenode
  83. tn.InternalNode = value
  84. }
  85. return true
  86. }
  87. // Get the tree node of the key
  88. func (t *tree)internalGet(key string) (*treeNode, bool) {
  89. nodesName := split(key)
  90. nodeMap := t.Root.NodeMap
  91. var i int
  92. for i = 0; i < len(nodesName) - 1; i++ {
  93. node, ok := nodeMap[nodesName[i]]
  94. if !ok || !node.Dir {
  95. return nil, false
  96. }
  97. nodeMap = node.NodeMap
  98. }
  99. tn, ok := nodeMap[nodesName[i]]
  100. if ok {
  101. return tn, ok
  102. } else {
  103. return nil, ok
  104. }
  105. }
  106. // get the internalNode of the key
  107. func (t *tree) get(key string) (Node, bool) {
  108. tn, ok := t.internalGet(key)
  109. if ok {
  110. if tn.Dir {
  111. return emptyNode, false
  112. }
  113. return tn.InternalNode, ok
  114. } else {
  115. return emptyNode, ok
  116. }
  117. }
  118. // get the internalNode of the key
  119. func (t *tree) list(directory string) ([]Node, []string, []bool, bool) {
  120. treeNode, ok := t.internalGet(directory)
  121. if !ok {
  122. return nil, nil, nil, ok
  123. } else {
  124. if !treeNode.Dir {
  125. nodes := make([]Node, 1)
  126. nodes[0] = treeNode.InternalNode
  127. return nodes, make([]string, 1), make([]bool, 1), true
  128. }
  129. length := len(treeNode.NodeMap)
  130. nodes := make([]Node, length)
  131. keys := make([]string, length)
  132. dirs := make([]bool, length)
  133. i := 0
  134. for key, node := range treeNode.NodeMap {
  135. nodes[i] = node.InternalNode
  136. keys[i] = key
  137. if node.Dir {
  138. dirs[i] = true
  139. } else {
  140. dirs[i] = false
  141. }
  142. i++
  143. }
  144. return nodes, keys, dirs, 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. // deep first search to traverse the tree
  175. // apply the func f to each internal node
  176. func dfs(key string, t *treeNode, f func(string, *Node)) {
  177. // base case
  178. if len(t.NodeMap) == 0{
  179. f(key, &t.InternalNode)
  180. // recursion
  181. } else {
  182. for tnKey, tn := range t.NodeMap {
  183. tnKey := key + "/" + tnKey
  184. dfs(tnKey, tn, f)
  185. }
  186. }
  187. }
  188. // sort deep first search to traverse the tree
  189. // apply the func f to each internal node
  190. func sortDfs(key string, t *treeNode, f func(string, *Node)) {
  191. // base case
  192. if len(t.NodeMap) == 0{
  193. f(key, &t.InternalNode)
  194. // recursion
  195. } else {
  196. s := make(tnWithKeySlice, len(t.NodeMap))
  197. i := 0
  198. // copy
  199. for tnKey, tn := range t.NodeMap {
  200. tnKey := key + "/" + tnKey
  201. s[i] = tnWithKey{tnKey, tn}
  202. i++
  203. }
  204. // sort
  205. sort.Sort(s)
  206. // traverse
  207. for i = 0; i < len(t.NodeMap); i++ {
  208. sortDfs(s[i].key, s[i].tn, f)
  209. }
  210. }
  211. }
  212. // split the key by '/', get the intermediate node name
  213. func split(key string) []string {
  214. key = "/" + key
  215. key = path.Clean(key)
  216. // get the intermidate nodes name
  217. nodesName := strings.Split(key, "/")
  218. // we do not need the root node, since we start with it
  219. nodesName = nodesName[1:]
  220. return nodesName
  221. }