Search in sources :

Example 36 with Hasher

use of com.google.common.hash.Hasher in project bazel by bazelbuild.

the class RepositoryCache method getChecksum.

/**
   * Obtain the checksum of a file.
   *
   * @param keyType The type of hash function. e.g. SHA-1, SHA-256.
   * @param path The path to the file.
   * @throws IOException
   */
public static String getChecksum(KeyType keyType, Path path) throws IOException {
    Hasher hasher = keyType.newHasher();
    byte[] byteBuffer = new byte[BUFFER_SIZE];
    try (InputStream stream = path.getInputStream()) {
        int numBytesRead = stream.read(byteBuffer);
        while (numBytesRead != -1) {
            if (numBytesRead != 0) {
                // If more than 0 bytes were read, add them to the hash.
                hasher.putBytes(byteBuffer, 0, numBytesRead);
            }
            numBytesRead = stream.read(byteBuffer);
        }
    }
    return hasher.hash().toString();
}
Also used : Hasher(com.google.common.hash.Hasher) InputStream(java.io.InputStream)

Example 37 with Hasher

use of com.google.common.hash.Hasher in project bazel by bazelbuild.

the class WorkerFilesHash method getWorkerFilesHash.

public static HashCode getWorkerFilesHash(Iterable<? extends ActionInput> toolFiles, ActionExecutionContext actionExecutionContext) throws IOException {
    Hasher hasher = Hashing.sha256().newHasher();
    for (ActionInput tool : toolFiles) {
        hasher.putString(tool.getExecPathString(), Charset.defaultCharset());
        hasher.putBytes(actionExecutionContext.getActionInputFileCache().getDigest(tool));
    }
    return hasher.hash();
}
Also used : Hasher(com.google.common.hash.Hasher) ActionInput(com.google.devtools.build.lib.actions.ActionInput)

Example 38 with Hasher

use of com.google.common.hash.Hasher in project che by eclipse.

the class ETagResponseFilter method doFilter.

/**
     * Filter the given container response
     *
     * @param containerResponse
     *         the response to use
     */
public void doFilter(GenericContainerResponse containerResponse) {
    // get entity of the response
    Object entity = containerResponse.getEntity();
    // no entity, skip
    if (entity == null) {
        return;
    }
    // Only handle JSON content
    if (!MediaType.APPLICATION_JSON_TYPE.equals(containerResponse.getContentType())) {
        return;
    }
    // Get the request
    ApplicationContext applicationContext = ApplicationContext.getCurrent();
    Request request = applicationContext.getRequest();
    // manage only GET requests
    if (!HttpMethod.GET.equals(request.getMethod())) {
        return;
    }
    // calculate hash with MD5
    HashFunction hashFunction = Hashing.md5();
    Hasher hasher = hashFunction.newHasher();
    boolean hashingSuccess = true;
    // Manage a list
    if (entity instanceof List) {
        List<?> entities = (List) entity;
        for (Object simpleEntity : entities) {
            hashingSuccess = addHash(simpleEntity, hasher);
            if (!hashingSuccess) {
                break;
            }
        }
    } else {
        hashingSuccess = addHash(entity, hasher);
    }
    // if we're able to handle the hash
    if (hashingSuccess) {
        // get result of the hash
        HashCode hashCode = hasher.hash();
        // Create the entity tag
        EntityTag entityTag = new EntityTag(hashCode.toString());
        // Check the etag
        Response.ResponseBuilder builder = request.evaluatePreconditions(entityTag);
        // not modified ?
        if (builder != null) {
            containerResponse.setResponse(builder.tag(entityTag).build());
        } else {
            // it has been changed, so send response with new ETag and entity
            Response.ResponseBuilder responseBuilder = Response.fromResponse(containerResponse.getResponse()).tag(entityTag);
            containerResponse.setResponse(responseBuilder.build());
        }
    }
}
Also used : GenericContainerResponse(org.everrest.core.GenericContainerResponse) Response(javax.ws.rs.core.Response) ApplicationContext(org.everrest.core.ApplicationContext) Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) HashFunction(com.google.common.hash.HashFunction) Request(javax.ws.rs.core.Request) List(java.util.List) EntityTag(javax.ws.rs.core.EntityTag)

Example 39 with Hasher

use of com.google.common.hash.Hasher in project buck by facebook.

the class VersionedTargetGraphBuilder method getVersionedFlavor.

/**
   * @return a flavor to which summarizes the given version selections.
   */
static Flavor getVersionedFlavor(SortedMap<BuildTarget, Version> versions) {
    Preconditions.checkArgument(!versions.isEmpty());
    Hasher hasher = Hashing.md5().newHasher();
    for (Map.Entry<BuildTarget, Version> ent : versions.entrySet()) {
        hasher.putString(ent.getKey().toString(), Charsets.UTF_8);
        hasher.putString(ent.getValue().getName(), Charsets.UTF_8);
    }
    return InternalFlavor.of("v" + hasher.hash().toString().substring(0, 7));
}
Also used : Hasher(com.google.common.hash.Hasher) BuildTarget(com.facebook.buck.model.BuildTarget) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 40 with Hasher

use of com.google.common.hash.Hasher in project buck by facebook.

the class JsonObjectHashingTest method nullFieldsAreIgnoredInTheHash.

/**
   * This helps keep the hashes more stable. While we include the buckversion in target hashes, when
   * making changes within Buck (such as adding or removing constructor arg fields) this means that
   * there are fewer places where tests fail, meaning less time being spent debugging "failed"
   * tests.
   * <p>
   * This means that two maps may generate the same hashcode, despite not being strictly equal. We
   * rely on the use case that this class was designed for (namely, generating target hashes) to
   * ensure that correct functionality is maintained.
   */
@Test
public void nullFieldsAreIgnoredInTheHash() {
    HashMap<String, Object> map1 = new HashMap<>();
    map1.put("firstKey", "value");
    map1.put("secondKey", null);
    HashMap<String, Object> map2 = new HashMap<>();
    map2.put("firstKey", "value");
    map2.put("ignoredKey", null);
    Hasher hasher1 = Hashing.sha1().newHasher();
    Hasher hasher2 = Hashing.sha1().newHasher();
    JsonObjectHashing.hashJsonObject(hasher1, map1);
    JsonObjectHashing.hashJsonObject(hasher2, map2);
    assertEquals(hasher1.hash().toString(), hasher2.hash().toString());
}
Also used : Hasher(com.google.common.hash.Hasher) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Hasher (com.google.common.hash.Hasher)59 IOException (java.io.IOException)10 HashCode (com.google.common.hash.HashCode)9 Test (org.junit.Test)9 InputStream (java.io.InputStream)5 Map (java.util.Map)5 BuildTarget (com.facebook.buck.model.BuildTarget)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4 SettableFakeClock (com.facebook.buck.timing.SettableFakeClock)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 HashFunction (com.google.common.hash.HashFunction)3 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)2 ArchiveMemberPath (com.facebook.buck.io.ArchiveMemberPath)2 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)2 BuildRuleType (com.facebook.buck.rules.BuildRuleType)2 Cell (com.facebook.buck.rules.Cell)2 ParamInfoException (com.facebook.buck.rules.ParamInfoException)2