Implement tx enabled db vise

This commit is contained in:
lash 2025-01-19 09:04:37 +00:00
parent c0534ede1b
commit 975720919c
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
8 changed files with 88 additions and 32 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module git.grassecon.net/grassrootseconomics/visedriver
go 1.23.0
require (
git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b
git.defalsify.org/vise.git v0.2.3-0.20250119084238-8ad1d92d2127
github.com/jackc/pgx/v5 v5.7.1
github.com/joho/godotenv v1.5.1
)

4
go.sum
View File

@ -1,5 +1,5 @@
git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b h1:rwWXMtNSn7aqhb4p1oVZkCA1vC7pVdohwW61QQM8fUs=
git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
git.defalsify.org/vise.git v0.2.3-0.20250119084238-8ad1d92d2127 h1:4AZoazfzGAHGNMn3kAWsXZQGO3Sme4PYLV8gvMjIZ0A=
git.defalsify.org/vise.git v0.2.3-0.20250119084238-8ad1d92d2127/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
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=

View File

@ -1,6 +1,8 @@
package request
import (
"context"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/persist"
@ -29,8 +31,8 @@ func NewBaseRequestHandler(cfg engine.Config, rs resource.Resource, stateDb db.D
}
}
func (f *BaseRequestHandler) Shutdown() {
err := f.provider.Close()
func (f *BaseRequestHandler) Shutdown(ctx context.Context) {
err := f.provider.Close(ctx)
if err != nil {
logg.Errorf("handler shutdown error", "err", err)
}
@ -96,9 +98,9 @@ func(f *BaseRequestHandler) Output(rqs RequestSession) (RequestSession, error)
return rqs, err
}
func(f *BaseRequestHandler) Reset(rqs RequestSession) (RequestSession, error) {
func(f *BaseRequestHandler) Reset(ctx context.Context, rqs RequestSession) (RequestSession, error) {
defer f.provider.Put(rqs.Config.SessionId, rqs.Storage)
return rqs, rqs.Engine.Finish()
return rqs, rqs.Engine.Finish(ctx)
}
func (f *BaseRequestHandler) GetConfig() engine.Config {

View File

@ -111,11 +111,11 @@ func(tdb *ThreadGdbmDb) Get(ctx context.Context, key []byte) ([]byte, error) {
return v, err
}
func(tdb *ThreadGdbmDb) Close() error {
func(tdb *ThreadGdbmDb) Close(ctx context.Context) error {
tdb.reserve()
close(dbC[tdb.connStr])
delete(dbC, tdb.connStr)
err := tdb.db.Close()
err := tdb.db.Close(ctx)
tdb.db = nil
return err
}
@ -125,3 +125,19 @@ func(tdb *ThreadGdbmDb) Dump(ctx context.Context, key []byte) (*db.Dumper, error
defer tdb.release()
return tdb.db.Dump(ctx, key)
}
func(tdb *ThreadGdbmDb) DecodeKey(ctx context.Context, key []byte) ([]byte, error) {
return tdb.db.DecodeKey(ctx, key)
}
func(tdb *ThreadGdbmDb) Abort(ctx context.Context) {
tdb.db.Abort(ctx)
}
func(tdb *ThreadGdbmDb) Start(ctx context.Context) error {
return tdb.db.Start(ctx)
}
func(tdb *ThreadGdbmDb) Stop(ctx context.Context) error {
return tdb.db.Stop(ctx)
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"path"
"path/filepath"
)
const (
@ -68,9 +69,19 @@ func probeGdbm(s string) (string, string, bool) {
}
func probeFs(s string) (string, string, bool) {
if !path.IsAbs(s) {
var err error
v, _ := url.Parse(s)
if v.Scheme != "" && v.Scheme != "file://" {
return "", "", false
}
if !path.IsAbs(s) {
s, err = filepath.Abs(s)
if err != nil {
panic(err)
}
}
s = path.Clean(s)
return s, "", true
}
@ -85,11 +96,13 @@ func probeMem(s string) (string, string, bool) {
func ToConnData(connStr string) (ConnData, error) {
var o ConnData
if connStr == "" {
v, domain, ok := probeMem(connStr)
if ok {
o.typ = DBTYPE_MEM
return o, nil
}
v, domain, ok := probePostgres(connStr)
v, domain, ok = probePostgres(connStr)
if ok {
o.typ = DBTYPE_POSTGRES
o.str = v
@ -111,11 +124,5 @@ func ToConnData(connStr string) (ConnData, error) {
return o, nil
}
v, _, ok = probeMem(connStr)
if ok {
o.typ = DBTYPE_MEM
return o, nil
}
return o, fmt.Errorf("invalid connection string: %s", connStr)
}

View File

@ -5,24 +5,53 @@ import (
)
func TestParseConnStr(t *testing.T) {
_, err := ToConnData("postgres://foo:bar@localhost:5432/baz")
v, err := ToConnData("postgres://foo:bar@localhost:5432/baz")
if err != nil {
t.Fatal(err)
}
_, err = ToConnData("/foo/bar")
if v.DbType() != DBTYPE_POSTGRES {
t.Fatalf("expected type %v, got %v", DBTYPE_POSTGRES, v.DbType())
}
v, err = ToConnData("gdbm:///foo/bar")
if err != nil {
t.Fatal(err)
}
_, err = ToConnData("/foo/bar/")
if v.DbType() != DBTYPE_GDBM {
t.Fatalf("expected type %v, got %v", DBTYPE_GDBM, v.DbType())
}
v, err = ToConnData("/foo/bar")
if err != nil {
t.Fatal(err)
}
_, err = ToConnData("foo/bar")
if v.DbType() != DBTYPE_FS {
t.Fatalf("expected type %v, got %v", DBTYPE_FS, v.DbType())
}
v, err = ToConnData("/foo/bar/")
if err != nil {
t.Fatal(err)
}
if v.DbType() != DBTYPE_FS {
t.Fatalf("expected type %v, got %v", DBTYPE_FS, v.DbType())
}
v, err = ToConnData("foo/bar")
if err != nil {
t.Fatal(err)
}
if v.DbType() != DBTYPE_FS {
t.Fatalf("expected type %v, got %v", DBTYPE_FS, v.DbType())
}
v, err = ToConnData("")
if err != nil {
t.Fatal(err)
}
if v.DbType() != DBTYPE_MEM {
t.Fatalf("expected type %v, got %v", DBTYPE_MEM, v.DbType())
}
v, err = ToConnData("http://foo/bar")
if err == nil {
t.Fatalf("expected error")
}
_, err = ToConnData("http://foo/bar")
if err == nil {
t.Fatalf("expected error")
if v.DbType() != DBTYPE_NONE {
t.Fatalf("expected type %v, got %v", DBTYPE_NONE, v.DbType())
}
}

View File

@ -1,6 +1,8 @@
package storage
import (
"context"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/persist"
)
@ -17,7 +19,7 @@ type Storage struct {
type StorageProvider interface {
Get(sessionId string) (*Storage, error)
Put(sessionId string, storage *Storage) error
Close() error
Close(ctx context.Context) error
}
type SimpleStorageProvider struct {
@ -43,6 +45,6 @@ func (p *SimpleStorageProvider) Put(sessionId string, storage *Storage) error {
return nil
}
func (p *SimpleStorageProvider) Close() error {
return p.Storage.UserdataDb.Close()
func (p *SimpleStorageProvider) Close(ctx context.Context) error {
return p.Storage.UserdataDb.Close(ctx)
}

View File

@ -195,10 +195,10 @@ func (ms *MenuStorageService) ensureDbDir() error {
return nil
}
func (ms *MenuStorageService) Close() error {
errA := ms.stateStore.Close()
errB := ms.userDataStore.Close()
errC := ms.resourceStore.Close()
func (ms *MenuStorageService) Close(ctx context.Context) error {
errA := ms.stateStore.Close(ctx)
errB := ms.userDataStore.Close(ctx)
errC := ms.resourceStore.Close(ctx)
if errA != nil || errB != nil || errC != nil {
return fmt.Errorf("%v %v %v", errA, errB, errC)
}