mirror of https://github.com/hak5/bolt.git
Persist sequence-only changes.
This commit fixes a bug where only calling NextSequence() on a Bucket does not cause the Bucket to be peristed. The simple fix is to simply materialize the root node so that the bucket is flushed out during commit. Thanks to Matthew Dawson (@MJDSys) for reporting. https://github.com/boltdb/bolt/issues/296master
parent
b124606822
commit
ac1149a3f5
|
@ -332,6 +332,12 @@ func (b *Bucket) NextSequence() (uint64, error) {
|
|||
return 0, ErrTxNotWritable
|
||||
}
|
||||
|
||||
// Materialize the root node if it hasn't been already so that the
|
||||
// bucket will be saved during commit.
|
||||
if b.rootNode == nil {
|
||||
_ = b.node(b.root, nil)
|
||||
}
|
||||
|
||||
// Increment and return the sequence.
|
||||
b.bucket.sequence++
|
||||
return b.bucket.sequence, nil
|
||||
|
|
|
@ -490,6 +490,33 @@ func TestBucket_NextSequence(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
// Ensure that a bucket will persist an autoincrementing sequence even if its
|
||||
// the only thing updated on the bucket.
|
||||
// https://github.com/boltdb/bolt/issues/296
|
||||
func TestBucket_NextSequence_Persist(t *testing.T) {
|
||||
db := NewTestDB()
|
||||
defer db.Close()
|
||||
db.Update(func(tx *bolt.Tx) error {
|
||||
_, _ = tx.CreateBucket([]byte("widgets"))
|
||||
return nil
|
||||
})
|
||||
|
||||
db.Update(func(tx *bolt.Tx) error {
|
||||
_, _ = tx.Bucket([]byte("widgets")).NextSequence()
|
||||
return nil
|
||||
})
|
||||
|
||||
db.Update(func(tx *bolt.Tx) error {
|
||||
seq, err := tx.Bucket([]byte("widgets")).NextSequence()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
} else if seq != 2 {
|
||||
t.Fatalf("unexpected sequence: %d", seq)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Ensure that retrieving the next sequence on a read-only bucket returns an error.
|
||||
func TestBucket_NextSequence_ReadOnly(t *testing.T) {
|
||||
db := NewTestDB()
|
||||
|
|
9
db.go
9
db.go
|
@ -694,13 +694,8 @@ func _assert(condition bool, msg string, v ...interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
func warn(v ...interface{}) {
|
||||
fmt.Fprintln(os.Stderr, v...)
|
||||
}
|
||||
|
||||
func warnf(msg string, v ...interface{}) {
|
||||
fmt.Fprintf(os.Stderr, msg+"\n", v...)
|
||||
}
|
||||
func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
|
||||
func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }
|
||||
|
||||
func printstack() {
|
||||
stack := strings.Join(strings.Split(string(debug.Stack()), "\n")[2:], "\n")
|
||||
|
|
|
@ -696,3 +696,6 @@ func fileSize(path string) int64 {
|
|||
}
|
||||
return fi.Size()
|
||||
}
|
||||
|
||||
func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
|
||||
func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }
|
||||
|
|
Loading…
Reference in New Issue