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.

172 lines
3.7 KiB

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) (entry *filer2.Entry, err error) {
  77. entry, err = store.FindEntry(fullpath)
  78. if err != nil {
  79. return nil, nil
  80. }
  81. _, err = store.Client.Del(string(fullpath)).Result()
  82. if err != nil {
  83. return entry, fmt.Errorf("delete %s : %v", entry.FullPath, err)
  84. }
  85. dir, name := fullpath.DirAndName()
  86. if name != "" {
  87. _, err = store.Client.SRem(genDirectoryListKey(dir), name).Result()
  88. if err != nil {
  89. return nil, fmt.Errorf("delete %s in parent dir: %v", entry.FullPath, err)
  90. }
  91. }
  92. return entry, nil
  93. }
  94. func (store *RedisStore) ListDirectoryEntries(fullpath filer2.FullPath, startFileName string, inclusive bool,
  95. limit int) (entries []*filer2.Entry, err error) {
  96. members, err := store.Client.SMembers(genDirectoryListKey(string(fullpath))).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(path)
  128. if err != nil {
  129. glog.V(0).Infof("list %s : %v", path, err)
  130. } else {
  131. entries = append(entries, entry)
  132. }
  133. }
  134. return entries, err
  135. }
  136. func genDirectoryListKey(dir string) (dirList string) {
  137. return dir + DIR_LIST_MARKER
  138. }