/
SnapshotChecksum.java
80 lines (71 loc) · 2.95 KB
/
SnapshotChecksum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
* one or more contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright ownership.
* Licensed under the Zeebe Community License 1.1. You may not use this file
* except in compliance with the Zeebe Community License 1.1.
*/
package io.camunda.zeebe.snapshots.impl;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
final class SnapshotChecksum {
private SnapshotChecksum() {
throw new IllegalStateException("Utility class");
}
public static SfvChecksum read(final Path checksumPath) throws IOException {
try (final RandomAccessFile checksumFile = new RandomAccessFile(checksumPath.toFile(), "r")) {
if (checksumFile.length() == 8) {
// compatibility mode
final long combinedChecksum = checksumFile.readLong();
return new SfvChecksum(combinedChecksum);
}
final SfvChecksum sfvChecksum = new SfvChecksum();
String line;
while ((line = checksumFile.readLine()) != null) {
sfvChecksum.updateFromSfvFile(line);
}
return sfvChecksum;
}
}
public static SfvChecksum calculate(final Path snapshotDirectory) throws IOException {
try (final var fileStream =
Files.list(snapshotDirectory).filter(SnapshotChecksum::isNotMetadataFile).sorted()) {
final SfvChecksum sfvChecksum = createCombinedChecksum(fileStream.toList());
// While persisting transient snapshot, the checksum of metadata file is added at the end.
// Hence when we recalculate the checksum, we must follow the same order. Otherwise base on
// the file name, the sorted file list will have a differnt order and thus result in a
// different checksum.
final var metadataFile = snapshotDirectory.resolve(FileBasedSnapshotStore.METADATA_FILE_NAME);
if (metadataFile.toFile().exists()) {
sfvChecksum.updateFromFile(metadataFile);
}
return sfvChecksum;
}
}
private static boolean isNotMetadataFile(final Path file) {
return !file.getFileName().toString().equals(FileBasedSnapshotStore.METADATA_FILE_NAME);
}
public static void persist(final Path checksumPath, final SfvChecksum checksum)
throws IOException {
try (final RandomAccessFile checksumFile = new RandomAccessFile(checksumPath.toFile(), "rwd")) {
final byte[] data = checksum.serializeSfvFileData();
checksumFile.write(data);
checksumFile.setLength(data.length);
}
}
/**
* computes a checksum for the files, in the order they're presented
*
* @return the SfvChecksum object
*/
private static SfvChecksum createCombinedChecksum(final List<Path> files) throws IOException {
final SfvChecksum checksum = new SfvChecksum();
for (final var f : files) {
checksum.updateFromFile(f);
}
return checksum;
}
}