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.

153 lines
3.6 KiB

6 years ago
6 years ago
6 years ago
  1. package redis
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/go-redis/redis"
  8. "sort"
  9. "strings"
  10. "time"
  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) ListDirectoryEntries(ctx context.Context, fullpath filer2.FullPath, startFileName string, inclusive bool,
  80. limit int) (entries []*filer2.Entry, err error) {
  81. members, err := store.Client.SMembers(genDirectoryListKey(string(fullpath))).Result()
  82. if err != nil {
  83. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  84. }
  85. // skip
  86. if startFileName != "" {
  87. var t []string
  88. for _, m := range members {
  89. if strings.Compare(m, startFileName) >= 0 {
  90. if m == startFileName {
  91. if inclusive {
  92. t = append(t, m)
  93. }
  94. } else {
  95. t = append(t, m)
  96. }
  97. }
  98. }
  99. members = t
  100. }
  101. // sort
  102. sort.Slice(members, func(i, j int) bool {
  103. return strings.Compare(members[i], members[j]) < 0
  104. })
  105. // limit
  106. if limit < len(members) {
  107. members = members[:limit]
  108. }
  109. // fetch entry meta
  110. for _, fileName := range members {
  111. path := filer2.NewFullPath(string(fullpath), fileName)
  112. entry, err := store.FindEntry(ctx, path)
  113. if err != nil {
  114. glog.V(0).Infof("list %s : %v", path, err)
  115. } else {
  116. entries = append(entries, entry)
  117. }
  118. }
  119. return entries, err
  120. }
  121. func genDirectoryListKey(dir string) (dirList string) {
  122. return dir + DIR_LIST_MARKER
  123. }