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.

193 lines
5.0 KiB

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