Add RWTransaction.Delete().

pull/34/head
Ben Johnson 2014-02-03 14:33:51 -07:00
parent b2a78a2364
commit 0cae98efc5
3 changed files with 43 additions and 5 deletions

View File

@ -191,6 +191,20 @@ func TestDBPutRandom(t *testing.T) {
}
}
// Ensure that a bucket can delete an existing key.
func TestDBDelete(t *testing.T) {
withOpenDB(func(db *DB, path string) {
db.CreateBucket("widgets")
db.Put("widgets", []byte("foo"), []byte("bar"))
err := db.Delete("widgets", []byte("foo"))
assert.NoError(t, err)
value, err := db.Get("widgets", []byte("foo"))
if assert.NoError(t, err) {
assert.Nil(t, value)
}
})
}
// withDB executes a function with a database reference.
func withDB(fn func(*DB, string)) {
f, _ := ioutil.TempFile("", "bolt-")

14
node.go
View File

@ -61,6 +61,20 @@ func (n *node) put(oldKey, newKey, value []byte, pgid pgid) {
inode.pgid = pgid
}
// del removes a key from the node.
func (n *node) del(key []byte) {
// Find index of key.
index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, key) != -1 })
// Exit if the key isn't found.
if !bytes.Equal(n.inodes[index].key, key) {
return
}
// Delete inode from the node.
n.inodes = append(n.inodes[:index], n.inodes[index+1:]...)
}
// read initializes the node from a page.
func (n *node) read(p *page) {
n.pgid = p.id

View File

@ -70,19 +70,29 @@ func (t *RWTransaction) Put(name string, key []byte, value []byte) error {
return &Error{"data too large", nil}
}
// Insert a new node.
// Move cursor to correct position.
c := b.cursor()
c.Get(key)
// Insert the key/value.
t.node(c.stack).put(key, key, value, 0)
return nil
}
func (t *RWTransaction) Delete(name string, key []byte) error {
// TODO: Traverse to the correct node.
// TODO: If missing, exit.
// TODO: Remove node from page.
// TODO: If page is empty then add it to the freelist.
b := t.Bucket(name)
if b == nil {
return &Error{"bucket not found", nil}
}
// Move cursor to correct position.
c := b.cursor()
c.Get(key)
// Delete the node if we have a matching key.
t.node(c.stack).del(key)
return nil
}