uqa_storage/key_value/occurrence_format/
accelerators.rs1use super::{other_error, BudgetedVec, StorageBackendResult, StorageReadControl};
10
11pub struct BlockMaxValue<'a> {
13 pub score: f64,
14 pub fingerprint: &'a str,
15}
16
17impl<'a> BlockMaxValue<'a> {
18 pub fn decode(bytes: &'a [u8]) -> StorageBackendResult<Self> {
19 if bytes.len() < 12 || &bytes[..4] != b"UBM1" {
20 return Err(other_error("invalid occurrence block-max value"));
21 }
22 let score = f64::from_le_bytes(bytes[4..12].try_into().expect("checked score width"));
23 crate::block_max_index::validate_score(score)?;
24 let fingerprint = std::str::from_utf8(&bytes[12..])
25 .map_err(|_| other_error("invalid block-max scorer identity"))?;
26 Ok(Self { score, fingerprint })
27 }
28
29 pub fn encode(&self, control: &StorageReadControl) -> StorageBackendResult<BudgetedVec<u8>> {
30 control.check()?;
31 crate::block_max_index::validate_score(self.score)?;
32 let mut bytes = BudgetedVec::new(control.memory());
33 bytes.extend_from_slice(b"UBM1")?;
34 bytes.extend_from_slice(&self.score.to_le_bytes())?;
35 for chunk in self.fingerprint.as_bytes().chunks(1024) {
36 control.check()?;
37 bytes.extend_from_slice(chunk)?;
38 }
39 Ok(bytes)
40 }
41}