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.

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