node.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package fileSystem
  2. import (
  3. "fmt"
  4. "path/filepath"
  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. removeChan chan bool // remove channel
  28. }
  29. func newFile(path string, value string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
  30. return &Node{
  31. Path: path,
  32. CreateIndex: createIndex,
  33. CreateTerm: createTerm,
  34. Parent: parent,
  35. ACL: ACL,
  36. removeChan: make(chan bool, 1),
  37. ExpireTime: expireTime,
  38. Value: value,
  39. }
  40. }
  41. func newDir(path string, createIndex uint64, createTerm uint64, parent *Node, ACL string) *Node {
  42. return &Node{
  43. Path: path,
  44. CreateIndex: createIndex,
  45. CreateTerm: createTerm,
  46. Parent: parent,
  47. ACL: ACL,
  48. removeChan: 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() { // key-value pair
  62. _, name := filepath.Split(n.Path)
  63. if n.Parent.Children[name] == n {
  64. delete(n.Parent.Children, name)
  65. n.removeChan <- true
  66. n.status = removed
  67. }
  68. return nil
  69. }
  70. if !recursive {
  71. return etcdErr.NewError(102, "")
  72. }
  73. for _, n := range n.Children { // delete all children
  74. n.Remove(true)
  75. }
  76. // delete self
  77. _, name := filepath.Split(n.Path)
  78. if n.Parent.Children[name] == n {
  79. delete(n.Parent.Children, name)
  80. n.removeChan <- true
  81. n.status = removed
  82. }
  83. return nil
  84. }
  85. // Get function gets the value of the node.
  86. // If the receiver node is not a key-value pair, a "Not A File" error will be returned.
  87. func (n *Node) Read() (string, error) {
  88. if n.IsDir() {
  89. return "", etcdErr.NewError(102, "")
  90. }
  91. return n.Value, nil
  92. }
  93. // Set function set the value of the node to the given value.
  94. // If the receiver node is a directory, a "Not A File" error will be returned.
  95. func (n *Node) Write(value string) error {
  96. if n.IsDir() {
  97. return etcdErr.NewError(102, "")
  98. }
  99. n.Value = value
  100. return nil
  101. }
  102. // List function return a slice of nodes under the receiver node.
  103. // If the receiver node is not a directory, a "Not A Directory" error will be returned.
  104. func (n *Node) List() ([]*Node, error) {
  105. n.mu.Lock()
  106. n.mu.Unlock()
  107. if !n.IsDir() {
  108. return nil, etcdErr.NewError(104, "")
  109. }
  110. nodes := make([]*Node, len(n.Children))
  111. i := 0
  112. for _, node := range n.Children {
  113. nodes[i] = node
  114. i++
  115. }
  116. return nodes, nil
  117. }
  118. // Add function adds a node to the receiver node.
  119. // If the receiver is not a directory, a "Not A Directory" error will be returned.
  120. // If there is a existing node with the same name under the directory, a "Already Exist"
  121. // error will be returned
  122. func (n *Node) Add(child *Node) error {
  123. n.mu.Lock()
  124. n.mu.Unlock()
  125. if n.status == removed {
  126. return etcdErr.NewError(100, "")
  127. }
  128. if !n.IsDir() {
  129. return etcdErr.NewError(104, "")
  130. }
  131. _, name := filepath.Split(child.Path)
  132. _, ok := n.Children[name]
  133. if ok {
  134. return etcdErr.NewError(105, "")
  135. }
  136. n.Children[name] = child
  137. return nil
  138. }
  139. // Clone function clone the node recursively and return the new node.
  140. // If the node is a directory, it will clone all the content under this directory.
  141. // If the node is a key-value pair, it will clone the pair.
  142. func (n *Node) Clone() *Node {
  143. n.mu.Lock()
  144. n.mu.Unlock()
  145. if !n.IsDir() {
  146. return newFile(n.Path, n.Value, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  147. }
  148. clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL)
  149. for key, child := range n.Children {
  150. clone.Children[key] = child.Clone()
  151. }
  152. return clone
  153. }
  154. // IsDir function checks whether the node is a directory.
  155. // If the node is a directory, the function will return true.
  156. // Otherwise the function will return false.
  157. func (n *Node) IsDir() bool {
  158. if n.Children == nil { // key-value pair
  159. return false
  160. }
  161. return true
  162. }
  163. func (n *Node) Expire() {
  164. for {
  165. duration := n.ExpireTime.Sub(time.Now())
  166. if duration <= 0 {
  167. n.Remove(true)
  168. return
  169. }
  170. select {
  171. // if timeout, delete the node
  172. case <-time.After(duration):
  173. n.Remove(true)
  174. return
  175. // if removed, return
  176. case <-n.removeChan:
  177. fmt.Println("node removed")
  178. return
  179. }
  180. }
  181. }