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.

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