mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-05-04 05:27:48 +08:00
update value
This commit is contained in:
parent
4f50f8c2ca
commit
d343b0db57
@ -1,5 +1,7 @@
|
|||||||
package skiplist
|
package skiplist
|
||||||
|
|
||||||
|
// adapted from https://github.com/MauriceGit/skiplist/blob/master/skiplist.go
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -402,6 +404,17 @@ func (t *SkipList) Prev(e *SkipListElement) *SkipListElement {
|
|||||||
return e.Prev.Load()
|
return e.Prev.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func (t *SkipList) ChangeValue(e *SkipListElement, newValue []byte) (ok bool) {
|
||||||
|
// The key needs to stay correct, so this is very important!
|
||||||
|
e.Value = newValue
|
||||||
|
e.Save()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// String returns a string format of the skiplist. Useful to get a graphical overview and/or debugging.
|
// String returns a string format of the skiplist. Useful to get a graphical overview and/or debugging.
|
||||||
func (t *SkipList) println() {
|
func (t *SkipList) println() {
|
||||||
|
|
||||||
|
@ -243,3 +243,30 @@ func TestFindGreaterOrEqual(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestChangeValue(t *testing.T) {
|
||||||
|
list := New()
|
||||||
|
|
||||||
|
for i := 0; i < maxN; i++ {
|
||||||
|
list.Insert(Element(i), []byte("value"))
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
if !ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
ok = list.ChangeValue(f1, []byte("different value"))
|
||||||
|
if !ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
f2, ok := list.Find(Element(i))
|
||||||
|
if !ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if bytes.Compare(f2.GetValue(), []byte("different value")) != 0 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user