commit a9d5a5e893fcda6a48db60f2a1c387a87f724478
parent 22e83078aa272b1263633ae6a8c41429750835bf
Author: lash <dev@holbrook.no>
Date: Fri, 30 Aug 2024 16:22:21 +0100
Move session id to base db class
Diffstat:
7 files changed, 40 insertions(+), 32 deletions(-)
diff --git a/db/db.go b/db/db.go
@@ -18,8 +18,8 @@ const (
type Db interface {
Connect(ctx context.Context, connStr string) error
Close() error
- Get(ctx context.Context, sessionId string, key []byte) ([]byte, error)
- Put(ctx context.Context, sessionId string, key []byte, val []byte) error
+ Get(ctx context.Context, key []byte) ([]byte, error)
+ Put(ctx context.Context, key []byte, val []byte) error
}
func ToDbKey(typ uint8, b []byte, l *lang.Language) []byte {
@@ -33,17 +33,21 @@ func ToDbKey(typ uint8, b []byte, l *lang.Language) []byte {
type BaseDb struct {
pfx uint8
+ sid []byte
}
func(db *BaseDb) SetPrefix(pfx uint8) {
db.pfx = pfx
}
-func(db *BaseDb) ToKey(sessionId string, key []byte) ([]byte, error) {
+func(db *BaseDb) SetSession(sessionId string) {
+ db.sid = append([]byte(sessionId), 0x2E)
+}
+
+func(db *BaseDb) ToKey(key []byte) ([]byte, error) {
if db.pfx == DATATYPE_UNKNOWN {
- return nil, errors.New("datatype prefix must be set explicitly")
+ return nil, errors.New("datatype prefix cannot be UNKNOWN")
}
- b := append([]byte(sessionId), 0x2E)
- b = append(b, key...)
+ b := append(db.sid, key...)
return ToDbKey(db.pfx, b, nil), nil
}
diff --git a/db/fs.go b/db/fs.go
@@ -25,8 +25,8 @@ func(fdb *FsDb) Connect(ctx context.Context, connStr string) error {
return nil
}
-func(fdb *FsDb) Get(ctx context.Context, sessionId string, key []byte) ([]byte, error) {
- fp, err := fdb.pathFor(sessionId, key)
+func(fdb *FsDb) Get(ctx context.Context, key []byte) ([]byte, error) {
+ fp, err := fdb.pathFor(key)
if err != nil {
return nil, err
}
@@ -42,8 +42,8 @@ func(fdb *FsDb) Get(ctx context.Context, sessionId string, key []byte) ([]byte,
return b, nil
}
-func(fdb *FsDb) Put(ctx context.Context, sessionId string, key []byte, val []byte) error {
- fp, err := fdb.pathFor(sessionId, key)
+func(fdb *FsDb) Put(ctx context.Context, key []byte, val []byte) error {
+ fp, err := fdb.pathFor(key)
if err != nil {
return err
}
@@ -54,8 +54,8 @@ func(fdb *FsDb) Close() error {
return nil
}
-func(fdb *FsDb) pathFor(sessionId string, key []byte) (string, error) {
- kb, err := fdb.ToKey(sessionId, key)
+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
@@ -16,22 +16,23 @@ func TestPutGetFs(t *testing.T) {
}
db := &FsDb{}
db.SetPrefix(DATATYPE_USERSTART)
+ db.SetSession(sid)
err = db.Connect(ctx, d)
if err != nil {
t.Fatal(err)
}
- err = db.Put(ctx, sid, []byte("foo"), []byte("bar"))
+ err = db.Put(ctx, []byte("foo"), []byte("bar"))
if err != nil {
t.Fatal(err)
}
- v, err := db.Get(ctx, sid, []byte("foo"))
+ v, err := db.Get(ctx, []byte("foo"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(v, []byte("bar")) {
t.Fatalf("expected value 'bar', found '%s'", v)
}
- _, err = db.Get(ctx, sid, []byte("bar"))
+ _, err = db.Get(ctx, []byte("bar"))
if err == nil {
t.Fatal("expected get error for key 'bar'")
}
diff --git a/db/gdbm.go b/db/gdbm.go
@@ -22,16 +22,16 @@ func(gdb *GdbmDb) Connect(ctx context.Context, connStr string) error {
return nil
}
-func(gdb *GdbmDb) Put(ctx context.Context, sessionId string, key []byte, val []byte) error {
- k, err := gdb.ToKey(sessionId, key)
+func(gdb *GdbmDb) Put(ctx context.Context, key []byte, val []byte) error {
+ k, err := gdb.ToKey(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, err := gdb.ToKey(sessionId, key)
+func(gdb *GdbmDb) Get(ctx context.Context, key []byte) ([]byte, error) {
+ k, err := gdb.ToKey(key)
if err != nil {
return nil, err
}
diff --git a/db/gdbm_test.go b/db/gdbm_test.go
@@ -16,22 +16,23 @@ func TestPutGetGdbm(t *testing.T) {
}
db := &GdbmDb{}
db.SetPrefix(DATATYPE_USERSTART)
+ db.SetSession(sid)
err = db.Connect(ctx, f.Name())
if err != nil {
t.Fatal(err)
}
- err = db.Put(ctx, sid, []byte("foo"), []byte("bar"))
+ err = db.Put(ctx, []byte("foo"), []byte("bar"))
if err != nil {
t.Fatal(err)
}
- v, err := db.Get(ctx, sid, []byte("foo"))
+ v, err := db.Get(ctx, []byte("foo"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(v, []byte("bar")) {
t.Fatalf("expected value 'bar', found '%s'", v)
}
- _, err = db.Get(ctx, sid, []byte("bar"))
+ _, err = db.Get(ctx, []byte("bar"))
if err == nil {
t.Fatal("expected get error for key 'bar'")
}
diff --git a/db/pg.go b/db/pg.go
@@ -90,8 +90,8 @@ func(pdb *PgDb) prepare(ctx context.Context) error {
return nil
}
-func(pdb *PgDb) Put(ctx context.Context, sessionId string, key []byte, val []byte) error {
- k, err := pdb.ToKey(sessionId, key)
+func(pdb *PgDb) Put(ctx context.Context, key []byte, val []byte) error {
+ k, err := pdb.ToKey(key)
if err != nil {
return err
}
@@ -109,8 +109,8 @@ func(pdb *PgDb) Put(ctx context.Context, sessionId string, key []byte, val []byt
return nil
}
-func(pdb *PgDb) Get(ctx context.Context, sessionId string, key []byte) ([]byte, error) {
- k, err := pdb.ToKey(sessionId, key)
+func(pdb *PgDb) Get(ctx context.Context, key []byte) ([]byte, error) {
+ k, err := pdb.ToKey(key)
if err != nil {
return nil, err
}
diff --git a/db/pg_test.go b/db/pg_test.go
@@ -6,31 +6,33 @@ import (
"testing"
)
-func TestCreate(t *testing.T) {
- t.Skip("need postgresql mock")
+func TestPutGetPg(t *testing.T) {
+ //t.Skip("need postgresql mock")
+ ses := "xyzzy"
db := NewPgDb().WithSchema("vvise")
db.SetPrefix(DATATYPE_USERSTART)
+ db.SetSession(ses)
ctx := context.Background()
err := db.Connect(ctx, "postgres://vise:esiv@localhost:5432/visedb")
if err != nil {
t.Fatal(err)
}
- err = db.Put(ctx, "xyzzy", []byte("foo"), []byte("bar"))
+ err = db.Put(ctx, []byte("foo"), []byte("bar"))
if err != nil {
t.Fatal(err)
}
- b, err := db.Get(ctx, "xyzzy", []byte("foo"))
+ b, err := db.Get(ctx, []byte("foo"))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte("bar")) {
t.Fatalf("expected 'bar', got %x", b)
}
- err = db.Put(ctx, "xyzzy", []byte("foo"), []byte("plugh"))
+ err = db.Put(ctx, []byte("foo"), []byte("plugh"))
if err != nil {
t.Fatal(err)
}
- b, err = db.Get(ctx, "xyzzy", []byte("foo"))
+ b, err = db.Get(ctx, []byte("foo"))
if err != nil {
t.Fatal(err)
}