Skip to content

Commit

Permalink
caching provider (with clone)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Dec 8, 2024
1 parent d4ada46 commit f5c242b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions v0.5/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion v0.5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ indexmap = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
string-interner = "0.18"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread", "fs"] }

13 changes: 9 additions & 4 deletions v0.5/fastn-package/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ impl fastn_package::Package {

impl fastn_continuation::Continuation for State {
type Output = fastn_package::Package;
type Needed = Vec<String>;
// File name
type Found = Vec<(String, Option<fastn_section::Document>)>;
type Needed = Vec<String>; // vec of file names
type Found = Vec<(
String, // file name
Result<fastn_section::Document, fastn_section::Error>,
)>;

fn continue_after(
self,
_n: Vec<(String, &Option<fastn_section::Document>)>,
_n: Vec<(
String,
Result<fastn_section::Document, fastn_section::Error>,
)>,
) -> fastn_continuation::Result<Self> {
todo!()
}
Expand Down
12 changes: 9 additions & 3 deletions v0.5/fastn-router/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ impl fastn_router::Router {

impl fastn_continuation::Continuation for State {
type Output = fastn_router::Router;
type Needed = Vec<String>;
type Found = Vec<(String, Option<fastn_section::Document>)>;
type Needed = Vec<String>; // vec of file names
type Found = Vec<(
String, // file name
Result<fastn_section::Document, fastn_section::Error>,
)>;

fn continue_after(
self,
_n: Vec<(String, Option<fastn_section::Document>)>,
_n: Vec<(
String,
Result<fastn_section::Document, fastn_section::Error>,
)>,
) -> fastn_continuation::Result<Self> {
todo!()
}
Expand Down
1 change: 1 addition & 0 deletions v0.5/fastn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository.workspace = true
homepage.workspace = true

[dependencies]
arcstr.workspace = true
async-trait.workspace = true
fastn-compiler.workspace = true
fastn-package.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions v0.5/fastn/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[tokio::main]
async fn main() {
let command = fastn::commands::parse();
let section_provider = fastn::SectionProvider::default();
let mut section_provider = fastn::SectionProvider::default();
let mut package = fastn_package::Package::reader()
.mut_consume_async(&section_provider)
.mut_consume_async(&mut section_provider)
.await;
let router = fastn_router::Router::reader()
.mut_consume_async(&section_provider)
.mut_consume_async(&mut section_provider)
.await;
// read config here and pass to everyone?
// do common build stuff here
Expand Down
23 changes: 19 additions & 4 deletions v0.5/fastn/src/section_provider.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
#[derive(Default)]
pub struct SectionProvider {
cache: std::collections::HashMap<String, Option<fastn_section::Document>>,
cache: std::collections::HashMap<String, Result<fastn_section::Document, fastn_section::Error>>,
}

#[async_trait::async_trait]
impl<'a> fastn_continuation::AsyncMutProvider for &'a SectionProvider {
impl fastn_continuation::AsyncMutProvider for &mut SectionProvider {
type Needed = Vec<String>;
type Found = Vec<(String, &'a Option<fastn_section::Document>)>;
type Found = Vec<(
String,
Result<fastn_section::Document, fastn_section::Error>,
)>;

async fn provide(&mut self, needed: Vec<String>) -> Self::Found {
let mut r = vec![];
for f in needed {
if let Some(doc) = self.cache.get(&f) {
r.push((f, doc));
r.push((f, doc.clone()));
continue;
}

match tokio::fs::read_to_string(&f).await {
Ok(v) => {
let d = fastn_section::Document::parse(&arcstr::ArcStr::from(v));
self.cache.insert(f.clone(), Ok(d.clone()));
r.push((f, Ok(d)));
}
Err(e) => {
todo!("error handler not ready for: {e:?}")
}
}
}
r
Expand Down

0 comments on commit f5c242b

Please sign in to comment.