seaweedfs/weed/util/skiplist/skiplist.go

532 lines
12 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-03 08:07:35 +00:00
startLevels [maxLevel]*SkipListElementReference
endLevels [maxLevel]*SkipListElementReference
maxNewLevel int
maxLevel int
2021-10-03 09:19:21 +00:00
listStore ListStore
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-03 08:07:35 +00:00
maxNewLevel: maxLevel,
maxLevel: 0,
2021-10-03 09:19:21 +00:00
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 {
return t.startLevels[0] == nil
}
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
}
func (t *SkipList) findEntryIndex(key []byte, level int) int {
// Find good entry point so we don't accidentally skip half the list.
for i := t.maxLevel; i >= 0; i-- {
if t.startLevels[i] != nil && bytes.Compare(t.startLevels[i].Key, key) < 0 || i <= level {
return i
}
}
return 0
}
2021-10-03 09:19:21 +00:00
func (t *SkipList) findExtended(key []byte, findGreaterOrEqual bool) (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-03 09:19:21 +00:00
currentNode, err = t.loadElement(t.startLevels[index])
if err != 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-03 09:19:21 +00:00
currentNode, err = t.loadElement(currentNode.Next[index])
if err != 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 09:19:21 +00:00
var currentNodeNext *SkipListElement
currentNodeNext, err = t.loadElement(currentNode.Next[0])
if err != 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-03 09:19:21 +00:00
foundElem, err = t.loadElement(currentNode.Next[index])
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 09:19:21 +00:00
func (t *SkipList) Find(key []byte) (elem *SkipListElement, ok bool, err error) {
2021-10-02 21:02:56 +00:00
if t == nil || key == nil {
return
}
2021-10-03 09:19:21 +00:00
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 09:19:21 +00:00
func (t *SkipList) FindGreaterOrEqual(key []byte) (elem *SkipListElement, ok bool, err error) {
2021-10-02 21:02:56 +00:00
if t == nil || key == nil {
return
}
2021-10-03 09:19:21 +00:00
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-03 09:19:21 +00:00
func (t *SkipList) Delete(key []byte) (err error) {
2021-10-02 21:02:56 +00:00
if t == nil || t.IsEmpty() || key == nil {
return
}
index := t.findEntryIndex(key, t.maxLevel)
var currentNode *SkipListElement
var nextNode *SkipListElement
for {
if currentNode == nil {
2021-10-03 09:19:21 +00:00
nextNode, err = t.loadElement(t.startLevels[index])
2021-10-02 21:02:56 +00:00
} else {
2021-10-03 09:19:21 +00:00
nextNode, err = t.loadElement(currentNode.Next[index])
}
if err != nil {
return 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-03 09:19:21 +00:00
if err = t.saveElement(currentNode); err != nil {
return err
}
2021-10-02 21:02:56 +00:00
}
if index == 0 {
if nextNode.Next[index] != nil {
2021-10-03 09:19:21 +00:00
nextNextNode, err := t.loadElement(nextNode.Next[index])
if err != nil {
return err
}
2021-10-02 21:02:56 +00:00
nextNextNode.Prev = currentNode.Reference()
2021-10-03 09:19:21 +00:00
if err = t.saveElement(nextNextNode); err != nil {
return err
}
2021-10-02 21:02:56 +00:00
}
2021-10-03 08:07:35 +00:00
// t.elementCount--
2021-10-03 09:19:21 +00:00
if err = t.deleteElement(nextNode); err != nil {
return err
}
2021-10-02 21:02:56 +00:00
}
// Link from start needs readjustments.
startNextKey := t.startLevels[index].Key
if compareElement(nextNode, startNextKey) == 0 {
t.startLevels[index] = nextNode.Next[index]
// This was our currently highest node!
if t.startLevels[index] == nil {
t.maxLevel = index - 1
}
}
// Link from end needs readjustments.
if nextNode.Next[index] == nil {
t.endLevels[index] = currentNode.Reference()
}
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-03 09:19:21 +00:00
func (t *SkipList) Insert(key, value []byte) (err error){
2021-10-02 21:02:56 +00:00
if t == nil || key == nil {
return
}
level := t.generateLevel(t.maxNewLevel)
// Only grow the height of the skiplist by one at a time!
if level > t.maxLevel {
level = t.maxLevel + 1
t.maxLevel = level
}
elem := &SkipListElement{
2021-10-03 08:07:35 +00:00
Id: rand.Int63(),
Next: make([]*SkipListElementReference, t.maxNewLevel, t.maxNewLevel),
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() {
newFirst = compareElement(elem, t.startLevels[0].Key) < 0
newLast = compareElement(elem, t.endLevels[0].Key) > 0
}
normallyInserted := false
if !newFirst && !newLast {
normallyInserted = true
index := t.findEntryIndex(key, level)
var currentNode *SkipListElement
var nextNodeRef *SkipListElementReference
for {
if currentNode == nil {
nextNodeRef = t.startLevels[index]
} 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-03 09:19:21 +00:00
if err = t.saveElement(currentNode); err != nil {
return
}
2021-10-02 21:02:56 +00:00
}
if index == 0 {
elem.Prev = currentNode.Reference()
if nextNodeRef != nil {
2021-10-03 09:19:21 +00:00
if nextNode, err = t.loadElement(nextNodeRef); err != nil {
return
}
2021-10-02 21:02:56 +00:00
nextNode.Prev = elem.Reference()
2021-10-03 09:19:21 +00:00
if err = t.saveElement(nextNode); err != nil {
return
}
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-03 09:19:21 +00:00
if nextNode, err = t.loadElement(nextNodeRef); err != nil {
return
}
2021-10-02 21:02:56 +00:00
}
currentNode = nextNode
} 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 {
if t.startLevels[i] == nil || bytes.Compare(t.startLevels[i].Key, key) > 0 {
if i == 0 && t.startLevels[i] != nil {
2021-10-03 09:19:21 +00:00
startLevelElement, err := t.loadElement(t.startLevels[i])
if err != nil {
return err
}
2021-10-02 21:02:56 +00:00
startLevelElement.Prev = elem.Reference()
2021-10-03 09:19:21 +00:00
if err = t.saveElement(startLevelElement); err != nil {
return err
}
2021-10-02 21:02:56 +00:00
}
elem.Next[i] = t.startLevels[i]
t.startLevels[i] = elem.Reference()
}
// link the endLevels to this element!
if elem.Next[i] == nil {
t.endLevels[i] = elem.Reference()
}
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 {
if t.endLevels[i] != nil {
2021-10-03 09:19:21 +00:00
endLevelElement, err := t.loadElement(t.endLevels[i])
if err != nil {
return err
}
2021-10-02 21:02:56 +00:00
endLevelElement.Next[i] = elem.Reference()
2021-10-03 09:19:21 +00:00
if err = t.saveElement(endLevelElement); err != nil {
return err
}
2021-10-02 21:02:56 +00:00
}
if i == 0 {
elem.Prev = t.endLevels[i]
}
t.endLevels[i] = elem.Reference()
}
// Link the startLevels to this element!
if t.startLevels[i] == nil || bytes.Compare(t.startLevels[i].Key, key) > 0 {
t.startLevels[i] = elem.Reference()
}
didSomething = true
}
if !didSomething {
break
}
}
2021-10-03 09:19:21 +00:00
if err = t.saveElement(elem); err != nil {
return err
}
return 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) {
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) {
return t.loadElement(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-03 09:19:21 +00:00
return t.loadElement(t.startLevels[0])
2021-10-02 21:02:56 +00:00
}
2021-10-03 09:19:21 +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-03 09:19:21 +00:00
return t.loadElement(t.endLevels[0])
2021-10-02 21:02:56 +00:00
}
2021-10-03 09:19:21 +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-03 09:19:21 +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 --> ")
for i, l := range t.startLevels {
if l == nil {
break
}
if i > 0 {
print(" -> ")
}
next := "---"
if l != nil {
next = string(l.Key)
}
print(fmt.Sprintf("[%v]", next))
}
println()
nodeRef := t.startLevels[0]
for nodeRef != nil {
print(fmt.Sprintf("%v: ", string(nodeRef.Key)))
2021-10-03 09:19:21 +00:00
node, _ := t.loadElement(nodeRef)
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(" -> ")
}
}
println()
nodeRef = node.Next[0]
}
print("end --> ")
for i, l := range t.endLevels {
if l == nil {
break
}
if i > 0 {
print(" -> ")
}
next := "---"
if l != nil {
next = string(l.Key)
}
print(fmt.Sprintf("[%v]", next))
}
println()
}