Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deletion of non-existing keys #23

Merged
merged 2 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,12 @@ func (b *Bucket) Delete(key []byte) error {

// Move cursor to correct position.
c := b.Cursor()
_, _, flags := c.seek(key)
k, _, flags := c.seek(key)

// Return nil if the key doesn't exist.
if !bytes.Equal(key, k) {
return nil
}

// Return an error if there is already existing bucket value.
if (flags & bucketLeafFlag) != 0 {
Expand Down
33 changes: 33 additions & 0 deletions bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,39 @@ func TestBucket_Delete_FreelistOverflow(t *testing.T) {
}
}

// Ensure that deleting of non-existing key is a no-op.
func TestBucket_Delete_NonExisting(t *testing.T) {
db := MustOpenDB()
defer db.MustClose()

if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
t.Fatal(err)
}

if _, err = b.CreateBucket([]byte("nested")); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}

if err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("widgets"))
if err := b.Delete([]byte("foo")); err != nil {
t.Fatal(err)
}
if b.Bucket([]byte("nested")) == nil {
t.Fatal("nested bucket has been deleted")
}
return nil
}); err != nil {
t.Fatal(err)
}
}

// Ensure that accessing and updating nested buckets is ok across transactions.
func TestBucket_Nested(t *testing.T) {
db := MustOpenDB()
Expand Down