item_list_serde.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package redis3
  2. import (
  3. "github.com/redis/go-redis/v9"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/util/skiplist"
  6. "google.golang.org/protobuf/proto"
  7. )
  8. func LoadItemList(data []byte, prefix string, client redis.UniversalClient, store skiplist.ListStore, batchSize int) *ItemList {
  9. nl := &ItemList{
  10. skipList: skiplist.New(store),
  11. batchSize: batchSize,
  12. client: client,
  13. prefix: prefix,
  14. }
  15. if len(data) == 0 {
  16. return nl
  17. }
  18. message := &skiplist.SkipListProto{}
  19. if err := proto.Unmarshal(data, message); err != nil {
  20. glog.Errorf("loading skiplist: %v", err)
  21. }
  22. nl.skipList.MaxNewLevel = int(message.MaxNewLevel)
  23. nl.skipList.MaxLevel = int(message.MaxLevel)
  24. for i, ref := range message.StartLevels {
  25. nl.skipList.StartLevels[i] = &skiplist.SkipListElementReference{
  26. ElementPointer: ref.ElementPointer,
  27. Key: ref.Key,
  28. }
  29. }
  30. for i, ref := range message.EndLevels {
  31. nl.skipList.EndLevels[i] = &skiplist.SkipListElementReference{
  32. ElementPointer: ref.ElementPointer,
  33. Key: ref.Key,
  34. }
  35. }
  36. return nl
  37. }
  38. func (nl *ItemList) HasChanges() bool {
  39. return nl.skipList.HasChanges
  40. }
  41. func (nl *ItemList) ToBytes() []byte {
  42. message := &skiplist.SkipListProto{}
  43. message.MaxNewLevel = int32(nl.skipList.MaxNewLevel)
  44. message.MaxLevel = int32(nl.skipList.MaxLevel)
  45. for _, ref := range nl.skipList.StartLevels {
  46. if ref == nil {
  47. break
  48. }
  49. message.StartLevels = append(message.StartLevels, &skiplist.SkipListElementReference{
  50. ElementPointer: ref.ElementPointer,
  51. Key: ref.Key,
  52. })
  53. }
  54. for _, ref := range nl.skipList.EndLevels {
  55. if ref == nil {
  56. break
  57. }
  58. message.EndLevels = append(message.EndLevels, &skiplist.SkipListElementReference{
  59. ElementPointer: ref.ElementPointer,
  60. Key: ref.Key,
  61. })
  62. }
  63. data, err := proto.Marshal(message)
  64. if err != nil {
  65. glog.Errorf("marshal skiplist: %v", err)
  66. }
  67. return data
  68. }