From b4bfaefa64d0ea93fe23654bc3390e0eeef66f7d Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 7 Jan 2022 22:10:54 +0800 Subject: [PATCH] start a cleanup pass on warnings --- .vscode/settings.json | 3 +- services/pddb/src/api.rs | 3 ++ services/pddb/src/backend/basis.rs | 4 +- services/pddb/src/frontend/pddbkey.rs | 1 + services/pddb/src/main.rs | 66 ++++++++++++++++++++++++--- 5 files changed, 68 insertions(+), 9 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index eb230f7ba..4312bfe05 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "rust-analyzer.checkOnSave.allTargets": false + "rust-analyzer.checkOnSave.allTargets": false, + "rust-analyzer.cargo.target":"riscv32imac-unknown-xous-elf" } diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index b5aa99689..db7510027 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -160,6 +160,9 @@ pub(crate) enum PddbRetcode { Ok = 1, BasisLost = 2, AccessDenied = 3, + UnexpectedEof = 4, + InternalError = 5, + DiskFull = 6, } /// PddbBuf is a C-representation of a page of memory that's used /// to shuttle data for streaming channels. It must be exactly one diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index dc3ca09bd..31c547f5f 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -1319,7 +1319,7 @@ impl BasisCacheEntry { if let Some(dict) = self.dicts.get_mut(&String::from(name)) { let dict_offset = VirtAddr::new(dict.index.get() as u64 * DICT_VSIZE).unwrap(); if !dict.clean { - let mut dict_name = DictName::try_from_str(name).or(Err(Error::new(ErrorKind::InvalidInput, "dictionary name invalid: invalid utf-8 or length")))?; + let dict_name = DictName::try_from_str(name).or(Err(Error::new(ErrorKind::InvalidInput, "dictionary name invalid: invalid utf-8 or length")))?; let dict_disk = Dictionary { flags: dict.flags, age: dict.age, @@ -1385,7 +1385,7 @@ impl BasisCacheEntry { log::debug!("merging in key {}", key_name); // key is within the current page, add it to the target list let mut dk_entry = DictKeyEntry::default(); - let mut kn = KeyName::try_from_str(key_name).or(Err(Error::new(ErrorKind::InvalidInput, "key name invalid: invalid utf-8 or length")))?; + let kn = KeyName::try_from_str(key_name).or(Err(Error::new(ErrorKind::InvalidInput, "key name invalid: invalid utf-8 or length")))?; let key_desc = KeyDescriptor { start: key.start, len: key.len, diff --git a/services/pddb/src/frontend/pddbkey.rs b/services/pddb/src/frontend/pddbkey.rs index 6ba98d433..56a6f1c62 100644 --- a/services/pddb/src/frontend/pddbkey.rs +++ b/services/pddb/src/frontend/pddbkey.rs @@ -134,6 +134,7 @@ impl<'a> Write for PddbKey<'a> { match FromPrimitive::from_u8(rcode as u8) { Some(PddbRetcode::Ok) => Ok(()), Some(PddbRetcode::BasisLost) => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")), + Some(PddbRetcode::DiskFull) => Err(Error::new(ErrorKind::OutOfMemory, "Out of disk space, some data lost on sync")), _ => Err(Error::new(ErrorKind::Interrupted, "Flush failed for unspecified reasons")), } } else { diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index a65a40f73..6c7572c14 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -399,6 +399,7 @@ struct TokenRecord { pub dict: String, pub key: String, pub basis: Option, + pub alloc_hint: Option, pub conn: xous::CID, // callback connection } @@ -724,6 +725,7 @@ fn xmain() -> ! { key: String::from(key), basis: if let Some(name) = bname {Some(String::from(name))} else {None}, conn: cid, + alloc_hint: if let Some(hint) = req.alloc_hint {Some(hint as usize)} else {None}, }; token_dict.insert(token, token_record); req.token = Some(token); @@ -948,7 +950,7 @@ fn xmain() -> ! { Some(Opcode::GetDictNameAtIndex) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut req = buffer.to_original::().unwrap(); - if let Some(token) = key_token { + if let Some(token) = dict_token { if req.token != token { req.code = PddbRequestCode::AccessDenied; } else { @@ -970,14 +972,66 @@ fn xmain() -> ! { buffer.replace(req).unwrap(); } Some(Opcode::ReadKey) => { - // placeholder + let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; + let pbuf = PddbBuf::from_slice_mut(buffer.as_mut()); // direct translation, no serialization necessary for performance + let token = pbuf.token; + if let Some(rec) = token_dict.get(&token) { + match basis_cache.key_read(&mut pddb_os, + &rec.dict, &rec.key, + &mut pbuf.data[..pbuf.len as usize], Some(pbuf.position as usize), + if let Some (name) = &rec.basis {Some(&name)} else {None}) { + Ok(readlen) => { + pbuf.len = readlen as u16; + pbuf.retcode = PddbRetcode::Ok; + } + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => pbuf.retcode = PddbRetcode::BasisLost, + std::io::ErrorKind::UnexpectedEof => pbuf.retcode = PddbRetcode::UnexpectedEof, + std::io::ErrorKind::OutOfMemory => pbuf.retcode = PddbRetcode::DiskFull, + _ => pbuf.retcode = PddbRetcode::InternalError, + } + } + } else { + pbuf.retcode = PddbRetcode::BasisLost; + } + // we don't nede a "replace" operation because all ops happen in-place } Some(Opcode::WriteKey) => { - // placeholder - } - Some(Opcode::WriteKeyFlush) => { - // placeholder + let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; + let pbuf = PddbBuf::from_slice_mut(buffer.as_mut()); // direct translation, no serialization necessary for performance + let token = pbuf.token; + if let Some(rec) = token_dict.get(&token) { + match basis_cache.key_update(&mut pddb_os, + &rec.dict, &rec.key, + &pbuf.data[..pbuf.len as usize], Some(pbuf.position as usize), + rec.alloc_hint, if let Some (name) = &rec.basis {Some(&name)} else {None}, + false + ) { + Ok(_) => { + pbuf.retcode = PddbRetcode::Ok; + } + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => pbuf.retcode = PddbRetcode::BasisLost, + std::io::ErrorKind::UnexpectedEof => pbuf.retcode = PddbRetcode::UnexpectedEof, + std::io::ErrorKind::OutOfMemory => pbuf.retcode = PddbRetcode::DiskFull, + _ => pbuf.retcode = PddbRetcode::InternalError, + } + } + } else { + pbuf.retcode = PddbRetcode::BasisLost; + } + // we don't nede a "replace" operation because all ops happen in-place } + Some(Opcode::WriteKeyFlush) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { + match basis_cache.sync(&mut pddb_os, None) { + Ok(_) => xous::return_scalar(msg.sender, PddbRetcode::Ok.to_usize().unwrap()).unwrap(), + Err(e) => match e.kind() { + std::io::ErrorKind::OutOfMemory => xous::return_scalar(msg.sender, PddbRetcode::DiskFull.to_usize().unwrap()).unwrap(), + std::io::ErrorKind::NotFound => xous::return_scalar(msg.sender, PddbRetcode::BasisLost.to_usize().unwrap()).unwrap(), + _ => xous::return_scalar(msg.sender, PddbRetcode::InternalError.to_usize().unwrap()).unwrap(), + } + }; + }), Some(Opcode::Quit) => { log::warn!("quitting the PDDB server"); send_message(