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.

202 lines
5.3 KiB

6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
  1. package redis
  2. import (
  3. "context"
  4. "fmt"
  5. "golang.org/x/exp/slices"
  6. "strings"
  7. "time"
  8. "github.com/go-redis/redis/v8"
  9. "github.com/seaweedfs/seaweedfs/weed/filer"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  12. "github.com/seaweedfs/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 *filer.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. if len(entry.Chunks) > filer.CountEntryChunksForGzip {
  35. value = util.MaybeGzipData(value)
  36. }
  37. _, err = store.Client.Set(ctx, string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Result()
  38. if err != nil {
  39. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  40. }
  41. dir, name := entry.FullPath.DirAndName()
  42. if name != "" {
  43. _, err = store.Client.SAdd(ctx, genDirectoryListKey(dir), name).Result()
  44. if err != nil {
  45. return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
  46. }
  47. }
  48. return nil
  49. }
  50. func (store *UniversalRedisStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  51. return store.InsertEntry(ctx, entry)
  52. }
  53. func (store *UniversalRedisStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
  54. data, err := store.Client.Get(ctx, string(fullpath)).Result()
  55. if err == redis.Nil {
  56. return nil, filer_pb.ErrNotFound
  57. }
  58. if err != nil {
  59. return nil, fmt.Errorf("get %s : %v", fullpath, err)
  60. }
  61. entry = &filer.Entry{
  62. FullPath: fullpath,
  63. }
  64. err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData([]byte(data)))
  65. if err != nil {
  66. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  67. }
  68. return entry, nil
  69. }
  70. func (store *UniversalRedisStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
  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. _, err = store.Client.SRem(ctx, genDirectoryListKey(dir), name).Result()
  78. if err != nil {
  79. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  80. }
  81. }
  82. return nil
  83. }
  84. func (store *UniversalRedisStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
  85. members, err := store.Client.SMembers(ctx, genDirectoryListKey(string(fullpath))).Result()
  86. if err != nil {
  87. return fmt.Errorf("delete folder %s : %v", fullpath, err)
  88. }
  89. for _, fileName := range members {
  90. path := util.NewFullPath(string(fullpath), fileName)
  91. _, err = store.Client.Del(ctx, string(path)).Result()
  92. if err != nil {
  93. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  94. }
  95. // not efficient, but need to remove if it is a directory
  96. store.Client.Del(ctx, genDirectoryListKey(string(path)))
  97. }
  98. return nil
  99. }
  100. func (store *UniversalRedisStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  101. return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
  102. }
  103. func (store *UniversalRedisStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  104. dirListKey := genDirectoryListKey(string(dirPath))
  105. members, err := store.Client.SMembers(ctx, dirListKey).Result()
  106. if err != nil {
  107. return lastFileName, fmt.Errorf("list %s : %v", dirPath, err)
  108. }
  109. // skip
  110. if startFileName != "" {
  111. var t []string
  112. for _, m := range members {
  113. if strings.Compare(m, startFileName) >= 0 {
  114. if m == startFileName {
  115. if includeStartFile {
  116. t = append(t, m)
  117. }
  118. } else {
  119. t = append(t, m)
  120. }
  121. }
  122. }
  123. members = t
  124. }
  125. // sort
  126. slices.SortFunc(members, func(a, b string) bool {
  127. return strings.Compare(a, b) < 0
  128. })
  129. // limit
  130. if limit < int64(len(members)) {
  131. members = members[:limit]
  132. }
  133. // fetch entry meta
  134. for _, fileName := range members {
  135. path := util.NewFullPath(string(dirPath), fileName)
  136. entry, err := store.FindEntry(ctx, path)
  137. lastFileName = fileName
  138. if err != nil {
  139. glog.V(0).Infof("list %s : %v", path, err)
  140. if err == filer_pb.ErrNotFound {
  141. continue
  142. }
  143. } else {
  144. if entry.TtlSec > 0 {
  145. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  146. store.Client.Del(ctx, string(path)).Result()
  147. store.Client.SRem(ctx, dirListKey, fileName).Result()
  148. continue
  149. }
  150. }
  151. if !eachEntryFunc(entry) {
  152. break
  153. }
  154. }
  155. }
  156. return lastFileName, err
  157. }
  158. func genDirectoryListKey(dir string) (dirList string) {
  159. return dir + DIR_LIST_MARKER
  160. }
  161. func (store *UniversalRedisStore) Shutdown() {
  162. store.Client.Close()
  163. }