cleanup bulk read on PDDB

we don't have to pass a "size" field anymore; since we're end-offset
from pos, just pass pos and use that to chain records.
This commit is contained in:
bunnie 2024-09-23 18:07:50 +08:00
parent 921be1cf8b
commit 8cd1cef679
2 changed files with 27 additions and 50 deletions

View file

@ -12,7 +12,7 @@ use std::thread::JoinHandle;
pub use frontend::*; pub use frontend::*;
use num_traits::*; use num_traits::*;
use xous::{msg_scalar_unpack, send_message, Message, CID, SID}; use xous::{CID, Message, SID, msg_scalar_unpack, send_message};
use xous_ipc::Buffer; use xous_ipc::Buffer;
pub(crate) static REFCOUNT: AtomicU32 = AtomicU32::new(0); pub(crate) static REFCOUNT: AtomicU32 = AtomicU32::new(0);
pub(crate) static POLLER_REFCOUNT: AtomicU32 = AtomicU32::new(0); pub(crate) static POLLER_REFCOUNT: AtomicU32 = AtomicU32::new(0);
@ -1043,14 +1043,12 @@ impl Pddb {
- `total`: u32 with total number of keys to be transmitted (shouldn't change over the life of the session) - `total`: u32 with total number of keys to be transmitted (shouldn't change over the life of the session)
- `token`: [u32; 4] confirming or establishing the session token (shouldn't change over the life of the session) - `token`: [u32; 4] confirming or establishing the session token (shouldn't change over the life of the session)
First record start at data[24]: First record start at data[24]:
- `size` of archived struct: u32
- `pos` of archived struct, relative to the current start: u32 - `pos` of archived struct, relative to the current start: u32
- data[u8] of length `size` - data[u8] of length `pos`
Next record start at data[24 + size]: Next record start at data[24 + pos]:
- `size` of archived struct: u32
- `pos` of archived struct, relative to the current start: u32 - `pos` of archived struct, relative to the current start: u32
- data[u8] of length `size` - data[u8] of length `pos`
If `size` and `pos` are both zero, then, there are no more valid records in the return structure. If `pos` is zero, then, there are no more valid records in the return structure.
The caller shall repeatedly call `DictBulkRead` until the `code` is `Last`, at which point the token The caller shall repeatedly call `DictBulkRead` until the `code` is `Last`, at which point the token
is deleted from the server, freeing it to service a new transaction. The caller should then collate the is deleted from the server, freeing it to service a new transaction. The caller should then collate the
@ -1153,35 +1151,28 @@ impl Pddb {
let mut key_count = 0; let mut key_count = 0;
log::debug!("header: {:?}", header); log::debug!("header: {:?}", header);
while key_count < header.len { while key_count < header.len {
if index + size_of::<u32>() * 2 > pages.len() { if index + size_of::<u32>() > pages.len() {
// quit if we don't have enough space to decode at least another two indices // quit if we don't have enough space to decode at least another two indices
break; break;
} }
// Safety: `u32` contains no undefined values // Safety: `u32` contains no undefined values
let size = unsafe {
u32::from_le_bytes(
pages.as_slice()[index..index + size_of::<u32>()].try_into().unwrap(),
)
};
index += size_of::<u32>();
// Safety: `u32` contains no undefined values
let pos = unsafe { let pos = unsafe {
u32::from_le_bytes( u32::from_le_bytes(
pages.as_slice()[index..index + size_of::<u32>()].try_into().unwrap(), pages.as_slice()[index..index + size_of::<u32>()].try_into().unwrap(),
) )
}; };
index += size_of::<u32>(); index += size_of::<u32>();
log::trace!("unpacking message at {}({})", size, pos); log::trace!("unpacking message from {}-{}", index, index + pos as usize);
if size != 0 && pos != 0 { if pos != 0 {
//log::info!("extract archive: {}, {}, {}, {}", index, size, pos, pages.len()); //log::info!("extract archive: {}, {}, {}, {}", index, size, pos, pages.len());
//log::info!("{:x?}", &pages.as_slice::<u8>()[index..index + (size as usize)]); //log::info!("{:x?}", &pages.as_slice::<u8>()[index..index + (size as usize)]);
let r = unsafe { let r = unsafe {
rkyv::access_unchecked::<ArchivedPddbKeyRecord>( rkyv::access_unchecked::<ArchivedPddbKeyRecord>(
&pages.as_slice()[index..index + (size as usize)], &pages.as_slice()[index..index + (pos as usize)],
) )
}; };
//log::info!("increment index"); //log::info!("increment index");
index += size as usize; index += pos as usize;
let key = match rkyv::deserialize::<PddbKeyRecord, rkyv::rancor::Error>(r) { let key = match rkyv::deserialize::<PddbKeyRecord, rkyv::rancor::Error>(r) {
Ok(r) => r, Ok(r) => r,
Err(e) => { Err(e) => {

View file

@ -395,20 +395,20 @@ use std::io::ErrorKind;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use std::rc::Rc; use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread; use std::thread;
use locales::t; use locales::t;
use num_traits::*; use num_traits::*;
use rkyv::{ use rkyv::{
Archive, Place, Serialize,
api::low::LowSerializer, api::low::LowSerializer,
rancor::Panic, rancor::Panic,
ser::{allocator::SubAllocator, writer::Buffer as RkyvBuffer, Positional}, ser::{Positional, allocator::SubAllocator, writer::Buffer as RkyvBuffer},
with::{ArchiveWith, SerializeWith}, with::{ArchiveWith, SerializeWith},
Archive, Place, Serialize,
}; };
use xous::{msg_blocking_scalar_unpack, send_message, Message}; use xous::{Message, msg_blocking_scalar_unpack, send_message};
use xous_ipc::Buffer; use xous_ipc::Buffer;
#[cfg(feature = "perfcounter")] #[cfg(feature = "perfcounter")]
@ -1834,11 +1834,11 @@ fn wrapped_main() -> ! {
// now loop through and pack data into the slice // now loop through and pack data into the slice
let (header_buf, mut buf) = buf.split_at_mut(size_of::<BulkReadHeader>()); let (header_buf, mut buf) = buf.split_at_mut(size_of::<BulkReadHeader>());
enum SerializeResult<'a> { enum SerializeResult<'a> {
Success(usize, usize, &'a mut [u8], String, &'a mut [u8]), Success(usize, &'a mut [u8], String, &'a mut [u8]),
Failure(String), Failure(String),
} }
loop { loop {
if buf.len() < size_of::<u32>() * 2 { if buf.len() < size_of::<u32>() {
// not enough space to hold our header records, break and get a new buf // not enough space to hold our header records, break and get a new buf
break; break;
} }
@ -1909,7 +1909,7 @@ fn wrapped_main() -> ! {
data: if d.len() > 0 { Some(d) } else { None }, data: if d.len() > 0 { Some(d) } else { None },
}; };
state.read_total += attr.len; // commit the read length early state.read_total += attr.len; // commit the read length early
let (prebuf, sbuf) = buf.split_at_mut(size_of::<u32>() * 2); let (prebuf, sbuf) = buf.split_at_mut(size_of::<u32>());
let wrap = Wrap(&rec, PhantomData::<Identity>); let wrap = Wrap(&rec, PhantomData::<Identity>);
let writer = RkyvBuffer::from(&mut *sbuf); let writer = RkyvBuffer::from(&mut *sbuf);
@ -1917,13 +1917,9 @@ fn wrapped_main() -> ! {
match rkyv::api::low::to_bytes_in_with_alloc::<_, _, Panic>( match rkyv::api::low::to_bytes_in_with_alloc::<_, _, Panic>(
&wrap, writer, alloc, &wrap, writer, alloc,
) { ) {
Ok(serbuf) => SerializeResult::Success( Ok(serbuf) => {
serbuf.pos(), SerializeResult::Success(serbuf.pos(), sbuf, key_name, prebuf)
size_of::<ArchivedPddbKeyRecord>(), }
sbuf,
key_name,
prebuf,
),
Err(_) => SerializeResult::Failure(key_name), Err(_) => SerializeResult::Failure(key_name),
} }
} }
@ -1957,13 +1953,9 @@ fn wrapped_main() -> ! {
match rkyv::api::low::to_bytes_in_with_alloc::<_, _, Panic>( match rkyv::api::low::to_bytes_in_with_alloc::<_, _, Panic>(
&wrap, writer, alloc, &wrap, writer, alloc,
) { ) {
Ok(serbuf) => SerializeResult::Success( Ok(serbuf) => {
serbuf.pos(), SerializeResult::Success(serbuf.pos(), buf, key_name, pre_buf)
size_of::<ArchivedPddbKeyRecord>(), }
buf,
key_name,
pre_buf,
),
Err(_) => SerializeResult::Failure(key_name), Err(_) => SerializeResult::Failure(key_name),
} }
} }
@ -1981,19 +1973,13 @@ fn wrapped_main() -> ! {
std::line!(), std::line!(),
); );
match ser_result { match ser_result {
SerializeResult::Success(pos, len, sbuf, _key_name, pre_buf) => { SerializeResult::Success(pos, sbuf, _key_name, pre_buf) => {
log::debug!("packing message of {}({})", len + pos, pos); log::debug!("packing message to pos {}", pos);
// data can fit, copy it into the buffer. // data can fit, copy it into the buffer.
state.buf_starting_key_index += 1; state.buf_starting_key_index += 1;
header.len += 1; header.len += 1;
// read length increment was handled when the data was copied into the pre_buf[..4].copy_from_slice(&(pos as u32).to_le_bytes());
// serialization buffer. (_, buf) = sbuf.split_at_mut(pos);
pre_buf[..4].copy_from_slice(
//&(sbuf.len() as u32).to_le_bytes()
&((len + pos) as u32).to_le_bytes(),
);
pre_buf[4..8].copy_from_slice(&(pos as u32).to_le_bytes());
(_, buf) = sbuf.split_at_mut(len + pos);
} }
SerializeResult::Failure(key_name) => { SerializeResult::Failure(key_name) => {
log::debug!( log::debug!(