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.

162 lines
4.2 KiB

  1. package redis2
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/go-redis/redis"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  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 *filer2.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 err = store.Client.Set(string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Err(); err != nil {
  33. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  34. }
  35. dir, name := entry.FullPath.DirAndName()
  36. if name != "" {
  37. if err = store.Client.ZAddNX(genDirectoryListKey(dir), redis.Z{Score: 0, Member: name}).Err(); err != nil {
  38. return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
  39. }
  40. }
  41. return nil
  42. }
  43. func (store *UniversalRedis2Store) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  44. return store.InsertEntry(ctx, entry)
  45. }
  46. func (store *UniversalRedis2Store) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer2.Entry, err error) {
  47. data, err := store.Client.Get(string(fullpath)).Result()
  48. if err == redis.Nil {
  49. return nil, filer_pb.ErrNotFound
  50. }
  51. if err != nil {
  52. return nil, fmt.Errorf("get %s : %v", fullpath, err)
  53. }
  54. entry = &filer2.Entry{
  55. FullPath: fullpath,
  56. }
  57. err = entry.DecodeAttributesAndChunks([]byte(data))
  58. if err != nil {
  59. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  60. }
  61. return entry, nil
  62. }
  63. func (store *UniversalRedis2Store) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
  64. _, err = store.Client.Del(string(fullpath)).Result()
  65. if err != nil {
  66. return fmt.Errorf("delete %s : %v", fullpath, err)
  67. }
  68. dir, name := fullpath.DirAndName()
  69. if name != "" {
  70. _, err = store.Client.ZRem(genDirectoryListKey(dir), name).Result()
  71. if err != nil {
  72. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  73. }
  74. }
  75. return nil
  76. }
  77. func (store *UniversalRedis2Store) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
  78. members, err := store.Client.ZRange(genDirectoryListKey(string(fullpath)), 0, -1).Result()
  79. if err != nil {
  80. return fmt.Errorf("delete folder %s : %v", fullpath, err)
  81. }
  82. for _, fileName := range members {
  83. path := util.NewFullPath(string(fullpath), fileName)
  84. _, err = store.Client.Del(string(path)).Result()
  85. if err != nil {
  86. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  87. }
  88. }
  89. return nil
  90. }
  91. func (store *UniversalRedis2Store) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool,
  92. limit int) (entries []*filer2.Entry, err error) {
  93. dirListKey := genDirectoryListKey(string(fullpath))
  94. start := int64(0)
  95. if startFileName != "" {
  96. start, _ = store.Client.ZRank(dirListKey, startFileName).Result()
  97. if !inclusive {
  98. start++
  99. }
  100. }
  101. members, err := store.Client.ZRange(dirListKey, start, start+int64(limit)-1).Result()
  102. if err != nil {
  103. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  104. }
  105. // fetch entry meta
  106. for _, fileName := range members {
  107. path := util.NewFullPath(string(fullpath), fileName)
  108. entry, err := store.FindEntry(ctx, path)
  109. if err != nil {
  110. glog.V(0).Infof("list %s : %v", path, err)
  111. } else {
  112. if entry.TtlSec > 0 {
  113. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  114. store.Client.Del(string(path)).Result()
  115. store.Client.ZRem(dirListKey, fileName).Result()
  116. continue
  117. }
  118. }
  119. entries = append(entries, entry)
  120. }
  121. }
  122. return entries, err
  123. }
  124. func genDirectoryListKey(dir string) (dirList string) {
  125. return dir + DIR_LIST_MARKER
  126. }
  127. func (store *UniversalRedis2Store) Shutdown() {
  128. store.Client.Close()
  129. }