node.go 5.5 KB

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