Skip to content

Commit

Permalink
chore: add docs and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo committed Sep 30, 2024
1 parent e247aff commit 790c423
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
28 changes: 9 additions & 19 deletions devenv-eval-cache/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum CommandError {

pub struct CachedCommand<'a> {
pool: &'a sqlx::SqlitePool,
refresh: bool,
force_refresh: bool,
extra_paths: Vec<PathBuf>,
excluded_paths: Vec<PathBuf>,
on_stderr: Option<Box<dyn Fn(&InternalLog) + Send>>,
Expand All @@ -29,7 +29,7 @@ impl<'a> CachedCommand<'a> {
pub fn new(pool: &'a SqlitePool) -> Self {
Self {
pool,
refresh: false,
force_refresh: false,
extra_paths: Vec::new(),
excluded_paths: Vec::new(),
on_stderr: None,
Expand All @@ -49,8 +49,8 @@ impl<'a> CachedCommand<'a> {
}

/// Force re-evaluation of the command.
pub fn refresh(&mut self) -> &mut Self {
self.refresh = true;
pub fn force_refresh(&mut self) -> &mut Self {
self.force_refresh = true;
self
}

Expand All @@ -71,7 +71,7 @@ impl<'a> CachedCommand<'a> {
let cmd_hash = hash::digest(&raw_cmd);

// Check whether the command has been previously run and the files it depends on have not been changed.
if !self.refresh {
if !self.force_refresh {
if let Ok(Some(output)) = query_cached_output(self.pool, &cmd_hash).await {
return Ok(output);
}
Expand Down Expand Up @@ -197,22 +197,18 @@ impl From<db::FilePathRow> for FilePath {
}
}

/// Represents the state of a file in the cache system.
/// Represents the various states of "modified" that we care about.
#[derive(Debug)]
#[allow(dead_code)]
enum FileState {
/// The file has not been modified since it was last cached.
Unchanged {
path: PathBuf,
modified_at: SystemTime,
},
Unchanged { path: PathBuf },
/// The file's metadata, i.e. timestamp, has changed, but its content remains the same.
MetadataModified {
path: PathBuf,
modified_at: SystemTime,
updated_at: SystemTime,
},
/// The file's content has been modified.
/// The file's contents have been modified.
Modified {
path: PathBuf,
new_hash: String,
Expand Down Expand Up @@ -328,24 +324,18 @@ fn check_file_state(file: db::FilePathRow) -> io::Result<FileState> {
};

let modified_at = metadata.modified().and_then(truncate_to_seconds)?;

if modified_at == file.modified_at {
// File has not been modified
return Ok(FileState::Unchanged {
path: file.path,
modified_at,
});
return Ok(FileState::Unchanged { path: file.path });
}

// File has been touched, recompute the hash
let new_hash = hash::compute_file_hash(&file.path)?;

if new_hash == file.content_hash {
// File touched but hash unchanged
Ok(FileState::MetadataModified {
path: file.path,
modified_at,
updated_at: file.updated_at,
})
} else {
// Hash has changed, return new hash
Expand Down
12 changes: 12 additions & 0 deletions devenv-eval-cache/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ pub async fn setup_db<P: AsRef<str>>(database_url: P) -> Result<SqlitePool, sqlx
Ok(pool)
}

/// The row type for the `cached_cmd` table.
#[derive(Clone, Debug)]
pub struct CommandRow {
/// The primary key
pub id: i64,
/// The raw command string (for debugging)
pub raw: String,
/// A hash of the command string
pub cmd_hash: String,
/// A hash of the content hashes of the input files
pub input_hash: String,
/// The raw output of the command
pub output: Vec<u8>,
/// The time the cached command was checked or created
pub updated_at: SystemTime,
}

Expand Down Expand Up @@ -221,11 +228,16 @@ where
Ok(file_ids)
}

/// The row type for the `file_path` table.
#[derive(Clone, Debug)]
pub struct FilePathRow {
/// A path
pub path: PathBuf,
/// The hash of the file's content
pub content_hash: String,
/// The last modified time of the file
pub modified_at: SystemTime,
/// The last time the row was updated
pub updated_at: SystemTime,
}

Expand Down
2 changes: 1 addition & 1 deletion devenv/src/cnix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ impl<'a> Nix<'a> {
cached_cmd.unwatch_path(self.devenv_root.join(".devenv.flake.nix"));

if self.global_options.refresh_eval_cache {
cached_cmd.refresh();
cached_cmd.force_refresh();
}

if options.logging {
Expand Down
4 changes: 2 additions & 2 deletions devenv/src/devenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,14 +840,14 @@ impl Devenv {
let env = self.nix.dev_env(json, &gc_root).await?;

std::fs::write(
self.devenv_dotfile.join("input_paths.txt"),
self.devenv_dotfile.join("input-paths.txt"),
env.paths
.iter()
.map(|fp| fp.path.to_string_lossy())
.collect::<Vec<_>>()
.join("\n"),
)
.expect("Failed to write input_paths.txt");
.expect("Failed to write input-paths.txt");

Ok(DevEnv {
output: env.stdout,
Expand Down
4 changes: 2 additions & 2 deletions direnvrc
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ use_devenv() {
exit 0
fi

if [[ -f "$flake_dir/.devenv/input_paths.txt" ]]; then
for file in $(cat "$flake_dir/.devenv/input_paths.txt"); do
if [[ -f "$flake_dir/.devenv/input-paths.txt" ]]; then
for file in $(cat "$flake_dir/.devenv/input-paths.txt"); do
files_to_watch+=("$file")
done
fi
Expand Down

0 comments on commit 790c423

Please sign in to comment.