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.

181 lines
4.3 KiB

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