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.

111 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package redis3
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/util/skiplist"
  7. "github.com/go-redis/redis/v8"
  8. )
  9. const maxNameBatchSizeLimit = 1000
  10. func insertChild(ctx context.Context, client redis.UniversalClient, key string, name string) error {
  11. data, err := client.Get(ctx, key).Result()
  12. if err != nil {
  13. if err != redis.Nil {
  14. return fmt.Errorf("read %s: %v", key, err)
  15. }
  16. }
  17. store := newSkipListElementStore(key, client)
  18. nameList := skiplist.LoadNameList([]byte(data), store, maxNameBatchSizeLimit)
  19. // println("add", key, name)
  20. if err := nameList.WriteName(name); err != nil {
  21. glog.Errorf("add %s %s: %v", key, name, err)
  22. return err
  23. }
  24. if !nameList.HasChanges() {
  25. return nil
  26. }
  27. if err := client.Set(ctx, key, nameList.ToBytes(), 0).Err(); err != nil {
  28. return err
  29. }
  30. return nil
  31. }
  32. func removeChild(ctx context.Context, client redis.UniversalClient, key string, name string) error {
  33. data, err := client.Get(ctx, key).Result()
  34. if err != nil {
  35. if err != redis.Nil {
  36. return fmt.Errorf("read %s: %v", key, err)
  37. }
  38. }
  39. store := newSkipListElementStore(key, client)
  40. nameList := skiplist.LoadNameList([]byte(data), store, maxNameBatchSizeLimit)
  41. if err := nameList.DeleteName(name); err != nil {
  42. return err
  43. }
  44. if !nameList.HasChanges() {
  45. return nil
  46. }
  47. if err := client.Set(ctx, key, nameList.ToBytes(), 0).Err(); err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. func removeChildren(ctx context.Context, client redis.UniversalClient, key string, onDeleteFn func(name string) error) error {
  53. data, err := client.Get(ctx, key).Result()
  54. if err != nil {
  55. if err != redis.Nil {
  56. return fmt.Errorf("read %s: %v", key, err)
  57. }
  58. }
  59. store := newSkipListElementStore(key, client)
  60. nameList := skiplist.LoadNameList([]byte(data), store, maxNameBatchSizeLimit)
  61. if err = nameList.ListNames("", func(name string) bool {
  62. if err := onDeleteFn(name); err != nil {
  63. glog.Errorf("delete %s child %s: %v", key, name, err)
  64. return false
  65. }
  66. return true
  67. }); err != nil {
  68. return err
  69. }
  70. if err = nameList.RemoteAllListElement(); err != nil {
  71. return err
  72. }
  73. return nil
  74. }
  75. func listChildren(ctx context.Context, client redis.UniversalClient, key string, startFileName string, eachFn func(name string) bool) error {
  76. data, err := client.Get(ctx, key).Result()
  77. if err != nil {
  78. if err != redis.Nil {
  79. return fmt.Errorf("read %s: %v", key, err)
  80. }
  81. }
  82. store := newSkipListElementStore(key, client)
  83. nameList := skiplist.LoadNameList([]byte(data), store, maxNameBatchSizeLimit)
  84. if err = nameList.ListNames(startFileName, func(name string) bool {
  85. return eachFn(name)
  86. }); err != nil {
  87. return err
  88. }
  89. return nil
  90. }