diff --git a/extension/storage/README.md b/extension/storage/README.md index f061bb68e64..2214994ad14 100644 --- a/extension/storage/README.md +++ b/extension/storage/README.md @@ -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. \ No newline at end of file +Note: It is the responsibility of each component to `Close` a storage client that it has requested. diff --git a/extension/storage/nop_client.go b/extension/storage/nop_client.go index a8bc61df5e3..10ee9df821c 100644 --- a/extension/storage/nop_client.go +++ b/extension/storage/nop_client.go @@ -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 } diff --git a/extension/storage/storage.go b/extension/storage/storage.go index eae7683626e..d1c904d0cba 100644 --- a/extension/storage/storage.go +++ b/extension/storage/storage.go @@ -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, + } +}