From 98598152b67fc18d43601977f8a88762adf073c5 Mon Sep 17 00:00:00 2001 From: tauraamui Date: Thu, 6 Jul 2023 16:42:41 +0100 Subject: [PATCH] refactor: massively simplify example --- example/example_data.go | 9 ---- example/main.go | 38 +++++++++++++ example/repo.go | 115 ---------------------------------------- example/repo_test.go | 38 ------------- 4 files changed, 38 insertions(+), 162 deletions(-) delete mode 100644 example/example_data.go create mode 100644 example/main.go delete mode 100644 example/repo.go delete mode 100644 example/repo_test.go diff --git a/example/example_data.go b/example/example_data.go deleted file mode 100644 index 8569d13..0000000 --- a/example/example_data.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -type ExampleData struct { - ID uint32 `mdb:"ignore"` - Title string -} - -func (d *ExampleData) SetID(id uint32) { d.ID = id } -func (d *ExampleData) Ref() any { return d } diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..c005183 --- /dev/null +++ b/example/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + + "github.com/tauraamui/kvs" + "github.com/tauraamui/kvs/storage" +) + +type Balloon struct { + ID uint32 `mdb:"ignore"` + Color string + Size int +} + +func (b Balloon) TableName() string { return "balloons" } +func (b *Balloon) SetID(id uint32) { b.ID = id } +func (b *Balloon) Ref() any { return b } + +func main() { + db, err := kvs.NewMemKVDB() + if err != nil { + panic(err) + } + defer db.Close() + + store := storage.New(db) + defer store.Close() + + store.Save(kvs.RootOwner{}, &Balloon{Color: "RED", Size: 695}) + store.Save(kvs.RootOwner{}, &Balloon{Color: "WHITE", Size: 366}) + + bs, err := storage.LoadAllByOwner(store, Balloon{}, kvs.RootOwner{}) + for rowID, balloon := range bs { + fmt.Printf("ROWID: %d, %+v\n", rowID, balloon) + } + +} diff --git a/example/repo.go b/example/repo.go deleted file mode 100644 index df6b98d..0000000 --- a/example/repo.go +++ /dev/null @@ -1,115 +0,0 @@ -package main - -import ( - "github.com/dgraph-io/badger/v3" - "github.com/tauraamui/kvs" -) - -const ( - exampleTableName = "example" -) - -type ExampleRepo struct { - DB kvs.KVDB - seq *badger.Sequence -} - -func (r ExampleRepo) Save(owner kvs.UUID, val Value) error { - rowID, err := r.nextRowID() - if err != nil { - return err - } - - return saveValue(r.DB, r.tableName(), owner, rowID, val) -} - -func (r ExampleRepo) FetchByOwner(owner kvs.UUID) ([]ExampleData, error) { - return fetchByOwner[ExampleData](r.DB, r.tableName(), owner) -} - -func (r ExampleRepo) tableName() string { - return exampleTableName -} - -func (r ExampleRepo) nextRowID() (uint32, error) { - if r.seq == nil { - seq, err := r.DB.GetSeq([]byte(exampleTableName), 1) - if err != nil { - return 0, err - } - r.seq = seq - } - - s, err := r.seq.Next() - if err != nil { - return 0, err - } - return uint32(s), nil -} - -func (r ExampleRepo) Close() error { - if r.seq == nil { - return nil - } - r.seq.Release() - return nil -} - -type Value interface { - SetID(id uint32) - Ref() interface{} -} - -func saveValue(db kvs.KVDB, tableName string, ownerID kvs.UUID, rowID uint32, v Value) error { - if v == nil { - return nil - } - entries := kvs.ConvertToEntries(tableName, ownerID, rowID, v) - for _, e := range entries { - if err := kvs.Store(db, e); err != nil { - return err - } - } - - v.SetID(rowID) - - return nil -} - -func fetchByOwner[E any](db kvs.KVDB, tableName string, owner kvs.UUID) ([]E, error) { - dest := []E{} - - typeRef := new(E) - - blankEntries := kvs.ConvertToBlankEntries(tableName, owner, 0, typeRef) - for _, ent := range blankEntries { - // iterate over all stored values for this entry - prefix := ent.PrefixKey() - db.View(func(txn *badger.Txn) error { - it := txn.NewIterator(badger.DefaultIteratorOptions) - defer it.Close() - - var rows uint32 = 0 - for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { - if len(dest) == 0 || rows >= uint32(len(dest)) { - dest = append(dest, *new(E)) - } - item := it.Item() - ent.RowID = rows - if err := item.Value(func(val []byte) error { - ent.Data = val - return nil - }); err != nil { - return err - } - - if err := kvs.LoadEntry(&dest[rows], ent); err != nil { - return err - } - rows++ - } - return nil - }) - } - return dest, nil -} diff --git a/example/repo_test.go b/example/repo_test.go deleted file mode 100644 index dd6fff9..0000000 --- a/example/repo_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "testing" - - "github.com/matryer/is" - "github.com/tauraamui/kvs" -) - -func resolveGenericRepo() (ExampleRepo, error) { - db, err := kvs.NewMemKVDB() - if err != nil { - return ExampleRepo{}, err - } - - return ExampleRepo{DB: db}, nil -} - -func TestSaveGeneric(t *testing.T) { - is := is.New(t) - - r, err := resolveGenericRepo() - is.NoErr(err) - defer r.Close() - - fakeDataOne := ExampleData{ - Title: "Fake", - } - - fakeDataTwo := ExampleData{ - Title: "Fakefake", - } - - is.NoErr(r.Save(kvs.RootOwner{}, &fakeDataOne)) - is.NoErr(r.Save(kvs.RootOwner{}, &fakeDataTwo)) - is.Equal(fakeDataOne.ID, uint32(0)) - is.Equal(fakeDataTwo.ID, uint32(1)) -}