Improve interfaces and fix more linter errors

This commit is contained in:
Daniel 2020-09-24 09:50:38 +02:00
parent 5bb73a7b4c
commit 362539692e
11 changed files with 55 additions and 69 deletions

View file

@ -134,7 +134,6 @@ func registerAsDatabase() error {
Name: "config", Name: "config",
Description: "Configuration Manager", Description: "Configuration Manager",
StorageType: "injected", StorageType: "injected",
PrimaryAPI: "",
}) })
if err != nil { if err != nil {
return err return err

View file

@ -247,8 +247,8 @@ func (c *Controller) Maintain(ctx context.Context) error {
return nil return nil
} }
if maintenance, ok := c.storage.(storage.Maintenance); ok { if maintainer, ok := c.storage.(storage.Maintainer); ok {
return maintenance.Maintain(ctx) return maintainer.Maintain(ctx)
} }
return nil return nil
} }
@ -262,8 +262,8 @@ func (c *Controller) MaintainThorough(ctx context.Context) error {
return nil return nil
} }
if maintenance, ok := c.storage.(storage.Maintenance); ok { if maintainer, ok := c.storage.(storage.Maintainer); ok {
return maintenance.MaintainThorough(ctx) return maintainer.MaintainThorough(ctx)
} }
return nil return nil
} }
@ -277,10 +277,7 @@ func (c *Controller) MaintainRecordStates(ctx context.Context, purgeDeletedBefor
return nil return nil
} }
if maintenance, ok := c.storage.(storage.Maintenance); ok { return c.storage.MaintainRecordStates(ctx, purgeDeletedBefore)
return maintenance.MaintainRecordStates(ctx, purgeDeletedBefore)
}
return nil
} }
// Shutdown shuts down the storage. // Shutdown shuts down the storage.

View file

@ -11,6 +11,13 @@ import (
"github.com/safing/portbase/database/query" "github.com/safing/portbase/database/query"
"github.com/safing/portbase/database/record" "github.com/safing/portbase/database/record"
"github.com/safing/portbase/database/storage"
)
var (
// Compile time interface checks.
_ storage.Interface = &Badger{}
_ storage.Maintainer = &Badger{}
) )
type TestRecord struct { type TestRecord struct {
@ -117,14 +124,19 @@ func TestBadger(t *testing.T) {
} }
// maintenance // maintenance
err = db.Maintain(context.TODO()) maintainer, ok := db.(storage.Maintainer)
if ok {
err = maintainer.Maintain(context.TODO())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = db.MaintainThorough(context.TODO()) err = maintainer.MaintainThorough(context.TODO())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} else {
t.Fatal("should implement Maintainer")
}
// shutdown // shutdown
err = db.Shutdown() err = db.Shutdown()

View file

@ -245,16 +245,6 @@ func (b *BBolt) Injected() bool {
return false return false
} }
// Maintain runs a light maintenance operation on the database.
func (b *BBolt) Maintain(_ context.Context) error {
return nil
}
// MaintainThorough runs a thorough maintenance operation on the database.
func (b *BBolt) MaintainThorough(_ context.Context) error {
return nil
}
// MaintainRecordStates maintains records states in the database. // MaintainRecordStates maintains records states in the database.
func (b *BBolt) MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error { func (b *BBolt) MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error {
now := time.Now().Unix() now := time.Now().Unix()

View file

@ -12,6 +12,13 @@ import (
"github.com/safing/portbase/database/query" "github.com/safing/portbase/database/query"
"github.com/safing/portbase/database/record" "github.com/safing/portbase/database/record"
"github.com/safing/portbase/database/storage"
)
var (
// Compile time interface checks.
_ storage.Interface = &BBolt{}
_ storage.Batcher = &BBolt{}
) )
type TestRecord struct { type TestRecord struct {
@ -146,14 +153,6 @@ func TestBBolt(t *testing.T) {
} }
// maintenance // maintenance
err = db.Maintain(context.TODO())
if err != nil {
t.Fatal(err)
}
err = db.MaintainThorough(context.TODO())
if err != nil {
t.Fatal(err)
}
err = db.MaintainRecordStates(context.TODO(), time.Now()) err = db.MaintainRecordStates(context.TODO(), time.Now())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -255,16 +255,6 @@ func (fst *FSTree) Injected() bool {
return false return false
} }
// Maintain runs a light maintenance operation on the database.
func (fst *FSTree) Maintain(_ context.Context) error {
return nil
}
// MaintainThorough runs a thorough maintenance operation on the database.
func (fst *FSTree) MaintainThorough(_ context.Context) error {
return nil
}
// MaintainRecordStates maintains records states in the database. // MaintainRecordStates maintains records states in the database.
func (fst *FSTree) MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error { func (fst *FSTree) MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error {
// TODO: implement MaintainRecordStates // TODO: implement MaintainRecordStates
@ -279,6 +269,7 @@ func (fst *FSTree) Shutdown() error {
// writeFile mirrors ioutil.WriteFile, replacing an existing file with the same // writeFile mirrors ioutil.WriteFile, replacing an existing file with the same
// name atomically. This is not atomic on Windows, but still an improvement. // name atomically. This is not atomic on Windows, but still an improvement.
// TODO: Replace with github.com/google/renamio.WriteFile as soon as it is fixed on Windows. // TODO: Replace with github.com/google/renamio.WriteFile as soon as it is fixed on Windows.
// TODO: This has become a wont-fix. Explore other options.
// This function is forked from https://github.com/google/renameio/blob/a368f9987532a68a3d676566141654a81aa8100b/writefile.go. // This function is forked from https://github.com/google/renameio/blob/a368f9987532a68a3d676566141654a81aa8100b/writefile.go.
func writeFile(filename string, data []byte, perm os.FileMode) error { func writeFile(filename string, data []byte, perm os.FileMode) error {
t, err := renameio.TempFile("", filename) t, err := renameio.TempFile("", filename)

View file

@ -0,0 +1,8 @@
package fstree
import "github.com/safing/portbase/database/storage"
var (
// Compile time interface checks.
_ storage.Interface = &FSTree{}
)

View file

@ -150,16 +150,6 @@ func (hm *HashMap) Injected() bool {
return false return false
} }
// Maintain runs a light maintenance operation on the database.
func (hm *HashMap) Maintain(_ context.Context) error {
return nil
}
// MaintainThorough runs a thorough maintenance operation on the database.
func (hm *HashMap) MaintainThorough(_ context.Context) error {
return nil
}
// MaintainRecordStates maintains records states in the database. // MaintainRecordStates maintains records states in the database.
func (hm *HashMap) MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error { func (hm *HashMap) MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error {
hm.dbLock.Lock() hm.dbLock.Lock()

View file

@ -2,15 +2,22 @@
package hashmap package hashmap
import ( import (
"context"
"reflect" "reflect"
"sync" "sync"
"testing" "testing"
"github.com/safing/portbase/database/storage"
"github.com/safing/portbase/database/query" "github.com/safing/portbase/database/query"
"github.com/safing/portbase/database/record" "github.com/safing/portbase/database/record"
) )
var (
// Compile time interface checks.
_ storage.Interface = &HashMap{}
_ storage.Batcher = &HashMap{}
)
type TestRecord struct { type TestRecord struct {
record.Base record.Base
sync.Mutex sync.Mutex
@ -130,16 +137,6 @@ func TestHashMap(t *testing.T) {
t.Fatal("should fail") t.Fatal("should fail")
} }
// maintenance
err = db.Maintain(context.TODO())
if err != nil {
t.Fatal(err)
}
err = db.MaintainThorough(context.TODO())
if err != nil {
t.Fatal(err)
}
// shutdown // shutdown
err = db.Shutdown() err = db.Shutdown()
if err != nil { if err != nil {

View file

@ -11,21 +11,25 @@ import (
// Interface defines the database storage API. // Interface defines the database storage API.
type Interface interface { type Interface interface {
// Primary Interface
Get(key string) (record.Record, error) Get(key string) (record.Record, error)
Put(m record.Record) (record.Record, error) Put(m record.Record) (record.Record, error)
Delete(key string) error Delete(key string) error
Query(q *query.Query, local, internal bool) (*iterator.Iterator, error) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error)
// Information and Control
ReadOnly() bool ReadOnly() bool
Injected() bool Injected() bool
Shutdown() error Shutdown() error
// Mandatory Record Maintenance
MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error
} }
// Maintenance defines the database storage API for backends that requ // Maintainer defines the database storage API for backends that require regular maintenance.
type Maintenance interface { type Maintainer interface {
Maintain(ctx context.Context) error Maintain(ctx context.Context) error
MaintainThorough(ctx context.Context) error MaintainThorough(ctx context.Context) error
MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error
} }
// Batcher defines the database storage API for backends that support batch operations. // Batcher defines the database storage API for backends that support batch operations.

View file

@ -48,7 +48,6 @@ func registerAsDatabase() error {
Name: "notifications", Name: "notifications",
Description: "Notifications", Description: "Notifications",
StorageType: "injected", StorageType: "injected",
PrimaryAPI: "",
}) })
if err != nil { if err != nil {
return err return err