Fastly Compute C++ SDK
Loading...
Searching...
No Matches
kv_store.h
Go to the documentation of this file.
1#ifndef FASTLY_KV_STORE_H
2#define FASTLY_KV_STORE_H
3#include <chrono>
5#include <fastly/error.h>
6#include <fastly/http/body.h>
7#include <fastly/sdk-sys.h>
8#include <functional>
9#include <optional>
10
11namespace fastly::kv_store {
12using fastly::sys::kv_store::KVStoreErrorCode;
14public:
15 KVStoreError(fastly::sys::kv_store::KVStoreError *e)
16 : err_(rust::Box<fastly::sys::kv_store::KVStoreError>::from_raw(e)) {};
17 KVStoreError(rust::Box<fastly::sys::kv_store::KVStoreError> e)
18 : err_(std::move(e)) {};
19 KVStoreErrorCode error_code();
20 std::string error_msg();
21
22private:
23 rust::Box<fastly::sys::kv_store::KVStoreError> err_;
24};
25template <class T = void> using expected = tl::expected<T, KVStoreError>;
26template <class T = void> using unexpected = tl::unexpected<T>;
27
28namespace detail {
29template <class HandleType> class KVPendingHandle {
30public:
34 std::uint32_t as_u32() const { return handle_; }
35
39 static HandleType from_u32(std::uint32_t handle) {
40 return HandleType(handle);
41 }
42
43protected:
44 explicit KVPendingHandle(std::uint32_t handle) : handle_(handle) {}
45 std::uint32_t handle_;
46};
47} // namespace detail
48
59
68
78
86
87class LookupBuilder;
88class LookupResponse {
89 friend LookupBuilder;
90
91public:
94
97 std::optional<Body> try_take_body();
98
101 std::vector<uint8_t> take_body_bytes();
102
104 std::optional<std::vector<std::uint8_t>> metadata() const;
105
107 std::uint64_t current_generation() const;
108
109private:
110 friend class KVStore;
111 LookupResponse(rust::Box<fastly::sys::kv_store::LookupResponse> response)
112 : response_(std::move(response)) {}
113 rust::Box<fastly::sys::kv_store::LookupResponse> response_;
114};
115
116class LookupBuilder {
117public:
119 expected<LookupResponse> execute(std::string_view key) const;
120
122 expected<PendingLookupHandle> execute_async(std::string_view key) const;
123
124private:
125 friend class KVStore;
126 LookupBuilder(rust::Box<fastly::sys::kv_store::LookupBuilder> builder)
127 : builder_(std::move(builder)) {}
128 rust::Box<fastly::sys::kv_store::LookupBuilder> builder_;
129};
130
131class EraseBuilder {
132public:
134 expected<> execute(std::string_view key) const;
135
137 expected<PendingEraseHandle> execute_async(std::string_view key) const;
138
139private:
140 friend class KVStore;
141 EraseBuilder(rust::Box<fastly::sys::kv_store::EraseBuilder> builder)
142 : builder_(std::move(builder)) {}
143 rust::Box<fastly::sys::kv_store::EraseBuilder> builder_;
144};
145
146using InsertMode = fastly::sys::kv_store::InsertMode;
147
148class KVStore;
149class InsertBuilder {
150public:
152 InsertBuilder mode(InsertMode mode) &&;
153
157 InsertBuilder background_fetch() &&;
158
163 InsertBuilder if_generation_match(std::uint64_t gen) &&;
164
166 InsertBuilder metadata(const std::string &data) &&;
167
170 InsertBuilder time_to_live(std::chrono::milliseconds ttl) &&;
171
175 expected<> execute(const std::string &key, Body body) &&;
176
179 Body body) &&;
180
181private:
182 friend class KVStore;
183 InsertBuilder(rust::Box<fastly::sys::kv_store::InsertBuilder> builder)
184 : builder_(std::move(builder)) {}
185 rust::Box<fastly::sys::kv_store::InsertBuilder> builder_;
186};
187
191 std::string other;
192};
193using ListMode = std::variant<ListModeStrong, ListModeEventual, ListModeOther>;
194
195class ListResponse;
196class KVStore;
197class ListPage {
198 friend ListResponse;
199 friend KVStore;
200
201public:
203 std::vector<std::string> keys() const;
206 std::vector<std::string> into_keys();
207
209 std::optional<std::string> next_cursor() const;
210
212 std::optional<std::string> prefix() const;
213
215 std::uint32_t limit() const;
216
218 ListMode mode() const;
219
220private:
221 friend class ListBuilder;
222 ListPage(rust::Box<fastly::sys::kv_store::ListPage> page)
223 : page_(std::move(page)) {}
224 rust::Box<fastly::sys::kv_store::ListPage> page_;
225};
226
228 ListResponse, fastly::sys::kv_store::ListResponse> {
229public:
231 ListResponse, fastly::sys::kv_store::ListResponse>::RustIteratorRange;
232
234 std::optional<expected<ListPage>> next() {
235 fastly::sys::kv_store::ListPage *page;
236 fastly::sys::kv_store::KVStoreError *err;
237 if (this->iter_->next(page, err)) {
238 if (err == nullptr) {
239 return ListPage(
240 rust::Box<fastly::sys::kv_store::ListPage>::from_raw(page));
241 } else {
243 rust::Box<fastly::sys::kv_store::KVStoreError>::from_raw(err)));
244 }
245 } else {
246 return std::nullopt;
247 }
248 }
249};
250
251class ListBuilder {
252public:
257 ListBuilder eventual_consistency() &&;
258
260 ListBuilder cursor(const std::string &cursor) &&;
261
263 ListBuilder limit(std::uint32_t limit) &&;
264
266 ListBuilder prefix(const std::string &prefix) &&;
267
270
274
277
278private:
279 friend class KVStore;
280 ListBuilder(rust::Box<fastly::sys::kv_store::ListBuilder> builder)
281 : builder_(std::move(builder)) {}
282 rust::Box<fastly::sys::kv_store::ListBuilder> builder_;
283};
284
285class KVStore {
286public:
290 static expected<std::optional<KVStore>> open(std::string_view name);
291 expected<LookupResponse> lookup(std::string_view key) const;
293
296 pending_lookup_wait(PendingLookupHandle pending_request_handle) const;
297
302 expected<> insert(std::string_view key, Body value) const;
303
308
311 pending_insert_wait(PendingInsertHandle pending_insert_handle) const;
312
314 expected<> erase(std::string_view key) const;
315
320
323
326
331
334 pending_list_wait(PendingListHandle pending_request_handle) const;
335
336private:
338 explicit KVStore(rust::Box<fastly::sys::kv_store::KVStore> store)
339 : store_(std::move(store)) {}
340
341 rust::Box<fastly::sys::kv_store::KVStore> store_;
342};
343} // namespace fastly::kv_store
344#endif
Definition rust_iterator_range.h:14
rust::Box< fastly::sys::kv_store::ListResponse > iter_
Definition rust_iterator_range.h:73
RustIteratorRange(rust::Box< fastly::sys::kv_store::ListResponse > iter)
Definition rust_iterator_range.h:16
Definition body.h:42
Definition kv_store.h:131
expected execute(std::string_view key) const
Execute the erasure and wait for the response.
expected< PendingEraseHandle > execute_async(std::string_view key) const
Execute the erasure asynchronously.
friend class KVStore
Definition kv_store.h:140
Definition kv_store.h:149
expected execute(const std::string &key, Body body) &&
expected< PendingInsertHandle > execute_async(const std::string &key, Body body) &&
Initiate async insert of a value into the KV Store.
InsertBuilder background_fetch() &&
InsertBuilder time_to_live(std::chrono::milliseconds ttl) &&
InsertBuilder metadata(const std::string &data) &&
Sets an arbitrary data field which can contain up to 2000B of data.
InsertBuilder if_generation_match(std::uint64_t gen) &&
InsertBuilder mode(InsertMode mode) &&
Change the behavior in the case when the new key matches an existing key.
friend class KVStore
Definition kv_store.h:182
Definition kv_store.h:13
KVStoreError(fastly::sys::kv_store::KVStoreError *e)
Definition kv_store.h:15
KVStoreError(rust::Box< fastly::sys::kv_store::KVStoreError > e)
Definition kv_store.h:17
KVStoreErrorCode error_code()
Definition kv_store.h:285
expected< ListPage > pending_list_wait(PendingListHandle pending_request_handle) const
Wait on a pending list operation.
static expected< std::optional< KVStore > > open(std::string_view name)
expected insert(std::string_view key, Body value) const
ListBuilder build_list() const
InsertBuilder build_insert() const
EraseBuilder build_erase() const
expected< LookupResponse > pending_lookup_wait(PendingLookupHandle pending_request_handle) const
Look up a value in the KV Store.
expected pending_erase_wait(PendingEraseHandle pending_erase_handle) const
Erase a value in the KV Store.
expected pending_insert_wait(PendingInsertHandle pending_insert_handle) const
Insert a value in the KV Store.
expected< LookupResponse > lookup(std::string_view key) const
LookupBuilder build_lookup() const
expected erase(std::string_view key) const
Erase a value in the KV Store.
expected< ListPage > list() const
List keys in the KV Store.
Definition kv_store.h:251
expected< PendingListHandle > execute_async() const
Initiate async list of values in the KV Store.
expected< ListPage > execute() &&
Initiate and wait on a list of values in the KV Store.
ListBuilder prefix(const std::string &prefix) &&
Set the prefix match for items to include in the resultset.
ListBuilder cursor(const std::string &cursor) &&
Change the cursor of the request.
ListBuilder limit(std::uint32_t limit) &&
Set the maximum number of items included in the response.
friend class KVStore
Definition kv_store.h:279
ListBuilder eventual_consistency() &&
Definition kv_store.h:197
std::uint32_t limit() const
Returns the limit used in the List operation.
std::optional< std::string > next_cursor() const
Returns the next cursor of the List operation.
std::vector< std::string > keys() const
Returns a vector of the listed keys in the current page.
ListMode mode() const
Returns the mode used in the List operation.
std::optional< std::string > prefix() const
Returns the prefix used in the List operation.
std::vector< std::string > into_keys()
friend class ListBuilder
Definition kv_store.h:221
Definition kv_store.h:228
std::optional< expected< ListPage > > next()
Gets the next page of results.
Definition kv_store.h:234
Definition kv_store.h:116
expected< LookupResponse > execute(std::string_view key) const
Execute the lookup and wait for the response.
expected< PendingLookupHandle > execute_async(std::string_view key) const
Execute the lookup asynchronously.
friend class KVStore
Definition kv_store.h:125
Body take_body()
Returns the body, making the LookupResponse bodyless.
std::optional< std::vector< std::uint8_t > > metadata() const
Reads the metadata of the KVStore item.
std::vector< uint8_t > take_body_bytes()
std::optional< Body > try_take_body()
std::uint64_t current_generation() const
Reads the generation of the KVStore item.
friend class KVStore
Definition kv_store.h:110
static HandleType from_u32(std::uint32_t handle)
Definition kv_store.h:39
KVPendingHandle(std::uint32_t handle)
Definition kv_store.h:44
std::uint32_t handle_
Definition kv_store.h:45
std::uint32_t as_u32() const
Definition kv_store.h:34
Definition kv_store.h:28
Definition body.h:18
fastly::sys::kv_store::InsertMode InsertMode
Definition kv_store.h:146
tl::unexpected< T > unexpected
Definition kv_store.h:26
std::variant< ListModeStrong, ListModeEventual, ListModeOther > ListMode
Definition kv_store.h:193
tl::expected< T, KVStoreError > expected
Definition kv_store.h:25
Definition backend.h:13
Definition kv_store.h:189
Definition kv_store.h:190
std::string other
Definition kv_store.h:191
Definition kv_store.h:188
Definition kv_store.h:75
Definition kv_store.h:83