Skip to content

Commit

Permalink
Replace Add/Set/Delete with single method for batch ops
Browse files Browse the repository at this point in the history
  • Loading branch information
pmm-sumo committed Jul 15, 2021
1 parent e287dc4 commit cf0dcc4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
9 changes: 5 additions & 4 deletions extension/storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ Delete(context.Context, string) error
Close(context.Context) error
```

As well as their batch counterparts (executed in a single transaction):
It is possible to execute several operations in a single transaction via `BatchOp`. The method takes
two collections as arguments:
* list of keys for which the values are retrieved and returned,
* a map, which specifies the key/value pairs that are going to be updated; when a value is `nil`, the key is deleted.
```
GetBatch(context.Context, []string) ([][]byte, error)
SetBatch(context.Context, map[string][]byte) error
DeleteBatch(context.Context, []string) error
BatchOp(context.Context, []string, map[]string[]byte) ([][]byte, error)
```

Note: All methods should return error only if a problem occurred. (For example, if a file is no longer accessible, or if a remote service is unavailable.)
Expand Down
14 changes: 2 additions & 12 deletions extension/storage/nop_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,7 @@ func (c nopClient) Close(context.Context) error {
return nil
}

// GetBatch does nothing, and returns nil, nil
func (c nopClient) GetBatch(ctx context.Context, strings []string) ([][]byte, error) {
// Batch does nothing, and returns nil, nil
func (c nopClient) Batch(ctx context.Context, strings []string, entries map[string][]byte) ([][]byte, error) {
return nil, nil // no result, but no problem
}

// SetBatch does nothing and returns nil
func (c nopClient) SetBatch(ctx context.Context, entries map[string][]byte) error {
return nil // no problem
}

// DeleteBatch does nothing and returns nil
func (c nopClient) DeleteBatch(ctx context.Context, strings []string) error {
return nil // no problem
}
21 changes: 8 additions & 13 deletions extension/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ type Extension interface {
// Client is the interface that storage clients must implement
// All methods should return error only if a problem occurred.
// This mirrors the behavior of a golang map:
// - Set and SetBatch don't error if a key already exists - they just overwrite the value.
// - Get and GetBatch don't error if a key is not found - they just returns nil.
// - Delete and DeleteBatch don't error if the key doesn't exist - they just no-ops.
// - Set doesn't error if a key already exists - it just overwrites the value.
// - Get doesn't error if a key is not found - it just returns nil.
// - Delete doesn't error if the key doesn't exist - it just no-ops.
// Similarly:
// - Batch doesn't error if any of the above happens for either retrieved or updated keys
// This also provides a way to differentiate data operations
// [overwrite | not-found | no-op] from "real" problems
type Client interface {
Expand All @@ -51,17 +53,10 @@ type Client interface {
// Delete will delete data associated with the specified key
Delete(context.Context, string) error

// GetBatch will retrieve data from storage that corresponds to the
// collection of keys. It will return an array of results, where each
// Batch will, respectively - get values for selected keys or upsert key/values. When the value specified
// is nil, the key is being deleted. It will return an array of results, where each
// one corresponds to a key at a given position and will be nil, if key is not found
GetBatch(context.Context, []string) ([][]byte, error)

// SetBatch will store data provided in the map.
// When a value for a given key is nil, the entry will be deleted
SetBatch(context.Context, map[string][]byte) error

// DeleteBatch will delete data associated with specified collection of keys
DeleteBatch(context.Context, []string) error
Batch(context.Context, []string, map[string][]byte) ([][]byte, error)

// Close will release any resources held by the client
Close(context.Context) error
Expand Down

0 comments on commit cf0dcc4

Please sign in to comment.