tree.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. return tn.InternalNode, ok
  111. } else {
  112. return emptyNode, ok
  113. }
  114. }
  115. // return the nodes information under the directory
  116. func (t *tree) list(directory string) ([]Node, []string, []string, bool) {
  117. treeNode, ok := t.internalGet(directory)
  118. if !ok {
  119. return nil, nil, nil, ok
  120. } else {
  121. length := len(treeNode.NodeMap)
  122. nodes := make([]Node, length)
  123. keys := make([]string, length)
  124. dirs := make([]string, length)
  125. i := 0
  126. for key, node := range treeNode.NodeMap {
  127. nodes[i] = node.InternalNode
  128. keys[i] = key
  129. if node.Dir {
  130. dirs[i] = "d"
  131. } else {
  132. dirs[i] = "f"
  133. }
  134. i++
  135. }
  136. return nodes, keys, dirs, ok
  137. }
  138. }
  139. // delete the key, return true if success
  140. func (t *tree) delete(key string) bool {
  141. nodesName := split(key)
  142. nodeMap := t.Root.NodeMap
  143. var i int
  144. for i = 0; i < len(nodesName) - 1; i++ {
  145. node, ok := nodeMap[nodesName[i]]
  146. if !ok || !node.Dir {
  147. return false
  148. }
  149. nodeMap = node.NodeMap
  150. }
  151. node, ok := nodeMap[nodesName[i]]
  152. if ok && !node.Dir{
  153. delete(nodeMap, nodesName[i])
  154. return true
  155. }
  156. return false
  157. }
  158. // traverse wrapper
  159. func (t *tree) traverse(f func(string, *Node), sort bool) {
  160. if sort {
  161. sortDfs("", t.Root, f)
  162. } else {
  163. dfs("", t.Root, f)
  164. }
  165. }
  166. // deep first search to traverse the tree
  167. // apply the func f to each internal node
  168. func dfs(key string, t *treeNode, f func(string, *Node)) {
  169. // base case
  170. if len(t.NodeMap) == 0{
  171. f(key, &t.InternalNode)
  172. // recursion
  173. } else {
  174. for tnKey, tn := range t.NodeMap {
  175. tnKey := key + "/" + tnKey
  176. dfs(tnKey, tn, f)
  177. }
  178. }
  179. }
  180. // sort deep first search to traverse the tree
  181. // apply the func f to each internal node
  182. func sortDfs(key string, t *treeNode, f func(string, *Node)) {
  183. // base case
  184. if len(t.NodeMap) == 0{
  185. f(key, &t.InternalNode)
  186. // recursion
  187. } else {
  188. s := make(tnWithKeySlice, len(t.NodeMap))
  189. i := 0
  190. // copy
  191. for tnKey, tn := range t.NodeMap {
  192. tnKey := key + "/" + tnKey
  193. s[i] = tnWithKey{tnKey, tn}
  194. i++
  195. }
  196. // sort
  197. sort.Sort(s)
  198. // traverse
  199. for i = 0; i < len(t.NodeMap); i++ {
  200. sortDfs(s[i].key, s[i].tn, f)
  201. }
  202. }
  203. }
  204. // split the key by '/', get the intermediate node name
  205. func split(key string) []string {
  206. key = "/" + key
  207. key = path.Clean(key)
  208. // get the intermidate nodes name
  209. nodesName := strings.Split(key, "/")
  210. // we do not need the root node, since we start with it
  211. nodesName = nodesName[1:]
  212. return nodesName
  213. }