seaweedfs/weed/util/skiplist/name_list_serde.go

72 lines
1.7 KiB
Go
Raw Normal View History

2021-10-04 08:01:31 +00:00
package skiplist
import (
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/golang/protobuf/proto"
)
func LoadNameList(data []byte, store ListStore, batchSize int) *NameList {
nl := &NameList{
skipList: New(store),
batchSize: batchSize,
}
if len(data) == 0 {
return nl
}
message := &SkipListProto{}
if err := proto.Unmarshal(data, message); err != nil {
glog.Errorf("loading skiplist: %v", err)
}
2021-10-07 01:18:24 +00:00
nl.skipList.MaxNewLevel = int(message.MaxNewLevel)
nl.skipList.MaxLevel = int(message.MaxLevel)
2021-10-04 08:01:31 +00:00
for i, ref := range message.StartLevels {
2021-10-07 01:18:24 +00:00
nl.skipList.StartLevels[i] = &SkipListElementReference{
2021-10-04 08:01:31 +00:00
ElementPointer: ref.ElementPointer,
Key: ref.Key,
}
}
for i, ref := range message.EndLevels {
2021-10-07 01:18:24 +00:00
nl.skipList.EndLevels[i] = &SkipListElementReference{
2021-10-04 08:01:31 +00:00
ElementPointer: ref.ElementPointer,
Key: ref.Key,
}
}
return nl
}
func (nl *NameList) HasChanges() bool {
2021-10-07 01:18:24 +00:00
return nl.skipList.HasChanges
2021-10-04 08:01:31 +00:00
}
func (nl *NameList) ToBytes() []byte {
message := &SkipListProto{}
2021-10-07 01:18:24 +00:00
message.MaxNewLevel = int32(nl.skipList.MaxNewLevel)
message.MaxLevel = int32(nl.skipList.MaxLevel)
for _, ref := range nl.skipList.StartLevels {
2021-10-04 08:01:31 +00:00
if ref == nil {
break
}
message.StartLevels = append(message.StartLevels, &SkipListElementReference{
ElementPointer: ref.ElementPointer,
Key: ref.Key,
})
}
2021-10-07 01:18:24 +00:00
for _, ref := range nl.skipList.EndLevels {
2021-10-04 08:01:31 +00:00
if ref == nil {
break
}
message.EndLevels = append(message.EndLevels, &SkipListElementReference{
ElementPointer: ref.ElementPointer,
Key: ref.Key,
})
}
data, err := proto.Marshal(message)
if err != nil {
glog.Errorf("marshal skiplist: %v", err)
}
return data
2021-10-08 04:13:31 +00:00
}