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

Commit

Permalink
add a limit on the number of find_many results
Browse files Browse the repository at this point in the history
  • Loading branch information
lostman committed Nov 22, 2023
1 parent 318e3d1 commit 8f925f9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/fuel-indexer-lib/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,6 @@ pub const DISABLE_TOOLCHAIN_VERSION_CHECK: bool = false;

/// Default Fuel network to use.
pub const NETWORK: &str = "beta-4";

/// Default limit for the number of `find_many` results.
pub const FIND_MANY_LIMIT: usize = 255;
11 changes: 8 additions & 3 deletions packages/fuel-indexer-plugin/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use crate::find::{Field, Filter, OptionField, ToFilter};
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, 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);
fn ff_put_many_to_many_record(ptr: *const u8, len: u32);
Expand Down Expand Up @@ -147,12 +147,17 @@ pub trait Entity<'a>: Sized + PartialEq + Eq + std::fmt::Debug {
}
}

fn find_many(query: impl ToFilter<Self>) -> Vec<Self> {
fn find_many(query: impl ToFilter<Self>, limit: Option<usize>) -> Vec<Self> {
unsafe {
let buff = bincode::serialize(&query.to_filter()).unwrap();
let mut bufflen = (buff.len() as u32).to_le_bytes();

let ptr = ff_find_many(Self::TYPE_ID, buff.as_ptr(), bufflen.as_mut_ptr());
let ptr = ff_find_many(
Self::TYPE_ID,
limit.unwrap_or(fuel_indexer_lib::defaults::FIND_MANY_LIMIT) as u64,
buff.as_ptr(),
bufflen.as_mut_ptr(),
);

if !ptr.is_null() {
let len = u32::from_le_bytes(bufflen) as usize;
Expand Down
15 changes: 15 additions & 0 deletions packages/fuel-indexer-tests/indexers/fuel-indexer-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ mod fuel_indexer_test {
.gt("f".to_string())
.order_by(FindEntity::value())
.asc(),
None,
);
assert_eq!(fs.len(), 4);
assert_eq!(fs[0].string_value, "find2");
Expand All @@ -665,13 +666,27 @@ mod fuel_indexer_test {
.gt("f".to_string())
.order_by(FindEntity::value())
.desc(),
None,
);

assert_eq!(fs.len(), 4);
assert_eq!(fs[0].string_value, "find5");
assert_eq!(fs[1].string_value, "find4");
assert_eq!(fs[2].string_value, "find3");
assert_eq!(fs[3].string_value, "find2");

// Test searching for multiple entities with a result limit
let fs: Vec<FindEntity> = FindEntity::find_many(
FindEntity::string_value()
.gt("f".to_string())
.order_by(FindEntity::value())
.desc(),
Some(2),
);

assert_eq!(fs.len(), 2);
assert_eq!(fs[0].string_value, "find5");
assert_eq!(fs[1].string_value, "find4");
} else if block_data.height == 6 {
// There is no such block. The lookup will fail.
IndexMetadataEntity::find(IndexMetadataEntity::block_height().eq(777))
Expand Down
3 changes: 2 additions & 1 deletion packages/fuel-indexer/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,15 @@ Do your WASM modules need to be rebuilt?
pub async fn find_many(
&mut self,
type_id: i64,
limit: usize,
constraints: String,
) -> IndexerResult<Vec<Vec<u8>>> {
let table = &self
.tables
.get(&type_id)
.ok_or(IndexerDatabaseError::TableMappingDoesNotExist(type_id))?;

let query = format!("SELECT object from {table} WHERE {constraints}");
let query = format!("SELECT object from {table} WHERE {constraints} LIMIT {limit}");

let conn = self
.stashed
Expand Down
3 changes: 2 additions & 1 deletion packages/fuel-indexer/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ fn find_first(
fn find_many(
mut env: FunctionEnvMut<IndexEnv>,
type_id: i64,
limit: u64,
ptr: u32,
len_ptr: u32,
) -> Result<u32, WasmIndexerError> {
Expand Down Expand Up @@ -317,7 +318,7 @@ fn find_many(
.db
.lock()
.await
.find_many(type_id, constraints)
.find_many(type_id, limit as usize, constraints)
.await
})
.unwrap();
Expand Down

0 comments on commit 8f925f9

Please sign in to comment.