Skip to content

Commit

Permalink
allow passage of metadata to state store API (dapr#262)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Shmulevich <[email protected]>
  • Loading branch information
dmitsh authored Feb 24, 2022
1 parent 49deb75 commit a0a4a9e
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 6,384 deletions.
6 changes: 3 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ data := []byte("hello")
store := "my-store" // defined in the component YAML

// save state with the key key1, default options: strong, last-write
if err := client.SaveState(ctx, store, "key1", data); err != nil {
if err := client.SaveState(ctx, store, "key1", data, nil); err != nil {
panic(err)
}

// get state for key key1
item, err := client.GetState(ctx, store, "key1")
item, err := client.GetState(ctx, store, "key1", nil)
if err != nil {
panic(err)
}
fmt.Printf("data [key:%s etag:%s]: %s", item.Key, item.Etag, string(item.Value))

// delete state for key key1
if err := client.DeleteState(ctx, store, "key1"); err != nil {
if err := client.DeleteState(ctx, store, "key1", nil); err != nil {
panic(err)
}
```
Expand Down
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ type Client interface {
GetBulkSecret(ctx context.Context, storeName string, meta map[string]string) (data map[string]map[string]string, err error)

// SaveState saves the raw data into store using default state options.
SaveState(ctx context.Context, storeName, key string, data []byte, so ...StateOption) error
SaveState(ctx context.Context, storeName, key string, data []byte, meta map[string]string, so ...StateOption) error

// SaveBulkState saves multiple state item to store with specified options.
SaveBulkState(ctx context.Context, storeName string, items ...*SetStateItem) error

// GetState retrieves state from specific store using default consistency option.
GetState(ctx context.Context, storeName, key string) (item *StateItem, err error)
GetState(ctx context.Context, storeName, key string, meta map[string]string) (item *StateItem, err error)

// GetStateWithConsistency retrieves state from specific store using provided state consistency.
GetStateWithConsistency(ctx context.Context, storeName, key string, meta map[string]string, sc StateConsistency) (item *StateItem, err error)
Expand All @@ -102,7 +102,7 @@ type Client interface {
QueryStateAlpha1(ctx context.Context, storeName, query string, meta map[string]string) (*QueryResponse, error)

// DeleteState deletes content from store using default state options.
DeleteState(ctx context.Context, storeName, key string) error
DeleteState(ctx context.Context, storeName, key string, meta map[string]string) error

// DeleteStateWithETag deletes content from store using provided state options and etag.
DeleteStateWithETag(ctx context.Context, storeName, key string, etag *ETag, meta map[string]string, opts *StateOptions) error
Expand All @@ -111,7 +111,7 @@ type Client interface {
ExecuteStateTransaction(ctx context.Context, storeName string, meta map[string]string, ops []*StateOperation) error

// DeleteBulkState deletes content for multiple keys from store.
DeleteBulkState(ctx context.Context, storeName string, keys []string) error
DeleteBulkState(ctx context.Context, storeName string, keys []string, meta map[string]string) error

// DeleteBulkState deletes content for multiple keys from store.
DeleteBulkStateItems(ctx context.Context, storeName string, items []*DeleteStateItem) error
Expand Down
22 changes: 14 additions & 8 deletions client/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,20 @@ func (c *GRPCClient) ExecuteStateTransaction(ctx context.Context, storeName stri
}

// SaveState saves the raw data into store, default options: strong, last-write.
func (c *GRPCClient) SaveState(ctx context.Context, storeName, key string, data []byte, so ...StateOption) error {
func (c *GRPCClient) SaveState(ctx context.Context, storeName, key string, data []byte, meta map[string]string, so ...StateOption) error {
stateOptions := new(StateOptions)
for _, o := range so {
o(stateOptions)
}
if len(so) == 0 {
stateOptions = copyStateOptionDefault()
}
item := &SetStateItem{Key: key, Value: data, Options: stateOptions}
item := &SetStateItem{
Key: key,
Value: data,
Metadata: meta,
Options: stateOptions,
}
return c.SaveBulkState(ctx, storeName, item)
}

Expand Down Expand Up @@ -354,8 +359,8 @@ func (c *GRPCClient) GetBulkState(ctx context.Context, storeName string, keys []
}

// GetState retrieves state from specific store using default consistency option.
func (c *GRPCClient) GetState(ctx context.Context, storeName, key string) (item *StateItem, err error) {
return c.GetStateWithConsistency(ctx, storeName, key, nil, StateConsistencyStrong)
func (c *GRPCClient) GetState(ctx context.Context, storeName, key string, meta map[string]string) (item *StateItem, err error) {
return c.GetStateWithConsistency(ctx, storeName, key, meta, StateConsistencyStrong)
}

// GetStateWithConsistency retrieves state from specific store using provided state consistency.
Expand Down Expand Up @@ -418,8 +423,8 @@ func (c *GRPCClient) QueryStateAlpha1(ctx context.Context, storeName, query stri
}

// DeleteState deletes content from store using default state options.
func (c *GRPCClient) DeleteState(ctx context.Context, storeName, key string) error {
return c.DeleteStateWithETag(ctx, storeName, key, nil, nil, nil)
func (c *GRPCClient) DeleteState(ctx context.Context, storeName, key string, meta map[string]string) error {
return c.DeleteStateWithETag(ctx, storeName, key, nil, meta, nil)
}

// DeleteStateWithETag deletes content from store using provided state options and etag.
Expand Down Expand Up @@ -450,15 +455,16 @@ func (c *GRPCClient) DeleteStateWithETag(ctx context.Context, storeName, key str
}

// DeleteBulkState deletes content for multiple keys from store.
func (c *GRPCClient) DeleteBulkState(ctx context.Context, storeName string, keys []string) error {
func (c *GRPCClient) DeleteBulkState(ctx context.Context, storeName string, keys []string, meta map[string]string) error {
if len(keys) == 0 {
return nil
}

items := make([]*DeleteStateItem, 0, len(keys))
for i := 0; i < len(keys); i++ {
item := &DeleteStateItem{
Key: keys[i],
Key: keys[i],
Metadata: meta,
}
items = append(items, item)
}
Expand Down
24 changes: 12 additions & 12 deletions client/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func TestSaveState(t *testing.T) {
key := "key1"

t.Run("save data", func(t *testing.T) {
err := testClient.SaveState(ctx, store, key, []byte(data))
err := testClient.SaveState(ctx, store, key, []byte(data), nil)
assert.Nil(t, err)
})

t.Run("get saved data", func(t *testing.T) {
item, err := testClient.GetState(ctx, store, key)
item, err := testClient.GetState(ctx, store, key, nil)
assert.Nil(t, err)
assert.NotNil(t, item)
assert.NotEmpty(t, item.Etag)
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestSaveState(t *testing.T) {
})

t.Run("delete data", func(t *testing.T) {
err := testClient.DeleteState(ctx, store, key)
err := testClient.DeleteState(ctx, store, key, nil)
assert.Nil(t, err)
})
}
Expand All @@ -111,7 +111,7 @@ func TestDeleteState(t *testing.T) {
key := "key1"

t.Run("delete not exist data", func(t *testing.T) {
err := testClient.DeleteState(ctx, store, key)
err := testClient.DeleteState(ctx, store, key, nil)
assert.Nil(t, err)
})
t.Run("delete not exist data with etag and meta", func(t *testing.T) {
Expand All @@ -121,11 +121,11 @@ func TestDeleteState(t *testing.T) {
})

t.Run("save data", func(t *testing.T) {
err := testClient.SaveState(ctx, store, key, []byte(data))
err := testClient.SaveState(ctx, store, key, []byte(data), nil)
assert.Nil(t, err)
})
t.Run("confirm data saved", func(t *testing.T) {
item, err := testClient.GetState(ctx, store, key)
item, err := testClient.GetState(ctx, store, key, nil)
assert.Nil(t, err)
assert.NotNil(t, item)
assert.NotEmpty(t, item.Etag)
Expand All @@ -134,11 +134,11 @@ func TestDeleteState(t *testing.T) {
})

t.Run("delete exist data", func(t *testing.T) {
err := testClient.DeleteState(ctx, store, key)
err := testClient.DeleteState(ctx, store, key, nil)
assert.Nil(t, err)
})
t.Run("confirm data deleted", func(t *testing.T) {
item, err := testClient.GetState(ctx, store, key)
item, err := testClient.GetState(ctx, store, key, nil)
assert.Nil(t, err)
assert.NotNil(t, item)
assert.NotEmpty(t, item.Etag)
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestDeleteBulkState(t *testing.T) {
keys := []string{"key1", "key2", "key3"}

t.Run("delete not exist data", func(t *testing.T) {
err := testClient.DeleteBulkState(ctx, store, keys)
err := testClient.DeleteBulkState(ctx, store, keys, nil)
assert.Nil(t, err)
})

Expand Down Expand Up @@ -233,7 +233,7 @@ func TestDeleteBulkState(t *testing.T) {
assert.Equal(t, len(keys), len(getItems))

// delete
err = testClient.DeleteBulkState(ctx, store, keys)
err = testClient.DeleteBulkState(ctx, store, keys, nil)
assert.NoError(t, err)

// confirm data deleted
Expand Down Expand Up @@ -370,9 +370,9 @@ func TestQueryState(t *testing.T) {
key2 := "key2"

t.Run("save data", func(t *testing.T) {
err := testClient.SaveState(ctx, store, key1, []byte(data))
err := testClient.SaveState(ctx, store, key1, []byte(data), nil)
assert.NoError(t, err)
err = testClient.SaveState(ctx, store, key2, []byte(data))
err = testClient.SaveState(ctx, store, key2, []byte(data), nil)
assert.NoError(t, err)
})

Expand Down
6 changes: 3 additions & 3 deletions daprdocs/content/en/go-sdk-docs/go-client/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ data := []byte("hello")
store := "my-store" // defined in the component YAML

// save state with the key key1, default options: strong, last-write
if err := client.SaveState(ctx, store, "key1", data); err != nil {
if err := client.SaveState(ctx, store, "key1", data, nil); err != nil {
panic(err)
}

// get state for key key1
item, err := client.GetState(ctx, store, "key1")
item, err := client.GetState(ctx, store, "key1", nil)
if err != nil {
panic(err)
}
fmt.Printf("data [key:%s etag:%s]: %s", item.Key, item.Etag, string(item.Value))

// delete state for key key1
if err := client.DeleteState(ctx, store, "key1"); err != nil {
if err := client.DeleteState(ctx, store, "key1", nil); err != nil {
panic(err)
}
```
Expand Down
6 changes: 3 additions & 3 deletions examples/hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ Then, depending on the command line argument, the app invokes corresponding meth

Persist the state:
```go
err := client.SaveState(ctx, stateStoreName, "order", []byte(strconv.Itoa(orderID)))
err := client.SaveState(ctx, stateStoreName, "order", []byte(strconv.Itoa(orderID)), nil)
```
Retrieve the state:
```go
item, err := client.GetState(ctx, stateStoreName, "order")
item, err := client.GetState(ctx, stateStoreName, "order", nil)
```
Delete the state:
```go
err := client.DeleteState(ctx, stateStoreName, "order")
err := client.DeleteState(ctx, stateStoreName, "order", nil)
```

## Step 3 - Run the app with Dapr
Expand Down
6 changes: 3 additions & 3 deletions examples/hello-world/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ require (
)

require (
github.com/dapr/dapr v1.6.0-rc.3 // indirect
github.com/dapr/dapr v1.6.1-0.20220223052122-c503710ab2d1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20210831024726-fe130286e0e2 // indirect
google.golang.org/grpc v1.40.0 // indirect
Expand Down
Loading

0 comments on commit a0a4a9e

Please sign in to comment.