| ... | @@ -34,7 +34,6 @@ pub const CacheHash = struct { | ... | @@ -34,7 +34,6 @@ pub const CacheHash = struct { |
| 34 | manifest_dir: fs.Dir, | 34 | manifest_dir: fs.Dir, |
| 35 | manifest_file: ?fs.File, | 35 | manifest_file: ?fs.File, |
| 36 | manifest_dirty: bool, | 36 | manifest_dirty: bool, |
| 37 | force_check_manifest: bool, | | |
| 38 | files: ArrayList(File), | 37 | files: ArrayList(File), |
| 39 | b64_digest: [BASE64_DIGEST_LEN]u8, | 38 | b64_digest: [BASE64_DIGEST_LEN]u8, |
| 40 | | 39 | |
| ... | @@ -48,7 +47,6 @@ pub const CacheHash = struct { | ... | @@ -48,7 +47,6 @@ pub const CacheHash = struct { |
| 48 | .manifest_dir = manifest_dir, | 47 | .manifest_dir = manifest_dir, |
| 49 | .manifest_file = null, | 48 | .manifest_file = null, |
| 50 | .manifest_dirty = false, | 49 | .manifest_dirty = false, |
| 51 | .force_check_manifest = false, | | |
| 52 | .files = ArrayList(File).init(alloc), | 50 | .files = ArrayList(File).init(alloc), |
| 53 | .b64_digest = undefined, | 51 | .b64_digest = undefined, |
| 54 | }; | 52 | }; |
| ... | @@ -104,22 +102,37 @@ pub const CacheHash = struct { | ... | @@ -104,22 +102,37 @@ pub const CacheHash = struct { |
| 104 | | 102 | |
| 105 | base64_encoder.encode(self.b64_digest[0..], &bin_digest); | 103 | base64_encoder.encode(self.b64_digest[0..], &bin_digest); |
| 106 | | 104 | |
| 107 | if (self.files.toSlice().len == 0 and !self.force_check_manifest) { | | |
| 108 | return self.b64_digest; | | |
| 109 | } | | |
| 110 | | | |
| 111 | self.blake3 = Blake3.init(); | 105 | self.blake3 = Blake3.init(); |
| 112 | self.blake3.update(&bin_digest); | 106 | self.blake3.update(&bin_digest); |
| 113 | | 107 | |
| 114 | { | 108 | const manifest_file_path = try fmt.allocPrint(self.alloc, "{}.txt", .{self.b64_digest}); |
| 115 | const manifest_file_path = try fmt.allocPrint(self.alloc, "{}.txt", .{self.b64_digest}); | 109 | defer self.alloc.free(manifest_file_path); |
| 116 | defer self.alloc.free(manifest_file_path); | | |
| 117 | | 110 | |
| | 111 | if (self.files.items.len != 0) { |
| 118 | self.manifest_file = try self.manifest_dir.createFile(manifest_file_path, .{ | 112 | self.manifest_file = try self.manifest_dir.createFile(manifest_file_path, .{ |
| 119 | .read = true, | 113 | .read = true, |
| 120 | .truncate = false, | 114 | .truncate = false, |
| 121 | .lock = .Exclusive, | 115 | .lock = .Exclusive, |
| 122 | }); | 116 | }); |
| | 117 | } else { |
| | 118 | // If there are no file inputs, we check if the manifest file exists instead of |
| | 119 | // comparing the hashes on the files used for the cached item |
| | 120 | self.manifest_file = self.manifest_dir.openFile(manifest_file_path, .{ |
| | 121 | .read = true, |
| | 122 | .write = true, |
| | 123 | .lock = .Exclusive, |
| | 124 | }) catch |err| switch (err) { |
| | 125 | error.FileNotFound => { |
| | 126 | self.manifest_dirty = true; |
| | 127 | self.manifest_file = try self.manifest_dir.createFile(manifest_file_path, .{ |
| | 128 | .read = true, |
| | 129 | .truncate = false, |
| | 130 | .lock = .Exclusive, |
| | 131 | }); |
| | 132 | return null; |
| | 133 | }, |
| | 134 | else => |e| return e, |
| | 135 | }; |
| 123 | } | 136 | } |
| 124 | | 137 | |
| 125 | // TODO: Figure out a good max value? | 138 | // TODO: Figure out a good max value? |
| ... | @@ -206,7 +219,7 @@ pub const CacheHash = struct { | ... | @@ -206,7 +219,7 @@ pub const CacheHash = struct { |
| 206 | return null; | 219 | return null; |
| 207 | } | 220 | } |
| 208 | | 221 | |
| 209 | if (idx < input_file_count or idx == 0) { | 222 | if (idx < input_file_count) { |
| 210 | self.manifest_dirty = true; | 223 | self.manifest_dirty = true; |
| 211 | while (idx < input_file_count) : (idx += 1) { | 224 | while (idx < input_file_count) : (idx += 1) { |
| 212 | var cache_hash_file = &self.files.items[idx]; | 225 | var cache_hash_file = &self.files.items[idx]; |
| ... | @@ -438,3 +451,34 @@ test "check that changing a file makes cache fail" { | ... | @@ -438,3 +451,34 @@ test "check that changing a file makes cache fail" { |
| 438 | try cwd.deleteTree(temp_manifest_dir); | 451 | try cwd.deleteTree(temp_manifest_dir); |
| 439 | try cwd.deleteFile(temp_file); | 452 | try cwd.deleteFile(temp_file); |
| 440 | } | 453 | } |
| | 454 | |
| | 455 | test "no file inputs" { |
| | 456 | const cwd = fs.cwd(); |
| | 457 | const temp_manifest_dir = "no_file_inputs_manifest_dir"; |
| | 458 | defer cwd.deleteTree(temp_manifest_dir) catch unreachable; |
| | 459 | |
| | 460 | var digest1: [BASE64_DIGEST_LEN]u8 = undefined; |
| | 461 | var digest2: [BASE64_DIGEST_LEN]u8 = undefined; |
| | 462 | |
| | 463 | { |
| | 464 | var ch = try CacheHash.init(testing.allocator, temp_manifest_dir); |
| | 465 | defer ch.release(); |
| | 466 | |
| | 467 | ch.add("1234"); |
| | 468 | |
| | 469 | // There should be nothing in the cache |
| | 470 | testing.expectEqual(@as(?[64]u8, null), try ch.hit()); |
| | 471 | |
| | 472 | digest1 = ch.final(); |
| | 473 | } |
| | 474 | { |
| | 475 | var ch = try CacheHash.init(testing.allocator, temp_manifest_dir); |
| | 476 | defer ch.release(); |
| | 477 | |
| | 478 | ch.add("1234"); |
| | 479 | |
| | 480 | digest2 = (try ch.hit()).?; |
| | 481 | } |
| | 482 | |
| | 483 | testing.expectEqual(digest1, digest2); |
| | 484 | } |