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.

170 lines
4.6 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(string(fullpath)).Result()
  68. if err != nil {
  69. return fmt.Errorf("delete %s : %v", fullpath, err)
  70. }
  71. dir, name := fullpath.DirAndName()
  72. if name != "" {
  73. _, err = store.Client.ZRem(genDirectoryListKey(dir), name).Result()
  74. if err != nil {
  75. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  76. }
  77. }
  78. return nil
  79. }
  80. func (store *UniversalRedis2Store) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
  81. members, err := store.Client.ZRange(genDirectoryListKey(string(fullpath)), 0, -1).Result()
  82. if err != nil {
  83. return fmt.Errorf("delete folder %s : %v", fullpath, err)
  84. }
  85. for _, fileName := range members {
  86. path := util.NewFullPath(string(fullpath), fileName)
  87. _, err = store.Client.Del(string(path)).Result()
  88. if err != nil {
  89. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  90. }
  91. }
  92. return nil
  93. }
  94. func (store *UniversalRedis2Store) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer.Entry, err error) {
  95. return nil, filer.ErrUnsupportedListDirectoryPrefixed
  96. }
  97. func (store *UniversalRedis2Store) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool,
  98. limit int) (entries []*filer.Entry, err error) {
  99. dirListKey := genDirectoryListKey(string(fullpath))
  100. start := int64(0)
  101. if startFileName != "" {
  102. start, _ = store.Client.ZRank(dirListKey, startFileName).Result()
  103. if !inclusive {
  104. start++
  105. }
  106. }
  107. members, err := store.Client.ZRange(dirListKey, start, start+int64(limit)-1).Result()
  108. if err != nil {
  109. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  110. }
  111. // fetch entry meta
  112. for _, fileName := range members {
  113. path := util.NewFullPath(string(fullpath), fileName)
  114. entry, err := store.FindEntry(ctx, path)
  115. if err != nil {
  116. glog.V(0).Infof("list %s : %v", path, err)
  117. } else {
  118. if entry.TtlSec > 0 {
  119. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  120. store.Client.Del(string(path)).Result()
  121. store.Client.ZRem(dirListKey, fileName).Result()
  122. continue
  123. }
  124. }
  125. entries = append(entries, entry)
  126. }
  127. }
  128. return entries, err
  129. }
  130. func genDirectoryListKey(dir string) (dirList string) {
  131. return dir + DIR_LIST_MARKER
  132. }
  133. func (store *UniversalRedis2Store) Shutdown() {
  134. store.Client.Close()
  135. }