commit 40bc8ea48e62104bce6b8a8190a3570ec6e8b4d7
parent d9deff6128c207538f6ede9f23cb3bc2453bbf08
Author: lash <dev@holbrook.no>
Date: Sat, 31 Aug 2024 00:05:07 +0100
Force use of Connect in memdb
Diffstat:
8 files changed, 23 insertions(+), 12 deletions(-)
diff --git a/db/db.go b/db/db.go
@@ -22,7 +22,7 @@ const (
// Db abstracts all data storage and retrieval as a key-value store
type Db interface {
- // Connect prepares the storage backend for use
+ // Connect prepares the storage backend for use. May panic or error if called more than once.
Connect(ctx context.Context, connStr string) error
// Close implements io.Closer
Close() error
diff --git a/db/fs.go b/db/fs.go
@@ -8,12 +8,13 @@ import (
"path"
)
-// fsDb is a pure filesystem backend implementation if the Db interface.
+// pure filesystem backend implementation if the Db interface.
type fsDb struct {
baseDb
dir string
}
+// NewFsDb creates a filesystem backed Db implementation.
func NewFsDb() *fsDb {
db := &fsDb{}
db.baseDb.defaultLock()
@@ -23,7 +24,7 @@ func NewFsDb() *fsDb {
// Connect implements Db
func(fdb *fsDb) Connect(ctx context.Context, connStr string) error {
if fdb.dir != "" {
- return nil
+ panic("already connected")
}
err := os.MkdirAll(connStr, 0700)
if err != nil {
diff --git a/db/gdbm.go b/db/gdbm.go
@@ -23,6 +23,9 @@ func NewGdbmDb() *gdbmDb {
// Connect implements Db
func(gdb *gdbmDb) Connect(ctx context.Context, connStr string) error {
+ if gdb.conn != nil {
+ panic("already connected")
+ }
var db *gdbm.Database
_, err := os.Stat(connStr)
if err != nil {
diff --git a/db/mem.go b/db/mem.go
@@ -12,18 +12,17 @@ type memDb struct {
store map[string][]byte
}
-// NewmemDb returns an already allocated
+// NewmemDb returns an already allocated memory backend (volatile) Db implementation.
func NewMemDb(ctx context.Context) *memDb {
db := &memDb{}
db.baseDb.defaultLock()
- _ = db.Connect(ctx, "")
return db
}
// Connect implements Db
func(mdb *memDb) Connect(ctx context.Context, connStr string) error {
if mdb.store != nil {
- return nil
+ panic("already connected")
}
mdb.store = make(map[string][]byte)
return nil
diff --git a/db/pg.go b/db/pg.go
@@ -16,7 +16,7 @@ type pgDb struct {
prefix uint8
}
-// NewpgDb creates a new pgDb reference.
+// NewpgDb creates a new postgres backed Db implementation.
func NewPgDb() *pgDb {
db := &pgDb{
schema: "public",
@@ -33,6 +33,9 @@ func(pdb *pgDb) WithSchema(schema string) *pgDb {
// Connect implements Db.
func(pdb *pgDb) Connect(ctx context.Context, connStr string) error {
+ if pdb.conn != nil {
+ panic("already connected")
+ }
var err error
conn, err := pgxpool.New(ctx, connStr)
if err != nil {
diff --git a/engine/persist_test.go b/engine/persist_test.go
@@ -13,6 +13,7 @@ import (
func TestRunPersist(t *testing.T) {
generateTestData(t)
+ ctx := context.Background()
cfg := Config{
OutputSize: 83,
SessionId: "xyzzy",
@@ -23,10 +24,11 @@ func TestRunPersist(t *testing.T) {
st := state.NewState(3)
ca := cache.NewCache().WithCacheSize(1024)
store := db.NewMemDb(context.Background())
+ store.Connect(ctx, "")
pr := persist.NewPersister(store).WithContent(&st, ca)
w := os.Stdout
- ctx := context.TODO()
+ ctx = context.Background()
st = state.NewState(cfg.FlagCount)
ca = cache.NewCache()
@@ -69,6 +71,7 @@ func TestRunPersist(t *testing.T) {
func TestEnginePersist(t *testing.T) {
generateTestData(t)
+ ctx := context.Background()
cfg := Config{
OutputSize: 83,
SessionId: "xyzzy",
@@ -79,11 +82,9 @@ func TestEnginePersist(t *testing.T) {
st := state.NewState(3)
ca := cache.NewCache().WithCacheSize(1024)
store := db.NewMemDb(context.Background())
+ store.Connect(ctx, "")
pr := persist.NewPersister(store).WithContent(&st, ca)
- //w := os.Stdout
- ctx := context.TODO()
-
st = state.NewState(cfg.FlagCount)
ca = cache.NewCache()
ca = ca.WithCacheSize(cfg.CacheSize)
diff --git a/persist/fs_test.go b/persist/fs_test.go
@@ -30,6 +30,7 @@ func TestSerializeState(t *testing.T) {
ctx := context.Background()
store := db.NewMemDb(ctx)
+ store.Connect(ctx, "")
pr := NewPersister(store).WithContext(context.Background()).WithSession("xyzzy").WithContent(&st, ca)
v, err := pr.Serialize()
if err != nil {
@@ -81,6 +82,7 @@ func TestSaveLoad(t *testing.T) {
ctx := context.Background()
store := db.NewMemDb(ctx)
+ store.Connect(ctx, "")
pr := NewPersister(store).WithContent(&st, ca)
err := pr.Save("xyzzy")
if err != nil {
@@ -115,12 +117,13 @@ func TestSaveLoad(t *testing.T) {
}
func TestSaveLoadFlags(t *testing.T) {
+ ctx := context.Background()
st := state.NewState(2)
st.SetFlag(8)
ca := cache.NewCache()
- ctx := context.Background()
store := db.NewMemDb(ctx)
+ store.Connect(ctx, "")
pr := NewPersister(store).WithContent(&st, ca)
err := pr.Save("xyzzy")
if err != nil {
diff --git a/resource/db_test.go b/resource/db_test.go
@@ -11,6 +11,7 @@ import (
func TestDb(t *testing.T) {
ctx := context.Background()
store := db.NewMemDb(ctx)
+ store.Connect(ctx, "")
tg, err := NewDbFuncGetter(store, db.DATATYPE_TEMPLATE)
if err != nil {
t.Fatal(err)