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.

174 lines
4.8 KiB

  1. package redis2
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/go-redis/redis"
  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 UniversalRedis2Store struct {
  16. Client redis.UniversalClient
  17. }
  18. func (store *UniversalRedis2Store) BeginTransaction(ctx context.Context) (context.Context, error) {
  19. return ctx, nil
  20. }
  21. func (store *UniversalRedis2Store) CommitTransaction(ctx context.Context) error {
  22. return nil
  23. }
  24. func (store *UniversalRedis2Store) RollbackTransaction(ctx context.Context) error {
  25. return nil
  26. }
  27. func (store *UniversalRedis2Store) 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(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 = store.Client.ZAddNX(genDirectoryListKey(dir), redis.Z{Score: 0, Member: name}).Err(); err != nil {
  41. return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
  42. }
  43. }
  44. return nil
  45. }
  46. func (store *UniversalRedis2Store) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  47. return store.InsertEntry(ctx, entry)
  48. }
  49. func (store *UniversalRedis2Store) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
  50. data, err := store.Client.Get(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 *UniversalRedis2Store) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
  67. _, err = store.Client.Del(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(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.ZRem(genDirectoryListKey(dir), name).Result()
  78. if err != nil {
  79. return fmt.Errorf("DeleteEntry %s in parent dir: %v", fullpath, err)
  80. }
  81. }
  82. return nil
  83. }
  84. func (store *UniversalRedis2Store) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
  85. members, err := store.Client.ZRange(genDirectoryListKey(string(fullpath)), 0, -1).Result()
  86. if err != nil {
  87. return fmt.Errorf("DeleteFolderChildren %s : %v", fullpath, err)
  88. }
  89. for _, fileName := range members {
  90. path := util.NewFullPath(string(fullpath), fileName)
  91. _, err = store.Client.Del(string(path)).Result()
  92. if err != nil {
  93. return fmt.Errorf("DeleteFolderChildren %s in parent dir: %v", fullpath, err)
  94. }
  95. }
  96. return nil
  97. }
  98. func (store *UniversalRedis2Store) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer.Entry, err error) {
  99. return nil, filer.ErrUnsupportedListDirectoryPrefixed
  100. }
  101. func (store *UniversalRedis2Store) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool,
  102. limit int) (entries []*filer.Entry, err error) {
  103. dirListKey := genDirectoryListKey(string(fullpath))
  104. start := int64(0)
  105. if startFileName != "" {
  106. start, _ = store.Client.ZRank(dirListKey, startFileName).Result()
  107. if !inclusive {
  108. start++
  109. }
  110. }
  111. members, err := store.Client.ZRange(dirListKey, start, start+int64(limit)-1).Result()
  112. if err != nil {
  113. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  114. }
  115. // fetch entry meta
  116. for _, fileName := range members {
  117. path := util.NewFullPath(string(fullpath), fileName)
  118. entry, err := store.FindEntry(ctx, path)
  119. if err != nil {
  120. glog.V(0).Infof("list %s : %v", path, err)
  121. } else {
  122. if entry.TtlSec > 0 {
  123. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  124. store.Client.Del(string(path)).Result()
  125. store.Client.ZRem(dirListKey, fileName).Result()
  126. continue
  127. }
  128. }
  129. entries = append(entries, entry)
  130. }
  131. }
  132. return entries, err
  133. }
  134. func genDirectoryListKey(dir string) (dirList string) {
  135. return dir + DIR_LIST_MARKER
  136. }
  137. func (store *UniversalRedis2Store) Shutdown() {
  138. store.Client.Close()
  139. }