node.go 6.4 KB

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