file_system.go 4.3 KB

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