use super::*;
use crate::native_v2::semantic_map::{Edit, WorkBudget};
#[derive(Clone, Copy, Debug)]
pub struct NativeSemanticWork {
pub pages: u64,
pub hydrated_bytes: u64,
}
impl NativeSemanticWork {
pub(super) fn budget(self) -> WorkBudget {
WorkBudget::new(self.pages, self.hydrated_bytes)
}
}
#[derive(Clone, Copy, Debug)]
pub struct NativeEditLimits {
pub edits: u64,
pub input_bytes: u64,
pub work: NativeSemanticWork,
}
pub struct NativeSemanticEdit<'a> {
pub key: &'a [u8],
pub value: Option<&'a [u8]>,
}
struct OwnedEdit {
key: Vec<u8>,
value: Option<Vec<u8>>,
}
pub(super) struct SemanticInputs {
entries: Vec<OwnedEdit>,
pub(super) limits: NativeEditLimits,
_charge: Arc<Charge>,
}
impl Drop for SemanticInputs {
fn drop(&mut self) {}
}
impl SemanticInputs {
pub(super) fn copy(
pages: &PageStore,
edits: &[NativeSemanticEdit<'_>],
limits: NativeEditLimits,
) -> Result<Self, ObjectLogError> {
if u64::try_from(edits.len()).map_err(|_| invalid("semantic edit count"))? > limits.edits {
return Err(invalid("semantic edit count limit"));
}
let mut bytes = 0usize;
for (ordinal, edit) in edits.iter().enumerate() {
if ordinal > 0 && edits[ordinal - 1].key >= edit.key {
return Err(invalid(
"semantic edits must have strictly increasing full keys",
));
}
bytes = bytes
.checked_add(edit.key.len())
.and_then(|n| n.checked_add(edit.value.map_or(0, <[u8]>::len)))
.ok_or_else(|| invalid("semantic input length overflow"))?;
}
if u64::try_from(bytes).map_err(|_| invalid("semantic input length"))? > limits.input_bytes
{
return Err(invalid("semantic input byte limit"));
}
let allocation = edits
.len()
.checked_mul(std::mem::size_of::<OwnedEdit>() + std::mem::size_of::<Edit<'_>>())
.and_then(|n| n.checked_add(bytes))
.and_then(|n| {
n.checked_add(std::mem::size_of::<Self>() + std::mem::size_of::<Vec<Edit<'_>>>())
})
.ok_or_else(|| invalid("semantic input allocation"))?;
let charge = pages.charge_output(allocation)?;
let entries = edits
.iter()
.map(|edit| OwnedEdit {
key: edit.key.to_vec(),
value: edit.value.map(<[u8]>::to_vec),
})
.collect();
Ok(Self {
entries,
limits,
_charge: charge,
})
}
pub(super) fn references(&self) -> Vec<Edit<'_>> {
self.entries
.iter()
.map(|edit| Edit {
key: &edit.key,
value: edit.value.as_deref(),
})
.collect()
}
}
impl NativeLog {
pub async fn prepare_with_edits(
&self,
writer: &NativeWriter,
expected_cut: &NativeReadCut,
inputs: &[NativeAppend<'_>],
edits: &[NativeSemanticEdit<'_>],
limits: NativeEditLimits,
) -> Result<NativePublication, ObjectLogError> {
self.prepare_inner(writer, inputs, Some((expected_cut, edits, limits)))
.await
}
}
pub struct NativeSemanticValue {
value: crate::native_v2::semantic_map::OwnedBytes,
}
impl NativeSemanticValue {
pub fn bytes(&self) -> &[u8] {
self.value.as_slice()
}
}
impl NativeLog {
pub async fn semantic_lookup(
&self,
cut: &NativeReadCut,
key: &[u8],
limits: NativeSemanticWork,
) -> Result<Option<NativeSemanticValue>, ObjectLogError> {
self.check(&cut.origin)?;
let mut work = limits.budget();
work.hydrate(u64::try_from(key.len()).map_err(|_| invalid("semantic lookup key length"))?)?;
let allocation = key
.len()
.checked_add(std::mem::size_of::<LiteralInput>())
.ok_or_else(|| invalid("semantic lookup key allocation"))?;
let charge = self.owner.pages.charge_output(allocation)?;
let key = LiteralInput {
bytes: key.to_vec(),
_charge: charge,
};
let read = RetainedRead {
captured: cut.read.captured.clone(),
binding: cut.read.binding.clone(),
knowledge: cut.read.knowledge.clone(),
charge: cut.read.charge.clone(),
};
let owner = self.owner.clone();
self.run(move |physical| async move {
let pages = owner
.pages
.in_operation(physical)?
.for_binding(&read.binding)?;
let map = read.captured.semantic_map(&pages, &mut work).await?;
map.lookup(&pages, &key.bytes, &mut work)
.await
.map(|value| value.map(|value| NativeSemanticValue { value }))
})
.await
}
}
mod scan;
pub use scan::{
NativeScanLimits, NativeScanStatus, NativeSemanticContinuation, NativeSemanticRange,
NativeSemanticRow, NativeSemanticScan,
};
#[cfg(test)]
mod tests;