Skip to content

Commit

Permalink
Change Bucket.NextSequence() to return uint64.
Browse files Browse the repository at this point in the history
This commit changes NextSequence() to return a uint64 instead of an int. This also
removes the ErrSequenceOverflow error condition since overflowing a uint64 is unlikely.

Fixes boltdb#39.
  • Loading branch information
benbjohnson committed Jun 22, 2014
1 parent 0a59a75 commit 0f58535
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 35 deletions.
15 changes: 2 additions & 13 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ var (
// on an existing non-bucket key or when trying to create or delete a
// non-bucket key on an existing bucket key.
ErrIncompatibleValue = errors.New("incompatible value")

// ErrSequenceOverflow is returned when the next sequence number will be
// larger than the maximum integer size.
ErrSequenceOverflow = errors.New("sequence overflow")
)

const (
Expand Down Expand Up @@ -336,23 +332,16 @@ func (b *Bucket) Delete(key []byte) error {
}

// NextSequence returns an autoincrementing integer for the bucket.
func (b *Bucket) NextSequence() (int, error) {
func (b *Bucket) NextSequence() (uint64, error) {
if b.tx.db == nil {
return 0, ErrTxClosed
} else if !b.Writable() {
return 0, ErrTxNotWritable
}

// Make sure next sequence number will not be larger than the maximum
// integer size of the system.
if b.bucket.sequence == uint64(maxInt) {
return 0, ErrSequenceOverflow
}

// Increment and return the sequence.
b.bucket.sequence++

return int(b.bucket.sequence), nil
return b.bucket.sequence, nil
}

// ForEach executes a function for each key/value pair in a bucket.
Expand Down
26 changes: 4 additions & 22 deletions bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,15 @@ func TestBucket_NextSequence(t *testing.T) {
// Make sure sequence increments.
seq, err := tx.Bucket([]byte("widgets")).NextSequence()
assert.NoError(t, err)
assert.Equal(t, seq, 1)
assert.Equal(t, seq, uint64(1))
seq, err = tx.Bucket([]byte("widgets")).NextSequence()
assert.NoError(t, err)
assert.Equal(t, seq, 2)
assert.Equal(t, seq, uint64(2))

// Buckets should be separate.
seq, err = tx.Bucket([]byte("woojits")).NextSequence()
assert.NoError(t, err)
assert.Equal(t, seq, 1)
assert.Equal(t, seq, uint64(1))
return nil
})
})
Expand All @@ -475,31 +475,13 @@ func TestBucket_NextSequence_ReadOnly(t *testing.T) {
db.View(func(tx *Tx) error {
b := tx.Bucket([]byte("widgets"))
i, err := b.NextSequence()
assert.Equal(t, i, 0)
assert.Equal(t, i, uint64(0))
assert.Equal(t, err, ErrTxNotWritable)
return nil
})
})
}

// Ensure that incrementing past the maximum sequence number will return an error.
func TestBucket_NextSequence_Overflow(t *testing.T) {
withOpenDB(func(db *DB, path string) {
db.Update(func(tx *Tx) error {
tx.CreateBucket([]byte("widgets"))
return nil
})
db.Update(func(tx *Tx) error {
b := tx.Bucket([]byte("widgets"))
b.bucket.sequence = uint64(maxInt)
seq, err := b.NextSequence()
assert.Equal(t, err, ErrSequenceOverflow)
assert.Equal(t, seq, 0)
return nil
})
})
}

// Ensure that retrieving the next sequence for a bucket on a closed database return an error.
func TestBucket_NextSequence_Closed(t *testing.T) {
withOpenDB(func(db *DB, path string) {
Expand Down

0 comments on commit 0f58535

Please sign in to comment.