go-vise

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

commit 98fd1ddd46ee92182afcca28d3bd9e7757e28681
parent 8343b4bd57be0991946478ae28500d9cd6857222
Author: lash <dev@holbrook.no>
Date:   Fri, 30 Aug 2024 15:46:47 +0100

Add gdbm db

Diffstat:
Mdb/fs_test.go | 2+-
Adb/gdbm.go | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adb/gdbm_test.go | 38++++++++++++++++++++++++++++++++++++++
Mdb/pg_test.go | 2+-
Mgo.mod | 5+++--
Mgo.sum | 6++++++
6 files changed, 105 insertions(+), 4 deletions(-)

diff --git a/db/fs_test.go b/db/fs_test.go @@ -7,7 +7,7 @@ import ( "testing" ) -func TestPutGet(t *testing.T) { +func TestPutGetFs(t *testing.T) { ctx := context.Background() sid := "ses" d, err := ioutil.TempDir("", "vise-db-*") diff --git a/db/gdbm.go b/db/gdbm.go @@ -0,0 +1,56 @@ +package db + +import ( + "context" + "errors" + + gdbm "github.com/graygnuorg/go-gdbm" +) + +type GdbmDb struct { + 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 { + return err + } + gdb.conn = db + 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) + 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) + v, err := gdb.conn.Fetch(k) + if err != nil { + if errors.Is(gdbm.ErrItemNotFound, err) { + return nil, NewErrNotFound(k) + } + return nil, err + } + return v, nil +} + +func(gdb *GdbmDb) Close() error { + return gdb.Close() +} diff --git a/db/gdbm_test.go b/db/gdbm_test.go @@ -0,0 +1,38 @@ +package db + +import ( + "bytes" + "context" + "io/ioutil" + "testing" +) + +func TestPutGetGdbm(t *testing.T) { + ctx := context.Background() + sid := "ses" + f, err := ioutil.TempFile("", "vise-db-*") + if err != nil { + t.Fatal(err) + } + db := NewGdbmDb() + err = db.Connect(ctx, f.Name()) + if err != nil { + t.Fatal(err) + } + err = db.Put(ctx, sid, []byte("foo"), []byte("bar")) + if err != nil { + t.Fatal(err) + } + v, err := db.Get(ctx, sid, []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")) + if err == nil { + t.Fatal("expected get error for key 'bar'") + } + +} diff --git a/db/pg_test.go b/db/pg_test.go @@ -7,7 +7,7 @@ import ( ) func TestCreate(t *testing.T) { -// t.Skip("need postgresql mock") + t.Skip("need postgresql mock") db := NewPgDb().WithSchema("vvise") ctx := context.Background() err := db.Connect(ctx, "postgres://vise:esiv@localhost:5432/visedb") diff --git a/go.mod b/go.mod @@ -6,18 +6,19 @@ require ( github.com/alecthomas/participle/v2 v2.0.0 github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c github.com/fxamacker/cbor/v2 v2.4.0 + github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 + github.com/jackc/pgx/v5 v5.6.0 github.com/peteole/testdata-loader v0.3.0 + gopkg.in/leonelquinteros/gotext.v1 v1.3.1 ) require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect - github.com/jackc/pgx/v5 v5.6.0 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a // indirect github.com/x448/float16 v0.8.4 // indirect golang.org/x/crypto v0.17.0 // indirect golang.org/x/sync v0.1.0 // indirect golang.org/x/text v0.14.0 // indirect - gopkg.in/leonelquinteros/gotext.v1 v1.3.1 // indirect ) diff --git a/go.sum b/go.sum @@ -5,8 +5,11 @@ github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c h1:H9Nm+I7Cg/YVPpEV1RzU3Wq2pjamPc/UtHDgItcb7lE= github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c/go.mod h1:rGod7o6KPeJ+hyBpHfhi4v7blx9sf+QsHsA7KAsdN6U= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88= github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= +github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 h1:U4kkNYryi/qfbBF8gh7Vsbuz+cVmhf5kt6pE9bYYyLo= +github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= @@ -20,10 +23,12 @@ github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a h1:0Q3H0YXzMHiciXtRcM github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a/go.mod h1:CdTTBOYzS5E4mWS1N8NWP6AHI19MP0A2B18n3hLzRMk= github.com/peteole/testdata-loader v0.3.0 h1:8jckE9KcyNHgyv/VPoaljvKZE0Rqr8+dPVYH6rfNr9I= github.com/peteole/testdata-loader v0.3.0/go.mod h1:Mt0ZbRtb56u8SLJpNP+BnQbENljMorYBpqlvt3cS83U= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= @@ -36,3 +41,4 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/leonelquinteros/gotext.v1 v1.3.1 h1:8d9/fdTG0kn/B7NNGV1BsEyvektXFAbkMsTZS2sFSCc= gopkg.in/leonelquinteros/gotext.v1 v1.3.1/go.mod h1:X1WlGDeAFIYsW6GjgMm4VwUwZ2XjI7Zan2InxSUQWrU= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=