file_system.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package fileSystem
  2. import (
  3. "fmt"
  4. "path"
  5. "sort"
  6. "strings"
  7. "time"
  8. etcdErr "github.com/coreos/etcd/error"
  9. )
  10. type FileSystem struct {
  11. Root *Node
  12. EventHistory *EventHistory
  13. WatcherHub *watcherHub
  14. Index uint64
  15. Term uint64
  16. }
  17. func New() *FileSystem {
  18. return &FileSystem{
  19. Root: newDir("/", 0, 0, nil, "", Permanent),
  20. WatcherHub: newWatchHub(1000),
  21. }
  22. }
  23. func (fs *FileSystem) Get(nodePath string, recusive, sorting bool, index uint64, term uint64) (*Event, error) {
  24. n, err := fs.InternalGet(nodePath, index, term)
  25. if err != nil {
  26. return nil, err
  27. }
  28. e := newEvent(Get, nodePath, index, term)
  29. if n.IsDir() { // node is dir
  30. e.Dir = true
  31. children, _ := n.List()
  32. e.KVPairs = make([]KeyValuePair, len(children))
  33. // we do not use the index in the children slice directly
  34. // we need to skip the hidden one
  35. i := 0
  36. for _, child := range children {
  37. if child.IsHidden() { // get will not list hidden node
  38. continue
  39. }
  40. e.KVPairs[i] = child.Pair(recusive)
  41. i++
  42. }
  43. // eliminate hidden nodes
  44. e.KVPairs = e.KVPairs[:i]
  45. if sorting {
  46. sort.Sort(e)
  47. }
  48. } else { // node is file
  49. e.Value = n.Value
  50. }
  51. return e, nil
  52. }
  53. // Create function creates the Node at nodePath. Create will help to create intermediate directories with no ttl.
  54. // If the node has already existed, create will fail.
  55. // If any node on the path is a file, create will fail.
  56. func (fs *FileSystem) Create(nodePath string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
  57. nodePath = path.Clean("/" + nodePath)
  58. // make sure we can create the node
  59. _, err := fs.InternalGet(nodePath, index, term)
  60. if err == nil { // key already exists
  61. return nil, etcdErr.NewError(105, nodePath)
  62. }
  63. etcdError, _ := err.(etcdErr.Error)
  64. if etcdError.ErrorCode == 104 { // we cannot create the key due to meet a file while walking
  65. return nil, err
  66. }
  67. dir, _ := path.Split(nodePath)
  68. // walk through the nodePath, create dirs and get the last directory node
  69. d, err := fs.walk(dir, fs.checkDir)
  70. if err != nil {
  71. return nil, err
  72. }
  73. e := newEvent(Create, nodePath, fs.Index, fs.Term)
  74. var n *Node
  75. if len(value) != 0 { // create file
  76. e.Value = value
  77. n = newFile(nodePath, value, fs.Index, fs.Term, d, "", expireTime)
  78. } else { // create directory
  79. e.Dir = true
  80. n = newDir(nodePath, fs.Index, fs.Term, d, "", expireTime)
  81. }
  82. err = d.Add(n)
  83. if err != nil {
  84. return nil, err
  85. }
  86. // Node with TTL
  87. if expireTime != Permanent {
  88. go n.Expire()
  89. e.Expiration = &n.ExpireTime
  90. e.TTL = int64(expireTime.Sub(time.Now()) / time.Second)
  91. }
  92. fs.WatcherHub.notify(e)
  93. return e, nil
  94. }
  95. // Update function updates the value/ttl of the node.
  96. // If the node is a file, the value and the ttl can be updated.
  97. // If the node is a directory, only the ttl can be updated.
  98. func (fs *FileSystem) Update(nodePath string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
  99. n, err := fs.InternalGet(nodePath, index, term)
  100. if err != nil { // if the node does not exist, return error
  101. return nil, err
  102. }
  103. e := newEvent(Update, nodePath, fs.Index, fs.Term)
  104. if n.IsDir() { // if the node is a directory, we can only update ttl
  105. if len(value) != 0 {
  106. return nil, etcdErr.NewError(102, nodePath)
  107. }
  108. } else { // if the node is a file, we can update value and ttl
  109. e.PrevValue = n.Value
  110. if len(value) != 0 {
  111. e.Value = value
  112. }
  113. n.Write(value, index, term)
  114. }
  115. // update ttl
  116. if n.ExpireTime != Permanent && expireTime != Permanent {
  117. n.stopExpire <- true
  118. }
  119. if expireTime != Permanent {
  120. n.ExpireTime = expireTime
  121. go n.Expire()
  122. e.Expiration = &n.ExpireTime
  123. e.TTL = int64(expireTime.Sub(time.Now()) / time.Second)
  124. }
  125. fs.WatcherHub.notify(e)
  126. return e, nil
  127. }
  128. func (fs *FileSystem) TestAndSet(nodePath string, prevValue string, prevIndex uint64,
  129. value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
  130. f, err := fs.InternalGet(nodePath, index, term)
  131. if err != nil {
  132. return nil, err
  133. }
  134. if f.IsDir() { // can only test and set file
  135. return nil, etcdErr.NewError(102, nodePath)
  136. }
  137. if f.Value == prevValue || f.ModifiedIndex == prevIndex {
  138. // if test succeed, write the value
  139. e := newEvent(TestAndSet, nodePath, index, term)
  140. e.PrevValue = f.Value
  141. e.Value = value
  142. f.Write(value, index, term)
  143. fs.WatcherHub.notify(e)
  144. return e, nil
  145. }
  146. cause := fmt.Sprintf("[%v/%v] [%v/%v]", prevValue, f.Value, prevIndex, f.ModifiedIndex)
  147. return nil, etcdErr.NewError(101, cause)
  148. }
  149. // Delete function deletes the node at the given path.
  150. // If the node is a directory, recursive must be true to delete it.
  151. func (fs *FileSystem) Delete(nodePath string, recursive bool, index uint64, term uint64) (*Event, error) {
  152. n, err := fs.InternalGet(nodePath, index, term)
  153. if err != nil { // if the node does not exist, return error
  154. return nil, err
  155. }
  156. e := newEvent(Delete, nodePath, index, term)
  157. if n.IsDir() {
  158. e.Dir = true
  159. } else {
  160. e.PrevValue = n.Value
  161. }
  162. callback := func(path string) { // notify function
  163. fs.WatcherHub.notifyWithPath(e, path, true)
  164. }
  165. err = n.Remove(recursive, callback)
  166. if err != nil {
  167. return nil, err
  168. }
  169. fs.WatcherHub.notify(e)
  170. return e, nil
  171. }
  172. // walk function walks all the nodePath and apply the walkFunc on each directory
  173. func (fs *FileSystem) walk(nodePath string, walkFunc func(prev *Node, component string) (*Node, error)) (*Node, error) {
  174. components := strings.Split(nodePath, "/")
  175. curr := fs.Root
  176. var err error
  177. for i := 1; i < len(components); i++ {
  178. if len(components[i]) == 0 { // ignore empty string
  179. return curr, nil
  180. }
  181. curr, err = walkFunc(curr, components[i])
  182. if err != nil {
  183. return nil, err
  184. }
  185. }
  186. return curr, nil
  187. }
  188. // InternalGet function get the node of the given nodePath.
  189. func (fs *FileSystem) InternalGet(nodePath string, index uint64, term uint64) (*Node, error) {
  190. nodePath = path.Clean("/" + nodePath)
  191. // update file system known index and term
  192. fs.Index, fs.Term = index, term
  193. walkFunc := func(parent *Node, name string) (*Node, error) {
  194. if !parent.IsDir() {
  195. return nil, etcdErr.NewError(104, parent.Path)
  196. }
  197. child, ok := parent.Children[name]
  198. if ok {
  199. return child, nil
  200. }
  201. return nil, etcdErr.NewError(100, path.Join(parent.Path, name))
  202. }
  203. f, err := fs.walk(nodePath, walkFunc)
  204. if err != nil {
  205. return nil, err
  206. }
  207. return f, nil
  208. }
  209. // checkDir function will check whether the component is a directory under parent node.
  210. // If it is a directory, this function will return the pointer to that node.
  211. // If it does not exist, this function will create a new directory and return the pointer to that node.
  212. // If it is a file, this function will return error.
  213. func (fs *FileSystem) checkDir(parent *Node, dirName string) (*Node, error) {
  214. subDir, ok := parent.Children[dirName]
  215. if ok {
  216. return subDir, nil
  217. }
  218. n := newDir(path.Join(parent.Path, dirName), fs.Index, fs.Term, parent, parent.ACL, Permanent)
  219. parent.Children[dirName] = n
  220. return n, nil
  221. }