diff --git a/database.go b/database.go index 8db6782..84a2cb9 100755 --- a/database.go +++ b/database.go @@ -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 { @@ -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 { @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() diff --git a/database_disk_test.go b/database_disk_test.go index 834af22..652e1b9 100755 --- a/database_disk_test.go +++ b/database_disk_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/jameycribbs/hare/datastores/disk" + "github.com/jameycribbs/hare/hare_err" ) func TestAllDatabaseDiskTests(t *testing.T) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { diff --git a/database_ram_test.go b/database_ram_test.go index 148c560..e4ce8a7 100755 --- a/database_ram_test.go +++ b/database_ram_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { diff --git a/datastores/disk/disk.go b/datastores/disk/disk.go index 198f76e..51e4ef3 100755 --- a/datastores/disk/disk.go +++ b/datastores/disk/disk.go @@ -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 { @@ -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) @@ -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 } } @@ -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 @@ -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 { diff --git a/datastores/disk/table_file.go b/datastores/disk/table_file.go index f3e3888..a3c1845 100755 --- a/datastores/disk/table_file.go +++ b/datastores/disk/table_file.go @@ -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 @@ -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) @@ -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) @@ -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) diff --git a/datastores/ram/ram.go b/datastores/ram/ram.go index 87723d6..0f1a62a 100755 --- a/datastores/ram/ram.go +++ b/datastores/ram/ram.go @@ -1,15 +1,6 @@ package ram -import ( - "errors" -) - -var ( - ErrNoTable = errors.New("hare: table with that name does not exist") - ErrTableExists = errors.New("hare: table with that name already exists") - ErrNoRecord = errors.New("hare: record with that id does not exist") - ErrRecordExists = errors.New("hare: record with that id already exists") -) +import "github.com/jameycribbs/hare/hare_err" type Ram struct { tables map[string]*table @@ -33,7 +24,7 @@ func (ram *Ram) Close() error { func (ram *Ram) CreateTable(tableName string) error { if ram.TableExists(tableName) { - return ErrTableExists + return hare_err.TableExists } ram.tables[tableName] = newTable() @@ -79,7 +70,7 @@ func (ram *Ram) InsertRec(tableName string, id int, rec []byte) error { } if table.recExists(id) { - return ErrRecordExists + return hare_err.IDExists } table.writeRec(id, rec) @@ -103,7 +94,7 @@ func (ram *Ram) ReadRec(tableName string, id int) ([]byte, error) { func (ram *Ram) RemoveTable(tableName string) error { if !ram.TableExists(tableName) { - return ErrNoTable + return hare_err.NoTable } delete(ram.tables, tableName) @@ -134,7 +125,7 @@ func (ram *Ram) UpdateRec(tableName string, id int, rec []byte) error { } if !table.recExists(id) { - return ErrNoRecord + return hare_err.NoRecord } table.writeRec(id, rec) @@ -149,7 +140,7 @@ func (ram *Ram) UpdateRec(tableName string, id int, rec []byte) error { func (ram *Ram) getTable(tableName string) (*table, error) { table, ok := ram.tables[tableName] if !ok { - return nil, ErrNoTable + return nil, hare_err.NoTable } return table, nil diff --git a/datastores/ram/table.go b/datastores/ram/table.go index 5ea70c9..54dae38 100755 --- a/datastores/ram/table.go +++ b/datastores/ram/table.go @@ -1,5 +1,7 @@ package ram +import "github.com/jameycribbs/hare/hare_err" + type table struct { records map[int][]byte } @@ -14,7 +16,7 @@ func newTable() *table { func (t *table) deleteRec(id int) error { if !t.recExists(id) { - return ErrNoRecord + return hare_err.NoRecord } delete(t.records, id) @@ -49,7 +51,7 @@ func (t *table) ids() []int { func (t *table) readRec(id int) ([]byte, error) { rec, ok := t.records[id] if !ok { - return nil, ErrNoRecord + return nil, hare_err.NoRecord } return rec, nil diff --git a/hare_err/hare_err.go b/hare_err/hare_err.go new file mode 100755 index 0000000..b4e8e13 --- /dev/null +++ b/hare_err/hare_err.go @@ -0,0 +1,10 @@ +package hare_err + +import "errors" + +var ( + IDExists = errors.New("hare: record with that id already exists") + NoRecord = errors.New("hare: no record with that id found") + NoTable = errors.New("hare: table with that name does not exist") + TableExists = errors.New("hare: table with that name already exists") +)