From d8a48f9eeaaa820a6e985e509d023cc05ae0702f Mon Sep 17 00:00:00 2001 From: James Mills <1290234+prologic@users.noreply.github.com> Date: Wed, 20 Mar 2019 07:39:03 +1000 Subject: [PATCH] Use pre-defined errors as they are comparable and useful as exported symbols --- bitcask.go | 20 ++++++++++++++------ bitcask_test.go | 12 ++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/bitcask.go b/bitcask.go index 6d85889..72bd560 100644 --- a/bitcask.go +++ b/bitcask.go @@ -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 diff --git a/bitcask_test.go b/bitcask_test.go index f3c584e..d8e8937 100644 --- a/bitcask_test.go +++ b/bitcask_test.go @@ -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 {