node.go 6.2 KB

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