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

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

Introduction

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

Prototype

@Override
Hasher putString(CharSequence charSequence, Charset charset);

Source Link

Document

Equivalent to putBytes(charSequence.toString().getBytes(charset)) .

Usage

From source file:org.bimserver.cache.DownloadDescriptor.java

public String getCacheKey() {
    Hasher hasher = hf.newHasher();
    // TODO This serializerOid actually makes the cache a per-user cache... Maybe not the most useful feature
    hasher.putLong(serializerOid);//  w ww .j a v  a  2  s.  c  o m
    for (long roid : roids) {
        hasher.putLong(roid);
    }
    if (jsonQuery != null) {
        // TODO This is based on the incoming JSON (if there is any), this should be replaced at some point by a canonical-form
        hasher.putString(jsonQuery, Charsets.UTF_8);
        HashCode hashcode = hasher.hash();
        return hashcode.toString();
    } else {
        // TODO This does not work because the toJson function is not complete
        ObjectNode json = new JsonQueryObjectModelConverter(packageMetaData).toJson(query);
        try {
            StringWriter stringWriter = new StringWriter();
            OBJECT_MAPPER.writeValue(stringWriter, json);
            System.out.println(query.toString());
            hasher.putString(stringWriter.toString(), Charsets.UTF_8);
            HashCode hashcode = hasher.hash();
            return hashcode.toString();
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:org.lightjason.agentspeak.common.CPath.java

@Override
public final int hashCode() {
    final Hasher l_hasher = org.lightjason.agentspeak.language.CCommon.getTermHashing();
    m_path.forEach(i -> l_hasher.putString(i, Charsets.UTF_8));
    return l_hasher.hash().hashCode();
}

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

private void generateNodeLocalHash(StreamNode node, Hasher hasher, int id) {
    hasher.putInt(id);/*from  w ww.j ava 2 s  .  c  o m*/

    hasher.putInt(node.getParallelism());

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

        hasher.putString(udfClassName, Charset.forName("UTF-8"));
    }
}

From source file:com.facebook.buck.parser.SerialDaemonicParserState.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private TargetNode<?> createTargetNode(BuckEventBus eventBus, Cell cell, Path buildFile, BuildTarget target,
        Map<String, Object> rawNode, TargetNodeListener nodeListener) {
    BuildRuleType buildRuleType = parseBuildRuleTypeFromRawRule(cell, rawNode);

    // Because of the way that the parser works, we know this can never return null.
    Description<?> description = cell.getDescription(buildRuleType);

    if (target.isFlavored()) {
        if (description instanceof Flavored) {
            if (!((Flavored) description).hasFlavors(ImmutableSet.copyOf(target.getFlavors()))) {
                throw new HumanReadableException("Unrecognized flavor in target %s while parsing %s%s.", target,
                        UnflavoredBuildTarget.BUILD_TARGET_PREFIX, MorePaths
                                .pathWithUnixSeparators(target.getBasePath().resolve(cell.getBuildFileName())));
            }/*from www.ja v  a2 s . com*/
        } else {
            LOG.warn(
                    "Target %s (type %s) must implement the Flavored interface "
                            + "before we can check if it supports flavors: %s",
                    target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
            throw new HumanReadableException(
                    "Target %s (type %s) does not currently support flavors (tried %s)",
                    target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
        }
    }

    Cell targetCell = cell.getCell(target);
    BuildRuleFactoryParams factoryParams = new BuildRuleFactoryParams(targetCell.getFilesystem(),
            target.withoutCell(),
            new FilesystemBackedBuildFileTree(cell.getFilesystem(), cell.getBuildFileName()),
            targetCell.isEnforcingBuckPackageBoundaries());
    Object constructorArg = description.createUnpopulatedConstructorArg();
    try {
        ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder();
        ImmutableSet.Builder<BuildTargetPattern> visibilityPatterns = ImmutableSet.builder();
        try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus,
                PerfEventId.of("MarshalledConstructorArg"), "target", target)) {
            marshaller.populate(targetCell.getCellRoots(), targetCell.getFilesystem(), factoryParams,
                    constructorArg, declaredDeps, visibilityPatterns, rawNode);
        }
        try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("CreatedTargetNode"),
                "target", target)) {
            Hasher hasher = Hashing.sha1().newHasher();
            hasher.putString(BuckVersion.getVersion(), UTF_8);
            JsonObjectHashing.hashJsonObject(hasher, rawNode);
            targetsCornucopia.put(target.getUnflavoredBuildTarget(), target);
            TargetNode<?> node = new TargetNode(hasher.hash(), description, constructorArg, typeCoercerFactory,
                    factoryParams, declaredDeps.build(), visibilityPatterns.build(), targetCell.getCellRoots());
            nodeListener.onCreate(buildFile, node);
            return node;
        }
    } catch (NoSuchBuildTargetException | TargetNode.InvalidSourcePathInputException e) {
        throw new HumanReadableException(e);
    } catch (ConstructorArgMarshalException e) {
        throw new HumanReadableException("%s: %s", target, e.getMessage());
    } catch (IOException e) {
        throw new HumanReadableException(e.getMessage(), e);
    }
}

From source file:com.facebook.buck.parser.ParallelDaemonicParserState.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private TargetNode<?> createTargetNode(BuckEventBus eventBus, Cell cell, Path buildFile, BuildTarget target,
        Map<String, Object> rawNode, TargetNodeListener nodeListener) {
    BuildRuleType buildRuleType = parseBuildRuleTypeFromRawRule(cell, rawNode);

    // Because of the way that the parser works, we know this can never return null.
    Description<?> description = cell.getDescription(buildRuleType);

    if (target.isFlavored()) {
        if (description instanceof Flavored) {
            if (!((Flavored) description).hasFlavors(ImmutableSet.copyOf(target.getFlavors()))) {
                throw new HumanReadableException("Unrecognized flavor in target %s while parsing %s%s.", target,
                        UnflavoredBuildTarget.BUILD_TARGET_PREFIX, MorePaths
                                .pathWithUnixSeparators(target.getBasePath().resolve(cell.getBuildFileName())));
            }/*from ww  w .j  a  va2s . com*/
        } else {
            LOG.warn(
                    "Target %s (type %s) must implement the Flavored interface "
                            + "before we can check if it supports flavors: %s",
                    target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
            throw new HumanReadableException(
                    "Target %s (type %s) does not currently support flavors (tried %s)",
                    target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
        }
    }

    Cell targetCell = cell.getCell(target);
    BuildRuleFactoryParams factoryParams = new BuildRuleFactoryParams(targetCell.getFilesystem(),
            target.withoutCell(),
            new FilesystemBackedBuildFileTree(cell.getFilesystem(), cell.getBuildFileName()),
            targetCell.isEnforcingBuckPackageBoundaries());
    Object constructorArg = description.createUnpopulatedConstructorArg();
    try {
        ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder();
        ImmutableSet.Builder<BuildTargetPattern> visibilityPatterns = ImmutableSet.builder();
        try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus,
                PerfEventId.of("MarshalledConstructorArg"), "target", target)) {
            marshaller.populate(targetCell.getCellRoots(), targetCell.getFilesystem(), factoryParams,
                    constructorArg, declaredDeps, visibilityPatterns, rawNode);
        }
        try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("CreatedTargetNode"),
                "target", target)) {
            Hasher hasher = Hashing.sha1().newHasher();
            hasher.putString(BuckVersion.getVersion(), UTF_8);
            JsonObjectHashing.hashJsonObject(hasher, rawNode);
            synchronized (this) {
                targetsCornucopia.put(target.getUnflavoredBuildTarget(), target);
            }
            TargetNode<?> node = new TargetNode(hasher.hash(), description, constructorArg, typeCoercerFactory,
                    factoryParams, declaredDeps.build(), visibilityPatterns.build(), targetCell.getCellRoots());
            nodeListener.onCreate(buildFile, node);
            return node;
        }
    } catch (NoSuchBuildTargetException | TargetNode.InvalidSourcePathInputException e) {
        throw new HumanReadableException(e);
    } catch (ConstructorArgMarshalException e) {
        throw new HumanReadableException("%s: %s", target, e.getMessage());
    } catch (IOException e) {
        throw new HumanReadableException(e.getMessage(), e);
    }
}

From source file:com.facebook.buck.util.config.Config.java

private HashCode computeOrderIndependentHashCode() {
    ImmutableMap<String, ImmutableMap<String, String>> rawValues = rawConfig.getValues();
    ImmutableSortedMap.Builder<String, ImmutableSortedMap<String, String>> expanded = ImmutableSortedMap
            .naturalOrder();//from w  w  w. j  a  v a2s  .c o  m
    for (String section : rawValues.keySet()) {
        expanded.put(section, ImmutableSortedMap.copyOf(get(section)));
    }

    ImmutableSortedMap<String, ImmutableSortedMap<String, String>> sortedConfigMap = expanded.build();

    Hasher hasher = Hashing.sha256().newHasher();
    for (Entry<String, ImmutableSortedMap<String, String>> entry : sortedConfigMap.entrySet()) {
        hasher.putString(entry.getKey(), StandardCharsets.UTF_8);
        for (Entry<String, String> nestedEntry : entry.getValue().entrySet()) {
            hasher.putString(nestedEntry.getKey(), StandardCharsets.UTF_8);
            hasher.putString(nestedEntry.getValue(), StandardCharsets.UTF_8);
        }
    }

    return hasher.hash();
}

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

/**
 * Generates a hash from a user-specified ID.
 *//*from  w ww  .j a  v  a2s  .  c o  m*/
private byte[] generateUserSpecifiedHash(StreamNode node, Hasher hasher) {
    hasher.putString(node.getTransformationId(), Charset.forName("UTF-8"));

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

From source file:org.lightjason.agentspeak.language.CLiteral.java

/**
 * ctor/*from  w ww. j a  v a  2  s. c  om*/
 *
 * @param p_at @ prefix is set
 * @param p_negated negated flag
 * @param p_functor functor of the literal
 * @param p_values initial list of values
 * @param p_annotations initial set of annotations
 */
public CLiteral(final boolean p_at, final boolean p_negated, final IPath p_functor,
        final Collection<ITerm> p_values, final Collection<ILiteral> p_annotations) {
    m_at = p_at;
    m_negated = p_negated;
    // create a full copy of the functor, because concurrency modification
    m_functor = new CPath(p_functor);

    // create immutable structures
    final Multimap<IPath, ILiteral> l_annotations = HashMultimap.create();
    p_annotations.forEach(i -> l_annotations.put(i.fqnfunctor(), i));
    m_annotations = ImmutableSetMultimap.copyOf(l_annotations);

    final Multimap<IPath, ITerm> l_values = LinkedListMultimap.create();
    p_values.forEach(i -> l_values.put(i.fqnfunctor(), i));
    m_values = ImmutableListMultimap.copyOf(l_values);

    m_orderedvalues = Collections.unmodifiableList(new LinkedList<>(p_values));

    // calculates hash value
    m_hash = m_functor.hashCode()
            ^ IntStream.range(0, m_orderedvalues.size()).boxed()
                    .mapToInt(i -> (i + 1) * m_orderedvalues.get(i).hashCode()).sum()
            ^ m_annotations.values().stream().mapToInt(Object::hashCode).sum() ^ (m_negated ? 0 : 55529)
            ^ (m_at ? 0 : 8081);

    // calculates the structure hash value (Murmur3) of the value and annotation definition
    // functor will be added iif no literal data exists ( hasher must be existing twice )
    final String l_functor = p_functor.getPath();

    final Hasher l_valuehasher = CCommon.getTermHashing();
    if (m_orderedvalues.stream().filter(i -> i instanceof ILiteral)
            .map(i -> l_valuehasher.putInt(((ILiteral) i).valuehash())).count() == 0) {
        l_valuehasher.putBoolean(m_negated);
        l_valuehasher.putString(l_functor, Charsets.UTF_8);
    }

    final Hasher l_annotationhasher = CCommon.getTermHashing();
    if (m_annotations.values().stream().map(i -> l_annotationhasher.putInt(i.valuehash())).count() == 0) {
        l_annotationhasher.putBoolean(m_negated);
        l_annotationhasher.putString(l_functor, Charsets.UTF_8);
    }

    m_annotationhash = l_annotationhasher.hash().asInt();
    m_valuehash = l_valuehasher.hash().asInt();
}

From source file:com.facebook.buck.cxx.PreprocessorDelegate.java

public String hashCommand(ImmutableList<String> flags) {
    Hasher hasher = Hashing.murmur3_128().newHasher();
    String workingDirString = workingDir.toString();
    // Skips the executable argument (the first one) as that is not sanitized.
    for (String part : sanitizer.sanitizeFlags(Iterables.skip(flags, 1))) {
        // TODO(#10251354): find a better way of dealing with getting a project dir normalized hash
        if (part.startsWith(workingDirString)) {
            part = "<WORKINGDIR>" + part.substring(workingDirString.length());
        }//  ww  w .  j a va  2s .c om
        hasher.putString(part, Charsets.UTF_8);
        hasher.putBoolean(false); // separator
    }
    return hasher.hash().toString();
}

From source file:fr.inria.eventcloud.api.Quadruple.java

/**
 * Returns a 128 bits hash value for the current quadruple.
 * //from  w ww  .  j  av  a 2s  .  c  o m
 * @return a 128 bits hash value for the current quadruple.
 */
public HashCode hashValue() {
    Hasher hasher = Hashing.murmur3_128().newHasher();

    for (int i = 0; i < this.nodes.length; i++) {
        hasher.putString(this.nodes[i].toString(), Charsets.UTF_8);
    }

    if (this.publicationSource != null) {
        hasher.putUnencodedChars(this.publicationSource);
    }
    hasher.putLong(this.publicationTime);

    return hasher.hash();
}