aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java
diff options
context:
space:
mode:
authorGravatar ccalvarin <ccalvarin@google.com>2018-08-01 19:25:22 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-08-01 19:26:50 -0700
commit78142a6bf3dbf802e3140c1098cf3fda8d4be883 (patch)
tree0cabf6381320a3c39fecbc14926ee1196e1182bf /src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java
parentc82074a3848e4b7340a85ce855f3d5b9fb157fd7 (diff)
Add a normal startup-option for setting the digest function.
We continue to support the jvm property -Dbazel.DigestFunction, for backwards compatibility, but this will go away. The startup-option is marked experimental for now as we iron out issues. (note: leaving this out of release notes until the experimental tag is removed) As part of this refactor, the default constructor calls for FileSystem and derived classes will now use this default. This should remove the need for constructors that accept custom hash functions. RELNOTES: None. PiperOrigin-RevId: 207035217
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java61
1 files changed, 56 insertions, 5 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java b/src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java
index be18f0ac92..12aee0757b 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/DigestHashFunction.java
@@ -26,13 +26,15 @@ import java.util.Map.Entry;
/** Type of hash function to use for digesting files. */
// The underlying HashFunctions are immutable and thread safe.
public class DigestHashFunction {
+ // This map must be declared first to make sure that calls to register() have it ready.
private static final HashMap<String, DigestHashFunction> hashFunctionRegistry = new HashMap<>();
- public static final DigestHashFunction MD5 = DigestHashFunction.register(Hashing.md5(), "MD5");
- public static final DigestHashFunction SHA1 =
- DigestHashFunction.register(Hashing.sha1(), "SHA-1", "SHA1");
- public static final DigestHashFunction SHA256 =
- DigestHashFunction.register(Hashing.sha256(), "SHA-256", "SHA256");
+ public static final DigestHashFunction MD5 = register(Hashing.md5(), "MD5");
+ public static final DigestHashFunction SHA1 = register(Hashing.sha1(), "SHA-1", "SHA1");
+ public static final DigestHashFunction SHA256 = register(Hashing.sha256(), "SHA-256", "SHA256");
+
+ private static DigestHashFunction defaultHash;
+ private static boolean defaultHasBeenSet = false;
private final HashFunction hash;
private final String name;
@@ -81,6 +83,55 @@ public class DigestHashFunction {
return hashFunction;
}
+ /**
+ * Returns the default DigestHashFunction for this instance of Bazel.
+ *
+ * <p>Note: This is a synchronized function, to make sure it does not occur concurrently with
+ * {@link #setDefault(DigestHashFunction)}. Once this value is set, it's a constant, so to prevent
+ * blocking calls, users should cache this value if needed.
+ *
+ * @throws DefaultNotSetException if the default has not yet been set by a previous call to {@link
+ * #setDefault}.
+ */
+ public static synchronized DigestHashFunction getDefault() throws DefaultNotSetException {
+ if (!defaultHasBeenSet) {
+ throw new DefaultNotSetException("DigestHashFunction default has not been set");
+ }
+ return defaultHash;
+ }
+
+ /** Indicates that the default has not been initialized. */
+ public static final class DefaultNotSetException extends Exception {
+ DefaultNotSetException(String message) {
+ super(message);
+ }
+ }
+
+ /**
+ * Sets the default DigestHashFunction for this instance of Bazel - can only be set once to
+ * prevent incongruities.
+ *
+ * @throws DefaultAlreadySetException if it was already set.
+ */
+ public static synchronized void setDefault(DigestHashFunction hash)
+ throws DefaultAlreadySetException {
+ if (defaultHasBeenSet) {
+ throw new DefaultAlreadySetException(
+ String.format(
+ "setDefault(%s) failed. The default has already been set to %s, you cannot reset it.",
+ hash.name, defaultHash.name));
+ }
+ defaultHash = hash;
+ defaultHasBeenSet = true;
+ }
+
+ /** Failure to set the default if the default already being set. */
+ public static final class DefaultAlreadySetException extends Exception {
+ DefaultAlreadySetException(String message) {
+ super(message);
+ }
+ }
+
/** Converts a string to its registered {@link DigestHashFunction}. */
public static class DigestFunctionConverter implements Converter<DigestHashFunction> {
@Override