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.

200 lines
5.1 KiB

5 years ago
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. "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. }