Example usage for com.google.common.hash Hasher hash

List of usage examples for com.google.common.hash Hasher hash

Introduction

In this page you can find the example usage for com.google.common.hash Hasher hash.

Prototype

@CheckReturnValue
HashCode hash();

Source Link

Document

Computes a hash code based on the data that have been provided to this hasher.

Usage

From source file:org.sfs.nodes.compute.object.WriteHttpServerResponseHeaders.java

@Override
public Void call(Void aVoid) {

    HttpServerResponse httpServerResponse = httpServerRequest.response();

    Optional<String> contentDisposition = parentVersion.getContentDisposition();
    Optional<String> contentEncoding = parentVersion.getContentEncoding();
    Optional<Long> deleteAt = parentVersion.getDeleteAt();
    Optional<String> contentType = parentVersion.getContentType();
    Optional<Boolean> serverSideEncryption = parentVersion.getServerSideEncryption();
    Optional<Boolean> oStaticLargeObject = parentVersion.getStaticLargeObject();
    Calendar createTs = parentVersion.getCreateTs();
    Calendar updateTs = parentVersion.getUpdateTs();

    httpServerResponse = httpServerResponse.putHeader(ACCEPT_RANGES, "none");
    httpServerResponse = httpServerResponse.putHeader(LAST_MODIFIED, toRfc1123String(updateTs));
    httpServerResponse = httpServerResponse.putHeader(DATE, toRfc1123String(getInstance()));
    httpServerResponse = httpServerResponse.putHeader(X_CONTENT_VERSION, valueOf(parentVersion.getId()));

    if (contentType.isPresent()) {
        httpServerResponse = httpServerResponse.putHeader(CONTENT_TYPE, contentType.get());
    } else {/*  ww w . jav  a2s . c  om*/
        httpServerResponse = httpServerResponse.putHeader(CONTENT_TYPE, OCTET_STREAM.toString());
    }

    if (contentDisposition.isPresent()) {
        httpServerResponse = httpServerResponse.putHeader(CONTENT_DISPOSITION, contentDisposition.get());
    }

    if (contentEncoding.isPresent()) {
        httpServerResponse = httpServerResponse.putHeader(CONTENT_ENCODING, contentEncoding.get());
    }

    if (deleteAt.isPresent()) {
        httpServerResponse = httpServerResponse.putHeader(X_DELETE_AT, valueOf(deleteAt.get()));
    }

    if (serverSideEncryption.isPresent()) {
        httpServerResponse = httpServerResponse.putHeader(X_SERVER_SIDE_ENCRYPTION,
                serverSideEncryption.get().toString());
    }

    if (oStaticLargeObject.isPresent()) {
        httpServerResponse = httpServerResponse.putHeader(X_STATIC_LARGE_OBJECT,
                oStaticLargeObject.get().toString());
    }

    if (isEmpty(largeObjectVersions)) {
        byte[] md5 = parentVersion.calculateMd5().get();
        byte[] sha512 = parentVersion.calculateSha512().get();

        httpServerResponse = httpServerResponse.setChunked(true);
        httpServerResponse = httpServerResponse.putHeader(ETAG, base16().lowerCase().encode(md5));
        httpServerResponse = httpServerResponse.putHeader(CONTENT_MD5, base64().encode(md5));
        httpServerResponse = httpServerResponse.putHeader(X_CONTENT_SHA512, base64().encode(sha512));

    } else {

        long contentLengthSum = 0;
        Hasher md5Hasher = md5().newHasher();
        Hasher sha512Hasher = sha512().newHasher();

        int count = 0;
        byte[] firstMd5 = null;
        byte[] firstSha512 = null;

        for (TransientVersion largeObjectVersion : largeObjectVersions) {
            contentLengthSum = checkedAdd(contentLengthSum, largeObjectVersion.calculateLength().get());

            byte[] calculatedMd5 = largeObjectVersion.calculateMd5().get();
            byte[] calculatedSha512 = largeObjectVersion.calculateSha512().get();

            if (count == 0) {
                firstMd5 = calculatedMd5;
                firstSha512 = calculatedSha512;
            }

            md5Hasher.putBytes(calculatedMd5);

            sha512Hasher.putBytes(calculatedSha512);

            count++;
        }

        byte[] md5 = count <= 1 ? firstMd5 : md5Hasher.hash().asBytes();
        byte[] sha512 = count <= 1 ? firstSha512 : sha512Hasher.hash().asBytes();

        httpServerResponse = httpServerResponse.setChunked(true);
        httpServerResponse = httpServerResponse.putHeader(ETAG, base16().lowerCase().encode(md5));
        httpServerResponse = httpServerResponse.putHeader(CONTENT_MD5, base64().encode(md5));
        httpServerResponse = httpServerResponse.putHeader(X_CONTENT_SHA512, base64().encode(sha512));
    }

    Metadata metaData = parentVersion.getMetadata();
    for (String key : metaData.keySet()) {
        SortedSet<String> values = metaData.get(key);
        httpServerResponse = httpServerResponse.putHeader(format("%s%s", X_ADD_OBJECT_META_PREFIX, key),
                values);
    }

    return null;
}

From source file:org.apache.twill.yarn.YarnTwillPreparer.java

private void createApplicationJar(final ApplicationBundler bundler, Map<String, LocalFile> localFiles)
        throws IOException {
    try {// w  w  w. jav  a2s  .  c o m
        final Set<Class<?>> classes = Sets.newIdentityHashSet();
        classes.addAll(dependencies);

        ClassLoader classLoader = getClassLoader();
        for (RuntimeSpecification spec : twillSpec.getRunnables().values()) {
            classes.add(classLoader.loadClass(spec.getRunnableSpecification().getClassName()));
        }

        // Add the TwillRunnableEventHandler class
        if (twillSpec.getEventHandler() != null) {
            classes.add(getClassLoader().loadClass(twillSpec.getEventHandler().getClassName()));
        }

        // The location name is computed from the MD5 of all the classes names
        // The localized name is always APPLICATION_JAR
        List<String> classList = Lists.newArrayList(Iterables.transform(classes, CLASS_TO_NAME));
        Collections.sort(classList);
        Hasher hasher = Hashing.md5().newHasher();
        for (String name : classList) {
            hasher.putString(name);
        }
        // Only depends on class list so that it can be reused across different launches
        String name = hasher.hash().toString() + "-" + Constants.Files.APPLICATION_JAR;

        LOG.debug("Create and copy {}", Constants.Files.APPLICATION_JAR);
        Location location = locationCache.get(name, new LocationCache.Loader() {
            @Override
            public void load(String name, Location targetLocation) throws IOException {
                bundler.createBundle(targetLocation, classes);
            }
        });

        LOG.debug("Done {}", Constants.Files.APPLICATION_JAR);

        localFiles.put(Constants.Files.APPLICATION_JAR,
                createLocalFile(Constants.Files.APPLICATION_JAR, location, true));

    } catch (ClassNotFoundException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.gradle.api.internal.changedetection.state.JvmClassHasher.java

public HashCode hashJarFile(FileDetails fileDetails) {
    HashCode signature = persistentCache.get(fileDetails.getContent().getContentMd5());
    if (signature != null) {
        return signature;
    }//  w  w w  .  j a  v a 2s.  co m

    File file = new File(fileDetails.getPath());
    final Hasher hasher = createHasher();
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        // Ensure we visit the zip entries in a deterministic order
        Map<String, ZipEntry> entriesByName = new TreeMap<String, ZipEntry>();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (!zipEntry.isDirectory() && zipEntry.getName().endsWith(".class")) {
                entriesByName.put(zipEntry.getName(), zipEntry);
            }
        }
        for (ZipEntry zipEntry : entriesByName.values()) {
            visit(zipFile, zipEntry, hasher);
        }
    } catch (Exception e) {
        throw new UncheckedIOException("Could not calculate the signature for Jar file " + file, e);
    } finally {
        IOUtils.closeQuietly(zipFile);
    }
    signature = hasher.hash();
    persistentCache.put(fileDetails.getContent().getContentMd5(), signature);
    return signature;
}

From source file:com.attribyte.essem.model.StoredGraph.java

/**
 * Creates a key./*from   w w w . j a va2 s . c o m*/
 * @param index The associated index.
 * @param uid The user id.
 * @param key The key.
 * @param createTime The create time.
 */
public StoredGraph(final String index, final String uid, final MetricKey key, final String rangeName,
        final long startTimestamp, final long endTimestamp, final String downsampleFn, final String rateUnit,
        final String title, final String description, final String xLabel, final String yLabel,
        final Collection<String> tags, final Date createTime) {
    this.index = Strings.nullToEmpty(index);
    this.uid = Strings.nullToEmpty(uid);
    this.key = key;
    this.createTime = createTime != null ? createTime : new Date();
    this.range = Strings.nullToEmpty(rangeName);
    this.downsampleFn = Strings.nullToEmpty(downsampleFn);
    this.rateUnit = Strings.nullToEmpty(rateUnit);
    this.startTimestamp = startTimestamp;
    this.endTimestamp = endTimestamp;

    final Hasher hasher = hashFunction.newHasher();
    hasher.putString(this.index).putString(this.uid).putString(key.application).putString(key.host)
            .putString(key.instance).putString(key.name).putString(key.field).putString(this.downsampleFn)
            .putString(this.rateUnit);

    if (startTimestamp > 0L && endTimestamp > 0L) {
        hasher.putLong(startTimestamp);
        hasher.putLong(endTimestamp);
    } else {
        hasher.putString(this.range);
    }

    this.id = hasher.hash().toString();
    this.sid = this.id.substring(0, 8);

    this.title = Strings.nullToEmpty(title);
    this.description = Strings.nullToEmpty(description);
    this.xLabel = Strings.nullToEmpty(xLabel);
    this.yLabel = Strings.nullToEmpty(yLabel);
    this.tags = tags != null ? ImmutableList.copyOf(cleanTags(tags)) : ImmutableList.<String>of();
    this.tagString = buildTagString();
}

From source file:org.sfs.vo.XVersion.java

public Optional<byte[]> calculateSha512() {
    Hasher hasher = sha512().newHasher();
    int size = segments.size();
    if (segments.isEmpty() && contentLength != null && contentLength <= 0) {
        return of(EMPTY_SHA512);
    } else if (size == 1) {
        return segments.first().getReadSha512();
    } else if (size >= 2) {
        for (TransientSegment transientSegment : segments) {
            hasher.putBytes(transientSegment.getReadSha512().get());
        }//from  w w  w .  j a  va  2  s . c o  m
        return of(hasher.hash().asBytes());
    } else {
        return absent();
    }
}

From source file:org.apache.flink.streaming.api.graph.StreamGraphHasherV2.java

/**
 * Generates a deterministic hash from node-local properties and input and
 * output edges./*from  w  ww. j  a v a 2  s. c om*/
 */
private byte[] generateDeterministicHash(StreamNode node, Hasher hasher, Map<Integer, byte[]> hashes,
        boolean isChainingEnabled) {

    // Include stream node to hash. We use the current size of the computed
    // hashes as the ID. We cannot use the node's ID, because it is
    // assigned from a static counter. This will result in two identical
    // programs having different hashes.
    generateNodeLocalHash(node, hasher, hashes.size());

    // Include chained nodes to hash
    for (StreamEdge outEdge : node.getOutEdges()) {
        if (isChainable(outEdge, isChainingEnabled)) {
            StreamNode chainedNode = outEdge.getTargetVertex();

            // Use the hash size again, because the nodes are chained to
            // this node. This does not add a hash for the chained nodes.
            generateNodeLocalHash(chainedNode, hasher, hashes.size());
        }
    }

    byte[] hash = hasher.hash().asBytes();

    // Make sure that all input nodes have their hash set before entering
    // this loop (calling this method).
    for (StreamEdge inEdge : node.getInEdges()) {
        byte[] otherHash = hashes.get(inEdge.getSourceId());

        // Sanity check
        if (otherHash == null) {
            throw new IllegalStateException("Missing hash for input node " + inEdge.getSourceVertex()
                    + ". Cannot generate hash for " + node + ".");
        }

        for (int j = 0; j < hash.length; j++) {
            hash[j] = (byte) (hash[j] * 37 ^ otherHash[j]);
        }
    }

    if (LOG.isDebugEnabled()) {
        String udfClassName = "";
        if (node.getOperator() instanceof AbstractUdfStreamOperator) {
            udfClassName = ((AbstractUdfStreamOperator<?, ?>) node.getOperator()).getUserFunction().getClass()
                    .getName();
        }

        LOG.debug("Generated hash '" + byteToHexString(hash) + "' for node " + "'" + node.toString() + "' {id: "
                + node.getId() + ", " + "parallelism: " + node.getParallelism() + ", " + "user function: "
                + udfClassName + "}");
    }

    return hash;
}

From source file:net.pterodactylus.sone.data.AlbumImpl.java

@Override
public String getFingerprint() {
    Hasher hash = Hashing.sha256().newHasher();
    hash.putString("Album(");
    hash.putString("ID(").putString(id).putString(")");
    hash.putString("Title(").putString(title).putString(")");
    hash.putString("Description(").putString(description).putString(")");
    if (albumImage != null) {
        hash.putString("AlbumImage(").putString(albumImage).putString(")");
    }/* ww  w  . ja  v  a 2 s . c o  m*/

    /* add nested albums. */
    hash.putString("Albums(");
    for (Album album : albums) {
        hash.putString(album.getFingerprint());
    }
    hash.putString(")");

    /* add images. */
    hash.putString("Images(");
    for (Image image : getImages()) {
        if (image.isInserted()) {
            hash.putString(image.getFingerprint());
        }
    }
    hash.putString(")");

    hash.putString(")");
    return hash.hash().toString();
}

From source file:edu.harvard.hms.dbmi.bd2k.irct.join.HashJoinImpl.java

private HashCode hashResultSetRow(int[] columns, PrimitiveDataType[] columnDataTypes, ResultSet resultSet)
        throws ResultSetException {
    Hasher columnHash = hashFunction.newHasher();

    for (int columnI = 0; columnI < columns.length; columnI++) {
        int column = columns[columnI];
        switch (columnDataTypes[columnI].getName()) {
        case "boolean":
            columnHash.putBoolean(resultSet.getBoolean(column));
            break;
        case "byte":
            columnHash.putByte(resultSet.getByte(column));
            break;
        case "double":
            columnHash.putDouble(resultSet.getDouble(column));
            break;
        case "float":
            columnHash.putFloat(resultSet.getFloat(column));
            break;
        case "integer":
            columnHash.putInt(resultSet.getInt(column));
            break;
        case "long":
            columnHash.putLong(resultSet.getLong(column));
            break;
        default:// w  w w  . jav a 2 s.c  om
            columnHash.putString(resultSet.getString(column), Charsets.UTF_8);
            break;
        }
    }

    return columnHash.hash();
}

From source file:zotmc.collect.recipe.BasicRecipeView.java

@Override
public int hashCode() {
    Hasher h = Hashing.goodFastHash(32).newHasher().putInt(asInfo(getOutput()).hashCode());

    Category cat = getCategory();/* ww w . j  a  va2 s. c  om*/

    if (cat == SHAPED) {
        h.putBoolean(false).putBoolean(true);

        Matrix<RecipeElement> input = getShapedInput();
        if (isMirrored())
            h.putInt(input.hashCode() ^ horizontalMirror(input).hashCode());
        else
            h.putInt(input.hashCode());
    }

    else if (cat == SHAPELESS)
        h.putBoolean(true).putBoolean(false).putInt(getShapelessInput().hashCode());

    else
        h.putBoolean(false).putBoolean(false);

    return h.hash().asInt();
}

From source file:com.turn.splicer.merge.ResultsMerger.java

public long signatureOf(TsdbResult result) {
    HashFunction hf = Hashing.goodFastHash(64);
    Hasher hasher = hf.newHasher();

    List<String> aggTags = result.getAggregateTags();
    if (aggTags != null) {
        List<String> sortedAggTags = Lists.newArrayList(aggTags);
        Collections.sort(sortedAggTags);
        for (String aggTag : sortedAggTags) {
            hasher.putString(aggTag, Charset.forName("ISO-8859-1"));
        }/*from   w w w .j  av  a  2 s  .  com*/
    }

    Map<String, String> tags;
    if (result.getTags() != null && (tags = result.getTags().getTags()) != null) {
        List<String> tagTokens = Lists.newArrayList(tags.keySet());
        Collections.sort(tagTokens);
        for (String s : tagTokens) {
            hasher.putString(s, Charset.forName("ISO-8859-1"));
            hasher.putString(tags.get(s), Charset.forName("ISO-8859-1"));
        }
    }

    List<String> tsuids = result.getTsuids();
    if (tsuids != null) {
        List<String> sortedTsUIDs = Lists.newArrayList(tsuids);
        Collections.sort(sortedTsUIDs);
        for (String tsuid : sortedTsUIDs) {
            hasher.putString(tsuid, Charset.forName("ISO-8859-1"));
        }
    }

    return hasher.hash().asLong();
}