Skip to content

Commit

Permalink
feat: ability to query specific records by their row ID
Browse files Browse the repository at this point in the history
  • Loading branch information
tauraamui committed Jul 8, 2023
1 parent 2f1d57a commit 559377a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/hiarchy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func hiarchy() {
store.Save(child.UUID, &healthyishCarrotCake)
store.Save(child.UUID, &redVelvetCake)

bs, err := storage.LoadAllByOwner(store, Cake{}, child.UUID)
bs, err := storage.LoadAll(store, Cake{}, child.UUID)
for _, cake := range bs {
fmt.Printf("ROWID: %d, %+v\n", cake.ID, cake)
}
Expand Down
2 changes: 1 addition & 1 deletion example/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
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{})
bs, err := storage.LoadAll(store, Balloon{}, kvs.RootOwner{})
for _, balloon := range bs {
fmt.Printf("ROWID: %d, %+v\n", balloon.ID, balloon)
}
Expand Down
33 changes: 32 additions & 1 deletion storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,38 @@ type TableNamer interface {
TableName() string
}

func LoadAllByOwner[T TableNamer](s Store, v T, owner kvs.UUID) ([]T, error) {
func Load[T TableNamer](s Store, dest T, owner kvs.UUID, rowID uint32) error {
db := s.db

blankEntries := kvs.ConvertToBlankEntries(dest.TableName(), owner, rowID, dest)
for _, ent := range blankEntries {
db.View(func(txn *badger.Txn) error {
item, err := txn.Get(ent.Key())
if err != nil {
return err
}

if err := item.Value(func(val []byte) error {
ent.Data = val
return nil
}); err != nil {
return err
}

return nil
})

ent.RowID = rowID

if err := kvs.LoadEntry(dest, ent); err != nil {
return err
}
}

return kvs.LoadID(dest, rowID)
}

func LoadAll[T TableNamer](s Store, v T, owner kvs.UUID) ([]T, error) {
db := s.db
dest := []T{}

Expand Down
33 changes: 32 additions & 1 deletion storage/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestStoreAndLoadMultipleBalloonsSuccess(t *testing.T) {
is.NoErr(store.Save(kvs.RootOwner{}, &smallYellowBalloon))
is.NoErr(store.Save(kvs.RootOwner{}, &mediumWhiteBalloon))

bs, err := storage.LoadAllByOwner(store, Balloon{}, kvs.RootOwner{})
bs, err := storage.LoadAll(store, Balloon{}, kvs.RootOwner{})
is.NoErr(err)

is.True(len(bs) == 3)
Expand All @@ -55,6 +55,37 @@ func TestStoreAndLoadMultipleBalloonsSuccess(t *testing.T) {
is.Equal(bs[2], Balloon{ID: 2, Color: "WHITE", Size: 366})
}

func TestStoreLoadMultipleLoadIndividualBalloonsSuccess(t *testing.T) {
is := is.New(t)

db, err := kvs.NewMemKVDB()
is.NoErr(err)
defer db.Close()

store := storage.New(db)
defer store.Close()

bigRedBalloon := Balloon{Color: "RED", Size: 695}
smallYellowBalloon := Balloon{Color: "YELLOW", Size: 112}
mediumWhiteBalloon := Balloon{Color: "WHITE", Size: 366}
is.NoErr(store.Save(kvs.RootOwner{}, &bigRedBalloon))
is.NoErr(store.Save(kvs.RootOwner{}, &smallYellowBalloon))
is.NoErr(store.Save(kvs.RootOwner{}, &mediumWhiteBalloon))

bs0 := Balloon{}
is.NoErr(storage.Load(store, &bs0, kvs.RootOwner{}, 0))

bs1 := Balloon{}
is.NoErr(storage.Load(store, &bs1, kvs.RootOwner{}, 1))

bs2 := Balloon{}
is.NoErr(storage.Load(store, &bs2, kvs.RootOwner{}, 2))

is.Equal(bs0, Balloon{ID: 0, Color: "RED", Size: 695})
is.Equal(bs1, Balloon{ID: 1, Color: "YELLOW", Size: 112})
is.Equal(bs2, Balloon{ID: 2, Color: "WHITE", Size: 366})
}

func TestStoreMultipleBalloonsSuccess(t *testing.T) {
is := is.New(t)

Expand Down

0 comments on commit 559377a

Please sign in to comment.