node.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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(keyPath string, value string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
  32. return &Node{
  33. Path: keyPath,
  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(keyPath string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
  46. return &Node{
  47. Path: keyPath,
  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) 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. n.stopExpire <- true
  73. n.status = removed
  74. }
  75. return nil
  76. }
  77. if !recursive {
  78. return etcdErr.NewError(102, "")
  79. }
  80. for _, child := range n.Children { // delete all children
  81. child.Remove(true)
  82. }
  83. // delete self
  84. _, name := path.Split(n.Path)
  85. if n.Parent.Children[name] == n {
  86. delete(n.Parent.Children, name)
  87. n.stopExpire <- true
  88. n.status = removed
  89. }
  90. return nil
  91. }
  92. // Get function gets the value of the node.
  93. // If the receiver node is not a key-value pair, a "Not A File" error will be returned.
  94. func (n *Node) Read() (string, error) {
  95. if n.IsDir() {
  96. return "", etcdErr.NewError(102, "")
  97. }
  98. return n.Value, nil
  99. }
  100. // Set function set the value of the node to the given value.
  101. // If the receiver node is a directory, a "Not A File" error will be returned.
  102. func (n *Node) Write(value string, index uint64, term uint64) error {
  103. if n.IsDir() {
  104. return etcdErr.NewError(102, "")
  105. }
  106. n.Value = value
  107. n.ModifiedIndex = index
  108. n.ModifiedTerm = term
  109. return nil
  110. }
  111. // List function return a slice of nodes under the receiver node.
  112. // If the receiver node is not a directory, a "Not A Directory" error will be returned.
  113. func (n *Node) List() ([]*Node, error) {
  114. n.mu.Lock()
  115. defer n.mu.Unlock()
  116. if !n.IsDir() {
  117. return nil, etcdErr.NewError(104, "")
  118. }
  119. nodes := make([]*Node, len(n.Children))
  120. i := 0
  121. for _, node := range n.Children {
  122. nodes[i] = node
  123. i++
  124. }
  125. return nodes, nil
  126. }
  127. func (n *Node) GetFile(name string) (*Node, error) {
  128. n.mu.Lock()
  129. defer n.mu.Unlock()
  130. if !n.IsDir() {
  131. return nil, etcdErr.NewError(104, n.Path)
  132. }
  133. f, ok := n.Children[name]
  134. if ok {
  135. if !f.IsDir() {
  136. return f, nil
  137. } else {
  138. return nil, etcdErr.NewError(102, f.Path)
  139. }
  140. }
  141. return nil, nil
  142. }
  143. // Add function adds a node to the receiver node.
  144. // If the receiver is not a directory, a "Not A Directory" error will be returned.
  145. // If there is a existing node with the same name under the directory, a "Already Exist"
  146. // error will be returned
  147. func (n *Node) Add(child *Node) error {
  148. n.mu.Lock()
  149. defer n.mu.Unlock()
  150. if n.status == removed {
  151. return etcdErr.NewError(100, "")
  152. }
  153. if !n.IsDir() {
  154. return etcdErr.NewError(104, "")
  155. }
  156. _, name := path.Split(child.Path)
  157. _, ok := n.Children[name]
  158. if ok {
  159. return etcdErr.NewError(105, "")
  160. }
  161. n.Children[name] = child
  162. return nil
  163. }
  164. // Clone function clone the node recursively and return the new node.
  165. // If the node is a directory, it will clone all the content under this directory.
  166. // If the node is a key-value pair, it will clone the pair.
  167. func (n *Node) Clone() *Node {
  168. n.mu.Lock()
  169. defer n.mu.Unlock()
  170. if !n.IsDir() {
  171. return newFile(n.Path, n.Value, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  172. }
  173. clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  174. for key, child := range n.Children {
  175. clone.Children[key] = child.Clone()
  176. }
  177. return clone
  178. }
  179. // IsDir function checks whether the node is a directory.
  180. // If the node is a directory, the function will return true.
  181. // Otherwise the function will return false.
  182. func (n *Node) IsDir() bool {
  183. if n.Children == nil { // key-value pair
  184. return false
  185. }
  186. return true
  187. }
  188. func (n *Node) Expire() {
  189. duration := n.ExpireTime.Sub(time.Now())
  190. if duration <= 0 {
  191. n.Remove(true)
  192. return
  193. }
  194. select {
  195. // if timeout, delete the node
  196. case <-time.After(duration):
  197. n.Remove(true)
  198. return
  199. // if stopped, return
  200. case <-n.stopExpire:
  201. fmt.Println("expire stopped")
  202. return
  203. }
  204. }
  205. // IsHidden function checks if the node is a hidden node. A hidden node
  206. // will begin with '_'
  207. // A hidden node will not be shown via get command under a directory
  208. // For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
  209. // will only return /foo/notHidden
  210. func (n *Node) IsHidden() bool {
  211. _, name := path.Split(n.Path)
  212. if name[0] == '_' { //hidden
  213. return true
  214. }
  215. return false
  216. }
  217. func (n *Node) Pair(recurisive bool) KeyValuePair {
  218. if n.IsDir() {
  219. pair := KeyValuePair{
  220. Key: n.Path,
  221. Dir: true,
  222. }
  223. if !recurisive {
  224. return pair
  225. }
  226. children, _ := n.List()
  227. pair.KVPairs = make([]KeyValuePair, len(children))
  228. // we do not use the index in the children slice directly
  229. // we need to skip the hidden one
  230. i := 0
  231. for _, child := range children {
  232. if child.IsHidden() { // get will not list hidden node
  233. continue
  234. }
  235. pair.KVPairs[i] = child.Pair(recurisive)
  236. i++
  237. }
  238. // eliminate hidden nodes
  239. pair.KVPairs = pair.KVPairs[:i]
  240. return pair
  241. }
  242. return KeyValuePair{
  243. Key: n.Path,
  244. Value: n.Value,
  245. }
  246. }