| 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package tkv defines a simple interface to a |
| 6 | // transactional key-value storage system. |
| 7 | package tkv |
| 8 | |
| 9 | import "context" |
| 10 | |
| 11 | // A Storage is a transaction key-value storage system. |
| 12 | type Storage interface { |
| 13 | // ReadOnly runs f in a read-only transaction. |
| 14 | // It is equivalent to ReadWrite except that the |
| 15 | // transaction's BufferWrite method will fail unconditionally. |
| 16 | // (The implementation may be able to optimize the |
| 17 | // transaction if it knows at the start that no writes will happen.) |
| 18 | ReadOnly(ctx context.Context, f func(context.Context, Transaction) error) error |
| 19 | |
| 20 | // ReadWrite runs f in a read-write transaction. |
| 21 | // If f returns an error, the transaction aborts and returns that error. |
| 22 | // If f returns nil, the transaction attempts to commit and then return nil. |
| 23 | // Otherwise it tries again. Note that f may be called multiple times and that |
| 24 | // the result only describes the effect of the final call to f. |
| 25 | // The caller must take care not to use any state computed during |
| 26 | // earlier calls to f, or even the last call to f when an error is returned. |
| 27 | ReadWrite(ctx context.Context, f func(context.Context, Transaction) error) error |
| 28 | } |
| 29 | |
| 30 | // A Transaction provides read and write operations within a transaction, |
| 31 | // as executed by Storage's ReadOnly or ReadWrite methods. |
| 32 | type Transaction interface { |
| 33 | // ReadValue reads the value associated with a single key. |
| 34 | // If there is no value associated with that key, ReadKey returns an empty value. |
| 35 | // An error is only returned for problems accessing the storage. |
| 36 | ReadValue(ctx context.Context, key string) (value string, err error) |
| 37 | |
| 38 | // ReadValues reads the values associated with the given keys. |
| 39 | // If there is no value stored for a given key, ReadValues returns an empty value for that key. |
| 40 | // An error is only returned for problems accessing the storage. |
| 41 | ReadValues(ctx context.Context, keys []string) (values []string, err error) |
| 42 | |
| 43 | // BufferWrites buffers the given writes, |
| 44 | // to be applied at the end of the transaction. |
| 45 | // BufferWrites panics if this is a ReadOnly transaction. |
| 46 | // It returns an error if it detects any other problems. |
| 47 | // The behavior of multiple writes buffered using the same key |
| 48 | // is undefined: it may return an error or not. |
| 49 | BufferWrites(writes []Write) error |
| 50 | } |
| 51 | |
| 52 | // A Write is a single change to be applied at the end of a read-write transaction. |
| 53 | // A Write with an empty value deletes the value associated with the given key. |
| 54 | type Write struct { |
| 55 | Key string |
| 56 | Value string |
| 57 | } |