Skip to content

Commit

Permalink
Add the object-store interface, and rework kv-store to match the host…
Browse files Browse the repository at this point in the history
…calls
  • Loading branch information
elliottt committed Aug 13, 2024
1 parent ab7f0f0 commit 68cc150
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 121 deletions.
22 changes: 11 additions & 11 deletions crates/adapter/src/fastly/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2213,9 +2213,9 @@ pub mod fastly_erl {
}
}

pub mod fastly_kv_store {
pub mod fastly_object_store {
use super::*;
use crate::bindings::fastly::api::kv_store;
use crate::bindings::fastly::api::object_store;
use core::slice;

#[export_name = "fastly_object_store#open"]
Expand All @@ -2225,7 +2225,7 @@ pub mod fastly_kv_store {
kv_store_handle_out: *mut KVStoreHandle,
) -> FastlyStatus {
let name = unsafe { slice::from_raw_parts(name_ptr, name_len) };
match kv_store::open(name) {
match object_store::open(name) {
Ok(None) => {
unsafe {
*kv_store_handle_out = INVALID_HANDLE;
Expand All @@ -2251,7 +2251,7 @@ pub mod fastly_kv_store {
body_handle_out: *mut BodyHandle,
) -> FastlyStatus {
let key = unsafe { slice::from_raw_parts(key_ptr, key_len) };
match kv_store::lookup(kv_store_handle, key) {
match object_store::lookup(kv_store_handle, key) {
Ok(res) => {
unsafe {
*body_handle_out = res.unwrap_or(INVALID_HANDLE);
Expand All @@ -2270,7 +2270,7 @@ pub mod fastly_kv_store {
pending_body_handle_out: *mut PendingObjectStoreLookupHandle,
) -> FastlyStatus {
let key = unsafe { slice::from_raw_parts(key_ptr, key_len) };
match kv_store::lookup_async(kv_store_handle, key) {
match object_store::lookup_async(kv_store_handle, key) {
Ok(res) => {
unsafe {
*pending_body_handle_out = res;
Expand All @@ -2286,7 +2286,7 @@ pub mod fastly_kv_store {
pending_body_handle: PendingObjectStoreLookupHandle,
body_handle_out: *mut BodyHandle,
) -> FastlyStatus {
match kv_store::pending_lookup_wait(pending_body_handle) {
match object_store::pending_lookup_wait(pending_body_handle) {
Ok(res) => {
unsafe {
*body_handle_out = res.unwrap_or(INVALID_HANDLE);
Expand All @@ -2305,7 +2305,7 @@ pub mod fastly_kv_store {
body_handle: BodyHandle,
) -> FastlyStatus {
let key = unsafe { slice::from_raw_parts(key_ptr, key_len) };
convert_result(kv_store::insert(kv_store_handle, key, body_handle))
convert_result(object_store::insert(kv_store_handle, key, body_handle))
}

#[export_name = "fastly_object_store#insert_async"]
Expand All @@ -2317,7 +2317,7 @@ pub mod fastly_kv_store {
pending_body_handle_out: *mut PendingObjectStoreInsertHandle,
) -> FastlyStatus {
let key = unsafe { slice::from_raw_parts(key_ptr, key_len) };
match kv_store::insert_async(kv_store_handle, key, body_handle) {
match object_store::insert_async(kv_store_handle, key, body_handle) {
Ok(res) => {
unsafe {
*pending_body_handle_out = res;
Expand All @@ -2332,7 +2332,7 @@ pub mod fastly_kv_store {
pub fn pending_insert_wait(
pending_body_handle: PendingObjectStoreInsertHandle,
) -> FastlyStatus {
convert_result(kv_store::pending_insert_wait(pending_body_handle))
convert_result(object_store::pending_insert_wait(pending_body_handle))
}

#[export_name = "fastly_object_store#delete_async"]
Expand All @@ -2343,7 +2343,7 @@ pub mod fastly_kv_store {
pending_body_handle_out: *mut PendingObjectStoreDeleteHandle,
) -> FastlyStatus {
let key = unsafe { slice::from_raw_parts(key_ptr, key_len) };
match kv_store::delete_async(kv_store_handle, key) {
match object_store::delete_async(kv_store_handle, key) {
Ok(res) => {
unsafe {
*pending_body_handle_out = res;
Expand All @@ -2358,7 +2358,7 @@ pub mod fastly_kv_store {
pub fn pending_delete_wait(
pending_body_handle: PendingObjectStoreDeleteHandle,
) -> FastlyStatus {
convert_result(kv_store::pending_delete_wait(pending_body_handle))
convert_result(object_store::pending_delete_wait(pending_body_handle))
}
}

Expand Down
Binary file modified lib/data/viceroy-component-adapter.wasm
Binary file not shown.
178 changes: 70 additions & 108 deletions lib/src/component/kv_store.rs
Original file line number Diff line number Diff line change
@@ -1,140 +1,102 @@
use {
super::fastly::api::{http_types, kv_store, types},
crate::{
body::Body,
object_store::{ObjectKey, ObjectStoreError},
session::{
PeekableTask, PendingKvDeleteTask, PendingKvInsertTask, PendingKvLookupTask, Session,
},
},
super::fastly::api::{http_body, kv_store, types},
crate::session::Session,
};

pub struct LookupResult;

#[async_trait::async_trait]
impl kv_store::Host for Session {
async fn open(&mut self, name: String) -> Result<Option<kv_store::Handle>, types::Error> {
if self.object_store.store_exists(&name)? {
let handle = self.obj_store_handle(&name)?;
Ok(Some(handle.into()))
} else {
Ok(None)
}
impl kv_store::HostLookupResult for Session {
async fn body(
&mut self,
_self_: wasmtime::component::Resource<kv_store::LookupResult>,
) -> http_body::BodyHandle {
todo!()
}

async fn lookup(
async fn metadata(
&mut self,
store: kv_store::Handle,
key: String,
) -> Result<Option<kv_store::BodyHandle>, types::Error> {
let store = self.get_obj_store_key(store.into()).unwrap();
let key = ObjectKey::new(&key)?;
match self.obj_lookup(store, &key) {
Ok(obj) => {
let new_handle = self.insert_body(Body::from(obj));
Ok(Some(new_handle.into()))
}
// Don't write to the invalid handle as the SDK will return Ok(None)
// if the object does not exist. We need to return `Ok(())` here to
// make sure Viceroy does not crash
Err(ObjectStoreError::MissingObject) => Ok(None),
Err(err) => Err(err.into()),
}
_self_: wasmtime::component::Resource<kv_store::LookupResult>,
) -> Option<Vec<u8>> {
todo!()
}

async fn lookup_async(
async fn generation(
&mut self,
store: kv_store::Handle,
key: String,
) -> Result<kv_store::PendingLookupHandle, types::Error> {
let store = self.get_obj_store_key(store.into()).unwrap();
let key = ObjectKey::new(key)?;
// just create a future that's already ready
let fut = futures::future::ok(self.obj_lookup(store, &key));
let task = PendingKvLookupTask::new(PeekableTask::spawn(fut).await);
Ok(self.insert_pending_kv_lookup(task).into())
_self_: wasmtime::component::Resource<kv_store::LookupResult>,
) -> u32 {
todo!()
}

async fn pending_lookup_wait(
fn drop(
&mut self,
pending: kv_store::PendingLookupHandle,
) -> Result<Option<kv_store::BodyHandle>, types::Error> {
let pending_obj = self
.take_pending_kv_lookup(pending.into())?
.task()
.recv()
.await?;
// proceed with the normal match from lookup()
match pending_obj {
Ok(obj) => Ok(Some(self.insert_body(Body::from(obj)).into())),
Err(ObjectStoreError::MissingObject) => Ok(None),
Err(err) => Err(err.into()),
}
_rep: wasmtime::component::Resource<kv_store::LookupResult>,
) -> wasmtime::Result<()> {
todo!()
}
}

async fn insert(
#[async_trait::async_trait]
impl kv_store::Host for Session {
async fn open(&mut self, _name: String) -> Result<Option<kv_store::Handle>, types::Error> {
todo!()
}

async fn lookup(
&mut self,
store: kv_store::Handle,
key: String,
body_handle: http_types::BodyHandle,
) -> Result<(), types::Error> {
let store = self.get_obj_store_key(store.into()).unwrap().clone();
let key = ObjectKey::new(&key)?;
let bytes = self.take_body(body_handle.into())?.read_into_vec().await?;
self.obj_insert(store, key, bytes)?;
_store: kv_store::Handle,
_key: String,
) -> Result<Option<kv_store::BodyHandle>, types::Error> {
todo!()
}

Ok(())
async fn lookup_wait(
&mut self,
_handle: kv_store::LookupHandle,
) -> Result<Option<wasmtime::component::Resource<kv_store::LookupResult>>, types::Error> {
todo!()
}

async fn insert_async(
async fn insert(
&mut self,
store: kv_store::Handle,
key: String,
body_handle: http_types::BodyHandle,
) -> Result<kv_store::PendingInsertHandle, types::Error> {
let store = self.get_obj_store_key(store.into()).unwrap().clone();
let key = ObjectKey::new(&key)?;
let bytes = self.take_body(body_handle.into())?.read_into_vec().await?;
let fut = futures::future::ok(self.obj_insert(store, key, bytes));
let task = PeekableTask::spawn(fut).await;
_store: kv_store::Handle,
_key: String,
_body_handle: kv_store::BodyHandle,
_mask: kv_store::InsertConfigOptions,
_config: kv_store::InsertConfig,
) -> Result<kv_store::InsertHandle, types::Error> {
todo!()
}

Ok(self
.insert_pending_kv_insert(PendingKvInsertTask::new(task))
.into())
async fn insert_wait(&mut self, _handle: kv_store::InsertHandle) -> Result<(), types::Error> {
todo!()
}

async fn pending_insert_wait(
async fn delete(
&mut self,
handle: kv_store::PendingInsertHandle,
) -> Result<(), types::Error> {
Ok((self
.take_pending_kv_insert(handle.into())?
.task()
.recv()
.await?)?)
_store: kv_store::Handle,
_key: String,
) -> Result<kv_store::DeleteHandle, types::Error> {
todo!()
}

async fn delete_async(
&mut self,
store: kv_store::Handle,
key: String,
) -> Result<kv_store::PendingDeleteHandle, types::Error> {
let store = self.get_obj_store_key(store.into()).unwrap().clone();
let key = ObjectKey::new(&key)?;
let fut = futures::future::ok(self.obj_delete(store, key));
let task = PeekableTask::spawn(fut).await;
async fn delete_wait(&mut self, _handle: kv_store::DeleteHandle) -> Result<(), types::Error> {
todo!()
}

Ok(self
.insert_pending_kv_delete(PendingKvDeleteTask::new(task))
.into())
async fn list(
&mut self,
_store: kv_store::Handle,
_mask: kv_store::ListConfigOptions,
_options: kv_store::ListConfig,
) -> Result<kv_store::ListHandle, types::Error> {
todo!()
}

async fn pending_delete_wait(
async fn list_wait(
&mut self,
handle: kv_store::PendingDeleteHandle,
) -> Result<(), types::Error> {
Ok((self
.take_pending_kv_delete(handle.into())?
.task()
.recv()
.await?)?)
_handle: kv_store::ListHandle,
) -> Result<kv_store::BodyHandle, types::Error> {
todo!()
}
}
2 changes: 2 additions & 0 deletions lib/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub fn link_host_functions(linker: &mut component::Linker<ComponentCtx>) -> anyh
fastly::api::http_resp::add_to_linker(linker, |x| x.session())?;
fastly::api::http_types::add_to_linker(linker, |x| x.session())?;
fastly::api::log::add_to_linker(linker, |x| x.session())?;
fastly::api::object_store::add_to_linker(linker, |x| x.session())?;
fastly::api::kv_store::add_to_linker(linker, |x| x.session())?;
fastly::api::purge::add_to_linker(linker, |x| x.session())?;
fastly::api::secret_store::add_to_linker(linker, |x| x.session())?;
Expand All @@ -79,6 +80,7 @@ pub mod http_resp;
pub mod http_types;
pub mod kv_store;
pub mod log;
pub mod object_store;
pub mod purge;
pub mod secret_store;
pub mod types;
Expand Down
Loading

0 comments on commit 68cc150

Please sign in to comment.