Skip to content

Commit

Permalink
support metadata in state bulk get state (#114)
Browse files Browse the repository at this point in the history
* update GetBulkState method: add metedata, change return struct to BulkStateItem

* generate code from dapr proto files; support metadata in get response

* update for code review
  • Loading branch information
skyao authored Dec 7, 2020
1 parent 0ca0bbe commit 32dbe98
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Client interface {
GetStateWithConsistency(ctx context.Context, store, key string, meta map[string]string, sc StateConsistency) (item *StateItem, err error)

// GetBulkState retrieves state for multiple keys from specific store.
GetBulkState(ctx context.Context, store string, keys []string, parallelism int32) ([]*StateItem, error)
GetBulkState(ctx context.Context, store string, keys []string, meta map[string]string, parallelism int32) ([]*BulkStateItem, error)

// DeleteState deletes content from store using default state options.
DeleteState(ctx context.Context, store, key string) error
Expand Down
24 changes: 18 additions & 6 deletions client/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ type StateItem struct {
Etag string
}

// StateItem represents a single state item.
type BulkStateItem struct {
Key string
Value []byte
Etag string
Metadata map[string]string
Error string
}

// SetStateItem represents a single state to be persisted.
type SetStateItem struct {
Key string
Expand Down Expand Up @@ -215,18 +224,19 @@ func (c *GRPCClient) SaveBulkState(ctx context.Context, store string, items ...*
}

// GetBulkState retreaves state for multiple keys from specific store.
func (c *GRPCClient) GetBulkState(ctx context.Context, store string, keys []string, parallelism int32) ([]*StateItem, error) {
func (c *GRPCClient) GetBulkState(ctx context.Context, store string, keys []string, meta map[string]string, parallelism int32) ([]*BulkStateItem, error) {
if store == "" {
return nil, errors.New("nil store")
}
if len(keys) == 0 {
return nil, errors.New("keys required")
}
items := make([]*StateItem, 0)
items := make([]*BulkStateItem, 0)

req := &pb.GetBulkStateRequest{
StoreName: store,
Keys: keys,
Metadata: meta,
Parallelism: parallelism,
}

Expand All @@ -240,10 +250,12 @@ func (c *GRPCClient) GetBulkState(ctx context.Context, store string, keys []stri
}

for _, r := range results.Items {
item := &StateItem{
Key: r.Key,
Etag: r.Etag,
Value: r.Data,
item := &BulkStateItem{
Key: r.Key,
Etag: r.Etag,
Value: r.Data,
Metadata: r.Metadata,
Error: r.Error,
}
items = append(items, item)
}
Expand Down
6 changes: 3 additions & 3 deletions client/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestStateTransactions(t *testing.T) {
})

t.Run("exec upserts", func(t *testing.T) {
items, err := testClient.GetBulkState(ctx, store, keys, 10)
items, err := testClient.GetBulkState(ctx, store, keys, nil, 10)
assert.Nil(t, err)
assert.NotNil(t, items)
assert.Len(t, items, len(keys))
Expand All @@ -207,7 +207,7 @@ func TestStateTransactions(t *testing.T) {
})

t.Run("get and validate inserts", func(t *testing.T) {
items, err := testClient.GetBulkState(ctx, store, keys, 10)
items, err := testClient.GetBulkState(ctx, store, keys, nil, 10)
assert.Nil(t, err)
assert.NotNil(t, items)
assert.Len(t, items, len(keys))
Expand All @@ -224,7 +224,7 @@ func TestStateTransactions(t *testing.T) {
})

t.Run("ensure deletes", func(t *testing.T) {
items, err := testClient.GetBulkState(ctx, store, keys, 3)
items, err := testClient.GetBulkState(ctx, store, keys, nil, 3)
assert.Nil(t, err)
assert.NotNil(t, items)
assert.Len(t, items, 0)
Expand Down

0 comments on commit 32dbe98

Please sign in to comment.