Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Commit

Permalink
Persist sequence-only changes.
Browse files Browse the repository at this point in the history
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.

#296
  • Loading branch information
benbjohnson committed Feb 2, 2015
1 parent b124606 commit ac1149a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
6 changes: 6 additions & 0 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 2 additions & 7 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...) }

0 comments on commit ac1149a

Please sign in to comment.