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.

177 lines
4.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package redis3
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/go-redis/redis/v8"
  7. "github.com/chrislusf/seaweedfs/weed/filer"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. const (
  13. DIR_LIST_MARKER = "\x00"
  14. )
  15. type UniversalRedis3Store struct {
  16. Client redis.UniversalClient
  17. }
  18. func (store *UniversalRedis3Store) BeginTransaction(ctx context.Context) (context.Context, error) {
  19. return ctx, nil
  20. }
  21. func (store *UniversalRedis3Store) CommitTransaction(ctx context.Context) error {
  22. return nil
  23. }
  24. func (store *UniversalRedis3Store) RollbackTransaction(ctx context.Context) error {
  25. return nil
  26. }
  27. func (store *UniversalRedis3Store) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
  28. value, err := entry.EncodeAttributesAndChunks()
  29. if err != nil {
  30. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  31. }
  32. if len(entry.Chunks) > 50 {
  33. value = util.MaybeGzipData(value)
  34. }
  35. if err = store.Client.Set(ctx, string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Err(); err != nil {
  36. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  37. }
  38. dir, name := entry.FullPath.DirAndName()
  39. if name != "" {
  40. if err = insertChild(ctx, store.Client, genDirectoryListKey(dir), name); err != nil {
  41. return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
  42. }
  43. }
  44. return nil
  45. }
  46. func (store *UniversalRedis3Store) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  47. return store.InsertEntry(ctx, entry)
  48. }
  49. func (store *UniversalRedis3Store) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
  50. data, err := store.Client.Get(ctx, string(fullpath)).Result()
  51. if err == redis.Nil {
  52. return nil, filer_pb.ErrNotFound
  53. }
  54. if err != nil {
  55. return nil, fmt.Errorf("get %s : %v", fullpath, err)
  56. }
  57. entry = &filer.Entry{
  58. FullPath: fullpath,
  59. }
  60. err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData([]byte(data)))
  61. if err != nil {
  62. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  63. }
  64. return entry, nil
  65. }
  66. func (store *UniversalRedis3Store) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
  67. _, err = store.Client.Del(ctx, genDirectoryListKey(string(fullpath))).Result()
  68. if err != nil {
  69. return fmt.Errorf("delete dir list %s : %v", fullpath, err)
  70. }
  71. _, err = store.Client.Del(ctx, string(fullpath)).Result()
  72. if err != nil {
  73. return fmt.Errorf("delete %s : %v", fullpath, err)
  74. }
  75. dir, name := fullpath.DirAndName()
  76. if name != "" {
  77. if err = removeChild(ctx, store.Client, genDirectoryListKey(dir), name); err != nil {
  78. return fmt.Errorf("DeleteEntry %s in parent dir: %v", fullpath, err)
  79. }
  80. }
  81. return nil
  82. }
  83. func (store *UniversalRedis3Store) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
  84. return removeChildren(ctx, store.Client, genDirectoryListKey(string(fullpath)), func(name string) error {
  85. path := util.NewFullPath(string(fullpath), name)
  86. _, err = store.Client.Del(ctx, string(path)).Result()
  87. if err != nil {
  88. return fmt.Errorf("DeleteFolderChildren %s in parent dir: %v", fullpath, err)
  89. }
  90. // not efficient, but need to remove if it is a directory
  91. store.Client.Del(ctx, genDirectoryListKey(string(path)))
  92. return nil
  93. })
  94. }
  95. func (store *UniversalRedis3Store) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  96. return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
  97. }
  98. func (store *UniversalRedis3Store) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  99. dirListKey := genDirectoryListKey(string(dirPath))
  100. counter := int64(0)
  101. err = listChildren(ctx, store.Client, dirListKey, startFileName, func(fileName string) bool {
  102. if startFileName != "" {
  103. if !includeStartFile && startFileName == fileName {
  104. return true
  105. }
  106. }
  107. path := util.NewFullPath(string(dirPath), fileName)
  108. entry, err := store.FindEntry(ctx, path)
  109. lastFileName = fileName
  110. if err != nil {
  111. glog.V(0).Infof("list %s : %v", path, err)
  112. if err == filer_pb.ErrNotFound {
  113. return true
  114. }
  115. } else {
  116. if entry.TtlSec > 0 {
  117. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  118. store.Client.Del(ctx, string(path)).Result()
  119. store.Client.ZRem(ctx, dirListKey, fileName).Result()
  120. return true
  121. }
  122. }
  123. counter++
  124. if !eachEntryFunc(entry) {
  125. return false
  126. }
  127. if counter >= limit {
  128. return false
  129. }
  130. }
  131. return true
  132. })
  133. return lastFileName, err
  134. }
  135. func genDirectoryListKey(dir string) (dirList string) {
  136. return dir + DIR_LIST_MARKER
  137. }
  138. func (store *UniversalRedis3Store) Shutdown() {
  139. store.Client.Close()
  140. }