1717package com .google .cloud .tools .jib .cache ;
1818
1919import com .google .cloud .tools .jib .cache .json .CacheMetadataTemplate ;
20- import com .google .cloud .tools .jib .image .LayerPropertyNotFoundException ;
2120import com .google .cloud .tools .jib .json .JsonTemplateMapper ;
2221import com .google .common .annotations .VisibleForTesting ;
2322import java .io .BufferedOutputStream ;
2726import java .nio .file .Files ;
2827import java .nio .file .NotDirectoryException ;
2928import java .nio .file .Path ;
30- import javax . annotation . Nullable ;
29+ import java . util . List ;
3130
3231/** Manages a cache. Implementation is thread-safe. */
3332public class Cache implements Closeable {
3433
35- /** The path to the root of the cache. */
36- private final Path cacheDirectory ;
37-
38- /** The metadata that corresponds to the cache at {@link #cacheDirectory}. */
39- private final CacheMetadata cacheMetadata ;
40-
4134 /**
4235 * Initializes a cache with a directory. This also loads the cache metadata if it exists in the
4336 * directory.
@@ -62,7 +55,7 @@ private static CacheMetadata loadCacheMetadata(Path cacheDirectory)
6255 Path cacheMetadataJsonFile = cacheDirectory .resolve (CacheFiles .METADATA_FILENAME );
6356
6457 if (!Files .exists (cacheMetadataJsonFile )) {
65- return new CacheMetadata ();
58+ return CacheMetadata . builder (). build ();
6659 }
6760
6861 try {
@@ -76,9 +69,19 @@ private static CacheMetadata loadCacheMetadata(Path cacheDirectory)
7669 }
7770 }
7871
72+ /** The path to the root of the cache. */
73+ private final Path cacheDirectory ;
74+
75+ /** The metadata that corresponds to the cache at {@link #cacheDirectory}. */
76+ private final CacheMetadata cacheMetadata ;
77+
78+ /** Builds the updated cache metadata to save back to the cache. */
79+ private final CacheMetadata .Builder cacheMetadataBuilder ;
80+
7981 private Cache (Path cacheDirectory , CacheMetadata cacheMetadata ) {
8082 this .cacheDirectory = cacheDirectory ;
8183 this .cacheMetadata = cacheMetadata ;
84+ cacheMetadataBuilder = cacheMetadata .newAppendingBuilder ();
8285 }
8386
8487 /**
@@ -92,15 +95,26 @@ public void close() throws IOException {
9295 }
9396
9497 /**
95- * Adds the cached layer to the cache metadata.
98+ * Adds the cached layer to the cache metadata. This is <b>NOT</b> thread-safe.
9699 *
97- * @param cachedLayer the layer to add.
98- * @param layerMetadata the metadata to add the layer to.
99- * @throws LayerPropertyNotFoundException if adding the layer fails.
100+ * @param cachedLayers the layers to add
100101 */
101- void addLayerToMetadata (CachedLayer cachedLayer , @ Nullable LayerMetadata layerMetadata )
102- throws LayerPropertyNotFoundException {
103- cacheMetadata .addLayer (new CachedLayerWithMetadata (cachedLayer , layerMetadata ));
102+ public void addCachedLayersToMetadata (List <CachedLayer > cachedLayers ) {
103+ for (CachedLayer cachedLayer : cachedLayers ) {
104+ cacheMetadataBuilder .addLayer (new CachedLayerWithMetadata (cachedLayer , null ));
105+ }
106+ }
107+
108+ /**
109+ * Adds the cached layer to the cache metadata. This is <b>NOT</b> thread-safe.
110+ *
111+ * @param cachedLayersWithMetadata the layers to add
112+ */
113+ public void addCachedLayersWithMetadataToMetadata (
114+ List <CachedLayerWithMetadata > cachedLayersWithMetadata ) {
115+ for (CachedLayerWithMetadata cachedLayerWithMetadata : cachedLayersWithMetadata ) {
116+ cacheMetadataBuilder .addLayer (cachedLayerWithMetadata );
117+ }
104118 }
105119
106120 @ VisibleForTesting
@@ -113,11 +127,17 @@ CacheMetadata getMetadata() {
113127 return cacheMetadata ;
114128 }
115129
130+ @ VisibleForTesting
131+ CacheMetadata getUpdatedMetadata () {
132+ return cacheMetadataBuilder .build ();
133+ }
134+
116135 /** Saves the updated cache metadata back to the cache. */
117136 private void saveCacheMetadata (Path cacheDirectory ) throws IOException {
118137 Path cacheMetadataJsonFile = cacheDirectory .resolve (CacheFiles .METADATA_FILENAME );
119138
120- CacheMetadataTemplate cacheMetadataJson = CacheMetadataTranslator .toTemplate (cacheMetadata );
139+ CacheMetadataTemplate cacheMetadataJson =
140+ CacheMetadataTranslator .toTemplate (cacheMetadataBuilder .build ());
121141
122142 try (OutputStream fileOutputStream =
123143 new BufferedOutputStream (Files .newOutputStream (cacheMetadataJsonFile ))) {
0 commit comments