aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2021-10-03 13:50:52 -0700
committerChris Lu <chris.lu@gmail.com>2021-10-03 13:50:52 -0700
commita481c4a45ef60de22d6dacf83010542ce8c6e1bb (patch)
tree9112a0f5a47d685a0085b8adae4dbbe18f5a3f5a
parent22d8684e88bea2a36063568ba95e6d205aac33b0 (diff)
downloadseaweedfs-a481c4a45ef60de22d6dacf83010542ce8c6e1bb.tar.xz
seaweedfs-a481c4a45ef60de22d6dacf83010542ce8c6e1bb.zip
return previous element if visited
-rw-r--r--weed/util/skiplist/skiplist.go15
-rw-r--r--weed/util/skiplist/skiplist_test.go18
2 files changed, 17 insertions, 16 deletions
diff --git a/weed/util/skiplist/skiplist.go b/weed/util/skiplist/skiplist.go
index 498af085d..b48a05b4a 100644
--- a/weed/util/skiplist/skiplist.go
+++ b/weed/util/skiplist/skiplist.go
@@ -67,17 +67,17 @@ func (t *SkipList) generateLevel(maxLevel int) int {
return level
}
-func (t *SkipList) findEntryIndex(key []byte, level int) int {
+func (t *SkipList) findEntryIndex(key []byte, minLevel 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 {
+ if t.startLevels[i] != nil && bytes.Compare(t.startLevels[i].Key, key) < 0 || i <= minLevel {
return i
}
}
return 0
}
-func (t *SkipList) findExtended(key []byte, findGreaterOrEqual bool) (foundElem *SkipListElement, ok bool, err error) {
+func (t *SkipList) findExtended(key []byte, findGreaterOrEqual bool) (prevElementIfVisited *SkipListElement, foundElem *SkipListElement, ok bool, err error) {
foundElem = nil
ok = false
@@ -120,6 +120,7 @@ func (t *SkipList) findExtended(key []byte, findGreaterOrEqual bool) (foundElem
// Early exit
if currentNode.Next[0] != nil && bytes.Compare(currentNode.Next[0].Key, key) == 0 {
+ prevElementIfVisited = currentNode
var currentNodeNext *SkipListElement
currentNodeNext, err = t.loadElement(currentNode.Next[0])
if err != nil {
@@ -150,26 +151,26 @@ func (t *SkipList) findExtended(key []byte, findGreaterOrEqual bool) (foundElem
// 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))
-func (t *SkipList) Find(key []byte) (elem *SkipListElement, ok bool, err error) {
+func (t *SkipList) Find(key []byte) (prevIfVisited *SkipListElement, elem *SkipListElement, ok bool, err error) {
if t == nil || key == nil {
return
}
- elem, ok, err = t.findExtended(key, false)
+ prevIfVisited, elem, ok, err = t.findExtended(key, false)
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))
-func (t *SkipList) FindGreaterOrEqual(key []byte) (elem *SkipListElement, ok bool, err error) {
+func (t *SkipList) FindGreaterOrEqual(key []byte) (prevIfVisited *SkipListElement, elem *SkipListElement, ok bool, err error) {
if t == nil || key == nil {
return
}
- elem, ok, err = t.findExtended(key, true)
+ prevIfVisited, elem, ok, err = t.findExtended(key, true)
return
}
diff --git a/weed/util/skiplist/skiplist_test.go b/weed/util/skiplist/skiplist_test.go
index 75cbc6cfd..115656cd9 100644
--- a/weed/util/skiplist/skiplist_test.go
+++ b/weed/util/skiplist/skiplist_test.go
@@ -23,12 +23,12 @@ func TestInsertAndFind(t *testing.T) {
var listPointer *SkipList
listPointer.Insert(k0, k0)
- if _, ok, _ := listPointer.Find(k0); ok {
+ if _, _, ok, _ := listPointer.Find(k0); ok {
t.Fail()
}
list = New(memStore)
- if _, ok, _ := list.Find(k0); ok {
+ if _, _, ok, _ := list.Find(k0); ok {
t.Fail()
}
if !list.IsEmpty() {
@@ -42,7 +42,7 @@ func TestInsertAndFind(t *testing.T) {
}
for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(maxN - i))
- if _, ok, _ := list.Find(key); !ok {
+ if _, _, ok, _ := list.Find(key); !ok {
t.Fail()
}
}
@@ -55,7 +55,7 @@ func TestInsertAndFind(t *testing.T) {
}
for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(i))
- if _, ok, _ := list.Find(key); !ok {
+ if _, _, ok, _ := list.Find(key); !ok {
t.Fail()
}
}
@@ -71,7 +71,7 @@ func TestInsertAndFind(t *testing.T) {
for _, e := range rList {
key := []byte(strconv.Itoa(e))
// println("find", e)
- if _, ok, _ := list.Find(key); !ok {
+ if _, _, ok, _ := list.Find(key); !ok {
t.Fail()
}
}
@@ -215,7 +215,7 @@ func TestFindGreaterOrEqual(t *testing.T) {
var listPointer *SkipList
// Test on empty list.
- if _, ok, _ := listPointer.FindGreaterOrEqual(Element(0)); ok {
+ if _, _, ok, _ := listPointer.FindGreaterOrEqual(Element(0)); ok {
t.Fail()
}
@@ -227,7 +227,7 @@ func TestFindGreaterOrEqual(t *testing.T) {
for i := 0; i < maxN; i++ {
key := Element(rand.Intn(maxNumber))
- if v, ok, _ := list.FindGreaterOrEqual(key); ok {
+ if _, v, ok, _ := list.FindGreaterOrEqual(key); ok {
// if f is v should be bigger than the element before
if v.Prev != nil && bytes.Compare(v.Prev.Key, key) >= 0 {
fmt.Printf("PrevV: %s\n key: %s\n\n", string(v.Prev.Key), string(key))
@@ -261,7 +261,7 @@ func TestChangeValue(t *testing.T) {
for i := 0; i < maxN; i++ {
// The key only looks at the int so the string doesn't matter here!
- f1, ok, _ := list.Find(Element(i))
+ _, f1, ok, _ := list.Find(Element(i))
if !ok {
t.Fail()
}
@@ -269,7 +269,7 @@ func TestChangeValue(t *testing.T) {
if err != nil {
t.Fail()
}
- f2, ok, _ := list.Find(Element(i))
+ _, f2, ok, _ := list.Find(Element(i))
if !ok {
t.Fail()
}