experiment with 'std' in rkyv for pddb crate

This commit is contained in:
bunnie 2022-10-25 17:41:12 +08:00
parent 41a95f4381
commit f5289be6fe
5 changed files with 89 additions and 1 deletions

7
Cargo.lock generated
View file

@ -3093,6 +3093,7 @@ dependencies = [
"memoffset",
"ptr_meta",
"rkyv_derive",
"seahash",
]
[[package]]
@ -3254,6 +3255,12 @@ dependencies = [
"version-compare",
]
[[package]]
name = "seahash"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
[[package]]
name = "sec1"
version = "0.2.1"

View file

@ -17,7 +17,7 @@ log = "0.4.14"
num-derive = {version = "0.3.3", default-features = false}
num-traits = {version = "0.2.14", default-features = false}
susres = {package = "xous-api-susres", version = "0.9.11"}
rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false}
rkyv = {version = "0.4.3", features = ["const_generics"]}
trng = {path = "../trng"}
spinor = {path = "../spinor"}
aes = {path="../aes"}

View file

@ -266,6 +266,7 @@ pub enum PddbRequestCode {
AccessDenied = 9,
Uninit = 10,
DuplicateEntry = 11,
BulkRead = 12,
}
// enum def is in rkyv_enum module
impl BasisRetentionPolicy {
@ -307,6 +308,8 @@ pub struct PddbKeyRequest {
pub alloc_hint: Option<u64>, // this is a usize but for IPC we must have defined memory sizes, so we pick the big option.
pub cb_sid: Option<[u32; 4]>,
pub result: PddbRequestCode,
/// used only to specify a readback size limit for bulk-return requests
pub bulk_limit: Option<u64>,
}
pub(crate) const MAX_PDDBKLISTLEN: usize = 4064;
@ -351,6 +354,29 @@ pub(crate) struct PddbBuf {
pub(crate) data: [u8; PDDB_BUF_DATA_LEN],
}
/// Returns key name, data, and attributes in a single record. For bulk reads.
/// The `dict` field is not specified, because, this had to be defined for the bulk read.
/// The `basis` field, however, must be specified because a dictionary can be composed of
/// data from multiple Bases.
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Debug)]
pub struct PddbKeyRecord {
pub name: String,
pub len: usize,
pub reserved: usize,
pub age: usize,
pub index: NonZeroU32,
pub basis: String,
/// If this is None, it means that the data read exceeded the bulk read limit
/// and the record must be explicitly re-read with a non-bulk call to fetch its data.
/// If the key is actually zero-length, a zero-length Some(Vec) is returned.
pub data: Option<Vec::<u8>>,
}
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub(crate) struct PddbKeyRecordWrapper {
pub keys: Vec::<PddbKeyRecord>,
}
#[allow(dead_code)]
/// Ensure that the `PddbBuf` struct is exactly one page big
const fn _assert_pddbbuf_is_4096_bytes() {

View file

@ -390,6 +390,7 @@ impl Pddb {
result: PddbRequestCode::Uninit,
cb_sid,
alloc_hint: if let Some(a) = alloc_hint {Some(a as u64)} else {None},
bulk_limit: None,
};
let mut buf = Buffer::into_buf(request)
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
@ -468,6 +469,7 @@ impl Pddb {
result: PddbRequestCode::Uninit,
cb_sid,
alloc_hint: None,
bulk_limit: None,
};
let mut buf = Buffer::into_buf(request)
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
@ -514,6 +516,7 @@ impl Pddb {
result: PddbRequestCode::Uninit,
cb_sid,
alloc_hint: None,
bulk_limit: None,
};
let mut buf = Buffer::into_buf(request)
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
@ -746,6 +749,10 @@ impl Pddb {
_ => Err(Error::new(ErrorKind::Other, "Return type not recognized")),
}
}
pub fn read_dict(&self, dict: &str, basis: Option<&str>) -> Result<Vec::<PddbKeyRecord>> {
Ok(Vec::new())
}
/// Triggers a dump of the PDDB to host disk
#[cfg(any(feature="hosted"))]
pub fn dbg_dump(&self, name: &str) -> Result<()> {

View file

@ -927,6 +927,54 @@ impl<'a> ShellCmdApi<'a> for PddbCmd {
}
}
}
#[cfg(feature="hosted")]
"rkyvtest" => {
use rkyv::{
archived_value,
de::deserializers::AllocDeserializer,
ser::{Serializer, serializers::WriteSerializer},
AlignedVec,
Archive,
Archived,
Deserialize,
Serialize,
};
let test = pddb::PddbKeyRecord {
name: "test".to_string(),
len: 64,
reserved: 128,
age: 0,
index: core::num::NonZeroU32::new(1).unwrap(),
basis: ".System".to_string(),
data: Some(vec![0; 64]),
};
let mut serializer = WriteSerializer::new(AlignedVec::new());
let pos = serializer.serialize_value(&test).unwrap();
let buf = serializer.into_inner();
log::info!("serialized test len: {}", buf.len());
log::info!("more buf props: {}, {}", buf.as_slice().len(), pos);
let archived = unsafe { archived_value::<pddb::PddbKeyRecord>(buf.as_slice(), pos) };
let deserialized = archived.deserialize(&mut AllocDeserializer).unwrap();
log::info!("deserialized: {:?}", deserialized);
let test2 = pddb::PddbKeyRecord {
name: "test test test".to_string(),
len: 5000,
reserved: 128,
age: 0,
index: core::num::NonZeroU32::new(1).unwrap(),
basis: ".System".to_string(),
data: Some(vec![0; 5000]),
};
let mut serializer = WriteSerializer::new(AlignedVec::new());
let pos = serializer.serialize_value(&test2).unwrap();
let buf = serializer.into_inner();
log::info!("serialized test2 len: {}", buf.len());
log::info!("more buf props: {}, {}", buf.as_slice().len(), pos);
let archived = unsafe { archived_value::<pddb::PddbKeyRecord>(buf.as_slice(), pos) };
let deserialized = archived.deserialize(&mut AllocDeserializer).unwrap();
log::info!("deserialized: {:?}", deserialized);
}
#[cfg(feature="shellperf")]
"v2p" => {
// don't generate a new object since it clears the data, just do a raw dump