mirror of https://github.com/etcd-io/bbolt.git
Add branch.put().
parent
4fb62e8980
commit
ef017f0404
22
branch.go
22
branch.go
|
@ -2,7 +2,6 @@ package bolt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"sort"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,24 +11,23 @@ type branch struct {
|
||||||
items branchItems
|
items branchItems
|
||||||
}
|
}
|
||||||
|
|
||||||
// insert inserts a new item after a given pgid.
|
// put adds a new node or replaces an existing node.
|
||||||
func (b *branch) insert(key []byte, previd pgid, id pgid) {
|
func (b *branch) put(id pgid, newid pgid, key []byte, replace bool) {
|
||||||
// Find previous insertion index.
|
var index int
|
||||||
index := sort.Search(len(b.items), func(i int) bool { return b.items[i].pgid >= previd })
|
for ; index < len(b.items); index++ {
|
||||||
|
if b.items[index].pgid == id {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If there is no existing key then add a new item.
|
if !replace {
|
||||||
|
index++
|
||||||
b.items = append(b.items, branchItem{})
|
b.items = append(b.items, branchItem{})
|
||||||
if index < len(b.items) {
|
if index < len(b.items) {
|
||||||
copy(b.items[index+1:], b.items[index:])
|
copy(b.items[index+1:], b.items[index:])
|
||||||
}
|
}
|
||||||
|
|
||||||
b.items[index].pgid = id
|
|
||||||
b.items[index].key = key
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace swaps out an existing node id for a new one id.
|
|
||||||
func (b *branch) replace(oldid pgid, newid pgid, key []byte) {
|
|
||||||
index := sort.Search(len(b.items), func(i int) bool { return b.items[i].pgid >= oldid })
|
|
||||||
b.items[index].pgid = newid
|
b.items[index].pgid = newid
|
||||||
b.items[index].key = key
|
b.items[index].key = key
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
package bolt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Ensure that a branch can replace a key.
|
||||||
|
func TestBranchPutReplace(t *testing.T) {
|
||||||
|
b := &branch{
|
||||||
|
items: branchItems{
|
||||||
|
branchItem{pgid: 1, key: []byte("bar")},
|
||||||
|
branchItem{pgid: 2, key: []byte("baz")},
|
||||||
|
branchItem{pgid: 3, key: []byte("foo")},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
b.put(1, 4, []byte("bar"), true)
|
||||||
|
b.put(2, 5, []byte("boo"), true)
|
||||||
|
assert.Equal(t, len(b.items), 3)
|
||||||
|
assert.Equal(t, b.items[0].pgid, pgid(4))
|
||||||
|
assert.Equal(t, string(b.items[0].key), "bar")
|
||||||
|
assert.Equal(t, b.items[1].pgid, pgid(5))
|
||||||
|
assert.Equal(t, string(b.items[1].key), "boo")
|
||||||
|
assert.Equal(t, b.items[2].pgid, pgid(3))
|
||||||
|
assert.Equal(t, string(b.items[2].key), "foo")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that a branch can insert a key.
|
||||||
|
func TestBranchPutInsert(t *testing.T) {
|
||||||
|
b := &branch{
|
||||||
|
items: branchItems{
|
||||||
|
branchItem{pgid: 1, key: []byte("bar")},
|
||||||
|
branchItem{pgid: 2, key: []byte("foo")},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
b.put(1, 4, []byte("baz"), false)
|
||||||
|
b.put(2, 5, []byte("zzz"), false)
|
||||||
|
assert.Equal(t, len(b.items), 4)
|
||||||
|
assert.Equal(t, b.items[0].pgid, pgid(1))
|
||||||
|
assert.Equal(t, string(b.items[0].key), "bar")
|
||||||
|
assert.Equal(t, b.items[1].pgid, pgid(4))
|
||||||
|
assert.Equal(t, string(b.items[1].key), "baz")
|
||||||
|
assert.Equal(t, b.items[2].pgid, pgid(2))
|
||||||
|
assert.Equal(t, string(b.items[2].key), "foo")
|
||||||
|
assert.Equal(t, b.items[3].pgid, pgid(5))
|
||||||
|
assert.Equal(t, string(b.items[3].key), "zzz")
|
||||||
|
}
|
Loading…
Reference in New Issue