From cf0dcc4c7d98b53189876222b48f9ead2e063a56 Mon Sep 17 00:00:00 2001 From: Przemek Maciolek Date: Thu, 15 Jul 2021 14:06:44 +0200 Subject: [PATCH] Replace Add/Set/Delete with single method for batch ops --- extension/storage/README.md | 9 +++++---- extension/storage/nop_client.go | 14 ++------------ extension/storage/storage.go | 21 ++++++++------------- 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/extension/storage/README.md b/extension/storage/README.md index a4a7173a0cf5..f061bb68e641 100644 --- a/extension/storage/README.md +++ b/extension/storage/README.md @@ -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.) diff --git a/extension/storage/nop_client.go b/extension/storage/nop_client.go index 996a077e88a1..a8bc61df5e39 100644 --- a/extension/storage/nop_client.go +++ b/extension/storage/nop_client.go @@ -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 -} diff --git a/extension/storage/storage.go b/extension/storage/storage.go index 6d98297cf050..eae7683626e2 100644 --- a/extension/storage/storage.go +++ b/extension/storage/storage.go @@ -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 { @@ -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