go-vise

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

commit ffab76edad7c521b3701ad820ff3d2ba2e8152a4
parent 30ddf1494884233ebe36d112a3ec9a2034d57db0
Author: lash <dev@holbrook.no>
Date:   Fri, 30 Aug 2024 19:20:28 +0100

Implement db perister for engine

Diffstat:
Mdb/fs.go | 3+++
Mengine/default.go | 29+++++++++--------------------
Mengine/persist.go | 6+++---
Mengine/persist_test.go | 32++++++++++++--------------------
4 files changed, 27 insertions(+), 43 deletions(-)

diff --git a/db/fs.go b/db/fs.go @@ -16,6 +16,9 @@ type FsDb struct { // Connect implements Db func(fdb *FsDb) Connect(ctx context.Context, connStr string) error { + if fdb.dir != "" { + return nil + } fi, err := os.Stat(connStr) if err != nil { return err diff --git a/engine/default.go b/engine/default.go @@ -3,17 +3,16 @@ package engine import ( "context" "fmt" - "os" - "path" "git.defalsify.org/vise.git/cache" "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/state" + "git.defalsify.org/vise.git/db" ) // NewDefaultEngine is a convenience function to instantiate a filesystem-backed engine with no output constraints. -func NewDefaultEngine(dir string, persisted bool, session *string) (EngineIsh, error) { +func NewDefaultEngine(dir string, persistDb db.Db, session *string) (EngineIsh, error) { var err error st := state.NewState(0) rs := resource.NewFsResource(dir) @@ -23,18 +22,13 @@ func NewDefaultEngine(dir string, persisted bool, session *string) (EngineIsh, e } if session != nil { cfg.SessionId = *session - } else if !persisted { + } else if persistDb != nil { return nil, fmt.Errorf("session must be set if persist is used") } ctx := context.TODO() var en EngineIsh - if persisted { - dp := path.Join(dir, ".state") - err = os.MkdirAll(dp, 0700) - if err != nil { - return nil, err - } - pr := persist.NewFsPersister(dp) + if persistDb != nil { + pr := persist.NewPersister(persistDb) en, err = NewPersistedEngine(ctx, cfg, pr, rs) if err != nil { Logg.Infof("persisted engine create error. trying again with persisting empty state first...") @@ -53,7 +47,7 @@ func NewDefaultEngine(dir string, persisted bool, session *string) (EngineIsh, e } // NewSizedEngine is a convenience function to instantiate a filesystem-backed engine with a specified output constraint. -func NewSizedEngine(dir string, size uint32, persisted bool, session *string) (EngineIsh, error) { +func NewSizedEngine(dir string, size uint32, persistDb db.Db, session *string) (EngineIsh, error) { var err error st := state.NewState(0) rs := resource.NewFsResource(dir) @@ -64,18 +58,13 @@ func NewSizedEngine(dir string, size uint32, persisted bool, session *string) (E } if session != nil { cfg.SessionId = *session - } else if !persisted { + } else if persistDb != nil { return nil, fmt.Errorf("session must be set if persist is used") } ctx := context.TODO() var en EngineIsh - if persisted { - dp := path.Join(dir, ".state") - err = os.MkdirAll(dp, 0700) - if err != nil { - return nil, err - } - pr := persist.NewFsPersister(dp) + if persistDb != nil { + pr := persist.NewPersister(persistDb) en, err = NewPersistedEngine(ctx, cfg, pr, rs) if err != nil { Logg.Infof("persisted engine create error. trying again with persisting empty state first...") diff --git a/engine/persist.go b/engine/persist.go @@ -11,12 +11,12 @@ import ( // PersistedEngine adds persisted state to the Engine object. It provides a persisted state option for synchronous/interactive clients. type PersistedEngine struct { *Engine - pr persist.Persister + pr *persist.Persister } // NewPersistedEngine creates a new PersistedEngine -func NewPersistedEngine(ctx context.Context, cfg Config, pr persist.Persister, rs resource.Resource) (PersistedEngine, error) { +func NewPersistedEngine(ctx context.Context, cfg Config, pr *persist.Persister, rs resource.Resource) (PersistedEngine, error) { err := pr.Load(cfg.SessionId) if err != nil { return PersistedEngine{}, err @@ -58,7 +58,7 @@ func(pe PersistedEngine) Finish() error { // initialized state actually is available for the identifier, otherwise the method will fail. // // It will also fail if execution by the underlying Engine fails. -func RunPersisted(cfg Config, rs resource.Resource, pr persist.Persister, input []byte, w io.Writer, ctx context.Context) error { +func RunPersisted(cfg Config, rs resource.Resource, pr *persist.Persister, input []byte, w io.Writer, ctx context.Context) error { err := pr.Load(cfg.SessionId) if err != nil { return err diff --git a/engine/persist_test.go b/engine/persist_test.go @@ -2,13 +2,13 @@ package engine import ( "context" - "io/ioutil" "os" "testing" "git.defalsify.org/vise.git/cache" "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/state" + "git.defalsify.org/vise.git/db" ) func TestRunPersist(t *testing.T) { @@ -20,14 +20,10 @@ func TestRunPersist(t *testing.T) { } rs := NewFsWrapper(dataDir, nil) - persistDir, err := ioutil.TempDir("", "vise_engine_persist") - if err != nil { - t.Fatal(err) - } - st := state.NewState(3) ca := cache.NewCache().WithCacheSize(1024) - pr := persist.NewFsPersister(persistDir).WithContent(&st, ca) + store := db.NewMemDb(context.Background()) + pr := persist.NewPersister(store).WithContent(&st, ca) w := os.Stdout ctx := context.TODO() @@ -35,13 +31,13 @@ func TestRunPersist(t *testing.T) { st = state.NewState(cfg.FlagCount) ca = cache.NewCache() ca = ca.WithCacheSize(cfg.CacheSize) - pr = persist.NewFsPersister(persistDir).WithContent(&st, ca) - err = pr.Save(cfg.SessionId) + pr = persist.NewPersister(store).WithContent(&st, ca) + err := pr.Save(cfg.SessionId) if err != nil { t.Fatal(err) } - pr = persist.NewFsPersister(persistDir) + pr = persist.NewPersister(store) inputs := []string{ "", // trigger init, will not exec "1", @@ -55,7 +51,7 @@ func TestRunPersist(t *testing.T) { } } - pr = persist.NewFsPersister(persistDir) + pr = persist.NewPersister(store) err = pr.Load(cfg.SessionId) if err != nil { t.Fatal(err) @@ -80,14 +76,10 @@ func TestEnginePersist(t *testing.T) { } rs := NewFsWrapper(dataDir, nil) - persistDir, err := ioutil.TempDir("", "vise_engine_persist") - if err != nil { - t.Fatal(err) - } - st := state.NewState(3) ca := cache.NewCache().WithCacheSize(1024) - pr := persist.NewFsPersister(persistDir).WithContent(&st, ca) + store := db.NewMemDb(context.Background()) + pr := persist.NewPersister(store).WithContent(&st, ca) //w := os.Stdout ctx := context.TODO() @@ -95,8 +87,8 @@ func TestEnginePersist(t *testing.T) { st = state.NewState(cfg.FlagCount) ca = cache.NewCache() ca = ca.WithCacheSize(cfg.CacheSize) - pr = persist.NewFsPersister(persistDir).WithContent(&st, ca) - err = pr.Save(cfg.SessionId) + pr = persist.NewPersister(store).WithContent(&st, ca) + err := pr.Save(cfg.SessionId) if err != nil { t.Fatal(err) } @@ -132,7 +124,7 @@ func TestEnginePersist(t *testing.T) { t.Fatalf("expected index '1', got %v", idx) } - pr = persist.NewFsPersister(persistDir) + pr = persist.NewPersister(store) en, err = NewPersistedEngine(ctx, cfg, pr, rs) if err != nil { t.Fatal(err)