Merge pull request #297 from benbjohnson/seq

Persist sequence-only changes
pull/34/head
Ben Johnson 2015-02-02 09:11:18 -07:00
commit b8dbe1101d
4 changed files with 38 additions and 7 deletions

View File

@ -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

View File

@ -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
View File

@ -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")

View File

@ -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...) }