Implement tx enabled db vise
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user