Skip to content

Commit

Permalink
Introduce Operation for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
pmm-sumo committed Jul 19, 2021
1 parent cf0dcc4 commit 8810ad8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
20 changes: 14 additions & 6 deletions extension/storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ Delete(context.Context, string) error
Close(context.Context) error
```

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.
It is possible to execute several operations in a single transaction via `Batch`. The method takes a collection of
`Operation` arguments (each of which contains `Key`, `Value` and `Type` properties):
```
BatchOp(context.Context, []string, map[]string[]byte) ([][]byte, error)
Batch(context.Context, ...Operation) error
```

The elements itself can be created using:

```
SetOperation(string, []byte) Operation
GetOperation(string) Operation
DeleteOperation(string) Operation
```

Get operation results are stored in-place into the given Operation and can be retrieved using its `Value` property.

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.)

Note: It is the responsibility of each component to `Close` a storage client that it has requested.
Note: It is the responsibility of each component to `Close` a storage client that it has requested.
4 changes: 2 additions & 2 deletions extension/storage/nop_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ func (c nopClient) Close(context.Context) 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
func (c nopClient) Batch(context.Context, ...Operation) error {
return nil // no result, but no problem
}
45 changes: 41 additions & 4 deletions extension/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,48 @@ type Client interface {
// Delete will delete data associated with the specified key
Delete(context.Context, string) error

// 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
Batch(context.Context, []string, map[string][]byte) ([][]byte, error)
// Batch handles specified operations in batch. Get operation results are put in-place
Batch(context.Context, ...Operation) error

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

const (
Get = iota
Set
Delete
)

type operation struct {
// Key specifies key which is going to be get/set/deleted
Key string
// Value specifies value that is going to be set or holds result of get operation
Value []byte
// Type describes the operation type
Type int
}

type Operation *operation

func SetOperation(key string, value []byte) Operation {
return &operation{
Key: key,
Value: value,
Type: Set,
}
}

func GetOperation(key string) Operation {
return &operation{
Key: key,
Type: Get,
}
}

func DeleteOperation(key string) Operation {
return &operation{
Key: key,
Type: Delete,
}
}

0 comments on commit 8810ad8

Please sign in to comment.