node.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package fileSystem
  2. import (
  3. "fmt"
  4. "path"
  5. "sort"
  6. "sync"
  7. "time"
  8. etcdErr "github.com/coreos/etcd/error"
  9. )
  10. var (
  11. Permanent time.Time
  12. )
  13. const (
  14. normal = iota
  15. removed
  16. )
  17. type Node struct {
  18. Path string
  19. CreateIndex uint64
  20. CreateTerm uint64
  21. ModifiedIndex uint64
  22. ModifiedTerm uint64
  23. Parent *Node
  24. ExpireTime time.Time
  25. ACL string
  26. Value string // for key-value pair
  27. Children map[string]*Node // for directory
  28. status int
  29. mu sync.Mutex
  30. stopExpire chan bool // stop expire routine channel
  31. }
  32. func newFile(nodePath string, value string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
  33. return &Node{
  34. Path: nodePath,
  35. CreateIndex: createIndex,
  36. CreateTerm: createTerm,
  37. ModifiedIndex: createIndex,
  38. ModifiedTerm: createTerm,
  39. Parent: parent,
  40. ACL: ACL,
  41. stopExpire: make(chan bool, 1),
  42. ExpireTime: expireTime,
  43. Value: value,
  44. }
  45. }
  46. func newDir(nodePath string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
  47. return &Node{
  48. Path: nodePath,
  49. CreateIndex: createIndex,
  50. CreateTerm: createTerm,
  51. Parent: parent,
  52. ACL: ACL,
  53. stopExpire: make(chan bool, 1),
  54. ExpireTime: expireTime,
  55. Children: make(map[string]*Node),
  56. }
  57. }
  58. // Remove function remove the node.
  59. // If the node is a directory and recursive is true, the function will recursively remove
  60. // add nodes under the receiver node.
  61. func (n *Node) Remove(recursive bool, callback func(path string)) error {
  62. n.mu.Lock()
  63. defer n.mu.Unlock()
  64. if n.status == removed {
  65. return nil
  66. }
  67. if !n.IsDir() { // file node: key-value pair
  68. _, name := path.Split(n.Path)
  69. if n.Parent.Children[name] == n {
  70. // This is the only pointer to Node object
  71. // Handled by garbage collector
  72. delete(n.Parent.Children, name)
  73. if callback != nil {
  74. callback(n.Path)
  75. }
  76. n.stopExpire <- true
  77. n.status = removed
  78. }
  79. return nil
  80. }
  81. if !recursive {
  82. return etcdErr.NewError(etcdErr.EcodeNotFile, "")
  83. }
  84. for _, child := range n.Children { // delete all children
  85. child.Remove(true, callback)
  86. }
  87. // delete self
  88. _, name := path.Split(n.Path)
  89. if n.Parent.Children[name] == n {
  90. delete(n.Parent.Children, name)
  91. if callback != nil {
  92. callback(n.Path)
  93. }
  94. n.stopExpire <- true
  95. n.status = removed
  96. }
  97. return nil
  98. }
  99. // Read function gets the value of the node.
  100. // If the receiver node is not a key-value pair, a "Not A File" error will be returned.
  101. func (n *Node) Read() (string, error) {
  102. if n.IsDir() {
  103. return "", etcdErr.NewError(etcdErr.EcodeNotFile, "")
  104. }
  105. return n.Value, nil
  106. }
  107. // Write function set the value of the node to the given value.
  108. // If the receiver node is a directory, a "Not A File" error will be returned.
  109. func (n *Node) Write(value string, index uint64, term uint64) error {
  110. if n.IsDir() {
  111. return etcdErr.NewError(etcdErr.EcodeNotFile, "")
  112. }
  113. n.Value = value
  114. n.ModifiedIndex = index
  115. n.ModifiedTerm = term
  116. return nil
  117. }
  118. // List function return a slice of nodes under the receiver node.
  119. // If the receiver node is not a directory, a "Not A Directory" error will be returned.
  120. func (n *Node) List() ([]*Node, error) {
  121. n.mu.Lock()
  122. defer n.mu.Unlock()
  123. if !n.IsDir() {
  124. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, "")
  125. }
  126. nodes := make([]*Node, len(n.Children))
  127. i := 0
  128. for _, node := range n.Children {
  129. nodes[i] = node
  130. i++
  131. }
  132. return nodes, nil
  133. }
  134. // GetFile function returns the file node under the directory node.
  135. // On success, it returns the file node
  136. // If the node that calls this function is not a directory, it returns
  137. // Not Directory Error
  138. // If the node corresponding to the name string is not file, it returns
  139. // Not File Error
  140. func (n *Node) GetFile(name string) (*Node, error) {
  141. n.mu.Lock()
  142. defer n.mu.Unlock()
  143. if !n.IsDir() {
  144. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, n.Path)
  145. }
  146. f, ok := n.Children[name]
  147. if ok {
  148. if !f.IsDir() {
  149. return f, nil
  150. } else {
  151. return nil, etcdErr.NewError(etcdErr.EcodeNotFile, f.Path)
  152. }
  153. }
  154. return nil, nil
  155. }
  156. // Add function adds a node to the receiver node.
  157. // If the receiver is not a directory, a "Not A Directory" error will be returned.
  158. // If there is a existing node with the same name under the directory, a "Already Exist"
  159. // error will be returned
  160. func (n *Node) Add(child *Node) error {
  161. n.mu.Lock()
  162. defer n.mu.Unlock()
  163. if n.status == removed {
  164. return etcdErr.NewError(etcdErr.EcodeKeyNotFound, "")
  165. }
  166. if !n.IsDir() {
  167. return etcdErr.NewError(etcdErr.EcodeNotDir, "")
  168. }
  169. _, name := path.Split(child.Path)
  170. _, ok := n.Children[name]
  171. if ok {
  172. return etcdErr.NewError(etcdErr.EcodeNodeExist, "")
  173. }
  174. n.Children[name] = child
  175. return nil
  176. }
  177. // Clone function clone the node recursively and return the new node.
  178. // If the node is a directory, it will clone all the content under this directory.
  179. // If the node is a key-value pair, it will clone the pair.
  180. func (n *Node) Clone() *Node {
  181. n.mu.Lock()
  182. defer n.mu.Unlock()
  183. if !n.IsDir() {
  184. return newFile(n.Path, n.Value, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  185. }
  186. clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  187. for key, child := range n.Children {
  188. clone.Children[key] = child.Clone()
  189. }
  190. return clone
  191. }
  192. // IsDir function checks whether the node is a directory.
  193. // If the node is a directory, the function will return true.
  194. // Otherwise the function will return false.
  195. func (n *Node) IsDir() bool {
  196. if n.Children == nil { // key-value pair
  197. return false
  198. }
  199. return true
  200. }
  201. func (n *Node) Expire() {
  202. duration := n.ExpireTime.Sub(time.Now())
  203. if duration <= 0 {
  204. n.Remove(true, nil)
  205. return
  206. }
  207. select {
  208. // if timeout, delete the node
  209. case <-time.After(duration):
  210. n.Remove(true, nil)
  211. return
  212. // if stopped, return
  213. case <-n.stopExpire:
  214. fmt.Println("expire stopped")
  215. return
  216. }
  217. }
  218. // IsHidden function checks if the node is a hidden node. A hidden node
  219. // will begin with '_'
  220. // A hidden node will not be shown via get command under a directory
  221. // For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
  222. // will only return /foo/notHidden
  223. func (n *Node) IsHidden() bool {
  224. _, name := path.Split(n.Path)
  225. if name[0] == '_' { //hidden
  226. return true
  227. }
  228. return false
  229. }
  230. func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
  231. if n.IsDir() {
  232. pair := KeyValuePair{
  233. Key: n.Path,
  234. Dir: true,
  235. }
  236. if !recurisive {
  237. return pair
  238. }
  239. children, _ := n.List()
  240. pair.KVPairs = make([]KeyValuePair, len(children))
  241. // we do not use the index in the children slice directly
  242. // we need to skip the hidden one
  243. i := 0
  244. for _, child := range children {
  245. if child.IsHidden() { // get will not list hidden node
  246. continue
  247. }
  248. pair.KVPairs[i] = child.Pair(recurisive, sorted)
  249. i++
  250. }
  251. // eliminate hidden nodes
  252. pair.KVPairs = pair.KVPairs[:i]
  253. if sorted {
  254. sort.Sort(pair)
  255. }
  256. return pair
  257. }
  258. return KeyValuePair{
  259. Key: n.Path,
  260. Value: n.Value,
  261. }
  262. }