2021-10-02 21:02:56 +00:00
|
|
|
package skiplist
|
|
|
|
|
|
|
|
import "bytes"
|
|
|
|
|
|
|
|
func compareElement(a *SkipListElement, key []byte) int {
|
2021-10-03 08:07:35 +00:00
|
|
|
if len(a.Key) == 0 {
|
2021-10-02 21:02:56 +00:00
|
|
|
return -1
|
|
|
|
}
|
2021-10-03 08:07:35 +00:00
|
|
|
return bytes.Compare(a.Key, key)
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *SkipListElement) Reference() *SkipListElementReference {
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &SkipListElementReference{
|
|
|
|
ElementPointer: node.Id,
|
2021-10-03 08:07:35 +00:00
|
|
|
Key: node.Key,
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-03 09:19:21 +00:00
|
|
|
|
|
|
|
func (t *SkipList) saveElement(element *SkipListElement) error {
|
|
|
|
if element == nil {
|
|
|
|
return nil
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
2021-10-03 09:19:21 +00:00
|
|
|
return t.listStore.SaveElement(element.Id, element)
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 09:19:21 +00:00
|
|
|
func (t *SkipList) deleteElement(element *SkipListElement) error {
|
|
|
|
if element == nil {
|
|
|
|
return nil
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
2021-10-03 09:19:21 +00:00
|
|
|
return t.listStore.DeleteElement(element.Id)
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 09:19:21 +00:00
|
|
|
func (t *SkipList) loadElement(ref *SkipListElementReference) (*SkipListElement, error) {
|
2021-10-04 09:30:44 +00:00
|
|
|
if ref.IsNil() {
|
2021-10-03 09:19:21 +00:00
|
|
|
return nil, nil
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
2021-10-03 09:19:21 +00:00
|
|
|
return t.listStore.LoadElement(ref.ElementPointer)
|
2021-10-02 21:02:56 +00:00
|
|
|
}
|
2021-10-04 09:30:44 +00:00
|
|
|
|
|
|
|
func (ref *SkipListElementReference) IsNil() bool {
|
|
|
|
if ref == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(ref.Key) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|