Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
implement find using find_many. remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
lostman committed Nov 22, 2023
1 parent c92c1d0 commit 97695a8
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 97 deletions.
6 changes: 1 addition & 5 deletions packages/fuel-indexer-api-server/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ pub(crate) fn check_wasm_toolchain_version(data: Vec<u8>) -> anyhow::Result<Stri
"ff_get_object".to_string(),
Function::new_typed(&mut store, |_: i64, _: i32, _: i32| 0i32),
);
exports.insert(
"ff_find_first".to_string(),
Function::new_typed(&mut store, |_: i64, _: i32, _: i32| 0i32),
);
exports.insert(
"ff_find_many".to_string(),
Function::new_typed(&mut store, |_: i64, _: i32, _: i32| 0i32),
Function::new_typed(&mut store, |_: i64, _: i64, _: i32, _: i32| 0i32),
);
exports.insert(
"ff_early_exit".to_string(),
Expand Down
18 changes: 2 additions & 16 deletions packages/fuel-indexer-plugin/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub use crate::find::{Field, Filter, OptionField, ToFilter};
// `Err` variant for ealy exit.
extern "C" {
fn ff_get_object(type_id: i64, ptr: *const u8, len: *mut u8) -> *mut u8;
fn ff_find_first(type_id: i64, ptr: *const u8, len: *mut u8) -> *mut u8;
fn ff_find_many(type_id: i64, limit: u64, ptr: *const u8, len: *mut u8) -> *mut u8;
fn ff_log_data(ptr: *const u8, len: u32, log_level: u32);
fn ff_put_object(type_id: i64, ptr: *const u8, len: u32);
Expand Down Expand Up @@ -130,21 +129,8 @@ pub trait Entity<'a>: Sized + PartialEq + Eq + std::fmt::Debug {

/// Finds the first entity that satisfies the given constraints.
fn find(query: impl ToFilter<Self>) -> Option<Self> {
unsafe {
let buff = bincode::serialize(&query.to_filter()).unwrap();
let mut bufflen = (buff.len() as u32).to_le_bytes();

let ptr = ff_find_first(Self::TYPE_ID, buff.as_ptr(), bufflen.as_mut_ptr());

if !ptr.is_null() {
let len = u32::from_le_bytes(bufflen) as usize;
let bytes = Vec::from_raw_parts(ptr, len, len);
let data = deserialize(&bytes).unwrap();
Some(Self::from_row(data))
} else {
None
}
}
let result = Self::find_many(query, Some(1));
result.into_iter().next()
}

fn find_many(query: impl ToFilter<Self>, limit: Option<usize>) -> Vec<Self> {
Expand Down
76 changes: 0 additions & 76 deletions packages/fuel-indexer/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,80 +206,6 @@ fn get_object(
}
}

fn find_first(
mut env: FunctionEnvMut<IndexEnv>,
type_id: i64,
ptr: u32,
len_ptr: u32,
) -> Result<u32, WasmIndexerError> {
let (idx_env, mut store) = env.data_and_store_mut();

if idx_env
.kill_switch
.load(std::sync::atomic::Ordering::SeqCst)
{
// If the kill switch has been flipped, returning an error will cause an
// early termination of WASM execution.
return Err(WasmIndexerError::KillSwitch);
}

let mem = idx_env
.memory
.as_mut()
.ok_or(WasmIndexerError::UninitializedMemory)?
.view(&store);

let len = WasmPtr::<u32>::new(len_ptr)
.deref(&mem)
.read()
.expect("Failed to read length from memory.");

let constraints = get_object_id(&mem, ptr + 1, len - 1).unwrap();

let rt = tokio::runtime::Handle::current();
let bytes = rt
.block_on(async {
idx_env
.db
.lock()
.await
.find_first(type_id, constraints)
.await
})
.unwrap();

if let Some(bytes) = bytes {
let alloc_fn = idx_env
.alloc
.as_mut()
.ok_or(WasmIndexerError::AllocMissing)?;

let size = bytes.len() as u32;
let result = alloc_fn
.call(&mut store, size)
.map_err(|_| WasmIndexerError::AllocFailed)?;
let range = result as usize..result as usize + size as usize;

let mem = idx_env
.memory
.as_mut()
.expect("Memory unitialized.")
.view(&store);
WasmPtr::<u32>::new(len_ptr)
.deref(&mem)
.write(size)
.expect("Failed to write length to memory.");

unsafe {
mem.data_unchecked_mut()[range].copy_from_slice(&bytes);
}

Ok(result)
} else {
Ok(0)
}
}

fn find_many(
mut env: FunctionEnvMut<IndexEnv>,
type_id: i64,
Expand Down Expand Up @@ -502,7 +428,6 @@ pub fn get_exports(store: &mut Store, env: &wasmer::FunctionEnv<IndexEnv>) -> Ex
let mut exports = Exports::new();

let f_get_obj = Function::new_typed_with_env(store, env, get_object);
let f_find_first = Function::new_typed_with_env(store, env, find_first);
let f_find_many = Function::new_typed_with_env(store, env, find_many);
let f_put_obj = Function::new_typed_with_env(store, env, put_object);
let f_log_data = Function::new_typed_with_env(store, env, log_data);
Expand All @@ -512,7 +437,6 @@ pub fn get_exports(store: &mut Store, env: &wasmer::FunctionEnv<IndexEnv>) -> Ex

exports.insert("ff_early_exit".to_string(), f_early_exit);
exports.insert("ff_get_object".to_string(), f_get_obj);
exports.insert("ff_find_first".to_string(), f_find_first);
exports.insert("ff_find_many".to_string(), f_find_many);
exports.insert("ff_put_object".to_string(), f_put_obj);
exports.insert(
Expand Down

0 comments on commit 97695a8

Please sign in to comment.