universal_redis_store.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package redis
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "time"
  8. "github.com/go-redis/redis/v8"
  9. "github.com/chrislusf/seaweedfs/weed/filer"
  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 *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) > 50 {
  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, limit int64) (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. }
  96. return nil
  97. }
  98. 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) {
  99. return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
  100. }
  101. func (store *UniversalRedisStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  102. dirListKey := genDirectoryListKey(string(dirPath))
  103. members, err := store.Client.SMembers(ctx, dirListKey).Result()
  104. if err != nil {
  105. return lastFileName, fmt.Errorf("list %s : %v", dirPath, err)
  106. }
  107. // skip
  108. if startFileName != "" {
  109. var t []string
  110. for _, m := range members {
  111. if strings.Compare(m, startFileName) >= 0 {
  112. if m == startFileName {
  113. if includeStartFile {
  114. t = append(t, m)
  115. }
  116. } else {
  117. t = append(t, m)
  118. }
  119. }
  120. }
  121. members = t
  122. }
  123. // sort
  124. sort.Slice(members, func(i, j int) bool {
  125. return strings.Compare(members[i], members[j]) < 0
  126. })
  127. // limit
  128. if limit < int64(len(members)) {
  129. members = members[:limit]
  130. }
  131. // fetch entry meta
  132. for _, fileName := range members {
  133. path := util.NewFullPath(string(dirPath), fileName)
  134. entry, err := store.FindEntry(ctx, path)
  135. lastFileName = fileName
  136. if err != nil {
  137. glog.V(0).Infof("list %s : %v", path, err)
  138. if err == filer_pb.ErrNotFound {
  139. continue
  140. }
  141. } else {
  142. if entry.TtlSec > 0 {
  143. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  144. store.Client.Del(ctx, string(path)).Result()
  145. store.Client.SRem(ctx, dirListKey, fileName).Result()
  146. continue
  147. }
  148. }
  149. if !eachEntryFunc(entry) {
  150. break
  151. }
  152. }
  153. }
  154. return lastFileName, err
  155. }
  156. func genDirectoryListKey(dir string) (dirList string) {
  157. return dir + DIR_LIST_MARKER
  158. }
  159. func (store *UniversalRedisStore) Shutdown() {
  160. store.Client.Close()
  161. }