-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Cursor inconsistent when mixing cursor.Delete() with Put() in same transaction #357
Comments
My first thought when looking at this was the bit in the docs about cursors:
So, I changed c := b.Cursor()
for key, _ := c.First(); key != nil; key, _ = c.First() {
if err := c.Delete(); err != nil {
return err
}
} But then, I split it into two updates and kept the db.Update(func(tx *bolt.Tx) error {
b, _ := tx.CreateBucket([]byte("widgets"))
for i := 0; i < count; i++ {
k := make([]byte, 8)
binary.BigEndian.PutUint64(k, uint64(i))
b.Put(k, make([]byte, 100))
}
return nil
})
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("widgets"))
c := b.Cursor()
for key, _ := c.First(); key != nil; key, _ = c.Next() {
if err := c.Delete(); err != nil {
return err
}
}
return nil
}) Now I am somewhat confused. Curious to learn what's going on. |
@thatguystone thanks for submitting the issue. I've been out of town and couldn't respond. @alaska is correct with his reference to the docs. The issue is that the When you call The alternative is to have the |
Oh, sorry, I misread the code. I didn't realize you were already using |
This one is probably best described with a (failing) test case:
It should delete all of the inserted keys, but instead, the following keys remain:
1, 3, 5, 7, 9
.This also seems to be the case when there's a pre-existing set of keys and a single key is put (for example):
The text was updated successfully, but these errors were encountered: