Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tauraamui/kvs
Browse files Browse the repository at this point in the history
  • Loading branch information
tauraamui committed Jul 11, 2023
2 parents df18ff8 + f22bf91 commit b065616
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
15 changes: 9 additions & 6 deletions kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ type KVDB struct {
}

func NewKVDB(db *badger.DB) (KVDB, error) {
return newKVDB(false)
return newKVDB(db)
}

func NewMemKVDB() (KVDB, error) {
return newKVDB(true)
return newKVDB(nil)
}

func newKVDB(inMemory bool) (KVDB, error) {
db, err := badger.Open(badger.DefaultOptions("").WithLogger(nil).WithInMemory(inMemory))
if err != nil {
return KVDB{}, err
func newKVDB(db *badger.DB) (KVDB, error) {
if db == nil {
db, err := badger.Open(badger.DefaultOptions("").WithLogger(nil).WithInMemory(true))
if err != nil {
return KVDB{}, err
}
return KVDB{conn: db}, nil
}

return KVDB{conn: db}, nil
Expand Down
17 changes: 17 additions & 0 deletions storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (s Store) Save(owner kvs.UUID, value Value) error {
return saveValue(s.db, value.TableName(), owner, rowID, value)
}

func (s Store) Update(owner kvs.UUID, value Value, rowID uint32) error {
return saveValue(s.db, value.TableName(), owner, rowID, value)
}

func saveValue(db kvs.KVDB, tableName string, ownerID kvs.UUID, rowID uint32, v Value) error {
if v == nil {
return nil
Expand All @@ -44,6 +48,19 @@ func saveValue(db kvs.KVDB, tableName string, ownerID kvs.UUID, rowID uint32, v
return kvs.LoadID(v, rowID)
}

func (s Store) Delete(owner kvs.UUID, value Value, rowID uint32) error {
db := s.db

blankEntries := kvs.ConvertToBlankEntries(value.TableName(), owner, rowID, value)
for _, ent := range blankEntries {
db.Update(func(txn *badger.Txn) error {
return txn.Delete(ent.Key())
})
}

return nil
}

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

Expand Down
61 changes: 61 additions & 0 deletions storage/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,67 @@ func TestStoreAndLoadMultipleBalloonsSuccess(t *testing.T) {
is.Equal(bs[2], Balloon{ID: 2, Color: "WHITE", Size: 366})
}

func TestStoreMultipleAndUpdateSingleBalloonsSuccess(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))

smallYellowBalloon.Color = "PINK"
is.NoErr(store.Update(kvs.RootOwner{}, &smallYellowBalloon, smallYellowBalloon.ID))

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

is.True(len(bs) == 3)

is.Equal(bs[0], Balloon{ID: 0, Color: "RED", Size: 695})
is.Equal(bs[1], Balloon{ID: 1, Color: "PINK", Size: 112})
is.Equal(bs[2], Balloon{ID: 2, Color: "WHITE", Size: 366})
}

func TestStoreAndDeleteSingleBalloonSuccess(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))

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

is.True(len(bs) == 3)

is.NoErr(store.Delete(kvs.RootOwner{}, &smallYellowBalloon, smallYellowBalloon.ID))

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

is.True(len(bs) == 2)
}

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

Expand Down

0 comments on commit b065616

Please sign in to comment.