commit 88277264aecd1b7d30351d8c076e73d92445a23f
parent 2c585fdd52a4b21304b4d386805f324ed3cf2d4e
Author: lash <dev@holbrook.no>
Date: Sun, 1 Sep 2024 00:23:27 +0100
Add alt legacy fs lookup to db
Diffstat:
4 files changed, 63 insertions(+), 9 deletions(-)
diff --git a/db/fs.go b/db/fs.go
@@ -21,7 +21,7 @@ func NewFsDb() *fsDb {
return db
}
-// Connect implements Db
+// Connect implements the Db interface.
func(fdb *fsDb) Connect(ctx context.Context, connStr string) error {
if fdb.dir != "" {
panic("already connected")
@@ -34,15 +34,24 @@ func(fdb *fsDb) Connect(ctx context.Context, connStr string) error {
return nil
}
-// Get implements Db
+// Get implements the Db interface.
func(fdb *fsDb) Get(ctx context.Context, key []byte) ([]byte, error) {
fp, err := fdb.pathFor(key)
if err != nil {
return nil, err
}
+ logg.TraceCtxf(ctx, "trying fs get", "key", key, "path", fp)
f, err := os.Open(fp)
if err != nil {
- return nil, NewErrNotFound([]byte(fp))
+ fp, err = fdb.altPathFor(key)
+ if err != nil {
+ return nil, err
+ }
+ logg.TraceCtxf(ctx, "trying fs get alt", "key", key, "path", fp)
+ f, err = os.Open(fp)
+ if err != nil {
+ return nil, NewErrNotFound([]byte(fp))
+ }
}
defer f.Close()
b, err := ioutil.ReadAll(f)
@@ -52,7 +61,7 @@ func(fdb *fsDb) Get(ctx context.Context, key []byte) ([]byte, error) {
return b, nil
}
-// Put implements Db
+// Put implements the Db interface.
func(fdb *fsDb) Put(ctx context.Context, key []byte, val []byte) error {
if !fdb.checkPut() {
return errors.New("unsafe put and safety set")
@@ -64,12 +73,12 @@ func(fdb *fsDb) Put(ctx context.Context, key []byte, val []byte) error {
return ioutil.WriteFile(fp, val, 0600)
}
-// Close implements Db
+// Close implements the Db interface..
func(fdb *fsDb) Close() error {
return nil
}
-// create a key safe for the filesystem
+// create a key safe for the filesystem.
func(fdb *fsDb) pathFor(key []byte) (string, error) {
kb, err := fdb.ToKey(key)
if err != nil {
@@ -78,3 +87,12 @@ func(fdb *fsDb) pathFor(key []byte) (string, error) {
kb[0] += 0x30
return path.Join(fdb.dir, string(kb)), nil
}
+
+// create a key safe for the filesystem, matching legacy resource.FsResource name.
+func(fdb *fsDb) altPathFor(key []byte) (string, error) {
+ kb, err := fdb.ToKey(key)
+ if err != nil {
+ return "", err
+ }
+ return path.Join(fdb.dir, string(kb[1:])), nil
+}
diff --git a/db/fs_test.go b/db/fs_test.go
@@ -4,6 +4,8 @@ import (
"bytes"
"context"
"io/ioutil"
+ "os"
+ "path"
"testing"
)
@@ -42,3 +44,37 @@ func TestPutGetFs(t *testing.T) {
t.Fatal("expected get error for key 'bar'")
}
}
+
+func TestPutGetFsAlt(t *testing.T) {
+ ctx := context.Background()
+ sid := "zezion"
+ d, err := ioutil.TempDir("", "vise-db-*")
+ if err != nil {
+ t.Fatal(err)
+ }
+ db := NewFsDb()
+ db.SetPrefix(DATATYPE_TEMPLATE)
+ db.SetSession(sid)
+
+ fp := path.Join(d, sid)
+ err = os.MkdirAll(fp, 0700)
+ if err != nil {
+ t.Fatal(err)
+ }
+ db.Connect(ctx, fp)
+ fp = path.Join(fp, "inky")
+
+ b := []byte("pinky blinky clyde")
+ err = ioutil.WriteFile(fp, b, 0700)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ v, err := db.Get(ctx, []byte("inky"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(v, b) {
+ t.Fatalf("expected %x, got %x", b, v)
+ }
+}
diff --git a/db/log.go b/db/log.go
@@ -5,5 +5,5 @@ import (
)
var (
- Logg logging.Logger = logging.NewVanilla().WithDomain("db")
+ logg logging.Logger = logging.NewVanilla().WithDomain("db")
)
diff --git a/db/mem.go b/db/mem.go
@@ -40,7 +40,7 @@ func(mdb *memDb) Get(ctx context.Context, key []byte) ([]byte, error) {
if err != nil {
return nil, err
}
- Logg.TraceCtxf(ctx, "mem get", "k", k)
+ logg.TraceCtxf(ctx, "mem get", "k", k)
v, ok := mdb.store[k]
if !ok {
b, _ := hex.DecodeString(k)
@@ -59,7 +59,7 @@ func(mdb *memDb) Put(ctx context.Context, key []byte, val []byte) error {
return err
}
mdb.store[k] = val
- Logg.TraceCtxf(ctx, "mem put", "k", k, "v", val)
+ logg.TraceCtxf(ctx, "mem put", "k", k, "v", val)
return nil
}