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.

166 lines
3.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  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. )
  10. const (
  11. DIR_LIST_MARKER = "\x00"
  12. )
  13. func init() {
  14. filer2.Stores = append(filer2.Stores, &RedisStore{})
  15. }
  16. type RedisStore struct {
  17. Client *redis.Client
  18. }
  19. func (store *RedisStore) GetName() string {
  20. return "redis"
  21. }
  22. func (store *RedisStore) Initialize(configuration filer2.Configuration) (err error) {
  23. return store.initialize(
  24. configuration.GetString("address"),
  25. configuration.GetString("password"),
  26. configuration.GetInt("database"),
  27. )
  28. }
  29. func (store *RedisStore) initialize(hostPort string, password string, database int) (err error) {
  30. store.Client = redis.NewClient(&redis.Options{
  31. Addr: hostPort,
  32. Password: password,
  33. DB: database,
  34. })
  35. return
  36. }
  37. func (store *RedisStore) InsertEntry(entry *filer2.Entry) (err error) {
  38. value, err := entry.EncodeAttributesAndChunks()
  39. if err != nil {
  40. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  41. }
  42. _, err = store.Client.Set(string(entry.FullPath), value, 0).Result()
  43. if err != nil {
  44. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  45. }
  46. dir, name := entry.FullPath.DirAndName()
  47. if name != "" {
  48. _, err = store.Client.SAdd(genDirectoryListKey(dir), name).Result()
  49. if err != nil {
  50. return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
  51. }
  52. }
  53. return nil
  54. }
  55. func (store *RedisStore) UpdateEntry(entry *filer2.Entry) (err error) {
  56. return store.InsertEntry(entry)
  57. }
  58. func (store *RedisStore) FindEntry(fullpath filer2.FullPath) (entry *filer2.Entry, err error) {
  59. data, err := store.Client.Get(string(fullpath)).Result()
  60. if err == redis.Nil {
  61. return nil, filer2.ErrNotFound
  62. }
  63. if err != nil {
  64. return nil, fmt.Errorf("get %s : %v", entry.FullPath, err)
  65. }
  66. entry = &filer2.Entry{
  67. FullPath: fullpath,
  68. }
  69. err = entry.DecodeAttributesAndChunks([]byte(data))
  70. if err != nil {
  71. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  72. }
  73. return entry, nil
  74. }
  75. func (store *RedisStore) DeleteEntry(fullpath filer2.FullPath) (err error) {
  76. _, err = store.Client.Del(string(fullpath)).Result()
  77. if err != nil {
  78. return fmt.Errorf("delete %s : %v", fullpath, err)
  79. }
  80. dir, name := fullpath.DirAndName()
  81. if name != "" {
  82. _, err = store.Client.SRem(genDirectoryListKey(dir), name).Result()
  83. if err != nil {
  84. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  85. }
  86. }
  87. return nil
  88. }
  89. func (store *RedisStore) ListDirectoryEntries(fullpath filer2.FullPath, startFileName string, inclusive bool,
  90. limit int) (entries []*filer2.Entry, err error) {
  91. members, err := store.Client.SMembers(genDirectoryListKey(string(fullpath))).Result()
  92. if err != nil {
  93. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  94. }
  95. // skip
  96. if startFileName != "" {
  97. var t []string
  98. for _, m := range members {
  99. if strings.Compare(m, startFileName) >= 0 {
  100. if m == startFileName {
  101. if inclusive {
  102. t = append(t, m)
  103. }
  104. } else {
  105. t = append(t, m)
  106. }
  107. }
  108. }
  109. members = t
  110. }
  111. // sort
  112. sort.Slice(members, func(i, j int) bool {
  113. return strings.Compare(members[i], members[j]) < 0
  114. })
  115. // limit
  116. if limit < len(members) {
  117. members = members[:limit]
  118. }
  119. // fetch entry meta
  120. for _, fileName := range members {
  121. path := filer2.NewFullPath(string(fullpath), fileName)
  122. entry, err := store.FindEntry(path)
  123. if err != nil {
  124. glog.V(0).Infof("list %s : %v", path, err)
  125. } else {
  126. entries = append(entries, entry)
  127. }
  128. }
  129. return entries, err
  130. }
  131. func genDirectoryListKey(dir string) (dirList string) {
  132. return dir + DIR_LIST_MARKER
  133. }