node.go 7.4 KB

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