mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
add listing routines
This commit is contained in:
parent
7d660a3592
commit
11c22b6d19
3 changed files with 242 additions and 0 deletions
|
|
@ -78,6 +78,12 @@ pub(crate) enum Opcode {
|
||||||
DeleteDict,
|
DeleteDict,
|
||||||
KeyAttributes,
|
KeyAttributes,
|
||||||
|
|
||||||
|
// routines to list available resources
|
||||||
|
KeyCountInDict,
|
||||||
|
GetKeyNameAtIndex,
|
||||||
|
DictCountInBasis,
|
||||||
|
GetDictNameAtIndex,
|
||||||
|
|
||||||
/// primary method for accessing the database
|
/// primary method for accessing the database
|
||||||
KeyRequest,
|
KeyRequest,
|
||||||
|
|
||||||
|
|
@ -120,6 +126,16 @@ pub struct PddbBasisRequest {
|
||||||
pub name: xous_ipc::String::<BASIS_NAME_LEN>,
|
pub name: xous_ipc::String::<BASIS_NAME_LEN>,
|
||||||
pub code: PddbRequestCode,
|
pub code: PddbRequestCode,
|
||||||
}
|
}
|
||||||
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
|
pub struct PddbDictRequest {
|
||||||
|
pub basis_specified: bool,
|
||||||
|
pub basis: xous_ipc::String::</* BASIS_NAME_LEN */ 64>, // pending https://github.com/rust-lang/rust/issues/90195
|
||||||
|
pub dict: xous_ipc::String::</*DICT_NAME_LEN*/ 111>, // pending https://github.com/rust-lang/rust/issues/90195
|
||||||
|
pub key: xous_ipc::String::</*KEY_NAME_LEN*/ 95>, // pending https://github.com/rust-lang/rust/issues/90195
|
||||||
|
pub index: u32,
|
||||||
|
pub token: [u32; 4],
|
||||||
|
pub code: PddbRequestCode,
|
||||||
|
}
|
||||||
|
|
||||||
/// A structure for requesting a token to access a particular key/value pair
|
/// A structure for requesting a token to access a particular key/value pair
|
||||||
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ pub struct Pddb {
|
||||||
/// in the case of a basis change. Basis changes are thought to be rare; so, big changes
|
/// in the case of a basis change. Basis changes are thought to be rare; so, big changes
|
||||||
/// like this are probably OK.
|
/// like this are probably OK.
|
||||||
keys: Arc<Mutex<HashMap<ApiToken, Box<dyn Fn() + 'static + Send> >>>,
|
keys: Arc<Mutex<HashMap<ApiToken, Box<dyn Fn() + 'static + Send> >>>,
|
||||||
|
trng: trng::Trng,
|
||||||
}
|
}
|
||||||
impl Pddb {
|
impl Pddb {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
|
@ -84,6 +85,7 @@ impl Pddb {
|
||||||
cb_sid: sid,
|
cb_sid: sid,
|
||||||
cb_handle: Some(handle),
|
cb_handle: Some(handle),
|
||||||
keys,
|
keys,
|
||||||
|
trng: trng::Trng::new(&xns).unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// return a list of all open bases
|
/// return a list of all open bases
|
||||||
|
|
@ -370,6 +372,124 @@ impl Pddb {
|
||||||
_ => Err(Error::new(ErrorKind::Other, "Internal error"))
|
_ => Err(Error::new(ErrorKind::Other, "Internal error"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn list_keys(&mut self, dict_name: &str, basis_name: Option<&str>) -> Result<Vec::<String>> {
|
||||||
|
if dict_name.len() > (DICT_NAME_LEN - 1) {
|
||||||
|
return Err(Error::new(ErrorKind::InvalidInput, "dictionary name too long"));
|
||||||
|
}
|
||||||
|
let bname = if let Some(bname) = basis_name {
|
||||||
|
if bname.len() > BASIS_NAME_LEN - 1 {
|
||||||
|
return Err(Error::new(ErrorKind::InvalidInput, "basis name too long"));
|
||||||
|
}
|
||||||
|
xous_ipc::String::<BASIS_NAME_LEN>::from_str(bname)
|
||||||
|
} else {
|
||||||
|
xous_ipc::String::<BASIS_NAME_LEN>::new()
|
||||||
|
};
|
||||||
|
// this is a two-phase query, because it's quite likely that the number of keys can be very large in a dict.
|
||||||
|
let token = [self.trng.get_u32().unwrap(), self.trng.get_u32().unwrap(), self.trng.get_u32().unwrap(), self.trng.get_u32().unwrap()];
|
||||||
|
let request = PddbDictRequest {
|
||||||
|
basis_specified: basis_name.is_some(),
|
||||||
|
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
||||||
|
dict: xous_ipc::String::<DICT_NAME_LEN>::from_str(dict_name),
|
||||||
|
key: xous_ipc::String::<KEY_NAME_LEN>::new(),
|
||||||
|
index: 0,
|
||||||
|
code: PddbRequestCode::Uninit,
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
let mut buf = Buffer::into_buf(request)
|
||||||
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||||
|
buf.lend_mut(self.conn, Opcode::KeyCountInDict.to_u32().unwrap())
|
||||||
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||||
|
|
||||||
|
let response = buf.to_original::<PddbDictRequest, _>().unwrap();
|
||||||
|
let count = match response.code {
|
||||||
|
PddbRequestCode::NoErr => response.index,
|
||||||
|
_ => return Err(Error::new(ErrorKind::Other, "Internal error")),
|
||||||
|
};
|
||||||
|
// very non-optimal, slow way of doing this, but let's just get it working first and optimize later.
|
||||||
|
// it's absolutely important that you access every entry, and the highest index last, because
|
||||||
|
// that is how the server knows you've finished with the list-out.
|
||||||
|
let mut key_list = Vec::<String>::new();
|
||||||
|
for index in 0..count {
|
||||||
|
let request = PddbDictRequest {
|
||||||
|
basis_specified: basis_name.is_some(),
|
||||||
|
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
||||||
|
dict: xous_ipc::String::<DICT_NAME_LEN>::from_str(dict_name),
|
||||||
|
key: xous_ipc::String::<KEY_NAME_LEN>::new(),
|
||||||
|
index,
|
||||||
|
code: PddbRequestCode::Uninit,
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
let mut buf = Buffer::into_buf(request)
|
||||||
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||||
|
buf.lend_mut(self.conn, Opcode::GetKeyNameAtIndex.to_u32().unwrap())
|
||||||
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||||
|
let response = buf.to_original::<PddbDictRequest, _>().unwrap();
|
||||||
|
match response.code {
|
||||||
|
PddbRequestCode::NoErr => key_list.push(String::from(response.key.as_str().expect("utf-8 parse error in key name"))),
|
||||||
|
_ => return Err(Error::new(ErrorKind::Other, "Internal error")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(key_list)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn list_dict(&mut self, basis_name: Option<&str>) -> Result<Vec::<String>> {
|
||||||
|
let bname = if let Some(bname) = basis_name {
|
||||||
|
if bname.len() > BASIS_NAME_LEN - 1 {
|
||||||
|
return Err(Error::new(ErrorKind::InvalidInput, "basis name too long"));
|
||||||
|
}
|
||||||
|
xous_ipc::String::<BASIS_NAME_LEN>::from_str(bname)
|
||||||
|
} else {
|
||||||
|
xous_ipc::String::<BASIS_NAME_LEN>::new()
|
||||||
|
};
|
||||||
|
// this is a two-phase query, because it's quite likely that the number of keys can be very large in a dict.
|
||||||
|
let token = [self.trng.get_u32().unwrap(), self.trng.get_u32().unwrap(), self.trng.get_u32().unwrap(), self.trng.get_u32().unwrap()];
|
||||||
|
let request = PddbDictRequest {
|
||||||
|
basis_specified: basis_name.is_some(),
|
||||||
|
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
||||||
|
dict: xous_ipc::String::<DICT_NAME_LEN>::new(),
|
||||||
|
key: xous_ipc::String::<KEY_NAME_LEN>::new(),
|
||||||
|
index: 0,
|
||||||
|
code: PddbRequestCode::Uninit,
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
let mut buf = Buffer::into_buf(request)
|
||||||
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||||
|
buf.lend_mut(self.conn, Opcode::DictCountInBasis.to_u32().unwrap())
|
||||||
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||||
|
|
||||||
|
let response = buf.to_original::<PddbDictRequest, _>().unwrap();
|
||||||
|
let count = match response.code {
|
||||||
|
PddbRequestCode::NoErr => response.index,
|
||||||
|
_ => return Err(Error::new(ErrorKind::Other, "Internal error")),
|
||||||
|
};
|
||||||
|
// very non-optimal, slow way of doing this, but let's just get it working first and optimize later.
|
||||||
|
// it's absolutely important that you access every entry, and the highest index last, because
|
||||||
|
// that is how the server knows you've finished with the list-out.
|
||||||
|
let mut dict_list = Vec::<String>::new();
|
||||||
|
for index in 0..count {
|
||||||
|
let request = PddbDictRequest {
|
||||||
|
basis_specified: basis_name.is_some(),
|
||||||
|
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
||||||
|
dict: xous_ipc::String::<DICT_NAME_LEN>::new(),
|
||||||
|
key: xous_ipc::String::<KEY_NAME_LEN>::new(),
|
||||||
|
index,
|
||||||
|
code: PddbRequestCode::Uninit,
|
||||||
|
token,
|
||||||
|
};
|
||||||
|
let mut buf = Buffer::into_buf(request)
|
||||||
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||||
|
buf.lend_mut(self.conn, Opcode::GetDictNameAtIndex.to_u32().unwrap())
|
||||||
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
||||||
|
let response = buf.to_original::<PddbDictRequest, _>().unwrap();
|
||||||
|
match response.code {
|
||||||
|
PddbRequestCode::NoErr => dict_list.push(String::from(response.dict.as_str().expect("utf-8 parse error in key name"))),
|
||||||
|
_ => return Err(Error::new(ErrorKind::Other, "Internal error")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(dict_list)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Pddb {
|
impl Drop for Pddb {
|
||||||
|
|
|
||||||
|
|
@ -537,6 +537,10 @@ fn xmain() -> ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// main server loop
|
// main server loop
|
||||||
|
let mut key_list = Vec::<String>::new(); // storage for key lists
|
||||||
|
let mut key_token: Option<[u32; 4]> = None;
|
||||||
|
let mut dict_list = Vec::<String>::new(); // storage for dict lists
|
||||||
|
let mut dict_token: Option<[u32; 4]> = None;
|
||||||
loop {
|
loop {
|
||||||
let mut msg = xous::receive_message(pddb_sid).unwrap();
|
let mut msg = xous::receive_message(pddb_sid).unwrap();
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
|
|
@ -863,6 +867,108 @@ fn xmain() -> ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some(Opcode::KeyCountInDict) => {
|
||||||
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
|
let mut req = buffer.to_original::<PddbDictRequest, _>().unwrap();
|
||||||
|
if key_token.is_some() {
|
||||||
|
req.code = PddbRequestCode::AccessDenied;
|
||||||
|
buffer.replace(req).unwrap();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
key_token = Some(req.token);
|
||||||
|
key_list.clear();
|
||||||
|
let bname = if req.basis_specified {
|
||||||
|
Some(req.basis.as_str().unwrap())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
let dict = req.dict.as_str().expect("dict utf-8 decode error");
|
||||||
|
match basis_cache.key_list(&mut pddb_os, dict, bname) {
|
||||||
|
Ok(list) => {
|
||||||
|
req.index = list.len() as u32;
|
||||||
|
for key in list {
|
||||||
|
key_list.push(key);
|
||||||
|
}
|
||||||
|
req.code = PddbRequestCode::NoErr;
|
||||||
|
}
|
||||||
|
Err(e) => match e.kind() {
|
||||||
|
std::io::ErrorKind::NotFound => req.code = PddbRequestCode::NotFound,
|
||||||
|
_ => req.code = PddbRequestCode::InternalError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buffer.replace(req).unwrap();
|
||||||
|
}
|
||||||
|
Some(Opcode::GetKeyNameAtIndex) => {
|
||||||
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
|
let mut req = buffer.to_original::<PddbDictRequest, _>().unwrap();
|
||||||
|
if let Some(token) = key_token {
|
||||||
|
if req.token != token {
|
||||||
|
req.code = PddbRequestCode::AccessDenied;
|
||||||
|
} else {
|
||||||
|
if req.index >= key_list.len() as u32 {
|
||||||
|
req.code = PddbRequestCode::InternalError;
|
||||||
|
} else {
|
||||||
|
req.key = xous_ipc::String::<KEY_NAME_LEN>::from_str(&key_list[req.index as usize]);
|
||||||
|
req.code = PddbRequestCode::NoErr;
|
||||||
|
// the last index requested must be the highest one!
|
||||||
|
if req.index == key_list.len() as u32 - 1 {
|
||||||
|
key_token = None;
|
||||||
|
key_list.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
req.code = PddbRequestCode::AccessDenied;
|
||||||
|
}
|
||||||
|
buffer.replace(req).unwrap();
|
||||||
|
}
|
||||||
|
Some(Opcode::DictCountInBasis) => {
|
||||||
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
|
let mut req = buffer.to_original::<PddbDictRequest, _>().unwrap();
|
||||||
|
if key_token.is_some() {
|
||||||
|
req.code = PddbRequestCode::AccessDenied;
|
||||||
|
buffer.replace(req).unwrap();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
dict_token = Some(req.token);
|
||||||
|
dict_list.clear();
|
||||||
|
let bname = if req.basis_specified {
|
||||||
|
Some(req.basis.as_str().unwrap())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
let list = basis_cache.dict_list(&mut pddb_os, bname);
|
||||||
|
req.index = list.len() as u32;
|
||||||
|
for dict in list {
|
||||||
|
dict_list.push(dict);
|
||||||
|
}
|
||||||
|
req.code = PddbRequestCode::NoErr;
|
||||||
|
buffer.replace(req).unwrap();
|
||||||
|
}
|
||||||
|
Some(Opcode::GetDictNameAtIndex) => {
|
||||||
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
|
let mut req = buffer.to_original::<PddbDictRequest, _>().unwrap();
|
||||||
|
if let Some(token) = key_token {
|
||||||
|
if req.token != token {
|
||||||
|
req.code = PddbRequestCode::AccessDenied;
|
||||||
|
} else {
|
||||||
|
if req.index >= dict_list.len() as u32 {
|
||||||
|
req.code = PddbRequestCode::InternalError;
|
||||||
|
} else {
|
||||||
|
req.dict = xous_ipc::String::<DICT_NAME_LEN>::from_str(&dict_list[req.index as usize]);
|
||||||
|
req.code = PddbRequestCode::NoErr;
|
||||||
|
// the last index requested must be the highest one!
|
||||||
|
if req.index == dict_list.len() as u32 - 1 {
|
||||||
|
dict_token = None;
|
||||||
|
dict_list.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
req.code = PddbRequestCode::AccessDenied;
|
||||||
|
}
|
||||||
|
buffer.replace(req).unwrap();
|
||||||
|
}
|
||||||
Some(Opcode::ReadKey) => {
|
Some(Opcode::ReadKey) => {
|
||||||
// placeholder
|
// placeholder
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue