commit d9deff6128c207538f6ede9f23cb3bc2453bbf08
parent 0298fed1acfe163ec21673cac7c3d000b3fac41a
Author: lash <dev@holbrook.no>
Date: Fri, 30 Aug 2024 23:48:58 +0100
Conceal db implementations and base db struct
Diffstat:
14 files changed, 63 insertions(+), 63 deletions(-)
diff --git a/db/db.go b/db/db.go
@@ -50,29 +50,29 @@ func ToDbKey(typ uint8, b []byte, l *lang.Language) []byte {
return append(k, b...)
}
-// BaseDb is a base class for all Db implementations.
-type BaseDb struct {
+// baseDb is a base class for all Db implementations.
+type baseDb struct {
pfx uint8
sid []byte
lock uint8
}
-func(db *BaseDb) defaultLock() {
+func(db *baseDb) defaultLock() {
db.lock = DATATYPE_BIN | DATATYPE_MENU | DATATYPE_TEMPLATE
}
// SetPrefix implements Db.
-func(db *BaseDb) SetPrefix(pfx uint8) {
+func(db *baseDb) SetPrefix(pfx uint8) {
db.pfx = pfx
}
// SetSession implements Db.
-func(db *BaseDb) SetSession(sessionId string) {
+func(db *baseDb) SetSession(sessionId string) {
db.sid = append([]byte(sessionId), 0x2E)
}
// SetSafety disables modification of data that
-func(db *BaseDb) SetLock(pfx uint8, lock bool) {
+func(db *baseDb) SetLock(pfx uint8, lock bool) {
if lock {
db.lock |= pfx
} else {
@@ -80,12 +80,12 @@ func(db *BaseDb) SetLock(pfx uint8, lock bool) {
}
}
-func(db *BaseDb) checkPut() bool {
+func(db *baseDb) checkPut() bool {
return db.pfx & db.lock == 0
}
// ToKey creates a DbKey within the current session context.
-func(db *BaseDb) ToKey(key []byte) ([]byte, error) {
+func(db *baseDb) ToKey(key []byte) ([]byte, error) {
var b []byte
if db.pfx == DATATYPE_UNKNOWN {
return nil, errors.New("datatype prefix cannot be UNKNOWN")
diff --git a/db/fs.go b/db/fs.go
@@ -8,20 +8,20 @@ import (
"path"
)
-// FsDb is a pure filesystem backend implementation if the Db interface.
-type FsDb struct {
- BaseDb
+// fsDb is a pure filesystem backend implementation if the Db interface.
+type fsDb struct {
+ baseDb
dir string
}
-func NewFsDb() *FsDb {
- db := &FsDb{}
- db.BaseDb.defaultLock()
+func NewFsDb() *fsDb {
+ db := &fsDb{}
+ db.baseDb.defaultLock()
return db
}
// Connect implements Db
-func(fdb *FsDb) Connect(ctx context.Context, connStr string) error {
+func(fdb *fsDb) Connect(ctx context.Context, connStr string) error {
if fdb.dir != "" {
return nil
}
@@ -34,7 +34,7 @@ func(fdb *FsDb) Connect(ctx context.Context, connStr string) error {
}
// Get implements Db
-func(fdb *FsDb) Get(ctx context.Context, key []byte) ([]byte, error) {
+func(fdb *fsDb) Get(ctx context.Context, key []byte) ([]byte, error) {
fp, err := fdb.pathFor(key)
if err != nil {
return nil, err
@@ -52,7 +52,7 @@ func(fdb *FsDb) Get(ctx context.Context, key []byte) ([]byte, error) {
}
// Put implements Db
-func(fdb *FsDb) Put(ctx context.Context, key []byte, val []byte) error {
+func(fdb *fsDb) Put(ctx context.Context, key []byte, val []byte) error {
if !fdb.checkPut() {
return errors.New("unsafe put and safety set")
}
@@ -64,12 +64,12 @@ func(fdb *FsDb) Put(ctx context.Context, key []byte, val []byte) error {
}
// Close implements Db
-func(fdb *FsDb) Close() error {
+func(fdb *fsDb) Close() error {
return nil
}
// create a key safe for the filesystem
-func(fdb *FsDb) pathFor(key []byte) (string, error) {
+func(fdb *fsDb) pathFor(key []byte) (string, error) {
kb, err := fdb.ToKey(key)
if err != nil {
return "", err
diff --git a/db/fs_test.go b/db/fs_test.go
@@ -15,7 +15,7 @@ func TestPutGetFs(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- db := &FsDb{}
+ db := NewFsDb()
db.SetPrefix(DATATYPE_USERSTART)
db.SetSession(sid)
diff --git a/db/gdbm.go b/db/gdbm.go
@@ -8,21 +8,21 @@ import (
gdbm "github.com/graygnuorg/go-gdbm"
)
-// GdbmDb is a gdbm backend implementation of the Db interface.
-type GdbmDb struct {
- BaseDb
+// gdbmDb is a gdbm backend implementation of the Db interface.
+type gdbmDb struct {
+ baseDb
conn *gdbm.Database
prefix uint8
}
-func NewGdbmDb() *GdbmDb {
- db := &GdbmDb{}
- db.BaseDb.defaultLock()
+func NewGdbmDb() *gdbmDb {
+ db := &gdbmDb{}
+ db.baseDb.defaultLock()
return db
}
// Connect implements Db
-func(gdb *GdbmDb) Connect(ctx context.Context, connStr string) error {
+func(gdb *gdbmDb) Connect(ctx context.Context, connStr string) error {
var db *gdbm.Database
_, err := os.Stat(connStr)
if err != nil {
@@ -42,7 +42,7 @@ func(gdb *GdbmDb) Connect(ctx context.Context, connStr string) error {
}
// Put implements Db
-func(gdb *GdbmDb) Put(ctx context.Context, key []byte, val []byte) error {
+func(gdb *gdbmDb) Put(ctx context.Context, key []byte, val []byte) error {
if !gdb.checkPut() {
return errors.New("unsafe put and safety set")
}
@@ -54,7 +54,7 @@ func(gdb *GdbmDb) Put(ctx context.Context, key []byte, val []byte) error {
}
// Get implements Db
-func(gdb *GdbmDb) Get(ctx context.Context, key []byte) ([]byte, error) {
+func(gdb *gdbmDb) Get(ctx context.Context, key []byte) ([]byte, error) {
k, err := gdb.ToKey(key)
if err != nil {
return nil, err
@@ -70,6 +70,6 @@ func(gdb *GdbmDb) Get(ctx context.Context, key []byte) ([]byte, error) {
}
// Close implements Db
-func(gdb *GdbmDb) Close() error {
+func(gdb *gdbmDb) Close() error {
return gdb.Close()
}
diff --git a/db/gdbm_test.go b/db/gdbm_test.go
@@ -15,7 +15,7 @@ func TestPutGetGdbm(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- db := &GdbmDb{}
+ db := NewGdbmDb()
db.SetPrefix(DATATYPE_USERSTART)
db.SetSession(sid)
diff --git a/db/mem.go b/db/mem.go
@@ -6,22 +6,22 @@ import (
"errors"
)
-// MemDb is a memory backend implementation of the Db interface.
-type MemDb struct {
- BaseDb
+// memDb is a memory backend implementation of the Db interface.
+type memDb struct {
+ baseDb
store map[string][]byte
}
-// NewMemDb returns an already allocated
-func NewMemDb(ctx context.Context) *MemDb {
- db := &MemDb{}
- db.BaseDb.defaultLock()
+// NewmemDb returns an already allocated
+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 {
+func(mdb *memDb) Connect(ctx context.Context, connStr string) error {
if mdb.store != nil {
return nil
}
@@ -30,13 +30,13 @@ func(mdb *MemDb) Connect(ctx context.Context, connStr string) error {
}
// convert to a supported map key type
-func(mdb *MemDb) toHexKey(key []byte) (string, error) {
+func(mdb *memDb) toHexKey(key []byte) (string, error) {
k, err := mdb.ToKey(key)
return hex.EncodeToString(k), err
}
// Get implements Db
-func(mdb *MemDb) Get(ctx context.Context, key []byte) ([]byte, error) {
+func(mdb *memDb) Get(ctx context.Context, key []byte) ([]byte, error) {
k, err := mdb.toHexKey(key)
if err != nil {
return nil, err
@@ -51,7 +51,7 @@ func(mdb *MemDb) Get(ctx context.Context, key []byte) ([]byte, error) {
}
// Put implements Db
-func(mdb *MemDb) Put(ctx context.Context, key []byte, val []byte) error {
+func(mdb *memDb) Put(ctx context.Context, key []byte, val []byte) error {
if !mdb.checkPut() {
return errors.New("unsafe put and safety set")
}
@@ -65,6 +65,6 @@ func(mdb *MemDb) Put(ctx context.Context, key []byte, val []byte) error {
}
// Close implements Db
-func(mdb *MemDb) Close() error {
+func(mdb *memDb) Close() error {
return nil
}
diff --git a/db/mem_test.go b/db/mem_test.go
@@ -10,7 +10,7 @@ func TestPutGetMem(t *testing.T) {
var dbi Db
ctx := context.Background()
sid := "ses"
- db := &MemDb{}
+ db := NewMemDb(ctx)
db.SetPrefix(DATATYPE_USERSTART)
db.SetSession(sid)
diff --git a/db/pg.go b/db/pg.go
@@ -8,31 +8,31 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
)
-// PgDb is a Postgresql backend implementation of the Db interface.
-type PgDb struct {
- BaseDb
+// pgDb is a Postgresql backend implementation of the Db interface.
+type pgDb struct {
+ baseDb
conn *pgxpool.Pool
schema string
prefix uint8
}
-// NewPgDb creates a new PgDb reference.
-func NewPgDb() *PgDb {
- db := &PgDb{
+// NewpgDb creates a new pgDb reference.
+func NewPgDb() *pgDb {
+ db := &pgDb{
schema: "public",
}
- db.BaseDb.defaultLock()
+ db.baseDb.defaultLock()
return db
}
// WithSchema sets the Postgres schema to use for the storage table.
-func(pdb *PgDb) WithSchema(schema string) *PgDb {
+func(pdb *pgDb) WithSchema(schema string) *pgDb {
pdb.schema = schema
return pdb
}
// Connect implements Db.
-func(pdb *PgDb) Connect(ctx context.Context, connStr string) error {
+func(pdb *pgDb) Connect(ctx context.Context, connStr string) error {
var err error
conn, err := pgxpool.New(ctx, connStr)
if err != nil {
@@ -43,7 +43,7 @@ func(pdb *PgDb) Connect(ctx context.Context, connStr string) error {
}
// Put implements Db.
-func(pdb *PgDb) Put(ctx context.Context, key []byte, val []byte) error {
+func(pdb *pgDb) Put(ctx context.Context, key []byte, val []byte) error {
if !pdb.checkPut() {
return errors.New("unsafe put and safety set")
}
@@ -66,7 +66,7 @@ func(pdb *PgDb) Put(ctx context.Context, key []byte, val []byte) error {
}
// Get implements Db.
-func(pdb *PgDb) Get(ctx context.Context, key []byte) ([]byte, error) {
+func(pdb *pgDb) Get(ctx context.Context, key []byte) ([]byte, error) {
k, err := pdb.ToKey(key)
if err != nil {
return nil, err
@@ -91,13 +91,13 @@ func(pdb *PgDb) Get(ctx context.Context, key []byte) ([]byte, error) {
}
// Close implements Db.
-func(pdb *PgDb) Close() error {
+func(pdb *pgDb) Close() error {
pdb.Close()
return nil
}
// set up table
-func(pdb *PgDb) prepare(ctx context.Context) error {
+func(pdb *pgDb) prepare(ctx context.Context) error {
tx, err := pdb.conn.Begin(ctx)
if err != nil {
tx.Rollback(ctx)
diff --git a/dev/interactive/main.go b/dev/interactive/main.go
@@ -27,7 +27,7 @@ func main() {
ctx := context.Background()
if persist != "" {
- store = &db.FsDb{}
+ store = db.NewFsDb()
err := store.Connect(ctx, persist)
if err != nil {
fmt.Fprintf(os.Stderr, "db connect error: %v", err)
diff --git a/examples/db/main.go b/examples/db/main.go
@@ -21,7 +21,7 @@ import (
var (
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "examples", "db")
- store = &db.FsDb{}
+ store = db.NewFsDb()
pr = persist.NewPersister(store)
data_selector = []byte("my_data")
)
diff --git a/examples/gdbm/main.go b/examples/gdbm/main.go
@@ -33,7 +33,7 @@ func main() {
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, scriptDir)
st := state.NewState(0)
- store := &db.GdbmDb{}
+ store := db.NewGdbmDb()
err := store.Connect(ctx, dbFile)
if err != nil {
panic(err)
diff --git a/examples/languages/main.go b/examples/languages/main.go
@@ -97,7 +97,7 @@ func main() {
ctx := context.Background()
dp := path.Join(scriptDir, ".state")
- store := &db.FsDb{}
+ store := db.NewFsDb()
err := store.Connect(ctx, dp)
if err != nil {
engine.Logg.ErrorCtxf(ctx, "db connect fail", "err", err)
diff --git a/examples/longmenu/main.go b/examples/longmenu/main.go
@@ -33,7 +33,7 @@ func main() {
ctx := context.Background()
dp := path.Join(scriptDir, ".state")
- store := &db.FsDb{}
+ store := db.NewFsDb()
err := store.Connect(ctx, dp)
if err != nil {
fmt.Fprintf(os.Stderr, "db connect error: %v", err)
diff --git a/examples/state_passive/main.go b/examples/state_passive/main.go
@@ -92,7 +92,7 @@ func main() {
}
dp := path.Join(dir, ".state")
- store := &db.FsDb{}
+ store := db.NewFsDb()
err := store.Connect(ctx, dp)
if err != nil {
fmt.Fprintf(os.Stderr, "db connect fail: %s", err)