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.

191 lines
4.8 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
4 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) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer2.Entry, err error) {
  96. return nil, filer2.ErrUnsupportedListDirectoryPrefixed
  97. }
  98. func (store *UniversalRedisStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool,
  99. limit int) (entries []*filer2.Entry, err error) {
  100. dirListKey := genDirectoryListKey(string(fullpath))
  101. members, err := store.Client.SMembers(dirListKey).Result()
  102. if err != nil {
  103. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  104. }
  105. // skip
  106. if startFileName != "" {
  107. var t []string
  108. for _, m := range members {
  109. if strings.Compare(m, startFileName) >= 0 {
  110. if m == startFileName {
  111. if inclusive {
  112. t = append(t, m)
  113. }
  114. } else {
  115. t = append(t, m)
  116. }
  117. }
  118. }
  119. members = t
  120. }
  121. // sort
  122. sort.Slice(members, func(i, j int) bool {
  123. return strings.Compare(members[i], members[j]) < 0
  124. })
  125. // limit
  126. if limit < len(members) {
  127. members = members[:limit]
  128. }
  129. // fetch entry meta
  130. for _, fileName := range members {
  131. path := util.NewFullPath(string(fullpath), fileName)
  132. entry, err := store.FindEntry(ctx, path)
  133. if err != nil {
  134. glog.V(0).Infof("list %s : %v", path, err)
  135. } else {
  136. if entry.TtlSec > 0 {
  137. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  138. store.Client.Del(string(path)).Result()
  139. store.Client.SRem(dirListKey, fileName).Result()
  140. continue
  141. }
  142. }
  143. entries = append(entries, entry)
  144. }
  145. }
  146. return entries, err
  147. }
  148. func genDirectoryListKey(dir string) (dirList string) {
  149. return dir + DIR_LIST_MARKER
  150. }
  151. func (store *UniversalRedisStore) Shutdown() {
  152. store.Client.Close()
  153. }