Skip to content

Commit

Permalink
refactor(content_hash): expose content_hash without `keys_being_cal…
Browse files Browse the repository at this point in the history
…culated` parameter

Callers will always initialize this with an empty hash set, so hide that implementation detail.
  • Loading branch information
arxanas committed Sep 6, 2022
1 parent 39a1e9b commit 7d527f3
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
8 changes: 3 additions & 5 deletions focus/commands/examples/calc_invalidation_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! ```
use std::{
collections::{HashMap, HashSet},
collections::HashMap,
ops::Deref,
path::PathBuf,
sync::{
Expand All @@ -19,7 +19,7 @@ use std::{

use clap::Parser;
use focus_internals::{
index::{content_hash_dependency_key, ContentHash, DependencyKey, HashContext},
index::{content_hash, ContentHash, DependencyKey, HashContext},
model::{repo::Repo, selection::Project},
target::{Target, TargetSet},
};
Expand Down Expand Up @@ -165,9 +165,7 @@ fn main() -> anyhow::Result<()> {
.iter()
.map(|target| {
let dep_key = DependencyKey::from(target.clone());
let hash =
content_hash_dependency_key(&hash_context, &dep_key, &mut HashSet::new())
.unwrap();
let hash = content_hash(&hash_context, &dep_key).unwrap();
((*commit_oid, target), hash)
})
.collect::<Vec<_>>()
Expand Down
7 changes: 3 additions & 4 deletions focus/internals/benches/bench_content_hash.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright 2022 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;

use content_addressed_cache::RocksDBCache;
use criterion::{criterion_group, criterion_main, Criterion};
use focus_internals::index::{
content_hash_dependency_key, ContentHash, DependencyKey, DependencyValue, HashContext,
ObjectDatabase, RocksDBMemoizationCacheExt, SimpleGitOdb,
content_hash, ContentHash, DependencyKey, DependencyValue, HashContext, ObjectDatabase,
RocksDBMemoizationCacheExt, SimpleGitOdb,
};
use focus_internals::model::repo::Repo;
use focus_util::app::App;
Expand All @@ -19,7 +18,7 @@ fn content_hash_dependency_keys(ctx: &HashContext, dep_keys: &[DependencyKey]) -
.iter()
.map(|dep_key| {
let dep_key = DependencyKey::DummyForTesting(Box::new(dep_key.clone()));
content_hash_dependency_key(ctx, &dep_key, &mut HashSet::new()).unwrap()
content_hash(ctx, &dep_key).unwrap()
})
.collect::<Vec<_>>()
}
Expand Down
6 changes: 5 additions & 1 deletion focus/internals/src/lib/index/content_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ impl std::fmt::Debug for HashContext<'_> {

/// Compute a content-addressable hash for the provided [`DependencyKey`] using
/// the context in `ctx`.
pub fn content_hash_dependency_key(
pub fn content_hash(ctx: &HashContext, key: &DependencyKey) -> anyhow::Result<ContentHash> {
content_hash_dependency_key(ctx, key, &mut HashSet::new())
}

fn content_hash_dependency_key(
ctx: &HashContext,
key: &DependencyKey,
keys_being_calculated: &mut HashSet<DependencyKey>,
Expand Down
2 changes: 1 addition & 1 deletion focus/internals/src/lib/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod content_hash;
mod dependency_graph;
mod object_database;

pub use content_hash::{content_hash_dependency_key, ContentHash, HashContext};
pub use content_hash::{content_hash, ContentHash, HashContext};
pub use dependency_graph::{
get_files_to_materialize, update_object_database_from_resolution, DependencyKey,
DependencyValue, PathsToMaterializeResult,
Expand Down
17 changes: 8 additions & 9 deletions focus/internals/src/lib/index/object_database.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright 2022 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;
use std::time::Duration;

use super::content_hash::HashContext;
use super::{content_hash_dependency_key, ContentHash, DependencyKey, DependencyValue};
use super::{content_hash, ContentHash, DependencyKey, DependencyValue};
use anyhow::Context;
use content_addressed_cache::Cache;
use tracing::{debug, error, info, info_span, warn};
Expand Down Expand Up @@ -46,7 +45,7 @@ impl<T: Cache> ObjectDatabase for T {
ctx: &HashContext,
key: &DependencyKey,
) -> anyhow::Result<(ContentHash, Option<DependencyValue>)> {
let hash = content_hash_dependency_key(ctx, key, &mut HashSet::new())?;
let hash = content_hash(ctx, key)?;
let result = self.get_direct(&hash)?;
Ok((hash, result))
}
Expand All @@ -66,7 +65,7 @@ impl<T: Cache> ObjectDatabase for T {
key: &DependencyKey,
value: DependencyValue,
) -> anyhow::Result<()> {
let hash = content_hash_dependency_key(ctx, key, &mut HashSet::new())?;
let hash = content_hash(ctx, key)?;
debug!(?hash, ?value, "Inserting entry into object database");
let payload = serde_json::to_vec(&value).context("serializing DependencyValue as JSON")?;

Expand Down Expand Up @@ -114,7 +113,7 @@ impl RocksDBMemoizationCacheExt for RocksDBCache {
#[cfg(test)]
/// Testing utilities.
pub mod testing {
use crate::index::content_hash_dependency_key;
use crate::index::content_hash;

use super::*;

Expand Down Expand Up @@ -146,7 +145,7 @@ pub mod testing {
ctx: &HashContext,
key: &DependencyKey,
) -> anyhow::Result<(ContentHash, Option<DependencyValue>)> {
let hash = content_hash_dependency_key(ctx, key, &mut HashSet::new())?;
let hash = content_hash(ctx, key)?;
let dep_value = self.get_direct(&hash)?;
Ok((hash, dep_value))
}
Expand All @@ -163,7 +162,7 @@ pub mod testing {
key: &DependencyKey,
value: DependencyValue,
) -> anyhow::Result<()> {
let hash = content_hash_dependency_key(ctx, key, &mut HashSet::new())?;
let hash = content_hash(ctx, key)?;
debug!(?hash, ?value, "Inserting entry into object database");

let mut entries = self.entries.lock().expect("poisoned mutex");
Expand Down Expand Up @@ -210,7 +209,7 @@ impl ObjectDatabase for SimpleGitOdb<'_> {
ctx: &HashContext,
key: &DependencyKey,
) -> anyhow::Result<(ContentHash, Option<DependencyValue>)> {
let hash = content_hash_dependency_key(ctx, key, &mut HashSet::new())?;
let hash = content_hash(ctx, key)?;
let result = self.get_direct(&hash)?;
Ok((hash, result))
}
Expand Down Expand Up @@ -255,7 +254,7 @@ impl ObjectDatabase for SimpleGitOdb<'_> {
key: &DependencyKey,
value: DependencyValue,
) -> anyhow::Result<()> {
let ContentHash(key_oid) = content_hash_dependency_key(ctx, key, &mut HashSet::new())?;
let ContentHash(key_oid) = content_hash(ctx, key)?;
let payload = serde_json::to_vec(&value).context("serializing DependencyValue as JSON")?;
let value_oid = ctx
.repo
Expand Down
4 changes: 2 additions & 2 deletions focus/operations/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use focus_util::paths::assert_focused_repo;
use tracing::{debug, debug_span, info};

use focus_internals::index::{
content_hash_dependency_key, get_files_to_materialize, ContentHash, DependencyKey, HashContext,
content_hash, get_files_to_materialize, ContentHash, DependencyKey, HashContext,
ObjectDatabase, PathsToMaterializeResult, RocksDBCache, RocksDBMemoizationCacheExt,
FUNCTION_ID,
};
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn hash(
for target in targets {
let target = Target::try_from(target.as_str())?;
let dep_key = DependencyKey::from(target);
let hash = content_hash_dependency_key(&hash_context, &dep_key, &mut HashSet::new())?;
let hash = content_hash(&hash_context, &dep_key)?;
println!("{hash} {dep_key:?}");
}

Expand Down

0 comments on commit 7d527f3

Please sign in to comment.