mirror of
https://github.com/safing/portbase
synced 2025-09-16 18:19:50 +00:00
Improve interfaces and fix more linter errors
This commit is contained in:
parent
5bb73a7b4c
commit
362539692e
11 changed files with 55 additions and 69 deletions
|
@ -134,7 +134,6 @@ func registerAsDatabase() error {
|
|||
Name: "config",
|
||||
Description: "Configuration Manager",
|
||||
StorageType: "injected",
|
||||
PrimaryAPI: "",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -247,8 +247,8 @@ func (c *Controller) Maintain(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
if maintenance, ok := c.storage.(storage.Maintenance); ok {
|
||||
return maintenance.Maintain(ctx)
|
||||
if maintainer, ok := c.storage.(storage.Maintainer); ok {
|
||||
return maintainer.Maintain(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -262,8 +262,8 @@ func (c *Controller) MaintainThorough(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
if maintenance, ok := c.storage.(storage.Maintenance); ok {
|
||||
return maintenance.MaintainThorough(ctx)
|
||||
if maintainer, ok := c.storage.(storage.Maintainer); ok {
|
||||
return maintainer.MaintainThorough(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -277,10 +277,7 @@ func (c *Controller) MaintainRecordStates(ctx context.Context, purgeDeletedBefor
|
|||
return nil
|
||||
}
|
||||
|
||||
if maintenance, ok := c.storage.(storage.Maintenance); ok {
|
||||
return maintenance.MaintainRecordStates(ctx, purgeDeletedBefore)
|
||||
}
|
||||
return nil
|
||||
return c.storage.MaintainRecordStates(ctx, purgeDeletedBefore)
|
||||
}
|
||||
|
||||
// Shutdown shuts down the storage.
|
||||
|
|
|
@ -11,6 +11,13 @@ import (
|
|||
|
||||
"github.com/safing/portbase/database/query"
|
||||
"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 {
|
||||
|
@ -117,14 +124,19 @@ func TestBadger(t *testing.T) {
|
|||
}
|
||||
|
||||
// maintenance
|
||||
err = db.Maintain(context.TODO())
|
||||
maintainer, ok := db.(storage.Maintainer)
|
||||
if ok {
|
||||
err = maintainer.Maintain(context.TODO())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = db.MaintainThorough(context.TODO())
|
||||
err = maintainer.MaintainThorough(context.TODO())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
t.Fatal("should implement Maintainer")
|
||||
}
|
||||
|
||||
// shutdown
|
||||
err = db.Shutdown()
|
||||
|
|
|
@ -245,16 +245,6 @@ func (b *BBolt) Injected() bool {
|
|||
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.
|
||||
func (b *BBolt) MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error {
|
||||
now := time.Now().Unix()
|
||||
|
|
|
@ -12,6 +12,13 @@ import (
|
|||
|
||||
"github.com/safing/portbase/database/query"
|
||||
"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 {
|
||||
|
@ -146,14 +153,6 @@ func TestBBolt(t *testing.T) {
|
|||
}
|
||||
|
||||
// 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())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
@ -255,16 +255,6 @@ func (fst *FSTree) Injected() bool {
|
|||
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.
|
||||
func (fst *FSTree) MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error {
|
||||
// TODO: implement MaintainRecordStates
|
||||
|
@ -279,6 +269,7 @@ func (fst *FSTree) Shutdown() error {
|
|||
// writeFile mirrors ioutil.WriteFile, replacing an existing file with the same
|
||||
// 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: This has become a wont-fix. Explore other options.
|
||||
// This function is forked from https://github.com/google/renameio/blob/a368f9987532a68a3d676566141654a81aa8100b/writefile.go.
|
||||
func writeFile(filename string, data []byte, perm os.FileMode) error {
|
||||
t, err := renameio.TempFile("", filename)
|
||||
|
|
8
database/storage/fstree/fstree_test.go
Normal file
8
database/storage/fstree/fstree_test.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package fstree
|
||||
|
||||
import "github.com/safing/portbase/database/storage"
|
||||
|
||||
var (
|
||||
// Compile time interface checks.
|
||||
_ storage.Interface = &FSTree{}
|
||||
)
|
|
@ -150,16 +150,6 @@ func (hm *HashMap) Injected() bool {
|
|||
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.
|
||||
func (hm *HashMap) MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error {
|
||||
hm.dbLock.Lock()
|
||||
|
|
|
@ -2,15 +2,22 @@
|
|||
package hashmap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/safing/portbase/database/storage"
|
||||
|
||||
"github.com/safing/portbase/database/query"
|
||||
"github.com/safing/portbase/database/record"
|
||||
)
|
||||
|
||||
var (
|
||||
// Compile time interface checks.
|
||||
_ storage.Interface = &HashMap{}
|
||||
_ storage.Batcher = &HashMap{}
|
||||
)
|
||||
|
||||
type TestRecord struct {
|
||||
record.Base
|
||||
sync.Mutex
|
||||
|
@ -130,16 +137,6 @@ func TestHashMap(t *testing.T) {
|
|||
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
|
||||
err = db.Shutdown()
|
||||
if err != nil {
|
||||
|
|
|
@ -11,21 +11,25 @@ import (
|
|||
|
||||
// Interface defines the database storage API.
|
||||
type Interface interface {
|
||||
// Primary Interface
|
||||
Get(key string) (record.Record, error)
|
||||
Put(m record.Record) (record.Record, error)
|
||||
Delete(key string) error
|
||||
Query(q *query.Query, local, internal bool) (*iterator.Iterator, error)
|
||||
|
||||
// Information and Control
|
||||
ReadOnly() bool
|
||||
Injected() bool
|
||||
Shutdown() error
|
||||
|
||||
// Mandatory Record Maintenance
|
||||
MaintainRecordStates(ctx context.Context, purgeDeletedBefore time.Time) error
|
||||
}
|
||||
|
||||
// Maintenance defines the database storage API for backends that requ
|
||||
type Maintenance interface {
|
||||
// Maintainer defines the database storage API for backends that require regular maintenance.
|
||||
type Maintainer interface {
|
||||
Maintain(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.
|
||||
|
|
|
@ -48,7 +48,6 @@ func registerAsDatabase() error {
|
|||
Name: "notifications",
|
||||
Description: "Notifications",
|
||||
StorageType: "injected",
|
||||
PrimaryAPI: "",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue