use super::{Database, Projection, QuerySource, SelectColumns, Selection};
use crate::{Entity, EntitySchema, Error, Id, IndexValue, PrimaryKey, primary::RowKey};
use ic_storage::{DefaultMemoryImpl, Memory};
use std::{cell::RefCell, rc::Rc};
#[must_use = "call fetch to execute the lookup"]
pub struct GetMany<'a, E: Entity, M: Memory = DefaultMemoryImpl> {
db: QuerySource<'a, M>,
keys: Result<Vec<E::Key>, Error>,
}
#[must_use = "call fetch to execute the lookup"]
pub struct SelectedGetMany<'a, E: Entity, S, M: Memory = DefaultMemoryImpl> {
lookup: GetMany<'a, E, M>,
selection: S,
}
impl<M: Memory> Database<M> {
pub fn get_many<E: Entity>(&self, keys: &[impl Copy + Into<Id<E>>]) -> GetMany<'_, E, M> {
GetMany::new(QuerySource::Direct(self), keys)
}
}
impl<'a, E: Entity, M: Memory> GetMany<'a, E, M> {
fn new(db: QuerySource<'a, M>, keys: &[impl Copy + Into<Id<E>>]) -> Self {
let keys = if keys.len() <= u32::BITS as usize {
Ok(keys.iter().map(|&key| key.into().key()).collect())
} else {
Err(Error::Limit)
};
Self { db, keys }
}
pub(crate) fn from_session(
db: Rc<RefCell<Database<M>>>,
keys: &[impl Copy + Into<Id<E>>],
) -> Self {
Self::new(QuerySource::Session(db), keys)
}
pub fn fetch(self) -> Result<Vec<Option<E>>, Error> {
self.fetch_with(|_, key, bytes| super::super::decode_primary::<E>(bytes, key))
}
pub fn select<S: Selection<E>>(
self,
fields: impl FnOnce(E::Columns) -> S,
) -> SelectedGetMany<'a, E, S, M>
where
E: SelectColumns,
{
SelectedGetMany {
lookup: self,
selection: fields(E::COLUMNS),
}
}
fn fetch_with<R>(
self,
decode: impl Fn(&EntitySchema, E::Key, &[u8]) -> Result<R, Error>,
) -> Result<Vec<Option<R>>, Error> {
let db = self.db.read()?;
let keys = self.keys?;
let entity = db.schema.admits(E::SCHEMA)?;
db.get_many_typed(entity, &keys, decode)
}
}
impl<E: SelectColumns, S: Selection<E>, M: Memory> SelectedGetMany<'_, E, S, M> {
pub fn fetch(self) -> Result<Vec<Option<S::Output>>, Error> {
let selection = self.selection;
let positions = selection.positions();
let positions = positions.as_ref();
Projection::Columns(positions).mask(E::SCHEMA)?;
let primary = E::SCHEMA
.field_position(E::SCHEMA.primary_key)
.ok_or(Error::Corrupt)?;
self.lookup.fetch_with(|entity, key, bytes| {
let mut spans = [(0, 0); crate::schema::MAX_FIELDS];
let mut values = [IndexValue::Absent; crate::schema::MAX_FIELDS];
entity.inspect_fields(bytes, 1 << primary, &mut values, Some(&mut spans))?;
if RowKey::from_index(values[primary])? != key.into_row_key() {
return Err(Error::Corrupt);
}
selection.decode_spans(bytes, &spans)
})
}
}