Skip to content

Commit 9d24384

Browse files
authored
Use Cache::open() terminology, not Cache::new() (#1317)
Addressing some feedback from #1298, no review required I have left `OakSourceHandler::new()` as is. Even though all it does is open the underlying caches, this name still felt more correct.
1 parent 3b34d9b commit 9d24384

4 files changed

Lines changed: 32 additions & 32 deletions

File tree

crates/ark/src/lsp/sources.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ impl OakSourceHandler {
3838
/// Build the handler, opening both caches against the shared on disk cache
3939
pub(crate) fn new(r: PathBuf) -> anyhow::Result<Self> {
4040
Ok(Self {
41-
srcref: SrcrefCache::new(r)?,
42-
source: SourceCache::new()?,
41+
srcref: SrcrefCache::open(r)?,
42+
source: SourceCache::open()?,
4343
})
4444
}
4545

@@ -48,8 +48,8 @@ impl OakSourceHandler {
4848
#[cfg(test)]
4949
pub(crate) fn new_in(root: &Path, r: PathBuf) -> anyhow::Result<Self> {
5050
Ok(Self {
51-
srcref: SrcrefCache::new_in(root.join("srcref"), r)?,
52-
source: SourceCache::new_in(root.join("source"))?,
51+
srcref: SrcrefCache::open_in(root.join("srcref"), r)?,
52+
source: SourceCache::open_in(root.join("source"))?,
5353
})
5454
}
5555
}

crates/oak_cache/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const COMPLETE_FILENAME: &str = ".complete";
5454
/// simultaneously. It keeps any [`PathBuf`] handed out by [`Cache::get`] valid for the
5555
/// life of the cache.
5656
///
57-
/// - **Exclusive** is only attempted once, at [`Cache::new`], to run [`Cache::clean`].
57+
/// - **Exclusive** is only attempted once, at [`Cache::open`], to run [`Cache::clean`].
5858
/// It is skipped if another session already holds the shared lock, in which case we
5959
/// just try again next time.
6060
///
@@ -81,13 +81,13 @@ impl Cache {
8181
/// Runs a best-effort [`Cache::clean`] under the exclusive root lock (skipped if
8282
/// another session holds the shared lock), then holds the shared root lock for the
8383
/// life of the returned `Cache` so handed-out paths stay valid.
84-
pub fn new(root: &str, capacity: usize) -> anyhow::Result<Self> {
85-
Self::new_in(cache_dir()?.join(root), capacity)
84+
pub fn open(root: &str, capacity: usize) -> anyhow::Result<Self> {
85+
Self::open_in(cache_dir()?.join(root), capacity)
8686
}
8787

88-
/// Like [`Cache::new`], but rooted at an explicit `root` rather than a subfolder of
88+
/// Like [`Cache::open`], but rooted at an explicit `root` rather than a subfolder of
8989
/// the shared cache directory. Only useful for testing against a temp directory.
90-
pub fn new_in(root: PathBuf, capacity: usize) -> anyhow::Result<Self> {
90+
pub fn open_in(root: PathBuf, capacity: usize) -> anyhow::Result<Self> {
9191
let root = Filesystem::new(root);
9292
root.create_dir()?;
9393

@@ -293,7 +293,7 @@ mod tests {
293293
/// alive for the test.
294294
fn new(capacity: usize) -> (TempDir, Cache) {
295295
let dir = TempDir::new().unwrap();
296-
let cache = Cache::new_in(dir.path().join("subfolder"), capacity).unwrap();
296+
let cache = Cache::open_in(dir.path().join("subfolder"), capacity).unwrap();
297297
(dir, cache)
298298
}
299299

@@ -357,15 +357,15 @@ mod tests {
357357

358358
// Populate one good entry, then forge a crashed partial (no `.complete`).
359359
{
360-
let cache = Cache::new_in(root.clone(), 10).unwrap();
360+
let cache = Cache::open_in(root.clone(), 10).unwrap();
361361
cache.insert("good", write_file("ok")).unwrap();
362362
}
363363
let partial = root.join("partial");
364364
std::fs::create_dir(&partial).unwrap();
365365
std::fs::write(partial.join("content.txt"), "junk").unwrap();
366366

367367
// Reopening runs `clean`, which removes the partial but keeps the good entry.
368-
let cache = Cache::new_in(root, 10).unwrap();
368+
let cache = Cache::open_in(root, 10).unwrap();
369369
assert!(!partial.exists());
370370
assert!(cache.get("good").is_some());
371371
}
@@ -376,7 +376,7 @@ mod tests {
376376
let root = dir.path().join("subfolder");
377377

378378
{
379-
let cache = Cache::new_in(root.clone(), 10).unwrap();
379+
let cache = Cache::open_in(root.clone(), 10).unwrap();
380380
cache.insert("good", write_file("ok")).unwrap();
381381
}
382382
// A stray file in the cache root that doesn't belong to any entry.
@@ -385,7 +385,7 @@ mod tests {
385385

386386
// Reopening runs `clean`, which removes the stray file but keeps our root
387387
// `.lock` and the good entry.
388-
let cache = Cache::new_in(root.clone(), 10).unwrap();
388+
let cache = Cache::open_in(root.clone(), 10).unwrap();
389389
assert!(!stray.exists());
390390
assert!(root.join(".lock").exists());
391391
assert!(cache.get("good").is_some());
@@ -400,15 +400,15 @@ mod tests {
400400
// Insert four entries under a capacity that won't evict, then stamp their
401401
// access times so the ordering is deterministic.
402402
{
403-
let cache = Cache::new_in(root.clone(), 10).unwrap();
403+
let cache = Cache::open_in(root.clone(), 10).unwrap();
404404
for (index, key) in ["oldest", "older", "newer", "newest"].iter().enumerate() {
405405
let entry = cache.insert(key, write_file(key)).unwrap().unwrap();
406406
set_accessed(&entry, now + Duration::from_secs(index as u64));
407407
}
408408
}
409409

410410
// Reopening with capacity 2 evicts the two least-recently-accessed.
411-
let cache = Cache::new_in(root, 2).unwrap();
411+
let cache = Cache::open_in(root, 2).unwrap();
412412
assert_eq!(cache.get("oldest"), None);
413413
assert_eq!(cache.get("older"), None);
414414
assert!(cache.get("newer").is_some());

crates/oak_source/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ pub struct SourceCache {
3939
}
4040

4141
impl SourceCache {
42-
pub fn new() -> anyhow::Result<Self> {
42+
pub fn open() -> anyhow::Result<Self> {
4343
Ok(Self {
44-
cran: Cache::new(&format!("source/{CACHE_VERSION}/cran"), CRAN_CAPACITY)?,
45-
r: Cache::new(&format!("source/{CACHE_VERSION}/r"), R_CAPACITY)?,
44+
cran: Cache::open(&format!("source/{CACHE_VERSION}/cran"), CRAN_CAPACITY)?,
45+
r: Cache::open(&format!("source/{CACHE_VERSION}/r"), R_CAPACITY)?,
4646
})
4747
}
4848

49-
/// Like [`SourceCache::new`], but rooted at an explicit `root` rather than the shared
50-
/// cache directory. Only useful for testing against a temp directory.
51-
pub fn new_in(root: PathBuf) -> anyhow::Result<Self> {
49+
/// Like [`SourceCache::open`], but rooted at an explicit `root` rather than the
50+
/// shared cache directory. Only useful for testing against a temp directory.
51+
pub fn open_in(root: PathBuf) -> anyhow::Result<Self> {
5252
Ok(Self {
53-
cran: Cache::new_in(root.join("cran"), CRAN_CAPACITY)?,
54-
r: Cache::new_in(root.join("r"), R_CAPACITY)?,
53+
cran: Cache::open_in(root.join("cran"), CRAN_CAPACITY)?,
54+
r: Cache::open_in(root.join("r"), R_CAPACITY)?,
5555
})
5656
}
5757

@@ -102,7 +102,7 @@ mod tests {
102102
#[test]
103103
fn test_cran_round_trip() {
104104
let dir = TempDir::new().unwrap();
105-
let source = SourceCache::new_in(dir.path().to_path_buf()).unwrap();
105+
let source = SourceCache::open_in(dir.path().to_path_buf()).unwrap();
106106

107107
// Miss before insert
108108
assert_eq!(source.get_cran("vctrs", "0.7.2"), None);
@@ -127,7 +127,7 @@ mod tests {
127127
#[test]
128128
fn test_cran_not_found() {
129129
let dir = TempDir::new().unwrap();
130-
let source = SourceCache::new_in(dir.path().to_path_buf()).unwrap();
130+
let source = SourceCache::open_in(dir.path().to_path_buf()).unwrap();
131131
assert_eq!(
132132
source.insert_cran("definitely_not_a_package", "0.0.0"),
133133
None
@@ -139,7 +139,7 @@ mod tests {
139139
#[test]
140140
fn test_r_round_trip() {
141141
let dir = TempDir::new().unwrap();
142-
let source = SourceCache::new_in(dir.path().to_path_buf()).unwrap();
142+
let source = SourceCache::open_in(dir.path().to_path_buf()).unwrap();
143143

144144
let root = source.insert_r("4.5.0").unwrap();
145145

@@ -156,7 +156,7 @@ mod tests {
156156
#[test]
157157
fn test_r_unknown_version() {
158158
let dir = TempDir::new().unwrap();
159-
let source = SourceCache::new_in(dir.path().to_path_buf()).unwrap();
159+
let source = SourceCache::open_in(dir.path().to_path_buf()).unwrap();
160160
assert_eq!(source.insert_r("0.0.0"), None);
161161
}
162162
}

crates/oak_srcref/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ pub struct SrcrefCache {
2828
}
2929

3030
impl SrcrefCache {
31-
pub fn new(r: PathBuf) -> anyhow::Result<Self> {
31+
pub fn open(r: PathBuf) -> anyhow::Result<Self> {
3232
Ok(Self {
3333
r,
34-
cache: Cache::new(&format!("srcref/{CACHE_VERSION}"), CACHE_CAPACITY)?,
34+
cache: Cache::open(&format!("srcref/{CACHE_VERSION}"), CACHE_CAPACITY)?,
3535
})
3636
}
3737

3838
/// Open a `SrcrefCache` rooted at an explicit directory instead of the shared cache
3939
///
4040
/// Useful for integration tests that don't want to touch the real on disk cache.
41-
pub fn new_in(root: PathBuf, r: PathBuf) -> anyhow::Result<Self> {
41+
pub fn open_in(root: PathBuf, r: PathBuf) -> anyhow::Result<Self> {
4242
Ok(Self {
4343
r,
44-
cache: Cache::new_in(root, CACHE_CAPACITY)?,
44+
cache: Cache::open_in(root, CACHE_CAPACITY)?,
4545
})
4646
}
4747

0 commit comments

Comments
 (0)