Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Oct 11, 2024
1 parent 1d9f91a commit 7292f19
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 8 deletions.
2 changes: 1 addition & 1 deletion runtime/fastly/builtins/kv-store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ bool KVStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
return false;
}

auto res = host_api::ObjectStore::open(name);
auto res = host_api::KVStore::open(name);
if (auto *err = res.to_err()) {
if (host_api::error_is_invalid_argument(*err)) {
JS_ReportErrorNumberASCII(cx, FastlyGetErrorMessage, nullptr, JSMSG_KV_STORE_DOES_NOT_EXIST,
Expand Down
107 changes: 101 additions & 6 deletions runtime/fastly/host-api/fastly.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#define fastly_H
#ifdef __cplusplus
extern "C" {
namespace fastly {
#endif

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
namespace fastly {

typedef struct fastly_world_string {
uint8_t *ptr;
Expand Down Expand Up @@ -193,7 +193,7 @@ typedef struct fastly_host_http_send_error_detail {
uint8_t tls_alert_id;
} fastly_host_http_send_error_detail;

// The values need to match https://docs.rs/fastly-sys/0.8.7/src/fastly_sys/lib.rs.html#86-108
// The values need to match https://docs.rs/fastly-sys/0.10.5/src/fastly_sys/lib.rs.html#111
#define BACKEND_CONFIG_RESERVED (1u << 0)
#define BACKEND_CONFIG_HOST_OVERRIDE (1u << 1)
#define BACKEND_CONFIG_CONNECT_TIMEOUT (1u << 2)
Expand Down Expand Up @@ -547,6 +547,101 @@ int object_store_pending_delete_wait(uint32_t handle);
WASM_IMPORT("fastly_object_store", "insert")
int object_store_insert(uint32_t object_store_handle, const char *key, size_t key_len,
uint32_t body_handle);

// Module fastly_kv_store
#define KV_LOOKUP_CONFIG_RESERVED (1u << 0)

typedef struct __attribute__((aligned(4))) KVLookupOptions {
uint32_t reserved;
} KVLookupOptions;

#define KV_INSERT_CONFIG_RESERVED (1u << 0)
#define KV_INSERT_CONFIG_BACKGROUND_FETCH (1u << 1)
#define KV_INSERT_CONFIG_IF_GENERATION_MATCH (1u << 2)
#define KV_INSERT_CONFIG_METADATA (1u << 3)
#define KV_INSERT_CONFIG_TIME_TO_LIVE_SEC (1u << 4)

#define KV_INSERT_MODE_OVERWRITE 0u
#define KV_INSERT_MODE_ADD 1u
#define KV_INSERT_MODE_APPEND 2u
#define KV_INSERT_MODE_PREPEND 3u

typedef struct __attribute__((aligned(4))) KVInsertOptions {
uint32_t mode;
uint32_t if_generation_match;
uint8_t *metadata;
uint32_t metadata_len;
uint32_t time_to_live_sec;
} KVInsertOptions;

#define KV_DELETE_CONFIG_RESERVED (1u << 0)

typedef struct __attribute__((aligned(4))) KVDeleteOptions {
uint32_t reserved;
} KVDeleteOptions;

#define KV_LIST_CONFIG_RESERVED (1u << 0)
#define KV_LIST_CONFIG_CURSOR (1u << 1)
#define KV_LIST_CONFIG_LIMIT (1u << 2)
#define KV_LIST_CONFIG_PREFIX (1u << 3)

#define KV_LIST_MODE_STRONG 0u
#define KV_LIST_MODE_EVENTUAL 1u

typedef struct __attribute__((aligned(4))) KVListOptions {
uint32_t mode;
uint8_t *cursor;
uint32_t cursor_len;
uint32_t limit;
uint8_t *prefix;
uint32_t prefix_len;
} KVListOptions;

#define KV_ERROR_UNINITIALIZED 0u
#define KV_ERROR_OK 1u
#define KV_ERROR_BAD_REQUEST 2u
#define KV_ERROR_NOT_FOUND 3u
#define KV_ERROR_PRECONDITION_FAILED 4u
#define KV_ERROR_PAYLOAD_TOO_LARGE 5u
#define KV_ERROR_INTERNAL_ERROR 6u
#define KV_ERROR_TOO_MANY_REQUESTS 7u

WASM_IMPORT("fastly_kv_store", "open")
int kv_store_open(const char *name, size_t name_len, uint32_t *kv_store_handle_out);

WASM_IMPORT("fastly_kv_store", "lookup")
int kv_store_lookup(uint32_t kv_store_handle, const char *key, size_t key_len,
uint32_t lookup_options_mask, KVLookupOptions *lookup_options,
uint32_t *kv_store_lookup_handle_out);

WASM_IMPORT("fastly_kv_store", "lookup_wait")
int kv_store_lookup_wait(uint32_t kv_store_handle_lookup_handle, uint32_t *body_handle_out,
uint8_t *metadata_buf_out, size_t metadata_buf_len, size_t *nwritten_out,
uint32_t *generation_out, uint32_t *kv_error_out);

WASM_IMPORT("fastly_kv_store", "insert")
int kv_store_insert(uint32_t kv_store_handle, const char *key, size_t key_len, uint32_t body_handle,
uint32_t insert_options_mask, KVInsertOptions *insert_options,
uint32_t *kv_store_insert_handle_out);

WASM_IMPORT("fastly_kv_store", "insert_wait")
int kv_store_insert_wait(uint32_t kv_store_insert_handle, uint32_t *kv_error_out);

WASM_IMPORT("fastly_kv_store", "delete")
int kv_store_delete(uint32_t handle, const char *key, size_t key_len, uint32_t delete_options_mask,
KVDeleteOptions *delete_options, uint32_t *kv_store_delete_handle_out);

WASM_IMPORT("fastly_kv_store", "delete_wait")
int kv_store_delete_wait(uint32_t kv_store_delete_handle, uint32_t *kv_error_out);

WASM_IMPORT("fastly_kv_store", "list")
int kv_store_list(uint32_t kv_store_handle, uint32_t list_options_mask, KVListOptions *list_options,
uint32_t *kv_store_list_handle_out);

WASM_IMPORT("fastly_kv_store", "list_wait")
int kv_store_list_wait(uint32_t kv_store_list_handle, uint32_t *body_handle_out,
uint32_t *kv_error_out);

WASM_IMPORT("fastly_geo", "lookup")
int geo_lookup(const uint8_t *addr_octets, size_t addr_len, char *buf, size_t buf_len,
size_t *nwritten);
Expand Down Expand Up @@ -578,11 +673,11 @@ int async_select(uint32_t handles[], size_t handles_len, uint32_t timeout_ms,
WASM_IMPORT("fastly_async_io", "is_ready")
int async_is_ready(uint32_t handle, uint32_t *is_ready_out);

struct __attribute__((aligned(4))) PurgeOptions {
typedef struct __attribute__((aligned(4))) PurgeOptions {
uint8_t *ret_buf_ptr;
size_t ret_buf_len;
size_t *ret_buf_nwritten_out;
};
} PurgeOptions;

#define FASTLY_HOST_PURGE_OPTIONS_MASK_SOFT_PURGE (1 << 0)
#define FASTLY_HOST_PURGE_OPTIONS_MASK_RET_BUF (1 << 1)
Expand Down Expand Up @@ -806,8 +901,8 @@ int device_detection_lookup(const char *user_agent, size_t user_agent_len, const
WASM_IMPORT("fastly_compute_runtime", "get_vcpu_ms")
int compute_get_vcpu_ms(uint64_t *vcpu_ms);

} // namespace fastly
#ifdef __cplusplus
}
} // namespace fastly
} // extern C
#endif
#endif
123 changes: 123 additions & 0 deletions runtime/fastly/host-api/host_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3061,6 +3061,129 @@ Result<HostString> DeviceDetection::lookup(std::string_view user_agent) {
return res;
}

Result<KVStore> KVStore::open(std::string_view name) {
Result<KVStore> res;

auto name_str = string_view_to_world_string(name);
KVStore::Handle ret;
fastly::fastly_host_error err;
if (!convert_result(
fastly::kv_store_open(reinterpret_cast<char *>(name_str.ptr), name_str.len, &ret),
&err)) {
res.emplace_err(err);
} else {
res.emplace(ret);
}

return res;
}

// Result<std::optional<HttpBody>> KVStore::lookup(std::string_view name) {
// Result<std::optional<HttpBody>> res;

// auto name_str = string_view_to_world_string(name);
// KVStore::Handle ret;
// fastly::fastly_host_error err;
// bool ok =
// convert_result(fastly::object_store_get(this->handle, reinterpret_cast<char *>(name_str.ptr),
// name_str.len, &ret),
// &err);
// if ((!ok && error_is_optional_none(err)) || ret == INVALID_HANDLE) {
// res.emplace(std::nullopt);
// } else {
// res.emplace(ret);
// }

// return res;
// }

// Result<KVStorePendingLookup::Handle> KVStore::lookup_async(std::string_view name) {
// Result<KVStorePendingLookup::Handle> res;

// auto name_str = string_view_to_world_string(name);
// KVStorePendingLookup::Handle ret;
// fastly::fastly_host_error err;
// if (!convert_result(fastly::object_store_get_async(
// this->handle, reinterpret_cast<char *>(name_str.ptr), name_str.len, &ret),
// &err)) {
// res.emplace_err(err);
// } else {
// res.emplace(ret);
// }

// return res;
// }

// Result<KVStorePendingDelete::Handle> KVStore::delete_async(std::string_view name) {
// Result<KVStorePendingDelete::Handle> res;

// auto name_str = string_view_to_world_string(name);
// KVStorePendingDelete::Handle ret;
// fastly::fastly_host_error err;
// if (!convert_result(fastly::object_store_delete_async(
// this->handle, reinterpret_cast<char *>(name_str.ptr), name_str.len, &ret),
// &err)) {
// res.emplace_err(err);
// } else {
// res.emplace(ret);
// }

// return res;
// }

// Result<Void> KVStore::insert(std::string_view name, HttpBody body) {
// Result<Void> res;

// auto name_str = string_view_to_world_string(name);
// fastly::fastly_host_error err;
// if (!convert_result(fastly::object_store_insert(this->handle,
// reinterpret_cast<char *>(name_str.ptr),
// name_str.len, body.handle),
// &err)) {
// res.emplace_err(err);
// } else {
// res.emplace();
// }

// return res;
// }

// FastlyResult<std::optional<HttpBody>, FastlyAPIError> KVStorePendingLookup::wait() {
// FastlyResult<std::optional<HttpBody>, FastlyAPIError> res;

// fastly::fastly_host_error err;
// HttpBody::Handle ret = INVALID_HANDLE;
// bool ok = convert_result(fastly::object_store_pending_lookup_wait(this->handle, &ret), &err);
// if ((!ok && error_is_optional_none(err)) || ret == INVALID_HANDLE) {
// res.emplace(std::nullopt);
// } else {
// res.emplace(ret);
// }

// return res;
// }

// FastlyAsyncTask::Handle KVStorePendingLookup::async_handle() const {
// return FastlyAsyncTask::Handle{this->handle};
// }

// Result<Void> KVStorePendingDelete::wait() {
// Result<Void> res;

// fastly::fastly_host_error err;
// if (!convert_result(fastly::object_store_pending_delete_wait(this->handle), &err)) {
// res.emplace_err(err);
// } else {
// res.emplace(Void{});
// }

// return res;
// }

// FastlyAsyncTask::Handle KVStorePendingDelete::async_handle() const {
// return FastlyAsyncTask::Handle{this->handle};
// }

Result<uint64_t> Compute::get_vcpu_ms() {
Result<uint64_t> res;
uint64_t ret;
Expand Down
Loading

0 comments on commit 7292f19

Please sign in to comment.