go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 22e83078aa272b1263633ae6a8c41429750835bf
parent 98fd1ddd46ee92182afcca28d3bd9e7757e28681
Author: lash <dev@holbrook.no>
Date:   Fri, 30 Aug 2024 16:01:52 +0100

Consolidate db key construction across db implementations

Diffstat:
Mdb/db.go | 18++++++++++++++++++
Mdb/fs.go | 24+++++++++++++++---------
Mdb/fs_test.go | 1+
Mdb/gdbm.go | 24+++++++++---------------
Mdb/gdbm_test.go | 3++-
Mdb/pg.go | 28+++++++++-------------------
Mdb/pg_test.go | 1+
7 files changed, 55 insertions(+), 44 deletions(-)

diff --git a/db/db.go b/db/db.go @@ -2,6 +2,7 @@ package db import ( "context" + "errors" "git.defalsify.org/vise.git/lang" ) @@ -29,3 +30,20 @@ func ToDbKey(typ uint8, b []byte, l *lang.Language) []byte { } return append(k, b...) } + +type BaseDb struct { + pfx uint8 +} + +func(db *BaseDb) SetPrefix(pfx uint8) { + db.pfx = pfx +} + +func(db *BaseDb) ToKey(sessionId string, key []byte) ([]byte, error) { + if db.pfx == DATATYPE_UNKNOWN { + return nil, errors.New("datatype prefix must be set explicitly") + } + b := append([]byte(sessionId), 0x2E) + b = append(b, key...) + return ToDbKey(db.pfx, b, nil), nil +} diff --git a/db/fs.go b/db/fs.go @@ -9,7 +9,7 @@ import ( ) type FsDb struct { - ready bool + BaseDb dir string } @@ -26,7 +26,10 @@ func(fdb *FsDb) Connect(ctx context.Context, connStr string) error { } func(fdb *FsDb) Get(ctx context.Context, sessionId string, key []byte) ([]byte, error) { - fp := fdb.pathFor(sessionId, key) + fp, err := fdb.pathFor(sessionId, key) + if err != nil { + return nil, err + } f, err := os.Open(fp) if err != nil { return nil, err @@ -40,7 +43,10 @@ func(fdb *FsDb) Get(ctx context.Context, sessionId string, key []byte) ([]byte, } func(fdb *FsDb) Put(ctx context.Context, sessionId string, key []byte, val []byte) error { - fp := fdb.pathFor(sessionId, key) + fp, err := fdb.pathFor(sessionId, key) + if err != nil { + return err + } return ioutil.WriteFile(fp, val, 0600) } @@ -48,11 +54,11 @@ func(fdb *FsDb) Close() error { return nil } -func(fdb *FsDb) pathFor(sessionId string, key []byte) string{ - k := append([]byte(sessionId), 0x2E) - k = append(k, key...) - kb := ToDbKey(DATATYPE_USERSTART, k, nil) +func(fdb *FsDb) pathFor(sessionId string, key []byte) (string, error) { + kb, err := fdb.ToKey(sessionId, key) + if err != nil { + return "", err + } kb[0] += 30 - return path.Join(fdb.dir, string(kb)) + return path.Join(fdb.dir, string(kb)), nil } - diff --git a/db/fs_test.go b/db/fs_test.go @@ -15,6 +15,7 @@ func TestPutGetFs(t *testing.T) { t.Fatal(err) } db := &FsDb{} + db.SetPrefix(DATATYPE_USERSTART) err = db.Connect(ctx, d) if err != nil { t.Fatal(err) diff --git a/db/gdbm.go b/db/gdbm.go @@ -8,16 +8,11 @@ import ( ) type GdbmDb struct { + BaseDb conn *gdbm.Database prefix uint8 } -func NewGdbmDb() *GdbmDb { - return &GdbmDb{ - prefix: DATATYPE_USERSTART, - } - -} func(gdb *GdbmDb) Connect(ctx context.Context, connStr string) error { db, err := gdbm.Open(connStr, gdbm.ModeWrcreat) if err != nil { @@ -27,20 +22,19 @@ func(gdb *GdbmDb) Connect(ctx context.Context, connStr string) error { return nil } -// TODO: DRY -func(gdb *GdbmDb) dbKey(sessionId string, key []byte) []byte { - b := append([]byte(sessionId), 0x2E) - b = append(b, key...) - return ToDbKey(gdb.prefix, b, nil) -} - func(gdb *GdbmDb) Put(ctx context.Context, sessionId string, key []byte, val []byte) error { - k := gdb.dbKey(sessionId, key) + k, err := gdb.ToKey(sessionId, key) + if err != nil { + return err + } return gdb.conn.Store(k, val, true) } func(gdb *GdbmDb) Get(ctx context.Context, sessionId string, key []byte) ([]byte, error) { - k := gdb.dbKey(sessionId, key) + k, err := gdb.ToKey(sessionId, key) + if err != nil { + return nil, err + } v, err := gdb.conn.Fetch(k) if err != nil { if errors.Is(gdbm.ErrItemNotFound, err) { diff --git a/db/gdbm_test.go b/db/gdbm_test.go @@ -14,7 +14,8 @@ func TestPutGetGdbm(t *testing.T) { if err != nil { t.Fatal(err) } - db := NewGdbmDb() + db := &GdbmDb{} + db.SetPrefix(DATATYPE_USERSTART) err = db.Connect(ctx, f.Name()) if err != nil { t.Fatal(err) diff --git a/db/pg.go b/db/pg.go @@ -3,13 +3,12 @@ package db import ( "context" "fmt" -// "errors" "github.com/jackc/pgx/v5/pgxpool" -// pgx "github.com/jackc/pgx/v5" ) type PgDb struct { + BaseDb conn *pgxpool.Pool schema string prefix uint8 @@ -18,7 +17,6 @@ type PgDb struct { func NewPgDb() *PgDb { return &PgDb{ schema: "public", - prefix: DATATYPE_USERSTART, } } @@ -27,14 +25,6 @@ func(pdb *PgDb) WithSchema(schema string) *PgDb { return pdb } -func(pdb *PgDb) SetPrefix(pfx uint8) error { - if pfx < DATATYPE_USERSTART { - return fmt.Errorf("prefix cannot be < %d", DATATYPE_USERSTART) - } - pdb.prefix = pfx - return nil -} - func(pdb *PgDb) Connect(ctx context.Context, connStr string) error { var err error conn, err := pgxpool.New(ctx, connStr) @@ -100,14 +90,11 @@ func(pdb *PgDb) prepare(ctx context.Context) error { return nil } -func(pdb *PgDb) dbKey(sessionId string, key []byte) []byte { - b := append([]byte(sessionId), 0x2E) - b = append(b, key...) - return ToDbKey(pdb.prefix, b, nil) -} - func(pdb *PgDb) Put(ctx context.Context, sessionId string, key []byte, val []byte) error { - k := pdb.dbKey(sessionId, key) + k, err := pdb.ToKey(sessionId, key) + if err != nil { + return err + } tx, err := pdb.conn.Begin(ctx) if err != nil { return err @@ -123,7 +110,10 @@ func(pdb *PgDb) Put(ctx context.Context, sessionId string, key []byte, val []byt } func(pdb *PgDb) Get(ctx context.Context, sessionId string, key []byte) ([]byte, error) { - k := pdb.dbKey(sessionId, key) + k, err := pdb.ToKey(sessionId, key) + if err != nil { + return nil, err + } tx, err := pdb.conn.Begin(ctx) if err != nil { return nil, err diff --git a/db/pg_test.go b/db/pg_test.go @@ -9,6 +9,7 @@ import ( func TestCreate(t *testing.T) { t.Skip("need postgresql mock") db := NewPgDb().WithSchema("vvise") + db.SetPrefix(DATATYPE_USERSTART) ctx := context.Background() err := db.Connect(ctx, "postgres://vise:esiv@localhost:5432/visedb") if err != nil {