go-vise

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

commit 0298fed1acfe163ec21673cac7c3d000b3fac41a
parent c0b1f3302eeb366555bb62a32d097be4b6ce0081
Author: lash <dev@holbrook.no>
Date:   Fri, 30 Aug 2024 23:42:24 +0100

Set per datatype lock

Diffstat:
Mdb/db.go | 20+++++++++++++-------
Mdb/fs.go | 6++++++
Mdb/gdbm.go | 6++++++
Mdb/mem.go | 1+
Mdb/pg.go | 4+++-
Mexamples/db/main.go | 4++--
Mresource/db_test.go | 12++++++------
7 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/db/db.go b/db/db.go @@ -54,7 +54,11 @@ func ToDbKey(typ uint8, b []byte, l *lang.Language) []byte { type BaseDb struct { pfx uint8 sid []byte - unsafe bool + lock uint8 +} + +func(db *BaseDb) defaultLock() { + db.lock = DATATYPE_BIN | DATATYPE_MENU | DATATYPE_TEMPLATE } // SetPrefix implements Db. @@ -67,15 +71,17 @@ func(db *BaseDb) SetSession(sessionId string) { db.sid = append([]byte(sessionId), 0x2E) } -func(db *BaseDb) SetSafety(safe bool) { - db.unsafe = !safe +// SetSafety disables modification of data that +func(db *BaseDb) SetLock(pfx uint8, lock bool) { + if lock { + db.lock |= pfx + } else { + db.lock &= ^pfx + } } func(db *BaseDb) checkPut() bool { - if db.unsafe { - return true - } - return db.pfx > datatype_sessioned_threshold + return db.pfx & db.lock == 0 } // ToKey creates a DbKey within the current session context. diff --git a/db/fs.go b/db/fs.go @@ -14,6 +14,12 @@ type FsDb struct { dir string } +func NewFsDb() *FsDb { + db := &FsDb{} + db.BaseDb.defaultLock() + return db +} + // Connect implements Db func(fdb *FsDb) Connect(ctx context.Context, connStr string) error { if fdb.dir != "" { diff --git a/db/gdbm.go b/db/gdbm.go @@ -15,6 +15,12 @@ type GdbmDb struct { prefix uint8 } +func NewGdbmDb() *GdbmDb { + db := &GdbmDb{} + db.BaseDb.defaultLock() + return db +} + // Connect implements Db func(gdb *GdbmDb) Connect(ctx context.Context, connStr string) error { var db *gdbm.Database diff --git a/db/mem.go b/db/mem.go @@ -15,6 +15,7 @@ type MemDb struct { // NewMemDb returns an already allocated func NewMemDb(ctx context.Context) *MemDb { db := &MemDb{} + db.BaseDb.defaultLock() _ = db.Connect(ctx, "") return db } diff --git a/db/pg.go b/db/pg.go @@ -18,9 +18,11 @@ type PgDb struct { // NewPgDb creates a new PgDb reference. func NewPgDb() *PgDb { - return &PgDb{ + db := &PgDb{ schema: "public", } + db.BaseDb.defaultLock() + return db } // WithSchema sets the Postgres schema to use for the storage table. diff --git a/examples/db/main.go b/examples/db/main.go @@ -102,7 +102,7 @@ func main() { store.Connect(ctx, dataDir) store.SetSession("xyzzy") - store.SetSafety(false) + store.SetLock(db.DATATYPE_TEMPLATE | db.DATATYPE_MENU | db.DATATYPE_BIN, false) err := genCode(ctx, store) if err != nil { panic(err) @@ -117,7 +117,7 @@ func main() { if err != nil { panic(err) } - store.SetSafety(true) + store.SetLock(db.DATATYPE_TEMPLATE | db.DATATYPE_MENU | db.DATATYPE_BIN, true) tg, err := resource.NewDbFuncGetter(store, db.DATATYPE_TEMPLATE, db.DATATYPE_MENU, db.DATATYPE_BIN) if err != nil { diff --git a/resource/db_test.go b/resource/db_test.go @@ -29,12 +29,12 @@ func TestDb(t *testing.T) { if err == nil { t.Fatal("expected error") } - store.SetSafety(false) + store.SetLock(db.DATATYPE_TEMPLATE, false) err = store.Put(ctx, []byte("foo"), []byte("bar")) if err != nil { t.Fatal(err) } - store.SetSafety(true) + store.SetLock(db.DATATYPE_TEMPLATE, true) s, err = rs.GetTemplate(ctx, "foo") if err != nil { t.Fatal(err) @@ -45,12 +45,12 @@ func TestDb(t *testing.T) { // test support check store.SetPrefix(db.DATATYPE_BIN) - store.SetSafety(false) + store.SetLock(db.DATATYPE_BIN, false) err = store.Put(ctx, []byte("xyzzy"), []byte("deadbeef")) if err != nil { t.Fatal(err) } - store.SetSafety(true) + store.SetLock(db.DATATYPE_BIN, true) rs.WithCodeGetter(tg.GetCode) b, err := rs.GetCode(ctx, "xyzzy") @@ -78,12 +78,12 @@ func TestDb(t *testing.T) { t.Fatal(err) } store.SetPrefix(db.DATATYPE_MENU) - store.SetSafety(false) + store.SetLock(db.DATATYPE_MENU, false) err = store.Put(ctx, []byte("inky"), []byte("pinky")) if err != nil { t.Fatal(err) } - store.SetSafety(true) + store.SetLock(db.DATATYPE_MENU, true) rs.WithMenuGetter(tg.GetMenu) }