seaweedfs/weed/util/skiplist/skiplist.go

572 lines
13 KiB
Go
Raw Normal View History

2021-10-02 21:02:56 +00:00
package skiplist
2021-10-03 08:15:14 +00:00
// adapted from https://github.com/MauriceGit/skiplist/blob/master/skiplist.go
2021-10-02 21:02:56 +00:00
import (
"bytes"
"fmt"
"math/bits"
"math/rand"
"time"
)
const (
// maxLevel denotes the maximum height of the skiplist. This height will keep the skiplist
// efficient for up to 34m entries. If there is a need for much more, please adjust this constant accordingly.
maxLevel = 25
)
type SkipList struct {
2021-10-07 01:18:24 +00:00
StartLevels [maxLevel]*SkipListElementReference
EndLevels [maxLevel]*SkipListElementReference
MaxNewLevel int
MaxLevel int
ListStore ListStore
HasChanges bool
2021-10-03 08:07:35 +00:00
// elementCount int
2021-10-02 21:02:56 +00:00
}
// NewSeedEps returns a new empty, initialized Skiplist.
// Given a seed, a deterministic height/list behaviour can be achieved.
// Eps is used to compare keys given by the ExtractKey() function on equality.
2021-10-03 09:19:21 +00:00
func NewSeed(seed int64, listStore ListStore) *SkipList {
2021-10-02 21:02:56 +00:00
// Initialize random number generator.
rand.Seed(seed)
//fmt.Printf("SkipList seed: %v\n", seed)
list := &SkipList{
2021-10-07 01:18:24 +00:00
MaxNewLevel: maxLevel,
MaxLevel: 0,
ListStore: listStore,
2021-10-03 08:07:35 +00:00
// elementCount: 0,
2021-10-02 21:02:56 +00:00
}
return list
}
// New returns a new empty, initialized Skiplist.
2021-10-03 09:19:21 +00:00
func New(listStore ListStore) *SkipList {
return NewSeed(time.Now().UTC().UnixNano(), listStore)
2021-10-02 21:02:56 +00:00
}
// IsEmpty checks, if the skiplist is empty.
func (t *SkipList) IsEmpty() bool {
2021-10-07 01:18:24 +00:00
return t.StartLevels[0] == nil
2021-10-02 21:02:56 +00:00
}
func (t *SkipList) generateLevel(maxLevel int) int {
level := maxLevel - 1
// First we apply some mask which makes sure that we don't get a level
// above our desired level. Then we find the first set bit.
var x = rand.Uint64() & ((1 << uint(maxLevel-1)) - 1)
zeroes := bits.TrailingZeros64(x)
if zeroes <= maxLevel {
level = zeroes
}
return level
}
2021-10-03 20:50:52 +00:00
func (t *SkipList) findEntryIndex(key []byte, minLevel int) int {
2021-10-02 21:02:56 +00:00
// Find good entry point so we don't accidentally skip half the list.
2021-10-07 01:18:24 +00:00
for i := t.MaxLevel; i >= 0; i-- {
if t.StartLevels[i] != nil && bytes.Compare(t.StartLevels[i].Key, key) < 0 || i <= minLevel {
2021-10-02 21:02:56 +00:00
return i
}
}
return 0
}
2021-10-03 20:50:52 +00:00
func (t *SkipList) findExtended(key []byte, findGreaterOrEqual bool) (prevElementIfVisited *SkipListElement, foundElem *SkipListElement, ok bool, err error) {
2021-10-02 21:02:56 +00:00
foundElem = nil
ok = false
if t.IsEmpty() {
return
}
index := t.findEntryIndex(key, 0)
var currentNode *SkipListElement
2021-10-07 01:18:24 +00:00
currentNode, err = t.LoadElement(t.StartLevels[index])
2021-10-03 09:19:21 +00:00
if err != nil {
return
}
2021-10-04 08:01:31 +00:00
if currentNode == nil {
return
}
2021-10-02 21:02:56 +00:00
// In case, that our first element is already greater-or-equal!
if findGreaterOrEqual && compareElement(currentNode, key) > 0 {
foundElem = currentNode
ok = true
return
}
for {
if compareElement(currentNode, key) == 0 {
foundElem = currentNode
ok = true
return
}
// Which direction are we continuing next time?
if currentNode.Next[index] != nil && bytes.Compare(currentNode.Next[index].Key, key) <= 0 {
// Go right
2021-10-07 01:18:24 +00:00
currentNode, err = t.LoadElement(currentNode.Next[index])
2021-10-03 09:19:21 +00:00
if err != nil {
return
}
2021-10-04 08:01:31 +00:00
if currentNode == nil {
return
}
2021-10-02 21:02:56 +00:00
} else {
if index > 0 {
// Early exit
if currentNode.Next[0] != nil && bytes.Compare(currentNode.Next[0].Key, key) == 0 {
2021-10-03 20:50:52 +00:00
prevElementIfVisited = currentNode
2021-10-03 09:19:21 +00:00
var currentNodeNext *SkipListElement
2021-10-07 01:18:24 +00:00
currentNodeNext, err = t.LoadElement(currentNode.Next[0])
2021-10-03 09:19:21 +00:00
if err != nil {
return
}
2021-10-04 08:01:31 +00:00
if currentNodeNext == nil {
return
}
2021-10-02 21:02:56 +00:00
foundElem = currentNodeNext
ok = true
return
}
// Go down
index--
} else {
// Element is not found and we reached the bottom.
if findGreaterOrEqual {
2021-10-07 01:18:24 +00:00
foundElem, err = t.LoadElement(currentNode.Next[index])
2021-10-03 09:19:21 +00:00
if err != nil {
return
}
2021-10-02 21:02:56 +00:00
ok = foundElem != nil
}
return
}
}
}
}
// Find tries to find an element in the skiplist based on the key from the given ListElement.
// elem can be used, if ok is true.
// Find runs in approx. O(log(n))
2021-10-03 20:50:52 +00:00
func (t *SkipList) Find(key []byte) (prevIfVisited *SkipListElement, elem *SkipListElement, ok bool, err error) {
2021-10-02 21:02:56 +00:00
if t == nil || key == nil {
return
}
2021-10-03 20:50:52 +00:00
prevIfVisited, elem, ok, err = t.findExtended(key, false)
2021-10-02 21:02:56 +00:00
return
}
// FindGreaterOrEqual finds the first element, that is greater or equal to the given ListElement e.
// The comparison is done on the keys (So on ExtractKey()).
// FindGreaterOrEqual runs in approx. O(log(n))
2021-10-03 20:50:52 +00:00
func (t *SkipList) FindGreaterOrEqual(key []byte) (prevIfVisited *SkipListElement, elem *SkipListElement, ok bool, err error) {
2021-10-02 21:02:56 +00:00
if t == nil || key == nil {
return
}
2021-10-03 20:50:52 +00:00
prevIfVisited, elem, ok, err = t.findExtended(key, true)
2021-10-02 21:02:56 +00:00
return
}
// Delete removes an element equal to e from the skiplist, if there is one.
// If there are multiple entries with the same value, Delete will remove one of them
// (Which one will change based on the actual skiplist layout)
// Delete runs in approx. O(log(n))
2021-10-07 01:18:24 +00:00
func (t *SkipList) DeleteByKey(key []byte) (id int64, err error) {
2021-10-02 21:02:56 +00:00
if t == nil || t.IsEmpty() || key == nil {
return
}
2021-10-07 01:18:24 +00:00
index := t.findEntryIndex(key, t.MaxLevel)
2021-10-02 21:02:56 +00:00
var currentNode *SkipListElement
var nextNode *SkipListElement
for {
if currentNode == nil {
2021-10-07 01:18:24 +00:00
nextNode, err = t.LoadElement(t.StartLevels[index])
2021-10-02 21:02:56 +00:00
} else {
2021-10-07 01:18:24 +00:00
nextNode, err = t.LoadElement(currentNode.Next[index])
2021-10-03 09:19:21 +00:00
}
if err != nil {
2021-10-07 01:18:24 +00:00
return id, err
2021-10-02 21:02:56 +00:00
}
// Found and remove!
if nextNode != nil && compareElement(nextNode, key) == 0 {
if currentNode != nil {
currentNode.Next[index] = nextNode.Next[index]
2021-10-07 01:18:24 +00:00
if err = t.SaveElement(currentNode); err != nil {
return id, err
2021-10-03 09:19:21 +00:00
}
2021-10-02 21:02:56 +00:00
}
if index == 0 {
if nextNode.Next[index] != nil {
2021-10-07 01:18:24 +00:00
nextNextNode, err := t.LoadElement(nextNode.Next[index])
2021-10-03 09:19:21 +00:00
if err != nil {
2021-10-07 01:18:24 +00:00
return id, err
2021-10-03 09:19:21 +00:00
}
2021-10-04 08:01:31 +00:00
if nextNextNode != nil {
nextNextNode.Prev = currentNode.Reference()
2021-10-07 01:18:24 +00:00
if err = t.SaveElement(nextNextNode); err != nil {
return id, err
2021-10-04 08:01:31 +00:00
}
2021-10-03 09:19:21 +00:00
}
2021-10-02 21:02:56 +00:00
}
2021-10-03 08:07:35 +00:00
// t.elementCount--
2021-10-07 01:18:24 +00:00
id = nextNode.Id
if err = t.DeleteElement(nextNode); err != nil {
return id, err
2021-10-03 09:19:21 +00:00
}
2021-10-02 21:02:56 +00:00
}
// Link from start needs readjustments.
2021-10-07 01:18:24 +00:00
startNextKey := t.StartLevels[index].Key
2021-10-02 21:02:56 +00:00
if compareElement(nextNode, startNextKey) == 0 {
2021-10-07 01:18:24 +00:00
t.HasChanges = true
t.StartLevels[index] = nextNode.Next[index]
2021-10-02 21:02:56 +00:00
// This was our currently highest node!
2021-10-07 01:18:24 +00:00
if t.StartLevels[index] == nil {
t.MaxLevel = index - 1
2021-10-02 21:02:56 +00:00
}
}
// Link from end needs readjustments.
if nextNode.Next[index] == nil {
2021-10-07 01:18:24 +00:00
t.EndLevels[index] = currentNode.Reference()
t.HasChanges = true
2021-10-02 21:02:56 +00:00
}
nextNode.Next[index] = nil
}
if nextNode != nil && compareElement(nextNode, key) < 0 {
// Go right
currentNode = nextNode
} else {
// Go down
index--
if index < 0 {
break
}
}
}
2021-10-03 09:19:21 +00:00
return
2021-10-02 21:02:56 +00:00
}
// Insert inserts the given ListElement into the skiplist.
// Insert runs in approx. O(log(n))
2021-10-07 01:18:24 +00:00
func (t *SkipList) InsertByKey(key []byte, idIfKnown int64, value []byte) (id int64, err error) {
2021-10-02 21:02:56 +00:00
if t == nil || key == nil {
return
}
2021-10-07 01:18:24 +00:00
level := t.generateLevel(t.MaxNewLevel)
2021-10-02 21:02:56 +00:00
// Only grow the height of the skiplist by one at a time!
2021-10-07 01:18:24 +00:00
if level > t.MaxLevel {
level = t.MaxLevel + 1
t.MaxLevel = level
t.HasChanges = true
2021-10-02 21:02:56 +00:00
}
2021-10-07 01:18:24 +00:00
id = idIfKnown
if id == 0 {
id = rand.Int63()
}
2021-10-02 21:02:56 +00:00
elem := &SkipListElement{
2021-10-07 01:18:24 +00:00
Id: id,
Next: make([]*SkipListElementReference, t.MaxNewLevel, t.MaxNewLevel),
2021-10-03 08:07:35 +00:00
Level: int32(level),
Key: key,
Value: value,
2021-10-02 21:02:56 +00:00
}
2021-10-03 08:07:35 +00:00
// t.elementCount++
2021-10-02 21:02:56 +00:00
newFirst := true
newLast := true
if !t.IsEmpty() {
2021-10-07 01:18:24 +00:00
newFirst = compareElement(elem, t.StartLevels[0].Key) < 0
newLast = compareElement(elem, t.EndLevels[0].Key) > 0
2021-10-02 21:02:56 +00:00
}
normallyInserted := false
if !newFirst && !newLast {
normallyInserted = true
index := t.findEntryIndex(key, level)
var currentNode *SkipListElement
var nextNodeRef *SkipListElementReference
for {
if currentNode == nil {
2021-10-07 01:18:24 +00:00
nextNodeRef = t.StartLevels[index]
2021-10-02 21:02:56 +00:00
} else {
nextNodeRef = currentNode.Next[index]
}
var nextNode *SkipListElement
// Connect node to next
if index <= level && (nextNodeRef == nil || bytes.Compare(nextNodeRef.Key, key) > 0) {
elem.Next[index] = nextNodeRef
if currentNode != nil {
currentNode.Next[index] = elem.Reference()
2021-10-07 01:18:24 +00:00
if err = t.SaveElement(currentNode); err != nil {
2021-10-03 09:19:21 +00:00
return
}
2021-10-02 21:02:56 +00:00
}
if index == 0 {
elem.Prev = currentNode.Reference()
if nextNodeRef != nil {
2021-10-07 01:18:24 +00:00
if nextNode, err = t.LoadElement(nextNodeRef); err != nil {
2021-10-03 09:19:21 +00:00
return
}
2021-10-04 08:01:31 +00:00
if nextNode != nil {
nextNode.Prev = elem.Reference()
2021-10-07 01:18:24 +00:00
if err = t.SaveElement(nextNode); err != nil {
2021-10-04 08:01:31 +00:00
return
}
2021-10-03 09:19:21 +00:00
}
2021-10-02 21:02:56 +00:00
}
}
}
if nextNodeRef != nil && bytes.Compare(nextNodeRef.Key, key) <= 0 {
// Go right
if nextNode == nil {
// reuse nextNode when index == 0
2021-10-07 01:18:24 +00:00
if nextNode, err = t.LoadElement(nextNodeRef); err != nil {
2021-10-03 09:19:21 +00:00
return
}
2021-10-02 21:02:56 +00:00
}
currentNode = nextNode
2021-10-04 08:01:31 +00:00
if currentNode == nil {
return
}
2021-10-02 21:02:56 +00:00
} else {
// Go down
index--
if index < 0 {
break
}
}
}
}
// Where we have a left-most position that needs to be referenced!
for i := level; i >= 0; i-- {
didSomething := false
if newFirst || normallyInserted {
2021-10-07 01:18:24 +00:00
if t.StartLevels[i] == nil || bytes.Compare(t.StartLevels[i].Key, key) > 0 {
if i == 0 && t.StartLevels[i] != nil {
startLevelElement, err := t.LoadElement(t.StartLevels[i])
2021-10-03 09:19:21 +00:00
if err != nil {
2021-10-07 01:18:24 +00:00
return id, err
2021-10-03 09:19:21 +00:00
}
2021-10-04 08:01:31 +00:00
if startLevelElement != nil {
startLevelElement.Prev = elem.Reference()
2021-10-07 01:18:24 +00:00
if err = t.SaveElement(startLevelElement); err != nil {
return id, err
2021-10-04 08:01:31 +00:00
}
2021-10-03 09:19:21 +00:00
}
2021-10-02 21:02:56 +00:00
}
2021-10-07 01:18:24 +00:00
elem.Next[i] = t.StartLevels[i]
t.StartLevels[i] = elem.Reference()
t.HasChanges = true
2021-10-02 21:02:56 +00:00
}
2021-10-07 01:18:24 +00:00
// link the EndLevels to this element!
2021-10-02 21:02:56 +00:00
if elem.Next[i] == nil {
2021-10-07 01:18:24 +00:00
t.EndLevels[i] = elem.Reference()
t.HasChanges = true
2021-10-02 21:02:56 +00:00
}
didSomething = true
}
if newLast {
// Places the element after the very last element on this level!
// This is very important, so we are not linking the very first element (newFirst AND newLast) to itself!
if !newFirst {
2021-10-07 01:18:24 +00:00
if t.EndLevels[i] != nil {
endLevelElement, err := t.LoadElement(t.EndLevels[i])
2021-10-03 09:19:21 +00:00
if err != nil {
2021-10-07 01:18:24 +00:00
return id, err
2021-10-03 09:19:21 +00:00
}
2021-10-04 08:01:31 +00:00
if endLevelElement != nil {
endLevelElement.Next[i] = elem.Reference()
2021-10-07 01:18:24 +00:00
if err = t.SaveElement(endLevelElement); err != nil {
return id, err
2021-10-04 08:01:31 +00:00
}
2021-10-03 09:19:21 +00:00
}
2021-10-02 21:02:56 +00:00
}
if i == 0 {
2021-10-07 01:18:24 +00:00
elem.Prev = t.EndLevels[i]
2021-10-02 21:02:56 +00:00
}
2021-10-07 01:18:24 +00:00
t.EndLevels[i] = elem.Reference()
t.HasChanges = true
2021-10-02 21:02:56 +00:00
}
// Link the startLevels to this element!
2021-10-07 01:18:24 +00:00
if t.StartLevels[i] == nil || bytes.Compare(t.StartLevels[i].Key, key) > 0 {
t.StartLevels[i] = elem.Reference()
t.HasChanges = true
2021-10-02 21:02:56 +00:00
}
didSomething = true
}
if !didSomething {
break
}
}
2021-10-07 01:18:24 +00:00
if err = t.SaveElement(elem); err != nil {
return id, err
2021-10-03 09:19:21 +00:00
}
2021-10-07 01:18:24 +00:00
return id, nil
2021-10-02 21:02:56 +00:00
}
// GetSmallestNode returns the very first/smallest node in the skiplist.
// GetSmallestNode runs in O(1)
2021-10-03 09:19:21 +00:00
func (t *SkipList) GetSmallestNode() (*SkipListElement, error) {
2021-10-07 01:18:24 +00:00
return t.LoadElement(t.StartLevels[0])
2021-10-02 21:02:56 +00:00
}
// GetLargestNode returns the very last/largest node in the skiplist.
// GetLargestNode runs in O(1)
2021-10-03 09:19:21 +00:00
func (t *SkipList) GetLargestNode() (*SkipListElement, error) {
2021-10-07 01:18:24 +00:00
return t.LoadElement(t.EndLevels[0])
2021-10-02 21:02:56 +00:00
}
2021-10-08 04:13:31 +00:00
func (t *SkipList) GetLargestNodeReference() *SkipListElementReference {
2021-10-07 05:01:17 +00:00
return t.EndLevels[0]
}
2021-10-02 21:02:56 +00:00
// Next returns the next element based on the given node.
// Next will loop around to the first node, if you call it on the last!
2021-10-03 09:19:21 +00:00
func (t *SkipList) Next(e *SkipListElement) (*SkipListElement, error) {
2021-10-02 21:02:56 +00:00
if e.Next[0] == nil {
2021-10-07 01:18:24 +00:00
return t.LoadElement(t.StartLevels[0])
2021-10-02 21:02:56 +00:00
}
2021-10-07 01:18:24 +00:00
return t.LoadElement(e.Next[0])
2021-10-02 21:02:56 +00:00
}
// Prev returns the previous element based on the given node.
// Prev will loop around to the last node, if you call it on the first!
2021-10-03 09:19:21 +00:00
func (t *SkipList) Prev(e *SkipListElement) (*SkipListElement, error) {
2021-10-02 21:02:56 +00:00
if e.Prev == nil {
2021-10-07 01:18:24 +00:00
return t.LoadElement(t.EndLevels[0])
2021-10-02 21:02:56 +00:00
}
2021-10-07 01:18:24 +00:00
return t.LoadElement(e.Prev)
2021-10-02 21:02:56 +00:00
}
2021-10-03 08:15:14 +00:00
// ChangeValue can be used to change the actual value of a node in the skiplist
// without the need of Deleting and reinserting the node again.
// Be advised, that ChangeValue only works, if the actual key from ExtractKey() will stay the same!
// ok is an indicator, wether the value is actually changed.
2021-10-03 09:19:21 +00:00
func (t *SkipList) ChangeValue(e *SkipListElement, newValue []byte) (err error) {
2021-10-03 08:15:14 +00:00
// The key needs to stay correct, so this is very important!
e.Value = newValue
2021-10-07 01:18:24 +00:00
return t.SaveElement(e)
2021-10-03 08:15:14 +00:00
}
2021-10-02 21:02:56 +00:00
// String returns a string format of the skiplist. Useful to get a graphical overview and/or debugging.
func (t *SkipList) println() {
print("start --> ")
2021-10-07 01:18:24 +00:00
for i, l := range t.StartLevels {
2021-10-02 21:02:56 +00:00
if l == nil {
break
}
if i > 0 {
print(" -> ")
}
next := "---"
if l != nil {
next = string(l.Key)
}
print(fmt.Sprintf("[%v]", next))
}
println()
2021-10-07 01:18:24 +00:00
nodeRef := t.StartLevels[0]
2021-10-02 21:02:56 +00:00
for nodeRef != nil {
print(fmt.Sprintf("%v: ", string(nodeRef.Key)))
2021-10-07 01:18:24 +00:00
node, _ := t.LoadElement(nodeRef)
2021-10-04 08:01:31 +00:00
if node == nil {
break
}
2021-10-02 21:02:56 +00:00
for i := 0; i <= int(node.Level); i++ {
l := node.Next[i]
next := "---"
if l != nil {
next = string(l.Key)
}
if i == 0 {
prev := "---"
if node.Prev != nil {
prev = string(node.Prev.Key)
}
print(fmt.Sprintf("[%v|%v]", prev, next))
} else {
print(fmt.Sprintf("[%v]", next))
}
if i < int(node.Level) {
print(" -> ")
}
}
nodeRef = node.Next[0]
2021-10-04 08:01:31 +00:00
println()
2021-10-02 21:02:56 +00:00
}
print("end --> ")
2021-10-07 01:18:24 +00:00
for i, l := range t.EndLevels {
2021-10-02 21:02:56 +00:00
if l == nil {
break
}
if i > 0 {
print(" -> ")
}
next := "---"
if l != nil {
next = string(l.Key)
}
print(fmt.Sprintf("[%v]", next))
}
println()
}