From 1dc8b054eb3e18b153fce9f72b13476e29aca141 Mon Sep 17 00:00:00 2001 From: lash Date: Fri, 27 Sep 2024 21:10:03 +0100 Subject: [PATCH 1/2] Add sub-prefix db wrapper --- internal/storage/db.go | 43 +++++++++++++++++++++++++++++ internal/storage/db_test.go | 54 +++++++++++++++++++++++++++++++++++++ internal/storage/storage.go | 2 +- 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 internal/storage/db.go create mode 100644 internal/storage/db_test.go diff --git a/internal/storage/db.go b/internal/storage/db.go new file mode 100644 index 0000000..b2ac6a9 --- /dev/null +++ b/internal/storage/db.go @@ -0,0 +1,43 @@ +package storage + +import ( + "context" + + "git.defalsify.org/vise.git/db" +) + +const ( + DATATYPE_USERSUB = 64 +) + +type SubPrefixDb struct { + store db.Db + pfx []byte +} + +func NewSubPrefixDb(store db.Db, pfx []byte) *SubPrefixDb { + return &SubPrefixDb{ + store: store, + pfx: pfx, + } +} + +func(s *SubPrefixDb) toKey(k []byte) []byte { + return append(s.pfx, k...) +} + +func(s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) { + s.store.SetPrefix(DATATYPE_USERSUB) + key = s.toKey(key) + v, err := s.store.Get(ctx, key) + if err != nil { + return nil, err + } + return v, nil +} + +func(s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error { + s.store.SetPrefix(DATATYPE_USERSUB) + key = s.toKey(key) + return s.store.Put(ctx, key, val) +} diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go new file mode 100644 index 0000000..9e2183c --- /dev/null +++ b/internal/storage/db_test.go @@ -0,0 +1,54 @@ +package storage + +import ( + "bytes" + "context" + "testing" + + memdb "git.defalsify.org/vise.git/db/mem" +) + +func TestSubPrefix(t *testing.T) { + ctx := context.Background() + db := memdb.NewMemDb() + err := db.Connect(ctx, "") + if err != nil { + t.Fatal(err) + } + sdba := NewSubPrefixDb(db, []byte("tinkywinky")) + err = sdba.Put(ctx, []byte("foo"), []byte("dipsy")) + if err != nil { + t.Fatal(err) + } + + r, err := sdba.Get(ctx, []byte("foo")) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(r, []byte("dipsy")) { + t.Fatalf("expected 'dipsy', got %s", r) + } + + sdbb := NewSubPrefixDb(db, []byte("lala")) + r, err = sdbb.Get(ctx, []byte("foo")) + if err == nil { + t.Fatal("expected not found") + } + + err = sdbb.Put(ctx, []byte("foo"), []byte("pu")) + if err != nil { + t.Fatal(err) + } + r, err = sdbb.Get(ctx, []byte("foo")) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(r, []byte("pu")) { + t.Fatalf("expected 'pu', got %s", r) + } + + r, err = sdba.Get(ctx, []byte("foo")) + if !bytes.Equal(r, []byte("dipsy")) { + t.Fatalf("expected 'dipsy', got %s", r) + } +} diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 009819e..8fc92f4 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -6,7 +6,7 @@ import ( ) const ( - DATATYPE_CUSTOM = 128 + DATATYPE_EXTEND = 128 ) type Storage struct { From 2a93ea7a0c78a4f4e5bff5524aa1b2f95fe54953 Mon Sep 17 00:00:00 2001 From: lash Date: Fri, 27 Sep 2024 21:16:08 +0100 Subject: [PATCH 2/2] Remove out-of-context extend key --- internal/storage/storage.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 8fc92f4..53f4392 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -5,10 +5,6 @@ import ( "git.defalsify.org/vise.git/persist" ) -const ( - DATATYPE_EXTEND = 128 -) - type Storage struct { Persister *persist.Persister UserdataDb db.Db