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.

213 lines
5.8 KiB

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