diff --git a/v2/Makefile b/v2/Makefile new file mode 100644 index 0000000..015e0ed --- /dev/null +++ b/v2/Makefile @@ -0,0 +1,30 @@ +.DEFAULT_GOAL := default + +.PHONY: test +test: + gotestsum ./... + +.PHONY: test-verbose +test-verbose: + gotestsum --format standard-verbose ./... + +.PHONY: coverage +coverage: + go test -coverpkg=./... -coverprofile=coverage.out ./... && go tool cover -func coverage.out && rm coverage.out + +.PHONY: coverage-persist +coverage-persist: + go test -coverpkg=./... -coverprofile=coverage.out ./... && go tool cover -func coverage.out + +.PHONY: install-gotestsum +install-gotestsum: + go get github.com/gotestyourself/gotestsum + +.PHONY: install-linter +install-linter: + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.41.1 + +.PHONY: lint +lint: + golangci-lint run + diff --git a/v2/entry.go b/v2/entry.go new file mode 100644 index 0000000..8a4a681 --- /dev/null +++ b/v2/entry.go @@ -0,0 +1,243 @@ +package kvs + +import ( + "encoding/json" + "errors" + "fmt" + "reflect" + "strings" + + "github.com/dgraph-io/badger/v3" + "github.com/google/uuid" +) + +type Entry struct { + TableName string + ColumnName string + OwnerUUID UUID + RowID uint32 + Data []byte +} + +func (e Entry) PrefixKey() []byte { + return []byte(fmt.Sprintf("%s.%s.%s", e.TableName, e.ColumnName, e.resolveOwnerID())) +} + +func (e Entry) Key() []byte { + return []byte(fmt.Sprintf("%s.%s.%s.%d", e.TableName, e.ColumnName, e.resolveOwnerID(), e.RowID)) +} + +func (e Entry) resolveOwnerID() string { + if e.OwnerUUID == nil { + e.OwnerUUID = RootOwner{} + } + return e.OwnerUUID.String() +} + +func Store(db KVDB, e Entry) error { + return db.conn.Update(func(txn *badger.Txn) error { + return txn.Set([]byte(e.Key()), e.Data) + }) +} + +func Get(db KVDB, e *Entry) error { + return db.conn.View(func(txn *badger.Txn) error { + lookupKey := e.Key() + item, err := txn.Get(lookupKey) + if err != nil { + return fmt.Errorf("%s: %s", strings.ToLower(err.Error()), lookupKey) + } + + if err := item.Value(func(val []byte) error { + e.Data = val + return nil + }); err != nil { + return err + } + + return nil + }) +} + +func ConvertToBlankEntries(tableName string, ownerID UUID, rowID uint32, x any) []Entry { + v := reflect.ValueOf(x) + return convertToEntries(tableName, ownerID, rowID, v, false) +} + +func ConvertToEntries(tableName string, ownerID UUID, rowID uint32, x any) []Entry { + v := reflect.ValueOf(x) + return convertToEntries(tableName, ownerID, rowID, v, true) +} + +type UUID interface { + String() string +} + +type RootOwner struct{} + +func (o RootOwner) String() string { return "root" } + +func LoadEntry(s interface{}, entry Entry) error { + // convert the interface value to a reflect.Value so we can access its fields + val := reflect.ValueOf(s).Elem() + + field, err := resolveFieldRef(val, entry.ColumnName) + if err != nil { + return err + } + + // convert the entry's Data field to the type of the target field + if err := convertFromBytes(entry.Data, field.Addr().Interface()); err != nil { + return fmt.Errorf("failed to convert entry data to field type: %v", err) + } + + return nil +} + +func LoadID(s any, rowID uint32) error { + // convert the interface value to a reflect.Value so we can access its fields + val := reflect.ValueOf(s).Elem() + + field, err := resolveFieldRef(val, "ID") + if err != nil { + return err + } + + // convert the entry's Data field to the type of the target field + if err := assignUint32(rowID, field.Addr().Interface()); err != nil { + return fmt.Errorf("failed to convert entry data to field type: %v", err) + } + + return nil +} + +func resolveFieldRef(v reflect.Value, nameToMatch string) (reflect.Value, error) { + t := v.Type() + + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + + if strings.EqualFold(field.Name, nameToMatch) { + return v.Field(i), nil + } + } + + return reflect.Zero(reflect.TypeOf(v)), fmt.Errorf("struct does not have a field with name %q", nameToMatch) +} + +func LoadEntries(s interface{}, entries []Entry) error { + for _, entry := range entries { + if err := LoadEntry(s, entry); err != nil { + return err + } + } + + return nil +} + +func convertToEntries(tableName string, ownerUUID UUID, rowID uint32, v reflect.Value, includeData bool) []Entry { + entries := []Entry{} + + if v.Kind() == reflect.Pointer { + v = v.Elem() + } + for i := 0; i < v.NumField(); i++ { + vv := reflect.Indirect(v) + f := vv.Type().Field(i) + + fOpts := resolveFieldOptions(f) + if fOpts.Ignore { + continue + } + + e := Entry{ + TableName: tableName, + ColumnName: strings.ToLower(f.Name), + OwnerUUID: ownerUUID, + RowID: rowID, + } + + if includeData { + bd, err := convertToBytes(v.Field(i).Interface()) + if err != nil { + return entries + } + e.Data = bd + } + + entries = append(entries, e) + } + + return entries +} + +func convertToBytes(i interface{}) ([]byte, error) { + // Check the type of the interface. + switch v := i.(type) { + case []byte: + // Return the input as a []byte if it is already a []byte. + return v, nil + case string: + // Convert the string to a []byte and return it. + return []byte(v), nil + default: + // Use json.Marshal to convert the interface to a []byte. + return json.Marshal(v) + } +} + +func assignUint32(data uint32, dest any) error { + // Check that the destination argument is a pointer. + if reflect.TypeOf(dest).Kind() != reflect.Ptr { + return fmt.Errorf("destination must be a pointer") + } + + switch v := dest.(type) { + case *uint32: + *v = data + return nil + } + + return errors.New("struct field ID is not of type uint32") +} + +func convertFromBytes(data []byte, i interface{}) error { + // Check that the destination argument is a pointer. + if reflect.TypeOf(i).Kind() != reflect.Ptr { + return fmt.Errorf("destination must be a pointer") + } + + // Check the type of the interface. + switch v := i.(type) { + case *[]byte: + // Set the value of the interface to the []byte if it is a pointer to a []byte. + *v = data + return nil + case *string: + // Convert the []byte to a string and set the value of the interface to the string. + *v = string(data) + return nil + case *UUID: + // Convert the []byte to a UUID instance and set the value of the interface to it. + uuidv, err := uuid.ParseBytes(data) + if err != nil { + return err + } + *v = uuidv + return nil + default: + // Use json.Unmarshal to convert the []byte to the interface. + return json.Unmarshal(data, v) + } +} + +type mdbFieldOptions struct { + Ignore bool +} + +func resolveFieldOptions(f reflect.StructField) mdbFieldOptions { + mdbTagValue := f.Tag.Get("mdb") + return mdbFieldOptions{ + Ignore: strings.Contains(mdbTagValue, "ignore"), + } +} diff --git a/v2/entry_internal_test.go b/v2/entry_internal_test.go new file mode 100644 index 0000000..b513c3f --- /dev/null +++ b/v2/entry_internal_test.go @@ -0,0 +1,63 @@ +package kvs + +import ( + "testing" + + "github.com/matryer/is" +) + +func TestConvertToBytes(t *testing.T) { + is := is.New(t) + + // Test input and expected output. + tests := []struct { + input interface{} + expected []byte + }{ + {[]byte{1, 2, 3}, []byte{1, 2, 3}}, + {"hello", []byte("hello")}, + {struct{ A int }{5}, []byte("{\"A\":5}")}, + } + + // Iterate over the tests and compare the output of convertToBytes to the expected output. + for _, test := range tests { + result, err := convertToBytes(test.input) + is.NoErr(err) + is.Equal(result, test.expected) + } +} + +func TestConvertBytesFromBytes(t *testing.T) { + is := is.New(t) + + input := []byte{1, 2, 3} + var destination []byte + err := convertFromBytes(input, &destination) + is.NoErr(err) + is.Equal(destination, input) + +} + +func TestConvertStringFromBytes(t *testing.T) { + is := is.New(t) + + input := []byte("hello") + var destination string + err := convertFromBytes(input, &destination) + is.NoErr(err) + is.Equal(destination, string(input)) +} + +func TestConvertStructFromBytes(t *testing.T) { + is := is.New(t) + + type TestStruct struct { + A int + B string + } + input := []byte("{\"A\":5,\"B\":\"hello\"}") + var destination TestStruct + err := convertFromBytes(input, &destination) + is.NoErr(err) + is.Equal(destination, TestStruct{A: 5, B: "hello"}) +} diff --git a/v2/entry_test.go b/v2/entry_test.go new file mode 100644 index 0000000..3ef0e84 --- /dev/null +++ b/v2/entry_test.go @@ -0,0 +1,197 @@ +package kvs_test + +import ( + "testing" + + "github.com/google/uuid" + "github.com/matryer/is" + "github.com/tauraamui/kvs" +) + +func TestEntryStoreValuesInTable(t *testing.T) { + is := is.New(t) + + e := kvs.Entry{ + TableName: "users", + ColumnName: "email", + OwnerUUID: uuid.UUID{}, + Data: []byte{0x33}, + } + + db, err := kvs.NewMemKVDB() + is.NoErr(err) + defer db.Close() + + seq, err := db.GetSeq(e.PrefixKey(), 100) + is.NoErr(err) // error occurred on getting db sequence + defer seq.Release() + + id, err := seq.Next() + is.NoErr(err) // error occurred when aquiring next iter value + + e.RowID = uint32(id) + + is.NoErr(kvs.Store(db, e)) // error occurred when calling store + + newEntry := kvs.Entry{ + TableName: e.TableName, + ColumnName: e.ColumnName, + OwnerUUID: uuid.UUID{}, + RowID: e.RowID, + Data: nil, + } + is.NoErr(kvs.Get(db, &newEntry)) + + is.Equal(newEntry.Data, []byte{0x33}) +} + +type uuidstr string + +func (u uuidstr) String() string { return string(u) } + +func TestGettingEntryOutOfTableErrorIncorrectKey(t *testing.T) { + is := is.New(t) + + owner := uuidstr("11") + e := kvs.Entry{ + TableName: "user", + ColumnName: "email", + OwnerUUID: owner, + Data: []byte{0x33}, + } + + db, err := kvs.NewMemKVDB() + is.NoErr(err) + defer db.Close() + + seq, err := db.GetSeq(e.PrefixKey(), 100) + is.NoErr(err) // error occurred on getting db sequence + defer seq.Release() + + id, err := seq.Next() + is.NoErr(err) // error occurred when aquiring next iter value + + e.RowID = uint32(id) + + is.NoErr(kvs.Store(db, e)) // error occurred when calling store + + newEntry := kvs.Entry{ + TableName: e.TableName, + ColumnName: "emailz", + OwnerUUID: owner, + RowID: e.RowID, + Data: nil, + } + + err = kvs.Get(db, &newEntry) + is.True(err != nil) + is.Equal(err.Error(), "key not found: user.emailz.11.0") + is.Equal(newEntry.Data, nil) +} + +func TestConvertToEntries(t *testing.T) { + is := is.New(t) + + source := struct { + Foo string + Bar int + }{ + Foo: "Foo", + Bar: 4, + } + + owner := uuidstr("39") + e := kvs.ConvertToEntries("test", owner, 0, source) + is.Equal(len(e), 2) + + is = is.NewRelaxed(t) + + is.Equal(kvs.Entry{ + OwnerUUID: owner, + TableName: "test", + ColumnName: "foo", + Data: []byte{70, 111, 111}, + }, e[0]) + + is.Equal(kvs.Entry{ + OwnerUUID: owner, + TableName: "test", + ColumnName: "bar", + Data: []byte{52}, + }, e[1]) +} + +func TestLoadEntriesIntoStruct(t *testing.T) { + // Define a struct type to use for the test + type TestStruct struct { + Field1 string + Field2 int + Field3 bool + } + + // Create a slice of Entry values to use as input + entries := []kvs.Entry{ + {ColumnName: "field1", Data: []byte("hello")}, + {ColumnName: "field2", Data: []byte("123")}, + {ColumnName: "field3", Data: []byte("true")}, + } + + s := TestStruct{} + + is := is.New(t) + + is.NoErr(kvs.LoadEntries(&s, entries)) // LoadEntries returned an error + // Check that the values of the TestStruct fields were updated correctly + expected := TestStruct{Field1: "hello", Field2: 123, Field3: true} + is.Equal(s, expected) // Use the Equal method of the is package to compare the values +} + +func TestSequences(t *testing.T) { + is := is.New(t) + + db, err := kvs.NewMemKVDB() + is.NoErr(err) + defer db.Close() + + fruitEntry := kvs.Entry{ + TableName: "fruits", + ColumnName: "color", + } + + chocolateEntry := kvs.Entry{ + TableName: "chocolate", + ColumnName: "flavour", + } + + fruitSeq, err := db.GetSeq(fruitEntry.PrefixKey(), 100) + is.NoErr(err) // error occurred on getting db sequence + defer fruitSeq.Release() + + chocolateSeq, err := db.GetSeq(chocolateEntry.PrefixKey(), 100) + is.NoErr(err) // error occurred on getting db sequence + defer fruitSeq.Release() + + id, err := fruitSeq.Next() + is.NoErr(err) // error occurred when aquiring next iter value + is.Equal(id, uint64(0)) + + id, err = chocolateSeq.Next() + is.NoErr(err) // error occurred when aquiring next iter value + is.Equal(id, uint64(0)) + + id, err = fruitSeq.Next() + is.NoErr(err) // error occurred when aquiring next iter value + is.Equal(id, uint64(1)) + + id, err = chocolateSeq.Next() + is.NoErr(err) // error occurred when aquiring next iter value + is.Equal(id, uint64(1)) + + id, err = fruitSeq.Next() + is.NoErr(err) // error occurred when aquiring next iter value + is.Equal(id, uint64(2)) + + id, err = chocolateSeq.Next() + is.NoErr(err) // error occurred when aquiring next iter value + is.Equal(id, uint64(2)) +} diff --git a/v2/go.mod b/v2/go.mod new file mode 100644 index 0000000..505cd74 --- /dev/null +++ b/v2/go.mod @@ -0,0 +1,28 @@ +module github.com/tauraamui/kvs/v2 + +go 1.19 + +require ( + github.com/dgraph-io/badger/v3 v3.2103.5 + github.com/google/uuid v1.3.0 + github.com/matryer/is v1.4.1 + github.com/tauraamui/kvs v1.3.1 +) + +require ( + github.com/cespare/xxhash v1.1.0 // indirect + github.com/cespare/xxhash/v2 v2.1.1 // indirect + github.com/dgraph-io/ristretto v0.1.1 // indirect + github.com/dustin/go-humanize v1.0.0 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect + github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect + github.com/golang/protobuf v1.3.1 // indirect + github.com/golang/snappy v0.0.3 // indirect + github.com/google/flatbuffers v1.12.1 // indirect + github.com/klauspost/compress v1.12.3 // indirect + github.com/pkg/errors v0.9.1 // indirect + go.opencensus.io v0.22.5 // indirect + golang.org/x/net v0.3.0 // indirect + golang.org/x/sys v0.3.0 // indirect +) diff --git a/v2/go.sum b/v2/go.sum new file mode 100644 index 0000000..e0e6185 --- /dev/null +++ b/v2/go.sum @@ -0,0 +1,145 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= +github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg= +github.com/dgraph-io/badger/v3 v3.2103.5/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw= +github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= +github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA= +github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA= +github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/flatbuffers v1.12.1 h1:MVlul7pQNoDzWRLTw5imwYsl+usrS1TXG2H4jg6ImGw= +github.com/google/flatbuffers v1.12.1/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.12.3 h1:G5AfA94pHPysR56qqrkO2pxEexdDzrpFJ6yt/VqWxVU= +github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ= +github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= +github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/tauraamui/kvs v1.3.1 h1:vwblP9Dyv/gaJ28avTuf3XpuAp6zr/BrZBGyHucnmC4= +github.com/tauraamui/kvs v1.3.1/go.mod h1:hBUpb9q5bbAww5RVSa45ghYEiraWkVbCHV42NMzaCow= +github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0= +go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.3.0 h1:VWL6FNY2bEEmsGVKabSlHu5Irp34xmMRoqb/9lF9lxk= +golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/v2/kvdb.go b/v2/kvdb.go new file mode 100644 index 0000000..4ff4236 --- /dev/null +++ b/v2/kvdb.go @@ -0,0 +1,74 @@ +package kvs + +import ( + "fmt" + "io" + "os" + + "github.com/dgraph-io/badger/v3" +) + +type KVDB struct { + conn *badger.DB +} + +func NewKVDB(db *badger.DB) (KVDB, error) { + return newKVDB(db) +} + +func NewMemKVDB() (KVDB, error) { + return newKVDB(nil) +} + +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 +} + +func (db KVDB) GetSeq(key []byte, bandwidth uint64) (*badger.Sequence, error) { + return db.conn.GetSequence(key, bandwidth) +} + +func (db KVDB) View(f func(txn *badger.Txn) error) error { + return db.conn.View(f) +} + +func (db KVDB) Update(f func(txn *badger.Txn) error) error { + return db.conn.Update(f) +} + +func (db KVDB) DumpTo(w io.Writer) error { + return db.conn.View(func(txn *badger.Txn) error { + opts := badger.DefaultIteratorOptions + opts.PrefetchSize = 10 + it := txn.NewIterator(opts) + defer it.Close() + for it.Rewind(); it.Valid(); it.Next() { + item := it.Item() + k := item.Key() + err := item.Value(func(v []byte) error { + fmt.Fprintf(w, "key=%s, value=%s\n", k, v) + return nil + }) + if err != nil { + return err + } + } + return nil + }) +} + +func (db KVDB) DumpToStdout() error { + return db.DumpTo(os.Stdout) +} + +func (db KVDB) Close() error { + return db.conn.Close() +} diff --git a/v2/storage/store.go b/v2/storage/store.go new file mode 100644 index 0000000..356fd94 --- /dev/null +++ b/v2/storage/store.go @@ -0,0 +1,205 @@ +package storage + +import ( + "fmt" + "strconv" + "strings" + + "github.com/dgraph-io/badger/v3" + "github.com/tauraamui/kvs" +) + +type Value interface { + TableName() string +} + +type Store struct { + db kvs.KVDB + pks map[string]*badger.Sequence +} + +func New(db kvs.KVDB) Store { + return Store{db: db, pks: map[string]*badger.Sequence{}} +} + +func (s Store) Save(owner kvs.UUID, value Value) error { + rowID, err := nextRowID(s.db, owner, value.TableName(), s.pks) + if err != nil { + return err + } + + 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 + } + entries := kvs.ConvertToEntries(tableName, ownerID, rowID, v) + for _, e := range entries { + if err := kvs.Store(db, e); err != nil { + return err + } + } + + 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 + + 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 extractRowFromKey(k string) (int, error) { + rowPos := strings.LastIndex(k, ".") + return strconv.Atoi(k[rowPos+1:]) +} + +func LoadAll[T Value](s Store, v T, owner kvs.UUID) ([]T, error) { + db := s.db + dest := []T{} + + blankEntries := kvs.ConvertToBlankEntries(v.TableName(), owner, 0, v) + for _, ent := range blankEntries { + // iterate over all stored values for this entry + prefix := ent.PrefixKey() + if err := db.View(func(txn *badger.Txn) error { + it := txn.NewIterator(badger.DefaultIteratorOptions) + defer it.Close() + + var structFieldIndex uint32 = 0 + for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { + item := it.Item() + + if len(dest) == 0 || structFieldIndex >= uint32(len(dest)) { + key := string(item.Key()) + rowID, err := extractRowFromKey(string(key)) + if err != nil { + return err + } + + dest = append(dest, *new(T)) + + // for reasons, we have to just keep assigning the current "field we're on" as the full entry's ID + if err := kvs.LoadID(&dest[structFieldIndex], uint32(rowID)); err != nil { + return err + } + } + + ent.RowID = structFieldIndex + if err := item.Value(func(val []byte) error { + ent.Data = val + return nil + }); err != nil { + return err + } + + if err := kvs.LoadEntry(&dest[structFieldIndex], ent); err != nil { + return err + } + structFieldIndex++ + } + return nil + }); err != nil { + return nil, err + } + } + return dest, nil +} + +func (s Store) Close() (err error) { + if s.pks == nil { + return + } + for _, seq := range s.pks { + if seq == nil { + continue + } + // TODO:(tauraamui) should collect all errors into group here rather than immediately escape + if err = seq.Release(); err != nil { + return + } + } + + s.pks = nil + + return +} + +func nextRowID(db kvs.KVDB, owner kvs.UUID, tableName string, pks map[string]*badger.Sequence) (uint32, error) { + seq, err := resolveSequence(db, fmt.Sprintf("%s.%s", owner, tableName), pks) + if err != nil { + return 0, err + } + + s, err := seq.Next() + if err != nil { + return 0, err + } + return uint32(s), nil +} + +func nextSequence(seq *badger.Sequence) (uint32, error) { + s, err := seq.Next() + if err != nil { + return 0, err + } + return uint32(s), nil +} + +func resolveSequence(db kvs.KVDB, sequenceKey string, pks map[string]*badger.Sequence) (*badger.Sequence, error) { + seq, ok := pks[sequenceKey] + var err error + if !ok { + seq, err = db.GetSeq([]byte(sequenceKey), 1) + if err != nil { + return nil, err + } + pks[sequenceKey] = seq + } + + return seq, nil +} diff --git a/v2/storage/store_test.go b/v2/storage/store_test.go new file mode 100644 index 0000000..e5623cc --- /dev/null +++ b/v2/storage/store_test.go @@ -0,0 +1,199 @@ +package storage_test + +import ( + "testing" + + "github.com/matryer/is" + "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" } + +type Cake struct { + ID uint32 `mdb:"ignore"` + Type string + Calories int +} + +func (b Cake) TableName() string { return "cakes" } + +func TestStoreAndLoadMultipleBalloonsSuccess(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.Equal(len(bs), 3) + + is.Equal(bs[0], Balloon{ID: 0, Color: "RED", Size: 695}) + is.Equal(bs[1], Balloon{ID: 1, Color: "YELLOW", Size: 112}) + 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) + + 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) + + 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)) + + is.Equal(bigRedBalloon.ID, uint32(0)) + is.Equal(smallYellowBalloon.ID, uint32(1)) + is.Equal(mediumWhiteBalloon.ID, uint32(2)) +} + +func TestStoreMultipleBalloonsAndCakesInSuccessionRetainsCorrectRowIDs(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} + disguistingVeganCake := Cake{Type: "INEDIBLE", Calories: -38} + smallYellowBalloon := Balloon{Color: "YELLOW", Size: 112} + healthyishCarrotCake := Cake{Type: "CARROT", Calories: 280} + mediumWhiteBalloon := Balloon{Color: "WHITE", Size: 366} + redVelvetCake := Cake{Type: "RED_VELVET", Calories: 410} + + is.NoErr(store.Save(kvs.RootOwner{}, &bigRedBalloon)) + is.NoErr(store.Save(kvs.RootOwner{}, &smallYellowBalloon)) + is.NoErr(store.Save(kvs.RootOwner{}, &mediumWhiteBalloon)) + + is.NoErr(store.Save(kvs.RootOwner{}, &disguistingVeganCake)) + is.NoErr(store.Save(kvs.RootOwner{}, &healthyishCarrotCake)) + is.NoErr(store.Save(kvs.RootOwner{}, &redVelvetCake)) + + is.Equal(bigRedBalloon.ID, uint32(0)) + is.Equal(disguistingVeganCake.ID, uint32(0)) + is.Equal(smallYellowBalloon.ID, uint32(1)) + is.Equal(healthyishCarrotCake.ID, uint32(1)) + is.Equal(mediumWhiteBalloon.ID, uint32(2)) + is.Equal(redVelvetCake.ID, uint32(2)) +}