file_system.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package fileSystem
  2. import (
  3. "fmt"
  4. "path"
  5. "strings"
  6. "time"
  7. etcdErr "github.com/coreos/etcd/error"
  8. )
  9. type FileSystem struct {
  10. Root *Node
  11. EventHistory *EventHistory
  12. WatcherHub *watcherHub
  13. Index uint64
  14. Term uint64
  15. }
  16. func New() *FileSystem {
  17. return &FileSystem{
  18. Root: newDir("/", 0, 0, nil, ""),
  19. WatcherHub: newWatchHub(1000),
  20. }
  21. }
  22. func (fs *FileSystem) Get(keyPath string, recusive bool, index uint64, term uint64) (*Event, error) {
  23. // TODO: add recursive get
  24. n, err := fs.InternalGet(keyPath, index, term)
  25. if err != nil {
  26. return nil, err
  27. }
  28. e := newEvent(Get, keyPath, index, term)
  29. if n.IsDir() { // node is dir
  30. e.KVPairs = make([]KeyValuePair, len(n.Children))
  31. i := 0
  32. for _, child := range n.Children {
  33. if child.IsHidden() { // get will not list hidden node
  34. continue
  35. }
  36. e.KVPairs[i] = child.Pair()
  37. i++
  38. }
  39. // eliminate hidden nodes
  40. e.KVPairs = e.KVPairs[:i]
  41. } else { // node is file
  42. e.Value = n.Value
  43. }
  44. return e, nil
  45. }
  46. func (fs *FileSystem) Set(keyPath string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
  47. keyPath = path.Clean("/" + keyPath)
  48. // update file system known index and term
  49. fs.Index, fs.Term = index, term
  50. dir, name := path.Split(keyPath)
  51. // walk through the keyPath and get the last directory node
  52. d, err := fs.walk(dir, fs.checkDir)
  53. if err != nil {
  54. return nil, err
  55. }
  56. e := newEvent(Set, keyPath, fs.Index, fs.Term)
  57. e.Value = value
  58. f, err := d.GetFile(name)
  59. if err == nil {
  60. if f != nil { // update previous file if exist
  61. e.PrevValue = f.Value
  62. f.Write(e.Value, index, term)
  63. // if the previous ExpireTime is not Permanent and expireTime is given
  64. // we stop the previous expire routine
  65. if f.ExpireTime != Permanent && expireTime != Permanent {
  66. f.stopExpire <- true
  67. }
  68. } else { // create new file
  69. f = newFile(keyPath, value, fs.Index, fs.Term, d, "", expireTime)
  70. err = d.Add(f)
  71. }
  72. }
  73. if err != nil {
  74. return nil, err
  75. }
  76. // Node with TTL
  77. if expireTime != Permanent {
  78. go f.Expire()
  79. e.Expiration = &f.ExpireTime
  80. e.TTL = int64(expireTime.Sub(time.Now()) / time.Second)
  81. }
  82. return e, nil
  83. }
  84. func (fs *FileSystem) TestAndSet(keyPath string, prevValue string, prevIndex uint64, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
  85. f, err := fs.InternalGet(keyPath, index, term)
  86. if err != nil {
  87. etcdError, _ := err.(etcdErr.Error)
  88. if etcdError.ErrorCode == 100 { // file does not exist
  89. if prevValue == "" && prevIndex == 0 { // test against if prevValue is empty
  90. fs.Set(keyPath, value, expireTime, index, term)
  91. e := newEvent(TestAndSet, keyPath, index, term)
  92. e.Value = value
  93. return e, nil
  94. }
  95. return nil, err
  96. }
  97. return nil, err
  98. }
  99. if f.IsDir() { // can only test and set file
  100. return nil, etcdErr.NewError(102, keyPath)
  101. }
  102. if f.Value == prevValue || f.ModifiedIndex == prevIndex {
  103. // if test succeed, write the value
  104. e := newEvent(TestAndSet, keyPath, index, term)
  105. e.PrevValue = f.Value
  106. e.Value = value
  107. f.Write(value, index, term)
  108. return e, nil
  109. }
  110. cause := fmt.Sprintf("[%v/%v] [%v/%v]", prevValue, f.Value, prevIndex, f.ModifiedIndex)
  111. return nil, etcdErr.NewError(101, cause)
  112. }
  113. func (fs *FileSystem) Delete(keyPath string, recurisive bool, index uint64, term uint64) (*Event, error) {
  114. n, err := fs.InternalGet(keyPath, index, term)
  115. if err != nil {
  116. return nil, err
  117. }
  118. err = n.Remove(recurisive)
  119. if err != nil {
  120. return nil, err
  121. }
  122. e := newEvent(Delete, keyPath, index, term)
  123. if n.IsDir() {
  124. e.Dir = true
  125. } else {
  126. e.PrevValue = n.Value
  127. }
  128. return e, nil
  129. }
  130. // walk function walks all the keyPath and apply the walkFunc on each directory
  131. func (fs *FileSystem) walk(keyPath string, walkFunc func(prev *Node, component string) (*Node, error)) (*Node, error) {
  132. components := strings.Split(keyPath, "/")
  133. curr := fs.Root
  134. var err error
  135. for i := 1; i < len(components); i++ {
  136. if len(components[i]) == 0 { // ignore empty string
  137. return curr, nil
  138. }
  139. curr, err = walkFunc(curr, components[i])
  140. if err != nil {
  141. return nil, err
  142. }
  143. }
  144. return curr, nil
  145. }
  146. // InternalGet function get the node of the given keyPath.
  147. func (fs *FileSystem) InternalGet(keyPath string, index uint64, term uint64) (*Node, error) {
  148. keyPath = path.Clean("/" + keyPath)
  149. // update file system known index and term
  150. fs.Index, fs.Term = index, term
  151. walkFunc := func(parent *Node, name string) (*Node, error) {
  152. if !parent.IsDir() {
  153. return nil, etcdErr.NewError(104, parent.Path)
  154. }
  155. child, ok := parent.Children[name]
  156. if ok {
  157. return child, nil
  158. }
  159. return nil, etcdErr.NewError(100, path.Join(parent.Path, name))
  160. }
  161. f, err := fs.walk(keyPath, walkFunc)
  162. if err != nil {
  163. return nil, err
  164. }
  165. return f, nil
  166. }
  167. // checkDir function will check whether the component is a directory under parent node.
  168. // If it is a directory, this function will return the pointer to that node.
  169. // If it does not exist, this function will create a new directory and return the pointer to that node.
  170. // If it is a file, this function will return error.
  171. func (fs *FileSystem) checkDir(parent *Node, dirName string) (*Node, error) {
  172. subDir, ok := parent.Children[dirName]
  173. if ok {
  174. return subDir, nil
  175. }
  176. n := newDir(path.Join(parent.Path, dirName), fs.Index, fs.Term, parent, parent.ACL)
  177. parent.Children[dirName] = n
  178. return n, nil
  179. }