file_system.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package fileSystem
  2. import (
  3. "fmt"
  4. "path/filepath"
  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(path string, recusive bool, index uint64, term uint64) (*Event, error) {
  23. // TODO: add recursive get
  24. n, err := fs.InternalGet(path, index, term)
  25. if err != nil {
  26. return nil, err
  27. }
  28. e := newEvent(Get, path, index, term)
  29. if n.IsDir() { // node is dir
  30. e.KVPairs = make([]KeyValuePair, len(n.Children))
  31. i := 0
  32. for _, subN := range n.Children {
  33. if subN.IsHidden() { // get will not list hidden node
  34. continue
  35. }
  36. if subN.IsDir() {
  37. e.KVPairs[i] = KeyValuePair{
  38. Key: subN.Path,
  39. Dir: true,
  40. }
  41. } else {
  42. e.KVPairs[i] = KeyValuePair{
  43. Key: subN.Path,
  44. Value: subN.Value,
  45. }
  46. }
  47. i++
  48. }
  49. // eliminate hidden nodes
  50. e.KVPairs = e.KVPairs[:i]
  51. } else { // node is file
  52. e.Value = n.Value
  53. }
  54. return e, nil
  55. }
  56. func (fs *FileSystem) Set(path string, value string, expireTime time.Time, index uint64, term uint64) (*Event, error) {
  57. path = filepath.Clean("/" + path)
  58. // update file system known index and term
  59. fs.Index, fs.Term = index, term
  60. dir, name := filepath.Split(path)
  61. // walk through the path and get the last directory node
  62. d, err := fs.walk(dir, fs.checkDir)
  63. if err != nil {
  64. return nil, err
  65. }
  66. f := newFile(path, value, fs.Index, fs.Term, d, "", expireTime)
  67. e := newEvent(Set, path, fs.Index, fs.Term)
  68. e.Value = f.Value
  69. // remove previous file if exist
  70. oldFile, err := d.GetFile(name)
  71. if err == nil {
  72. if oldFile != nil {
  73. oldFile.Remove(false)
  74. e.PrevValue = oldFile.Value
  75. }
  76. } else {
  77. return nil, err
  78. }
  79. err = d.Add(f)
  80. if err != nil {
  81. return nil, err
  82. }
  83. // Node with TTL
  84. if expireTime != Permanent {
  85. go f.Expire()
  86. e.Expiration = &f.ExpireTime
  87. e.TTL = int64(expireTime.Sub(time.Now()) / time.Second)
  88. }
  89. return e, nil
  90. }
  91. func (fs *FileSystem) TestAndSet(path string, recurisive bool, index uint64, term uint64) {
  92. }
  93. func (fs *FileSystem) TestIndexAndSet() {
  94. }
  95. func (fs *FileSystem) Delete(path string, recurisive bool, index uint64, term uint64) (*Event, error) {
  96. n, err := fs.InternalGet(path, index, term)
  97. if err != nil {
  98. return nil, err
  99. }
  100. err = n.Remove(recurisive)
  101. if err != nil {
  102. return nil, err
  103. }
  104. e := newEvent(Delete, path, index, term)
  105. if n.IsDir() {
  106. e.Dir = true
  107. } else {
  108. e.PrevValue = n.Value
  109. }
  110. return e, nil
  111. }
  112. // walk function walks all the path and apply the walkFunc on each directory
  113. func (fs *FileSystem) walk(path string, walkFunc func(prev *Node, component string) (*Node, error)) (*Node, error) {
  114. components := strings.Split(path, "/")
  115. curr := fs.Root
  116. var err error
  117. for i := 1; i < len(components); i++ {
  118. if len(components[i]) == 0 { // ignore empty string
  119. return curr, nil
  120. }
  121. curr, err = walkFunc(curr, components[i])
  122. if err != nil {
  123. return nil, err
  124. }
  125. }
  126. return curr, nil
  127. }
  128. // InternalGet function get the node of the given path.
  129. func (fs *FileSystem) InternalGet(path string, index uint64, term uint64) (*Node, error) {
  130. fmt.Println("GET: ", path)
  131. path = filepath.Clean("/" + path)
  132. // update file system known index and term
  133. fs.Index, fs.Term = index, term
  134. walkFunc := func(parent *Node, dirName string) (*Node, error) {
  135. child, ok := parent.Children[dirName]
  136. if ok {
  137. return child, nil
  138. }
  139. return nil, etcdErr.NewError(100, "get")
  140. }
  141. f, err := fs.walk(path, walkFunc)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return f, nil
  146. }
  147. // checkDir function will check whether the component is a directory under parent node.
  148. // If it is a directory, this function will return the pointer to that node.
  149. // If it does not exist, this function will create a new directory and return the pointer to that node.
  150. // If it is a file, this function will return error.
  151. func (fs *FileSystem) checkDir(parent *Node, dirName string) (*Node, error) {
  152. subDir, ok := parent.Children[dirName]
  153. if ok {
  154. return subDir, nil
  155. }
  156. n := newDir(filepath.Join(parent.Path, dirName), fs.Index, fs.Term, parent, parent.ACL)
  157. parent.Children[dirName] = n
  158. return n, nil
  159. }