Use pre-defined errors as they are comparable and useful as exported symbols

This commit is contained in:
James Mills 2019-03-20 07:39:03 +10:00
parent 65e7877bdf
commit d8a48f9eea
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
2 changed files with 20 additions and 12 deletions

@ -1,7 +1,7 @@
package bitcask
import (
"fmt"
"errors"
"hash/crc32"
"io"
"io/ioutil"
@ -15,6 +15,14 @@ import (
"github.com/prologic/bitcask/internal"
)
var (
ErrKeyNotFound = errors.New("error: key not found")
ErrKeyTooLarge = errors.New("error: key too large")
ErrValueTooLarge = errors.New("error: value too large")
ErrChecksumFailed = errors.New("error: checksum failed")
ErrDatabaseLocked = errors.New("error: database locked")
)
type Bitcask struct {
*flock.Flock
@ -49,7 +57,7 @@ func (b *Bitcask) Get(key string) ([]byte, error) {
item, ok := b.keydir.Get(key)
if !ok {
return nil, fmt.Errorf("error: key not found %s", key)
return nil, ErrKeyNotFound
}
if item.FileID == b.curr.FileID() {
@ -65,7 +73,7 @@ func (b *Bitcask) Get(key string) ([]byte, error) {
checksum := crc32.ChecksumIEEE(e.Value)
if checksum != e.Checksum {
return nil, fmt.Errorf("error: checksum falied %s %d != %d", key, e.Checksum, checksum)
return nil, ErrChecksumFailed
}
return e.Value, nil
@ -73,10 +81,10 @@ func (b *Bitcask) Get(key string) ([]byte, error) {
func (b *Bitcask) Put(key string, value []byte) error {
if len(key) > b.config.MaxKeySize {
return fmt.Errorf("error: key too large %d > %d", len(key), b.config.MaxKeySize)
return ErrKeyTooLarge
}
if len(value) > b.config.MaxValueSize {
return fmt.Errorf("error: value too large %d > %d", len(value), b.config.MaxValueSize)
return ErrValueTooLarge
}
offset, err := b.put(key, value)
@ -369,7 +377,7 @@ func Open(path string, options ...option) (*Bitcask, error) {
}
if !locked {
return nil, fmt.Errorf("error: database locked %s", path)
return nil, ErrDatabaseLocked
}
return bitcask, nil

@ -45,7 +45,7 @@ func TestAll(t *testing.T) {
assert.NoError(err)
_, err = db.Get("foo")
assert.Error(err)
assert.Equal("error: key not found foo", err.Error())
assert.Equal(ErrKeyNotFound, err)
})
t.Run("Sync", func(t *testing.T) {
@ -92,7 +92,7 @@ func TestDeletedKeys(t *testing.T) {
assert.NoError(err)
_, err = db.Get("foo")
assert.Error(err)
assert.Equal("error: key not found foo", err.Error())
assert.Equal(ErrKeyNotFound, err)
})
t.Run("Sync", func(t *testing.T) {
@ -120,7 +120,7 @@ func TestDeletedKeys(t *testing.T) {
t.Run("Get", func(t *testing.T) {
_, err = db.Get("foo")
assert.Error(err)
assert.Equal("error: key not found foo", err.Error())
assert.Equal(ErrKeyNotFound, err)
})
t.Run("Close", func(t *testing.T) {
@ -148,7 +148,7 @@ func TestMaxKeySize(t *testing.T) {
value := []byte("foobar")
err = db.Put(key, value)
assert.Error(err)
assert.Equal("error: key too large 17 > 16", err.Error())
assert.Equal(ErrKeyTooLarge, err)
})
}
@ -170,7 +170,7 @@ func TestMaxValueSize(t *testing.T) {
value := []byte(strings.Repeat(" ", 17))
err = db.Put(key, value)
assert.Error(err)
assert.Equal("error: value too large 17 > 16", err.Error())
assert.Equal(ErrValueTooLarge, err)
})
}
@ -387,7 +387,7 @@ func TestLocking(t *testing.T) {
_, err = Open(testdir)
assert.Error(err)
assert.Equal(fmt.Sprintf("error: database locked %s", testdir), err.Error())
assert.Equal(ErrDatabaseLocked, err)
}
type benchmarkTestCase struct {