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.

142 lines
3.1 KiB

  1. package redis
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/filer2"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/go-redis/redis"
  7. "sort"
  8. "strings"
  9. "time"
  10. )
  11. const (
  12. DIR_LIST_MARKER = "\x00"
  13. )
  14. type UniversalRedisStore struct {
  15. Client redis.UniversalClient
  16. }
  17. func (store *UniversalRedisStore) InsertEntry(entry *filer2.Entry) (err error) {
  18. value, err := entry.EncodeAttributesAndChunks()
  19. if err != nil {
  20. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  21. }
  22. _, err = store.Client.Set(string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Result()
  23. if err != nil {
  24. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  25. }
  26. dir, name := entry.FullPath.DirAndName()
  27. if name != "" {
  28. _, err = store.Client.SAdd(genDirectoryListKey(dir), name).Result()
  29. if err != nil {
  30. return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
  31. }
  32. }
  33. return nil
  34. }
  35. func (store *UniversalRedisStore) UpdateEntry(entry *filer2.Entry) (err error) {
  36. return store.InsertEntry(entry)
  37. }
  38. func (store *UniversalRedisStore) FindEntry(fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  39. data, err := store.Client.Get(string(fullpath)).Result()
  40. if err == redis.Nil {
  41. return nil, filer2.ErrNotFound
  42. }
  43. if err != nil {
  44. return nil, fmt.Errorf("get %s : %v", fullpath, err)
  45. }
  46. entry = &filer2.Entry{
  47. FullPath: fullpath,
  48. }
  49. err = entry.DecodeAttributesAndChunks([]byte(data))
  50. if err != nil {
  51. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  52. }
  53. return entry, nil
  54. }
  55. func (store *UniversalRedisStore) DeleteEntry(fullpath filer2.FullPath) (err error) {
  56. _, err = store.Client.Del(string(fullpath)).Result()
  57. if err != nil {
  58. return fmt.Errorf("delete %s : %v", fullpath, err)
  59. }
  60. dir, name := fullpath.DirAndName()
  61. if name != "" {
  62. _, err = store.Client.SRem(genDirectoryListKey(dir), name).Result()
  63. if err != nil {
  64. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  65. }
  66. }
  67. return nil
  68. }
  69. func (store *UniversalRedisStore) ListDirectoryEntries(fullpath filer2.FullPath, startFileName string, inclusive bool,
  70. limit int) (entries []*filer2.Entry, err error) {
  71. members, err := store.Client.SMembers(genDirectoryListKey(string(fullpath))).Result()
  72. if err != nil {
  73. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  74. }
  75. // skip
  76. if startFileName != "" {
  77. var t []string
  78. for _, m := range members {
  79. if strings.Compare(m, startFileName) >= 0 {
  80. if m == startFileName {
  81. if inclusive {
  82. t = append(t, m)
  83. }
  84. } else {
  85. t = append(t, m)
  86. }
  87. }
  88. }
  89. members = t
  90. }
  91. // sort
  92. sort.Slice(members, func(i, j int) bool {
  93. return strings.Compare(members[i], members[j]) < 0
  94. })
  95. // limit
  96. if limit < len(members) {
  97. members = members[:limit]
  98. }
  99. // fetch entry meta
  100. for _, fileName := range members {
  101. path := filer2.NewFullPath(string(fullpath), fileName)
  102. entry, err := store.FindEntry(path)
  103. if err != nil {
  104. glog.V(0).Infof("list %s : %v", path, err)
  105. } else {
  106. entries = append(entries, entry)
  107. }
  108. }
  109. return entries, err
  110. }
  111. func genDirectoryListKey(dir string) (dirList string) {
  112. return dir + DIR_LIST_MARKER
  113. }