Skip to content

Commit

Permalink
Updating with CacheResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaslyang authored and NicholasLYang committed Jul 13, 2023
1 parent a4bfd76 commit ff94dd1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
33 changes: 17 additions & 16 deletions crates/turborepo-cache/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::{
backtrace::Backtrace,
fs::{metadata, OpenOptions},
};
use std::{backtrace::Backtrace, fs::OpenOptions};

use serde::{Deserialize, Serialize};
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf, AnchoredSystemPathBuf};

use crate::{
cache_archive::{CacheReader, CacheWriter},
CacheError, CacheSource, ItemStatus,
CacheError, CacheResponse, CacheSource,
};

struct FSCache {

Check warning on line 11 in crates/turborepo-cache/src/fs.rs

View workflow job for this annotation

GitHub Actions / Go Unit Tests (ubuntu, ubuntu-latest)

struct `FSCache` is never constructed

Check warning on line 11 in crates/turborepo-cache/src/fs.rs

View workflow job for this annotation

GitHub Actions / Go Unit Tests (macos, macos-latest)

struct `FSCache` is never constructed

Check warning on line 11 in crates/turborepo-cache/src/fs.rs

View workflow job for this annotation

GitHub Actions / Go Unit Tests (windows, windows-latest)

struct `FSCache` is never constructed
Expand Down Expand Up @@ -54,7 +51,7 @@ impl FSCache {
&self,
anchor: &AbsoluteSystemPath,
hash: &str,
) -> Result<(ItemStatus, Vec<AnchoredSystemPathBuf>), CacheError> {
) -> Result<(CacheResponse, Vec<AnchoredSystemPathBuf>), CacheError> {
let uncompressed_cache_path = self
.cache_directory
.join_component(&format!("{}.tar", hash));
Expand All @@ -67,7 +64,7 @@ impl FSCache {
} else if compressed_cache_path.exists() {
compressed_cache_path
} else {
return Ok((ItemStatus::Miss, vec![]));
return Err(CacheError::CacheMiss);
};

let mut cache_reader = CacheReader::open(&cache_path)?;
Expand All @@ -81,15 +78,15 @@ impl FSCache {
)?;

Ok((
ItemStatus::Hit {
CacheResponse {
time_saved: meta.duration,
source: CacheSource::Local,
},
restored_files,
))
}

fn exists(&self, hash: &str) -> Result<ItemStatus, CacheError> {
fn exists(&self, hash: &str) -> Result<CacheResponse, CacheError> {
let uncompressed_cache_path = self
.cache_directory
.join_component(&format!("{}.tar", hash));
Expand All @@ -98,7 +95,7 @@ impl FSCache {
.join_component(&format!("{}.tar.zst", hash));

if !uncompressed_cache_path.exists() && !compressed_cache_path.exists() {
return Ok(ItemStatus::Miss);
return Err(CacheError::CacheMiss);
}

let duration = CacheMetadata::read(
Expand All @@ -109,7 +106,7 @@ impl FSCache {
.map(|meta| meta.duration)
.unwrap_or(0);

Ok(ItemStatus::Hit {
Ok(CacheResponse {
time_saved: duration,
source: CacheSource::Local,
})
Expand Down Expand Up @@ -144,7 +141,7 @@ impl FSCache {
let mut metadata_options = OpenOptions::new();
metadata_options.create(true).write(true);

let mut metadata_file = metadata_path.open_with_options(metadata_options)?;
let metadata_file = metadata_path.open_with_options(metadata_options)?;

serde_json::to_writer(metadata_file, &meta)
.map_err(|e| CacheError::InvalidMetadata(e, Backtrace::capture()))?;
Expand All @@ -155,6 +152,8 @@ impl FSCache {

#[cfg(test)]
mod test {
use std::assert_matches::assert_matches;

use anyhow::Result;
use futures::future::try_join_all;
use tempfile::tempdir;
Expand All @@ -176,8 +175,10 @@ mod test {

let cache = FSCache::new(None, &repo_root_path)?;

let expected_miss = cache.exists(&test_case.hash)?;
assert_eq!(expected_miss, ItemStatus::Miss);
let expected_miss = cache
.exists(&test_case.hash)
.expect_err("Expected cache miss");
assert_matches!(expected_miss, CacheError::CacheMiss);

cache.put(
repo_root_path,
Expand All @@ -189,7 +190,7 @@ mod test {
let expected_hit = cache.exists(&test_case.hash)?;
assert_eq!(
expected_hit,
ItemStatus::Hit {
CacheResponse {
time_saved: test_case.duration,
source: CacheSource::Local
}
Expand All @@ -198,7 +199,7 @@ mod test {
let (status, files) = cache.fetch(&repo_root_path, &test_case.hash)?;
assert_eq!(
status,
ItemStatus::Hit {
CacheResponse {
time_saved: test_case.duration,
source: CacheSource::Local
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-cache/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ mod test {

use crate::{
http::HttpCache,
CacheSource
test_cases::{get_test_cases, TestCase},
CacheSource,
};

#[tokio::test]
Expand Down
18 changes: 3 additions & 15 deletions crates/turborepo-cache/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(error_generic_member_access)]
#![feature(provide_any)]
#![feature(assert_matches)]

pub mod cache_archive;
pub mod fs;
Expand Down Expand Up @@ -57,21 +58,8 @@ pub enum CacheError {
InvalidMetadata(serde_json::Error, #[backtrace] Backtrace),
#[error("Failed to write cache metadata file")]
MetadataWriteFailure(serde_json::Error, #[backtrace] Backtrace),
}

#[derive(Debug, Clone, PartialEq)]
enum CacheSource {
Local,
Remote,
}

#[derive(Debug, Clone, PartialEq)]
enum ItemStatus {
Hit {
source: CacheSource,
time_saved: u32,
},
Miss,
#[error("Cache miss")]
CacheMiss,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down

0 comments on commit ff94dd1

Please sign in to comment.