Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add prefetch to value #67

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ pub fn build(project_root: &Path) -> anyhow::Result<()> {
}

for job in jobs {
job.execute(resources.get(&&job.pipeline().name).expect(&format!(
"could not find prefetched resource for pipeline \"{}\"",
job.pipeline().name
)))
job.execute(resources.get(&&job.pipeline().name).unwrap_or_else(|| panic!("could not find prefetched resource for pipeline \"{}\"",
job.pipeline().name)))
.with_context(|| {
format!(
"failed to complete job for pipeline \"{}\" and entry file \"{}\"",
Expand Down
8 changes: 5 additions & 3 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ impl Pipeline {
debug!("start");
match step {
Step::Load { key, with, .. } => {
if let Some(bytes) = resource.get_bytes(key) {
if let Some(value) = resource.get_value(&index) {
store.set(key.to_string(), value.clone());
} else if let Some(bytes) = resource.get_bytes(&index) {
let value = match with {
EnumLoader::Template => TemplateLoader::load(bytes),
EnumLoader::Json => todo!(),
Expand Down Expand Up @@ -96,7 +98,7 @@ impl Pipeline {
EnumTransformer::TemplateRenderer(template_renderer) => {
template_renderer
.transform(input, &store)
.with_context(|| format!("transformer failed"))?
.with_context(|| "transformer failed".to_string())?
}
};
debug!("tranform output {:?}", value);
Expand Down Expand Up @@ -150,7 +152,7 @@ impl Pipeline {
Job {
input_path: input_path.as_ref().to_path_buf(),
output_path: self.get_output_path(&input_path, &project_root),
pipeline: &self,
pipeline: self,
}
}
}
57 changes: 39 additions & 18 deletions src/pipeline/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,61 @@ use anyhow::Context;
use path_absolutize::Absolutize;
use tracing::{debug, info, span, Level};

use crate::{Loader, TemplateLoader, Value};

use super::Pipeline;

/// Resource holds the file contents needed for the build process.
/// One resource instance is created per [`Pipeline`](crate::pipeline::Pipeline).
#[derive(Debug)]
pub struct Resource(HashMap<String, Vec<u8>>);
pub struct Resource {
byte_map: HashMap<usize, Vec<u8>>,
value_map: HashMap<usize, Value>,
}

impl Resource {
pub(super) fn load_for(
pipeline: &Pipeline,
project_root: impl AsRef<Path>,
) -> anyhow::Result<Self> {
let mut map = HashMap::new();
let mut byte_map = HashMap::new();
let mut value_map = HashMap::new();
let project_root = project_root.as_ref();
for step in &pipeline.steps {
match step {
super::Step::Load { path, key, .. } => {
let load_span = span!(Level::INFO, "prefetch", key = key, path = path.to_str());
let _enter = load_span.enter();
info!("start");
let path = path.absolutize_from(&project_root).unwrap();
debug!("absolute path {}", path.display());
let bytes = fs::read(&path)
.with_context(|| format!("failed to load file {}", path.display()))?;
map.insert(key.to_string(), bytes);
info!("done");
for (index, step) in pipeline.steps.iter().enumerate() {
if let super::Step::Load { path, with, .. } = step {
let load_span = span!(Level::INFO, "prefetch", index = index, path = path.to_str());
let _enter = load_span.enter();
info!("start");

let path = path.absolutize_from(project_root).unwrap();
debug!("absolute path {}", path.display());
let bytes = fs::read(&path)
.with_context(|| format!("failed to load file {}", path.display()))?;
byte_map.insert(index, bytes);

let value = match with {
crate::pipeline::EnumLoader::Template => {
TemplateLoader::load(&byte_map.get(&index).unwrap()[..])
}
crate::pipeline::EnumLoader::Json => todo!(),
}
_ => {}
.with_context(|| format!("failed to preload with {:?}", with))?;
value_map.insert(index, value);

info!("done");
}
}
Ok(Self(map))
Ok(Self {
byte_map,
value_map,
})
}

pub fn get_bytes(&self, key: &usize) -> Option<&[u8]> {
self.byte_map.get(key).map(|v| &v[..])
}

pub fn get_bytes(&self, key: &str) -> Option<&[u8]> {
self.0.get(key).map(|v| &v[..])
pub fn get_value(&self, key: &usize) -> Option<&Value> {
self.value_map.get(key)
}
}
2 changes: 1 addition & 1 deletion src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod template_renderer {

engine
.register_template_from_string(&self.template_key, template_string.to_string())
.with_context(|| format!("failed to register template from string"))?;
.with_context(|| "failed to register template from string".to_string())?;

let result_string = engine
.render(
Expand Down
Loading