node.go 6.5 KB

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