|
| 1 | +/// A keyvalue interface that provides eventually consistent key-value operations. |
| 2 | +/// |
| 3 | +/// Each of these operations acts on a single key-value pair. |
| 4 | +/// |
| 5 | +/// The value in the key-value pair is defined as a `u8` byte array and the intention is that it is |
| 6 | +/// the common denominator for all data types defined by different key-value stores to handle data, |
| 7 | +/// ensuring compatibility between different key-value stores. Note: the clients will be expecting |
| 8 | +/// serialization/deserialization overhead to be handled by the key-value store. The value could be |
| 9 | +/// a serialized object from JSON, HTML or vendor-specific data types like AWS S3 objects. |
| 10 | +/// |
| 11 | +/// Data consistency in a key value store refers to the guarantee that once a write operation |
| 12 | +/// completes, all subsequent read operations will return the value that was written. |
| 13 | +/// |
| 14 | +/// Any implementation of this interface must have enough consistency to guarantee "reading your |
| 15 | +/// writes." In particular, this means that the client should never get a value that is older than |
| 16 | +/// the one it wrote, but it MAY get a newer value if one was written around the same time. These |
| 17 | +/// guarantees only apply to the same client (which will likely be provided by the host or an |
| 18 | +/// external capability of some kind). In this context a "client" is referring to the caller or |
| 19 | +/// guest that is consuming this interface. Once a write request is committed by a specific client, |
| 20 | +/// all subsequent read requests by the same client will reflect that write or any subsequent |
| 21 | +/// writes. Another client running in a different context may or may not immediately see the result |
| 22 | +/// due to the replication lag. As an example of all of this, if a value at a given key is A, and |
| 23 | +/// the client writes B, then immediately reads, it should get B. If something else writes C in |
| 24 | +/// quick succession, then the client may get C. However, a client running in a separate context may |
| 25 | +/// still see A or B |
| 26 | +interface store { |
| 27 | + /// The set of errors which may be raised by functions in this package |
| 28 | + variant error { |
| 29 | + /// The host does not recognize the store identifier requested. |
| 30 | + no-such-store, |
| 31 | + |
| 32 | + /// The requesting component does not have access to the specified store |
| 33 | + /// (which may or may not exist). |
| 34 | + access-denied, |
| 35 | + |
| 36 | + /// Some implementation-specific error has occurred (e.g. I/O) |
| 37 | + other(string) |
| 38 | + } |
| 39 | + |
| 40 | + /// A response to a `list-keys` operation. |
| 41 | + record key-response { |
| 42 | + /// The list of keys returned by the query. |
| 43 | + keys: list<string>, |
| 44 | + /// The continuation token to use to fetch the next page of keys. If this is `null`, then |
| 45 | + /// there are no more keys to fetch. |
| 46 | + cursor: option<u64> |
| 47 | + } |
| 48 | + |
| 49 | + /// Get the bucket with the specified identifier. |
| 50 | + /// |
| 51 | + /// `identifier` must refer to a bucket provided by the host. |
| 52 | + /// |
| 53 | + /// `error::no-such-store` will be raised if the `identifier` is not recognized. |
| 54 | + open: func(identifier: string) -> result<bucket, error>; |
| 55 | + |
| 56 | + /// A bucket is a collection of key-value pairs. Each key-value pair is stored as a entry in the |
| 57 | + /// bucket, and the bucket itself acts as a collection of all these entries. |
| 58 | + /// |
| 59 | + /// It is worth noting that the exact terminology for bucket in key-value stores can very |
| 60 | + /// depending on the specific implementation. For example: |
| 61 | + /// |
| 62 | + /// 1. Amazon DynamoDB calls a collection of key-value pairs a table |
| 63 | + /// 2. Redis has hashes, sets, and sorted sets as different types of collections |
| 64 | + /// 3. Cassandra calls a collection of key-value pairs a column family |
| 65 | + /// 4. MongoDB calls a collection of key-value pairs a collection |
| 66 | + /// 5. Riak calls a collection of key-value pairs a bucket |
| 67 | + /// 6. Memcached calls a collection of key-value pairs a slab |
| 68 | + /// 7. Azure Cosmos DB calls a collection of key-value pairs a container |
| 69 | + /// |
| 70 | + /// In this interface, we use the term `bucket` to refer to a collection of key-value pairs |
| 71 | + resource bucket { |
| 72 | + /// Get the value associated with the specified `key` |
| 73 | + /// |
| 74 | + /// The value is returned as an option. If the key-value pair exists in the |
| 75 | + /// store, it returns `Ok(value)`. If the key does not exist in the |
| 76 | + /// store, it returns `Ok(none)`. |
| 77 | + /// |
| 78 | + /// If any other error occurs, it returns an `Err(error)`. |
| 79 | + get: func(key: string) -> result<option<list<u8>>, error>; |
| 80 | + |
| 81 | + /// Set the value associated with the key in the store. If the key already |
| 82 | + /// exists in the store, it overwrites the value. |
| 83 | + /// |
| 84 | + /// If the key does not exist in the store, it creates a new key-value pair. |
| 85 | + /// |
| 86 | + /// If any other error occurs, it returns an `Err(error)`. |
| 87 | + set: func(key: string, value: list<u8>) -> result<_, error>; |
| 88 | + |
| 89 | + /// Delete the key-value pair associated with the key in the store. |
| 90 | + /// |
| 91 | + /// If the key does not exist in the store, it does nothing. |
| 92 | + /// |
| 93 | + /// If any other error occurs, it returns an `Err(error)`. |
| 94 | + delete: func(key: string) -> result<_, error>; |
| 95 | + |
| 96 | + /// Check if the key exists in the store. |
| 97 | + /// |
| 98 | + /// If the key exists in the store, it returns `Ok(true)`. If the key does |
| 99 | + /// not exist in the store, it returns `Ok(false)`. |
| 100 | + /// |
| 101 | + /// If any other error occurs, it returns an `Err(error)`. |
| 102 | + exists: func(key: string) -> result<bool, error>; |
| 103 | + |
| 104 | + /// Get all the keys in the store with an optional cursor (for use in pagination). It |
| 105 | + /// returns a list of keys. Please note that for most KeyValue implementations, this is a |
| 106 | + /// can be a very expensive operation and so it should be used judiciously. Implementations |
| 107 | + /// can return any number of keys in a single response, but they should never attempt to |
| 108 | + /// send more data than is reasonable (i.e. on a small edge device, this may only be a few |
| 109 | + /// KB, while on a large machine this could be several MB). Any response should also return |
| 110 | + /// a cursor that can be used to fetch the next page of keys. See the `key-response` record |
| 111 | + /// for more information. |
| 112 | + /// |
| 113 | + /// Note that the keys are not guaranteed to be returned in any particular order. |
| 114 | + /// |
| 115 | + /// If the store is empty, it returns an empty list. |
| 116 | + /// |
| 117 | + /// MAY show an out-of-date list of keys if there are concurrent writes to the store. |
| 118 | + /// |
| 119 | + /// If any error occurs, it returns an `Err(error)`. |
| 120 | + list-keys: func(cursor: option<u64>) -> result<key-response, error>; |
| 121 | + } |
| 122 | +} |
0 commit comments