This is an automated email from the ASF dual-hosted git repository.
mssun pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git
The following commit(s) were added to refs/heads/develop by this push:
new 122847a [types] Optimize StagedFiles usages (#248)
122847a is described below
commit 122847a4385b1065ecc45ea8d35d79e2c97e9c0d
Author: Zhaofeng Chen <[email protected]>
AuthorDate: Wed Mar 25 13:26:12 2020 -0700
[types] Optimize StagedFiles usages (#248)
---
function/src/context.rs | 9 ++-------
function/src/gbdt_prediction.rs | 6 +++---
function/src/gbdt_training.rs | 4 ++--
function/src/mesapy.rs | 11 +++--------
runtime/src/default.rs | 6 ++----
runtime/src/raw_io.rs | 2 --
types/src/macros.rs | 4 ++--
types/src/staged_file.rs | 10 +++++++---
8 files changed, 21 insertions(+), 31 deletions(-)
diff --git a/function/src/context.rs b/function/src/context.rs
index ff3f056..fd6280f 100644
--- a/function/src/context.rs
+++ b/function/src/context.rs
@@ -268,13 +268,8 @@ pub mod tests {
let in_fid = "in_f1";
let out_fid = "out_f1";
- let input_files = StagedFiles {
- entries: hashmap!(in_fid.to_string() => input_info),
- };
-
- let output_files = StagedFiles {
- entries: hashmap!(out_fid.to_string() => output_info),
- };
+ let input_files = StagedFiles::new(hashmap!(in_fid => input_info));
+ let output_files = StagedFiles::new(hashmap!(out_fid => output_info));
let runtime = Box::new(RawIoRuntime::new(input_files, output_files));
set_thread_context(Context::new(runtime)).unwrap();
diff --git a/function/src/gbdt_prediction.rs b/function/src/gbdt_prediction.rs
index 670b6e0..7926018 100644
--- a/function/src/gbdt_prediction.rs
+++ b/function/src/gbdt_prediction.rs
@@ -113,14 +113,14 @@ pub mod tests {
let expected_output =
"fixtures/functions/gbdt_prediction/expected_result.txt";
let input_files = StagedFiles::new(hashmap!(
- IN_MODEL.to_string() =>
+ IN_MODEL =>
StagedFileInfo::new(plain_if_model, TeaclaveFile128Key::random()),
- IN_DATA.to_string() =>
+ IN_DATA =>
StagedFileInfo::new(plain_if_data, TeaclaveFile128Key::random())
));
let output_files = StagedFiles::new(hashmap!(
- OUT_RESULT.to_string() =>
+ OUT_RESULT =>
StagedFileInfo::new(plain_output, TeaclaveFile128Key::random())
));
diff --git a/function/src/gbdt_training.rs b/function/src/gbdt_training.rs
index 4a7856e..9b63f99 100644
--- a/function/src/gbdt_training.rs
+++ b/function/src/gbdt_training.rs
@@ -157,12 +157,12 @@ pub mod tests {
let expected_output =
"fixtures/functions/gbdt_training/expected_model.txt";
let input_files = StagedFiles::new(hashmap!(
- IN_DATA.to_string() =>
+ IN_DATA =>
StagedFileInfo::new(plain_input, TeaclaveFile128Key::random())
));
let output_files = StagedFiles::new(hashmap!(
- OUT_MODEL.to_string() =>
+ OUT_MODEL =>
StagedFileInfo::new(plain_output, TeaclaveFile128Key::random())
));
diff --git a/function/src/mesapy.rs b/function/src/mesapy.rs
index 7d1f987..1264abb 100644
--- a/function/src/mesapy.rs
+++ b/function/src/mesapy.rs
@@ -161,20 +161,15 @@ def entrypoint(argv):
let output = "fixtures/functions/mesapy/output.txt";
let input_info = StagedFileInfo::new(input,
TeaclaveFile128Key::random());
-
let output_info = StagedFileInfo::new(output,
TeaclaveFile128Key::random());
- let input_files = StagedFiles {
- entries: hashmap!("in_f1".to_string() => input_info),
- };
+ let input_files = StagedFiles::new(hashmap!("in_f1" => input_info));
+ let output_files = StagedFiles::new(hashmap!("out_f1" => output_info));
- let output_files = StagedFiles {
- entries: hashmap!("out_f1".to_string() => output_info),
- };
let runtime = Box::new(RawIoRuntime::new(input_files, output_files));
let func_args = FunctionArguments::new(hashmap!(
- "py_payload" => py_payload.to_string(),
+ "py_payload" => py_payload,
"py_args" => serde_json::to_string(&py_args).unwrap()
));
diff --git a/runtime/src/default.rs b/runtime/src/default.rs
index 78a3bd2..0d09401 100644
--- a/runtime/src/default.rs
+++ b/runtime/src/default.rs
@@ -42,24 +42,22 @@ impl TeaclaveRuntime for DefaultRuntime {
fn open_input(&self, identifier: &str) -> anyhow::Result<Box<dyn
io::Read>> {
let file_info = self
.input_files
- .entries
.get(identifier)
.ok_or_else(|| anyhow::anyhow!("Invalid input file identifier."))?;
log::debug!("open_input: {:?}", file_info.path);
- let readable = file_info.get_readable_io()?;
+ let readable = file_info.create_readable_io()?;
Ok(readable)
}
fn create_output(&self, identifier: &str) -> anyhow::Result<Box<dyn
io::Write>> {
let file_info = self
.output_files
- .entries
.get(identifier)
.ok_or_else(|| anyhow::anyhow!("Invalide output file
identifier"))?;
log::debug!("create_output: {:?}", file_info.path);
- let writable = file_info.get_writable_io()?;
+ let writable = file_info.create_writable_io()?;
Ok(writable)
}
}
diff --git a/runtime/src/raw_io.rs b/runtime/src/raw_io.rs
index 73952d1..250d806 100644
--- a/runtime/src/raw_io.rs
+++ b/runtime/src/raw_io.rs
@@ -44,7 +44,6 @@ impl TeaclaveRuntime for RawIoRuntime {
fn open_input(&self, identifier: &str) -> anyhow::Result<Box<dyn
io::Read>> {
let file_info = self
.input_files
- .entries
.get(identifier)
.ok_or_else(|| anyhow::anyhow!("Invalid input file identifier."))?;
log::debug!("open_input: {:?}", file_info.path);
@@ -55,7 +54,6 @@ impl TeaclaveRuntime for RawIoRuntime {
fn create_output(&self, identifier: &str) -> anyhow::Result<Box<dyn
io::Write>> {
let file_info = self
.output_files
- .entries
.get(identifier)
.ok_or_else(|| anyhow::anyhow!("Invalide output file
identifier"))?;
let f = File::create(&file_info.path)?;
diff --git a/types/src/macros.rs b/types/src/macros.rs
index 26c38e8..2b5bfb4 100644
--- a/types/src/macros.rs
+++ b/types/src/macros.rs
@@ -17,10 +17,10 @@
#[macro_export]
macro_rules! hashmap {
- ($( $key: expr => $value: expr,)+) => { hashmap!($($key => $value),+) };
+ ($( $key: expr => $value: expr, )+) => { hashmap!($($key => $value),+) };
($( $key: expr => $value: expr ),*) => {{
let mut map = ::std::collections::HashMap::new();
$( map.insert($key.into(), $value.into()); )*
- map
+ map
}}
}
diff --git a/types/src/staged_file.rs b/types/src/staged_file.rs
index b98c3c3..2c1aa7d 100644
--- a/types/src/staged_file.rs
+++ b/types/src/staged_file.rs
@@ -44,12 +44,12 @@ impl StagedFileInfo {
}
}
- pub fn get_readable_io(&self) -> anyhow::Result<Box<dyn io::Read>> {
+ pub fn create_readable_io(&self) -> anyhow::Result<Box<dyn io::Read>> {
let f = ProtectedFile::open_ex(&self.path, &self.crypto_info.key)?;
Ok(Box::new(f))
}
- pub fn get_writable_io(&self) -> anyhow::Result<Box<dyn io::Write>> {
+ pub fn create_writable_io(&self) -> anyhow::Result<Box<dyn io::Write>> {
let f = ProtectedFile::create_ex(&self.path, &self.crypto_info.key)?;
Ok(Box::new(f))
}
@@ -122,11 +122,15 @@ pub fn convert_encrypted_input_file(
#[derive(Debug, Default)]
pub struct StagedFiles {
- pub entries: HashMap<String, StagedFileInfo>,
+ entries: HashMap<String, StagedFileInfo>,
}
impl StagedFiles {
pub fn new(entries: HashMap<String, StagedFileInfo>) -> Self {
StagedFiles { entries }
}
+
+ pub fn get(&self, key: &str) -> Option<&StagedFileInfo> {
+ self.entries.get(key)
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]