implement a blake3 content version layer for cache busting

works pretty much the same as our previous `?cache` parameter, but is implemented in a less ad-hoc way
This commit is contained in:
りき萌 2024-11-23 18:29:03 +01:00
parent 32f25ce863
commit 7169e65244
7 changed files with 86 additions and 4 deletions

View file

@ -15,6 +15,7 @@ chrono = "0.4.35"
clap = { version = "4.3.22", features = ["derive"] }
codespan-reporting = "0.11.1"
copy_dir = "0.1.3"
dashmap = "6.1.0"
env_logger = "0.10.0"
git2 = { version = "0.19.0", default-features = false, features = ["vendored-libgit2"] }
handlebars = "4.3.7"

View file

@ -9,7 +9,7 @@ use treehouse::cli::serve::serve;
use treehouse::dirs::Dirs;
use treehouse::generate::{self, Sources};
use treehouse::vfs::asynch::AsyncDir;
use treehouse::vfs::{AnchoredAtExt, DynDir, ToDynDir, VPathBuf};
use treehouse::vfs::{AnchoredAtExt, Blake3ContentVersionCache, DynDir, ToDynDir, VPathBuf};
use treehouse::vfs::{Cd, PhysicalDir};
use treehouse::{
cli::{
@ -42,6 +42,8 @@ fn vfs_sources() -> anyhow::Result<DynDir> {
PhysicalDir::new(PathBuf::from("content")).to_dyn(),
);
let root = Blake3ContentVersionCache::new(root);
Ok(root.to_dyn())
}

View file

@ -52,6 +52,7 @@ use std::{
mod anchored;
pub mod asynch;
mod cd;
mod content_version_cache;
mod edit;
mod empty;
mod file;
@ -62,6 +63,7 @@ mod physical;
pub use anchored::*;
pub use cd::*;
pub use content_version_cache::*;
pub use edit::*;
pub use empty::*;
pub use file::*;

View file

@ -0,0 +1,62 @@
use std::fmt::{self, Debug};
use dashmap::DashMap;
use log::debug;
use super::{Dir, DirEntry, EditPath, VPath, VPathBuf};
pub struct Blake3ContentVersionCache<T> {
inner: T,
cache: DashMap<VPathBuf, Option<String>>,
}
impl<T> Blake3ContentVersionCache<T> {
pub fn new(inner: T) -> Self {
Self {
inner,
cache: DashMap::new(),
}
}
}
impl<T> Dir for Blake3ContentVersionCache<T>
where
T: Dir,
{
fn dir(&self, path: &VPath) -> Vec<DirEntry> {
self.inner.dir(path)
}
fn content(&self, path: &VPath) -> Option<Vec<u8>> {
self.inner.content(path)
}
fn content_version(&self, path: &VPath) -> Option<String> {
self.cache
.entry(path.to_owned())
.or_insert_with(|| {
self.content(path).map(|content| {
let hash = blake3::hash(&content).to_hex();
format!("b3-{}", &hash[0..8])
})
})
.clone()
}
fn anchor(&self, path: &VPath) -> Option<VPathBuf> {
self.inner.anchor(path)
}
fn edit_path(&self, path: &VPath) -> Option<EditPath> {
self.inner.edit_path(path)
}
}
impl<T> fmt::Debug for Blake3ContentVersionCache<T>
where
T: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Blake3ContentVersionCache({:?})", self.inner)
}
}