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.

71 lines
1.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
  1. package skiplist
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/golang/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. }