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.

187 lines
4.5 KiB

5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 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"
  9. "github.com/chrislusf/seaweedfs/weed/filer2"
  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 *filer2.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. _, err = store.Client.Set(string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Result()
  35. if err != nil {
  36. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  37. }
  38. dir, name := entry.FullPath.DirAndName()
  39. if name != "" {
  40. _, err = store.Client.SAdd(genDirectoryListKey(dir), name).Result()
  41. if err != nil {
  42. return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
  43. }
  44. }
  45. return nil
  46. }
  47. func (store *UniversalRedisStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  48. return store.InsertEntry(ctx, entry)
  49. }
  50. func (store *UniversalRedisStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer2.Entry, err error) {
  51. data, err := store.Client.Get(string(fullpath)).Result()
  52. if err == redis.Nil {
  53. return nil, filer_pb.ErrNotFound
  54. }
  55. if err != nil {
  56. return nil, fmt.Errorf("get %s : %v", fullpath, err)
  57. }
  58. entry = &filer2.Entry{
  59. FullPath: fullpath,
  60. }
  61. err = entry.DecodeAttributesAndChunks([]byte(data))
  62. if err != nil {
  63. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  64. }
  65. return entry, nil
  66. }
  67. func (store *UniversalRedisStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
  68. _, err = store.Client.Del(string(fullpath)).Result()
  69. if err != nil {
  70. return fmt.Errorf("delete %s : %v", fullpath, err)
  71. }
  72. dir, name := fullpath.DirAndName()
  73. if name != "" {
  74. _, err = store.Client.SRem(genDirectoryListKey(dir), name).Result()
  75. if err != nil {
  76. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  77. }
  78. }
  79. return nil
  80. }
  81. func (store *UniversalRedisStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
  82. members, err := store.Client.SMembers(genDirectoryListKey(string(fullpath))).Result()
  83. if err != nil {
  84. return fmt.Errorf("delete folder %s : %v", fullpath, err)
  85. }
  86. for _, fileName := range members {
  87. path := util.NewFullPath(string(fullpath), fileName)
  88. _, err = store.Client.Del(string(path)).Result()
  89. if err != nil {
  90. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  91. }
  92. }
  93. return nil
  94. }
  95. func (store *UniversalRedisStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool,
  96. limit int) (entries []*filer2.Entry, err error) {
  97. dirListKey := genDirectoryListKey(string(fullpath))
  98. members, err := store.Client.SMembers(dirListKey).Result()
  99. if err != nil {
  100. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  101. }
  102. // skip
  103. if startFileName != "" {
  104. var t []string
  105. for _, m := range members {
  106. if strings.Compare(m, startFileName) >= 0 {
  107. if m == startFileName {
  108. if inclusive {
  109. t = append(t, m)
  110. }
  111. } else {
  112. t = append(t, m)
  113. }
  114. }
  115. }
  116. members = t
  117. }
  118. // sort
  119. sort.Slice(members, func(i, j int) bool {
  120. return strings.Compare(members[i], members[j]) < 0
  121. })
  122. // limit
  123. if limit < len(members) {
  124. members = members[:limit]
  125. }
  126. // fetch entry meta
  127. for _, fileName := range members {
  128. path := util.NewFullPath(string(fullpath), fileName)
  129. entry, err := store.FindEntry(ctx, path)
  130. if err != nil {
  131. glog.V(0).Infof("list %s : %v", path, err)
  132. } else {
  133. if entry.TtlSec > 0 {
  134. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  135. store.Client.Del(string(path)).Result()
  136. store.Client.SRem(dirListKey, fileName).Result()
  137. continue
  138. }
  139. }
  140. entries = append(entries, entry)
  141. }
  142. }
  143. return entries, err
  144. }
  145. func genDirectoryListKey(dir string) (dirList string) {
  146. return dir + DIR_LIST_MARKER
  147. }
  148. func (store *UniversalRedisStore) Shutdown() {
  149. store.Client.Close()
  150. }