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::*;
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;
pub(crate) static 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)
- `token`: [u32; 4] confirming or establishing the session token (shouldn't change over the life of the session)
First record start at data[24]:
- `size` of archived struct: u32
- `pos` of archived struct, relative to the current start: u32
- data[u8] of length `size`
Next record start at data[24 + size]:
- `size` of archived struct: u32
- data[u8] of length `pos`
Next record start at data[24 + pos]:
- `pos` of archived struct, relative to the current start: u32
- data[u8] of length `size`
If `size` and `pos` are both zero, then, there are no more valid records in the return structure.
- data[u8] of length `pos`
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
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;
log::debug!("header: {:?}", header);
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
break;
}
// 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 {
u32::from_le_bytes(
pages.as_slice()[index..index + size_of::<u32>()].try_into().unwrap(),
)
};
index += size_of::<u32>();
log::trace!("unpacking message at {}({})", size, pos);
if size != 0 && pos != 0 {
log::trace!("unpacking message from {}-{}", index, index + pos as usize);
if pos != 0 {
//log::info!("extract archive: {}, {}, {}, {}", index, size, pos, pages.len());
//log::info!("{:x?}", &pages.as_slice::<u8>()[index..index + (size as usize)]);
let r = unsafe {
rkyv::access_unchecked::<ArchivedPddbKeyRecord>(
&pages.as_slice()[index..index + (size as usize)],
&pages.as_slice()[index..index + (pos as usize)],
)
};
//log::info!("increment index");
index += size as usize;
index += pos as usize;
let key = match rkyv::deserialize::<PddbKeyRecord, rkyv::rancor::Error>(r) {
Ok(r) => r,
Err(e) => {

View file

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