commit 6c3cb1731bb08eebdb29674c58a4530508dbb2b2
parent ffab76edad7c521b3701ad820ff3d2ba2e8152a4
Author: lash <dev@holbrook.no>
Date: Fri, 30 Aug 2024 19:43:44 +0100
Fix examples and tools broken by persister change
Diffstat:
5 files changed, 38 insertions(+), 19 deletions(-)
diff --git a/db/fs.go b/db/fs.go
@@ -2,7 +2,6 @@ package db
import (
"context"
- "fmt"
"io/ioutil"
"os"
"path"
@@ -19,13 +18,10 @@ func(fdb *FsDb) Connect(ctx context.Context, connStr string) error {
if fdb.dir != "" {
return nil
}
- fi, err := os.Stat(connStr)
+ err := os.MkdirAll(connStr, 0700)
if err != nil {
return err
}
- if !fi.IsDir() {
- return fmt.Errorf("fs db %s is not a directory", connStr)
- }
fdb.dir = connStr
return nil
}
@@ -68,6 +64,6 @@ func(fdb *FsDb) pathFor(key []byte) (string, error) {
if err != nil {
return "", err
}
- kb[0] += 30
+ kb[0] += 0x30
return path.Join(fdb.dir, string(kb)), nil
}
diff --git a/dev/interactive/main.go b/dev/interactive/main.go
@@ -7,24 +7,34 @@ import (
"os"
"git.defalsify.org/vise.git/engine"
+ "git.defalsify.org/vise.git/db"
)
func main() {
+ var store db.Db
var dir string
var root string
var size uint
var sessionId string
- var persist bool
+ var persist string
flag.StringVar(&dir, "d", ".", "resource dir to read from")
flag.UintVar(&size, "s", 0, "max size of output")
flag.StringVar(&root, "root", "root", "entry point symbol")
flag.StringVar(&sessionId, "session-id", "default", "session id")
- flag.BoolVar(&persist, "persist", false, "use state persistence")
+ flag.StringVar(&persist, "p", "", "state persistence directory")
flag.Parse()
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
ctx := context.Background()
- en, err := engine.NewSizedEngine(dir, uint32(size), persist, &sessionId)
+ if persist != "" {
+ store = &db.FsDb{}
+ err := store.Connect(ctx, persist)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "db connect error: %v", err)
+ os.Exit(1)
+ }
+ }
+ en, err := engine.NewSizedEngine(dir, uint32(size), store, &sessionId)
if err != nil {
fmt.Fprintf(os.Stderr, "engine create error: %v", err)
os.Exit(1)
diff --git a/examples/languages/main.go b/examples/languages/main.go
@@ -15,6 +15,7 @@ import (
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state"
+ "git.defalsify.org/vise.git/db"
)
const (
@@ -96,19 +97,20 @@ func main() {
ctx := context.Background()
dp := path.Join(scriptDir, ".state")
- err := os.MkdirAll(dp, 0700)
+ store := &db.FsDb{}
+ err := store.Connect(ctx, dp)
if err != nil {
- engine.Logg.ErrorCtxf(ctx, "cannot create state dir", "err", err)
+ engine.Logg.ErrorCtxf(ctx, "db connect fail", "err", err)
os.Exit(1)
}
- pr := persist.NewFsPersister(dp)
+ pr := persist.NewPersister(store)
en, err := engine.NewPersistedEngine(ctx, cfg, pr, rs)
if err != nil {
engine.Logg.Infof("persisted engine create error. trying again with persisting empty state first...")
pr = pr.WithContent(&st, ca)
err = pr.Save(cfg.SessionId)
if err != nil {
- engine.Logg.ErrorCtxf(ctx, "fail state save: %v", err)
+ engine.Logg.ErrorCtxf(ctx, "fail state save", "err", err)
os.Exit(1)
}
en, err = engine.NewPersistedEngine(ctx, cfg, pr, rs)
diff --git a/examples/longmenu/main.go b/examples/longmenu/main.go
@@ -10,6 +10,7 @@ import (
testdataloader "github.com/peteole/testdata-loader"
"git.defalsify.org/vise.git/engine"
+ "git.defalsify.org/vise.git/db"
)
var (
baseDir = testdataloader.GetBasePath()
@@ -31,7 +32,15 @@ func main() {
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
ctx := context.Background()
- en, err := engine.NewSizedEngine(dir, uint32(size), persist, &sessionId)
+ dp := path.Join(scriptDir, ".state")
+ store := &db.FsDb{}
+ err := store.Connect(ctx, dp)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "db connect error: %v", err)
+ os.Exit(1)
+ }
+ defer store.Close()
+ en, err := engine.NewSizedEngine(dir, uint32(size), store, &sessionId)
if err != nil {
fmt.Fprintf(os.Stderr, "engine create error: %v", err)
os.Exit(1)
diff --git a/examples/state_passive/main.go b/examples/state_passive/main.go
@@ -13,6 +13,7 @@ import (
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/cache"
"git.defalsify.org/vise.git/persist"
+ "git.defalsify.org/vise.git/db"
)
const (
@@ -24,7 +25,7 @@ const (
type fsData struct {
path string
- persister persist.Persister
+ persister *persist.Persister
}
func (fsd *fsData) peek(ctx context.Context, sym string, input []byte) (resource.Result, error) {
@@ -91,14 +92,15 @@ func main() {
}
dp := path.Join(dir, ".state")
-
- err := os.MkdirAll(dp, 0700)
+ store := &db.FsDb{}
+ err := store.Connect(ctx, dp)
if err != nil {
- fmt.Fprintf(os.Stderr, "state dir create exited with error: %v\n", err)
+ fmt.Fprintf(os.Stderr, "db connect fail: %s", err)
os.Exit(1)
}
- pr := persist.NewFsPersister(dp)
+ pr := persist.NewPersister(store)
en, err := engine.NewPersistedEngine(ctx, cfg, pr, rs)
+
if err != nil {
pr = pr.WithContent(&st, ca)
err = pr.Save(cfg.SessionId)