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.

217 lines
5.2 KiB

5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package redis
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "time"
  8. "github.com/go-redis/redis"
  9. "github.com/chrislusf/seaweedfs/weed/filer2"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. const (
  15. DIR_LIST_MARKER = "\x00"
  16. )
  17. type UniversalRedisStore struct {
  18. Client redis.UniversalClient
  19. }
  20. func (store *UniversalRedisStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  21. return ctx, nil
  22. }
  23. func (store *UniversalRedisStore) CommitTransaction(ctx context.Context) error {
  24. return nil
  25. }
  26. func (store *UniversalRedisStore) RollbackTransaction(ctx context.Context) error {
  27. return nil
  28. }
  29. func (store *UniversalRedisStore) InsertEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  30. value, err := entry.EncodeAttributesAndChunks()
  31. if err != nil {
  32. return fmt.Errorf("encoding %s %+v: %v", entry.FullPath, entry.Attr, err)
  33. }
  34. _, err = store.Client.Set(string(entry.FullPath), value, time.Duration(entry.TtlSec)*time.Second).Result()
  35. if err != nil {
  36. return fmt.Errorf("persisting %s : %v", entry.FullPath, err)
  37. }
  38. dir, name := entry.FullPath.DirAndName()
  39. if name != "" {
  40. _, err = store.Client.SAdd(genDirectoryListKey(dir), name).Result()
  41. if err != nil {
  42. return fmt.Errorf("persisting %s in parent dir: %v", entry.FullPath, err)
  43. }
  44. }
  45. return nil
  46. }
  47. func (store *UniversalRedisStore) UpdateEntry(ctx context.Context, entry *filer2.Entry) (err error) {
  48. return store.InsertEntry(ctx, entry)
  49. }
  50. func (store *UniversalRedisStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer2.Entry, err error) {
  51. data, err := store.Client.Get(string(fullpath)).Result()
  52. if err == redis.Nil {
  53. return nil, filer_pb.ErrNotFound
  54. }
  55. if err != nil {
  56. return nil, fmt.Errorf("get %s : %v", fullpath, err)
  57. }
  58. entry = &filer2.Entry{
  59. FullPath: fullpath,
  60. }
  61. err = entry.DecodeAttributesAndChunks([]byte(data))
  62. if err != nil {
  63. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  64. }
  65. return entry, nil
  66. }
  67. func (store *UniversalRedisStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
  68. _, err = store.Client.Del(string(fullpath)).Result()
  69. if err != nil {
  70. return fmt.Errorf("delete %s : %v", fullpath, err)
  71. }
  72. dir, name := fullpath.DirAndName()
  73. if name != "" {
  74. _, err = store.Client.SRem(genDirectoryListKey(dir), name).Result()
  75. if err != nil {
  76. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  77. }
  78. }
  79. return nil
  80. }
  81. func (store *UniversalRedisStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
  82. members, err := store.Client.SMembers(genDirectoryListKey(string(fullpath))).Result()
  83. if err != nil {
  84. return fmt.Errorf("delete folder %s : %v", fullpath, err)
  85. }
  86. for _, fileName := range members {
  87. path := util.NewFullPath(string(fullpath), fileName)
  88. _, err = store.Client.Del(string(path)).Result()
  89. if err != nil {
  90. return fmt.Errorf("delete %s in parent dir: %v", fullpath, err)
  91. }
  92. }
  93. return nil
  94. }
  95. func (store *UniversalRedisStore) ListDirectoryPrefixedEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool, limit int, prefix string) (entries []*filer2.Entry, err error) {
  96. count := 0
  97. notPrefixed, err := store.ListDirectoryEntries(ctx, fullpath, startFileName, inclusive, limit)
  98. if err != nil {
  99. return nil, err
  100. }
  101. if prefix == "" {
  102. return notPrefixed, nil
  103. }
  104. for count < limit {
  105. for _, entry := range notPrefixed {
  106. if strings.HasPrefix(entry.Name(), prefix) {
  107. count++
  108. entries = append(entries, entry)
  109. }
  110. }
  111. if count >= limit {
  112. break
  113. }
  114. notPrefixed, err = store.ListDirectoryEntries(ctx, fullpath, startFileName, inclusive, limit)
  115. if err != nil {
  116. return nil, err
  117. }
  118. }
  119. return entries, nil
  120. }
  121. func (store *UniversalRedisStore) ListDirectoryEntries(ctx context.Context, fullpath util.FullPath, startFileName string, inclusive bool,
  122. limit int) (entries []*filer2.Entry, err error) {
  123. dirListKey := genDirectoryListKey(string(fullpath))
  124. members, err := store.Client.SMembers(dirListKey).Result()
  125. if err != nil {
  126. return nil, fmt.Errorf("list %s : %v", fullpath, err)
  127. }
  128. // skip
  129. if startFileName != "" {
  130. var t []string
  131. for _, m := range members {
  132. if strings.Compare(m, startFileName) >= 0 {
  133. if m == startFileName {
  134. if inclusive {
  135. t = append(t, m)
  136. }
  137. } else {
  138. t = append(t, m)
  139. }
  140. }
  141. }
  142. members = t
  143. }
  144. // sort
  145. sort.Slice(members, func(i, j int) bool {
  146. return strings.Compare(members[i], members[j]) < 0
  147. })
  148. // limit
  149. if limit < len(members) {
  150. members = members[:limit]
  151. }
  152. // fetch entry meta
  153. for _, fileName := range members {
  154. path := util.NewFullPath(string(fullpath), fileName)
  155. entry, err := store.FindEntry(ctx, path)
  156. if err != nil {
  157. glog.V(0).Infof("list %s : %v", path, err)
  158. } else {
  159. if entry.TtlSec > 0 {
  160. if entry.Attr.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
  161. store.Client.Del(string(path)).Result()
  162. store.Client.SRem(dirListKey, fileName).Result()
  163. continue
  164. }
  165. }
  166. entries = append(entries, entry)
  167. }
  168. }
  169. return entries, err
  170. }
  171. func genDirectoryListKey(dir string) (dirList string) {
  172. return dir + DIR_LIST_MARKER
  173. }
  174. func (store *UniversalRedisStore) Shutdown() {
  175. store.Client.Close()
  176. }