name_list_serde.go 1.7 KB

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