You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
5.3 KiB

5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package redis
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "time"
  8. "github.com/go-redis/redis"
  9. "github.com/chrislusf/seaweedfs/weed/filer2"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. const (
  15. DIR_LIST_MARKER = "\x00"
  16. )
  17. type UniversalRedisStore struct {
  18. Client redis.UniversalClient
  19. }
  20. func (store *UniversalRedisStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  21. return ctx, nil
  22. }
  23. func (store *UniversalRedisStore) CommitTransaction(ctx context.Context) error {
  24. return nil
  25. }
  26. func (store *UniversalRedisStore) RollbackTransaction(ctx context.Context) error {
  27. return nil
  28. }
  29. func (store *UniversalRedisStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  30. value, err := entry.EncodeAttributesAndChunks()
  31. if err != nil {
  32. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  33. }
  34. _, err = store.Client.Set(string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Result()
  35. if err != nil {
  36. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  37. }
  38. dir, name := entry.FullPath.DirAndName()
  39. if name != "" {
  40. _, err = store.Client.SAdd(genDirectoryListKey(dir), name).Result()
  41. if err != nil {
  42. return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
  43. }
  44. }
  45. return nil
  46. }
  47. func (store *UniversalRedisStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  48. return store.InsertEntry(ctx, entry)
  49. }
  50. func (store *UniversalRedisStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer2.Entry, err error) {
  51. data, err := store.Client.Get(string(fullpath)).Result()
  52. if err == redis.Nil {
  53. return nil, filer_pb.ErrNotFound
  54. }
  55. if err != nil {
  56. return nil, fmt.Errorf("get %s : %v", fullpath, err)
  57. }
  58. entry = &filer2.Entry{
  59. FullPath: fullpath,
  60. }
  61. err = entry.DecodeAttributesAndChunks([]byte(data))
  62. if err != nil {
  63. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  64. }
  65. return entry, nil
  66. }
  67. func (store *UniversalRedisStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
  68. _, err = store.Client.Del(string(fullpath)).Result()
  69. if err != nil {
  70. return fmt.Errorf("delete %s : %v", fullpath, err)
  71. }
  72. dir, name := fullpath.DirAndName()
  73. if name != "" {
  74. _, err = store.Client.SRem(genDirectoryListKey(dir), name).Result()
  75. if err != nil {
  76. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  77. }
  78. }
  79. return nil
  80. }
  81. func (store *UniversalRedisStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
  82. members, err := store.Client.SMembers(genDirectoryListKey(string(fullpath))).Result()
  83. if err != nil {
  84. return fmt.Errorf("delete folder %s : %v", fullpath, err)
  85. }
  86. for _, fileName := range members {
  87. path := util.NewFullPath(string(fullpath), fileName)
  88. _, err = store.Client.Del(string(path)).Result()
  89. if err != nil {
  90. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  91. }
  92. }
  93. return nil
  94. }
  95. func (store *UniversalRedisStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer2.Entry, err error) {
  96. count := 0
  97. notPrefixed, err := store.ListDirectoryEntries(ctx, fullpath, startFileName, inclusive, limit)
  98. if err != nil {
  99. return nil, err
  100. }
  101. if prefix == "" {
  102. return notPrefixed, nil
  103. }
  104. var lastFileName string
  105. for count < limit {
  106. for _, entry := range notPrefixed {
  107. lastFileName = entry.Name()
  108. if strings.HasPrefix(entry.Name(), prefix) {
  109. count++
  110. entries = append(entries, entry)
  111. }
  112. }
  113. if count >= limit {
  114. break
  115. }
  116. notPrefixed, err = store.ListDirectoryEntries(ctx, fullpath, lastFileName, inclusive, limit)
  117. if err != nil {
  118. return nil, err
  119. }
  120. if len(notPrefixed) == 0 {
  121. break
  122. }
  123. }
  124. return entries, nil
  125. }
  126. func (store *UniversalRedisStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool,
  127. limit int) (entries []*filer2.Entry, err error) {
  128. dirListKey := genDirectoryListKey(string(fullpath))
  129. members, err := store.Client.SMembers(dirListKey).Result()
  130. if err != nil {
  131. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  132. }
  133. // skip
  134. if startFileName != "" {
  135. var t []string
  136. for _, m := range members {
  137. if strings.Compare(m, startFileName) >= 0 {
  138. if m == startFileName {
  139. if inclusive {
  140. t = append(t, m)
  141. }
  142. } else {
  143. t = append(t, m)
  144. }
  145. }
  146. }
  147. members = t
  148. }
  149. // sort
  150. sort.Slice(members, func(i, j int) bool {
  151. return strings.Compare(members[i], members[j]) < 0
  152. })
  153. // limit
  154. if limit < len(members) {
  155. members = members[:limit]
  156. }
  157. // fetch entry meta
  158. for _, fileName := range members {
  159. path := util.NewFullPath(string(fullpath), fileName)
  160. entry, err := store.FindEntry(ctx, path)
  161. if err != nil {
  162. glog.V(0).Infof("list %s : %v", path, err)
  163. } else {
  164. if entry.TtlSec > 0 {
  165. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  166. store.Client.Del(string(path)).Result()
  167. store.Client.SRem(dirListKey, fileName).Result()
  168. continue
  169. }
  170. }
  171. entries = append(entries, entry)
  172. }
  173. }
  174. return entries, err
  175. }
  176. func genDirectoryListKey(dir string) (dirList string) {
  177. return dir + DIR_LIST_MARKER
  178. }
  179. func (store *UniversalRedisStore) Shutdown() {
  180. store.Client.Close()
  181. }