aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Klaus Aehlig <aehlig@google.com>2018-04-23 01:51:01 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-23 01:52:42 -0700
commitc49f9d8b50a7fe8984b51bc01d68b6923d52cc82 (patch)
treec851269360363bc39b25842e165c67a9b3bed7c7
parent84d3097537f045a78baaebc3c6f2ffef4d814cb5 (diff)
Repository cache: add a put method determining the key itself
Add a new put method to the repository cache, that computes the cache key itself. The key is returned, so it can be reused without having to recompute it. This is a convenient interface for caching a file that was downloaded without prior knowledge of its hash. Change-Id: I6ac844f4166bf64498b87e483896d155df35475e PiperOrigin-RevId: 193889444
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java16
-rw-r--r--src/test/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCacheTest.java16
2 files changed, 31 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java
index bd743b3532..651c0a4760 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCache.java
@@ -164,6 +164,20 @@ public class RepositoryCache {
FileSystemUtils.copyFile(sourcePath, cacheValue);
}
+ /**
+ * Copies a value from a specified path into the cache, computing the cache key itself.
+ *
+ * @param sourcePath The path of the value to be cached.
+ * @param keyType The type of key to be used.
+ * @throws IOException
+ * @return The key for the cached entry.
+ */
+ public synchronized String put(Path sourcePath, KeyType keyType) throws IOException {
+ String cacheKey = getChecksum(keyType, sourcePath);
+ put(cacheKey, sourcePath, keyType);
+ return cacheKey;
+ }
+
private void ensureCacheDirectoryExists(KeyType keyType) throws IOException {
Path directoryPath = keyType.getCachePath(contentAddressablePath);
if (!directoryPath.exists()) {
@@ -236,4 +250,4 @@ public class RepositoryCache {
return contentAddressablePath;
}
-} \ No newline at end of file
+}
diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCacheTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCacheTest.java
index dc1a690c86..35ce53c4fa 100644
--- a/src/test/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCacheTest.java
+++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/cache/RepositoryCacheTest.java
@@ -84,6 +84,22 @@ public class RepositoryCacheTest {
}
/**
+ * Test that the put mehtod without cache key correctly stores the downloaded file into the cache.
+ */
+ @Test
+ public void testPutCacheValueWithoutHash() throws IOException {
+ String cacheKey = repositoryCache.put(downloadedFile, KeyType.SHA256);
+ assertThat(cacheKey).isEqualTo(downloadedFileSha256);
+
+ Path cacheEntry =
+ KeyType.SHA256.getCachePath(contentAddressableCachePath).getChild(downloadedFileSha256);
+ Path cacheValue = cacheEntry.getChild(RepositoryCache.DEFAULT_CACHE_FILENAME);
+
+ assertThat(FileSystemUtils.readContent(downloadedFile, Charset.defaultCharset()))
+ .isEqualTo(FileSystemUtils.readContent(cacheValue, Charset.defaultCharset()));
+ }
+
+ /**
* Test that the put method is idempotent, i.e. two successive put calls
* should not affect the final state in the cache.
*/