use std::num::NonZeroUsize;
use miden_node_db::sqlite::ReadTx;
use miden_protocol::account::AccountId;
use crate::db::pagination::{Page, Paginated, key_cursor};
use crate::db::queries::VALID_FOREVER;
use crate::errors::DatabaseError;
const SQL: &str = include_str!("select_public_account_ids_page.sql");
#[derive(Debug, Clone, Copy)]
pub(crate) struct PublicAccountIdsPaged {
pub page_size: NonZeroUsize,
}
impl Paginated for PublicAccountIdsPaged {
type Item = AccountId;
type Cursor = Option<AccountId>;
fn page(
&self,
tx: &ReadTx<'_>,
next: &Option<AccountId>,
) -> Result<Page<AccountId, Option<AccountId>>, DatabaseError> {
let limit = i64::try_from(self.page_size.get() + 1).expect("page size fits within i64");
let account_ids =
tx.query(SQL, &[&limit, &VALID_FOREVER, &key_cursor(next.as_ref())], |row| {
row.get::<AccountId>(0)
})?;
Ok(Page::from_overflow(account_ids, self.page_size, |id| Some(*id)))
}
}