Skip to content

Commit

Permalink
moved all errors to new hare_err package
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamey Cribbs committed Jan 16, 2021
1 parent 9e3f1c0 commit 758918b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 66 deletions.
20 changes: 8 additions & 12 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ package hare

import (
"encoding/json"
"errors"
"sync"
)

var (
ErrNoTable = errors.New("hare: no table with that name found")
ErrTableExists = errors.New("hare: table with that name already exists")
"github.com/jameycribbs/hare/hare_err"
)

type Record interface {
Expand Down Expand Up @@ -79,7 +75,7 @@ func (db *Database) Close() error {

func (db *Database) CreateTable(tableName string) error {
if db.TableExists(tableName) {
return ErrTableExists
return hare_err.TableExists
}

if err := db.store.CreateTable(tableName); err != nil {
Expand All @@ -99,7 +95,7 @@ func (db *Database) CreateTable(tableName string) error {

func (db *Database) Delete(tableName string, id int) error {
if !db.TableExists(tableName) {
return ErrNoTable
return hare_err.NoTable
}

db.locks[tableName].Lock()
Expand All @@ -114,7 +110,7 @@ func (db *Database) Delete(tableName string, id int) error {

func (db *Database) DropTable(tableName string) error {
if !db.TableExists(tableName) {
return ErrNoTable
return hare_err.NoTable
}

db.locks[tableName].Lock()
Expand All @@ -135,7 +131,7 @@ func (db *Database) DropTable(tableName string) error {

func (db *Database) Find(tableName string, id int, rec Record) error {
if !db.TableExists(tableName) {
return ErrNoTable
return hare_err.NoTable
}

db.locks[tableName].RLock()
Expand All @@ -158,7 +154,7 @@ func (db *Database) Find(tableName string, id int, rec Record) error {

func (db *Database) IDs(tableName string) ([]int, error) {
if !db.TableExists(tableName) {
return nil, ErrNoTable
return nil, hare_err.NoTable
}

db.locks[tableName].Lock()
Expand All @@ -174,7 +170,7 @@ func (db *Database) IDs(tableName string) ([]int, error) {

func (db *Database) Insert(tableName string, rec Record) (int, error) {
if !db.TableExists(tableName) {
return 0, ErrNoTable
return 0, hare_err.NoTable
}

db.locks[tableName].Lock()
Expand Down Expand Up @@ -203,7 +199,7 @@ func (db *Database) TableExists(tableName string) bool {

func (db *Database) Update(tableName string, rec Record) error {
if !db.TableExists(tableName) {
return ErrNoTable
return hare_err.NoTable
}

db.locks[tableName].Lock()
Expand Down
19 changes: 10 additions & 9 deletions database_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/jameycribbs/hare/datastores/disk"
"github.com/jameycribbs/hare/hare_err"
)

func TestAllDatabaseDiskTests(t *testing.T) {
Expand Down Expand Up @@ -40,7 +41,7 @@ func TestAllDatabaseDiskTests(t *testing.T) {
db := newTestDatabaseDisk(t)
db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
gotErr := db.Find("contacts", 3, &Contact{})

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -86,7 +87,7 @@ func TestAllDatabaseDiskTests(t *testing.T) {
db := newTestDatabaseDisk(t)
defer db.Close()

wantErr := ErrTableExists
wantErr := hare_err.TableExists
gotErr := db.CreateTable("contacts")

if !errors.Is(gotErr, wantErr) {
Expand All @@ -104,7 +105,7 @@ func TestAllDatabaseDiskTests(t *testing.T) {
t.Fatal(err)
}

wantErr := disk.ErrNoRecord
wantErr := hare_err.NoRecord
gotErr := db.Find("contacts", 3, &Contact{})

if !errors.Is(gotErr, wantErr) {
Expand All @@ -117,7 +118,7 @@ func TestAllDatabaseDiskTests(t *testing.T) {
db := newTestDatabaseDisk(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
gotErr := db.Delete("nonexistent", 3)

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestAllDatabaseDiskTests(t *testing.T) {
db := newTestDatabaseDisk(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
gotErr := db.DropTable("nonexistent")

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -181,7 +182,7 @@ func TestAllDatabaseDiskTests(t *testing.T) {
db := newTestDatabaseDisk(t)
defer db.Close()

wantErr := disk.ErrNoRecord
wantErr := hare_err.NoRecord
gotErr := db.Find("contacts", 5, &Contact{})

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -219,7 +220,7 @@ func TestAllDatabaseDiskTests(t *testing.T) {
db := newTestDatabaseDisk(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
_, gotErr := db.IDs("nonexistent")

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -262,7 +263,7 @@ func TestAllDatabaseDiskTests(t *testing.T) {
db := newTestDatabaseDisk(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
_, gotErr := db.Insert("nonexistent", &Contact{FirstName: "Robin", LastName: "Williams", Age: 88})

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -320,7 +321,7 @@ func TestAllDatabaseDiskTests(t *testing.T) {
db := newTestDatabaseDisk(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
gotErr := db.Update("nonexistent", &Contact{ID: 4, FirstName: "Hazel", LastName: "Koller", Age: 26})

if !errors.Is(gotErr, wantErr) {
Expand Down
20 changes: 10 additions & 10 deletions database_ram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strconv"
"testing"

"github.com/jameycribbs/hare/datastores/disk"
"github.com/jameycribbs/hare/datastores/ram"
"github.com/jameycribbs/hare/hare_err"
)

func TestAllDatabaseRamTests(t *testing.T) {
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestAllDatabaseRamTests(t *testing.T) {
db := newTestDatabaseRam(t)
db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
gotErr := db.Find("contacts", 3, &Contact{})

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestAllDatabaseRamTests(t *testing.T) {
db := newTestDatabaseRam(t)
defer db.Close()

wantErr := ErrTableExists
wantErr := hare_err.TableExists
gotErr := db.CreateTable("contacts")

if !errors.Is(gotErr, wantErr) {
Expand All @@ -103,7 +103,7 @@ func TestAllDatabaseRamTests(t *testing.T) {
t.Fatal(err)
}

wantErr := disk.ErrNoRecord
wantErr := hare_err.NoRecord
gotErr := db.Find("contacts", 3, &Contact{})

if !errors.Is(gotErr, wantErr) {
Expand All @@ -116,7 +116,7 @@ func TestAllDatabaseRamTests(t *testing.T) {
db := newTestDatabaseRam(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
gotErr := db.Delete("nonexistent", 3)

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestAllDatabaseRamTests(t *testing.T) {
db := newTestDatabaseRam(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
gotErr := db.DropTable("nonexistent")

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestAllDatabaseRamTests(t *testing.T) {
db := newTestDatabaseRam(t)
defer db.Close()

wantErr := disk.ErrNoRecord
wantErr := hare_err.NoRecord
gotErr := db.Find("contacts", 5, &Contact{})

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestAllDatabaseRamTests(t *testing.T) {
db := newTestDatabaseRam(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
_, gotErr := db.IDs("nonexistent")

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestAllDatabaseRamTests(t *testing.T) {
db := newTestDatabaseRam(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
_, gotErr := db.Insert("nonexistent", &Contact{FirstName: "Robin", LastName: "Williams", Age: 88})

if !errors.Is(gotErr, wantErr) {
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestAllDatabaseRamTests(t *testing.T) {
db := newTestDatabaseRam(t)
defer db.Close()

wantErr := ErrNoTable
wantErr := hare_err.NoTable
gotErr := db.Update("nonexistent", &Contact{ID: 4, FirstName: "Hazel", LastName: "Koller", Age: 26})

if !errors.Is(gotErr, wantErr) {
Expand Down
15 changes: 5 additions & 10 deletions datastores/disk/disk.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package disk

import (
"errors"
"io/ioutil"
"os"
"strings"
)

var (
ErrNoTable = errors.New("disk: table with that name does not exist")
ErrTableExists = errors.New("disk: table with that name already exists")
ErrIDExists = errors.New("disk: record with that id already exists")
"github.com/jameycribbs/hare/hare_err"
)

type Disk struct {
Expand Down Expand Up @@ -48,7 +43,7 @@ func (dsk *Disk) Close() error {

func (dsk *Disk) CreateTable(tableName string) error {
if dsk.TableExists(tableName) {
return ErrTableExists
return hare_err.TableExists
}

filePtr, err := dsk.openFile(tableName, true)
Expand Down Expand Up @@ -106,7 +101,7 @@ func (dsk *Disk) InsertRec(tableName string, id int, rec []byte) error {
ids := tableFile.ids()
for _, i := range ids {
if id == i {
return ErrIDExists
return hare_err.IDExists
}
}

Expand Down Expand Up @@ -191,7 +186,7 @@ func (dsk *Disk) UpdateRec(tableName string, id int, rec []byte) error {
func (dsk *Disk) getTableFile(tableName string) (*tableFile, error) {
tableFile, ok := dsk.tableFiles[tableName]
if !ok {
return nil, ErrNoTable
return nil, hare_err.NoTable
}

return tableFile, nil
Expand Down Expand Up @@ -268,7 +263,7 @@ func (dsk Disk) openFile(tableName string, createIfNeeded bool) (*os.File, error
func (dsk *Disk) closeTable(tableName string) error {
tableFile, ok := dsk.tableFiles[tableName]
if !ok {
return ErrNoTable
return hare_err.NoTable
}

if err := tableFile.close(); err != nil {
Expand Down
13 changes: 5 additions & 8 deletions datastores/disk/table_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package disk
import (
"bufio"
"encoding/json"
"errors"
"io"
"os"

"github.com/jameycribbs/hare/hare_err"
)

const dummyRune = 'X'

var (
ErrNoRecord = errors.New("hare: no record with that id found")
)

type tableFile struct {
ptr *os.File
offsets map[int]int64
Expand Down Expand Up @@ -80,7 +77,7 @@ func (t *tableFile) close() error {
func (t *tableFile) deleteRec(id int) error {
offset, ok := t.offsets[id]
if !ok {
return ErrNoRecord
return hare_err.NoRecord
}

rec, err := t.readRec(id)
Expand Down Expand Up @@ -207,7 +204,7 @@ func (t *tableFile) overwriteRec(offset int64, recLen int) error {
func (t *tableFile) readRec(id int) ([]byte, error) {
offset, ok := t.offsets[id]
if !ok {
return nil, ErrNoRecord
return nil, hare_err.NoRecord
}

r := bufio.NewReader(t.ptr)
Expand All @@ -229,7 +226,7 @@ func (t *tableFile) updateRec(id int, rec []byte) error {

oldRecOffset, ok := t.offsets[id]
if !ok {
return ErrNoRecord
return hare_err.NoRecord
}

oldRec, err := t.readRec(id)
Expand Down
Loading

0 comments on commit 758918b

Please sign in to comment.