authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-06 22:19:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 13:48:43-04:00
log8e4b80522f70c7d5f636a967d61706cddc473f9e
tree3badbdf90abb02a055d62cf5755336bb01784a26
parente75a6e514444849ca32de88511804c888137b9a1

Return base64 digest instead of using an out variable


1 files changed, 22 insertions(+), 29 deletions(-)

lib/std/cache_hash.zig+22-29
......@@ -13,6 +13,7 @@ const os = @import("os.zig");
1313const base64_encoder = fs.base64_encoder;
1414const base64_decoder = fs.base64_decoder;
1515const BIN_DIGEST_LEN = 32;
16const BASE64_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN);
1617
1718pub const File = struct {
1819 path: ?[]const u8,
......@@ -41,7 +42,7 @@ pub const CacheHash = struct {
4142 manifest_dirty: bool,
4243 force_check_manifest: bool,
4344 files: ArrayList(File),
44 b64_digest: ArrayList(u8),
45 b64_digest: [BASE64_DIGEST_LEN]u8,
4546
4647 pub fn init(alloc: *Allocator, manifest_dir_path: []const u8) !@This() {
4748 try fs.cwd().makePath(manifest_dir_path);
......@@ -55,7 +56,7 @@ pub const CacheHash = struct {
5556 .manifest_dirty = false,
5657 .force_check_manifest = false,
5758 .files = ArrayList(File).init(alloc),
58 .b64_digest = ArrayList(u8).init(alloc),
59 .b64_digest = undefined,
5960 };
6061 }
6162
......@@ -126,27 +127,23 @@ pub const CacheHash = struct {
126127 self.addSlice(cache_hash_file.path.?);
127128 }
128129
129 pub fn hit(self: *@This(), out_digest: *ArrayList(u8)) !bool {
130 pub fn hit(self: *@This()) !?[BASE64_DIGEST_LEN]u8 {
130131 debug.assert(self.manifest_file == null);
131132
132133 var bin_digest: [BIN_DIGEST_LEN]u8 = undefined;
133134 self.blake3.final(&bin_digest);
134135
135 const OUT_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN);
136 try self.b64_digest.resize(OUT_DIGEST_LEN);
137 base64_encoder.encode(self.b64_digest.toSlice(), &bin_digest);
136 base64_encoder.encode(self.b64_digest[0..], &bin_digest);
138137
139138 if (self.files.toSlice().len == 0 and !self.force_check_manifest) {
140 try out_digest.resize(OUT_DIGEST_LEN);
141 mem.copy(u8, out_digest.toSlice(), self.b64_digest.toSlice());
142 return true;
139 return self.b64_digest;
143140 }
144141
145142 self.blake3 = Blake3.init();
146143 self.blake3.update(&bin_digest);
147144
148145 {
149 const manifest_file_path = try fmt.allocPrint(self.alloc, "{}.txt", .{self.b64_digest.toSlice()});
146 const manifest_file_path = try fmt.allocPrint(self.alloc, "{}.txt", .{self.b64_digest});
150147 defer self.alloc.free(manifest_file_path);
151148
152149 self.manifest_file = try self.manifest_dir.createFile(manifest_file_path, .{ .read = true, .truncate = false });
......@@ -231,7 +228,7 @@ pub const CacheHash = struct {
231228 for (self.files.toSlice()) |file| {
232229 self.blake3.update(&file.bin_digest);
233230 }
234 return false;
231 return null;
235232 }
236233
237234 if (idx < input_file_count or idx == 0) {
......@@ -244,11 +241,10 @@ pub const CacheHash = struct {
244241 return error.CacheUnavailable;
245242 };
246243 }
247 return false;
244 return null;
248245 }
249246
250 try self.final(out_digest);
251 return true;
247 return try self.final();
252248 }
253249
254250 pub fn populate_file_hash(self: *@This(), cache_hash_file: *File) !void {
......@@ -265,22 +261,22 @@ pub const CacheHash = struct {
265261 self.blake3.update(&cache_hash_file.bin_digest);
266262 }
267263
268 pub fn final(self: *@This(), out_digest: *ArrayList(u8)) !void {
264 pub fn final(self: *@This()) ![BASE64_DIGEST_LEN]u8 {
269265 debug.assert(self.manifest_file != null);
270266
271267 var bin_digest: [BIN_DIGEST_LEN]u8 = undefined;
272268 self.blake3.final(&bin_digest);
273269
274 const OUT_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN);
275 try out_digest.resize(OUT_DIGEST_LEN);
276 base64_encoder.encode(out_digest.toSlice(), &bin_digest);
270 var out_digest: [BASE64_DIGEST_LEN]u8 = undefined;
271 base64_encoder.encode(&out_digest, &bin_digest);
272
273 return out_digest;
277274 }
278275
279276 pub fn write_manifest(self: *@This()) !void {
280277 debug.assert(self.manifest_file != null);
281278
282 const OUT_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN);
283 var encoded_digest = try Buffer.initSize(self.alloc, OUT_DIGEST_LEN);
279 var encoded_digest = try Buffer.initSize(self.alloc, BASE64_DIGEST_LEN);
284280 defer encoded_digest.deinit();
285281 var contents = try Buffer.init(self.alloc, "");
286282 defer contents.deinit();
......@@ -308,7 +304,6 @@ pub const CacheHash = struct {
308304 file.deinit(self.alloc);
309305 }
310306 self.files.deinit();
311 self.b64_digest.deinit();
312307 self.manifest_dir.close();
313308 }
314309};
......@@ -332,10 +327,8 @@ test "cache file and the recall it" {
332327
333328 try cwd.writeFile("test.txt", "Hello, world!\n");
334329
335 var digest1 = try ArrayList(u8).initCapacity(testing.allocator, 32);
336 defer digest1.deinit();
337 var digest2 = try ArrayList(u8).initCapacity(testing.allocator, 32);
338 defer digest2.deinit();
330 var digest1: [BASE64_DIGEST_LEN]u8 = undefined;
331 var digest2: [BASE64_DIGEST_LEN]u8 = undefined;
339332
340333 {
341334 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
......@@ -347,9 +340,9 @@ test "cache file and the recall it" {
347340 try ch.addFile("test.txt");
348341
349342 // There should be nothing in the cache
350 debug.assert((try ch.hit(&digest1)) == false);
343 debug.assert((try ch.hit()) == null);
351344
352 try ch.final(&digest1);
345 digest1 = try ch.final();
353346 }
354347 {
355348 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
......@@ -361,10 +354,10 @@ test "cache file and the recall it" {
361354 try ch.addFile("test.txt");
362355
363356 // Cache hit! We just "built" the same file
364 debug.assert((try ch.hit(&digest2)) == true);
357 digest2 = (try ch.hit()).?;
365358 }
366359
367 debug.assert(mem.eql(u8, digest1.toSlice(), digest2.toSlice()));
360 debug.assert(mem.eql(u8, digest1[0..], digest2[0..]));
368361
369362 try cwd.deleteTree(temp_manifest_dir);
370363}