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

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

Introduction

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

Prototype

@Override
    Hasher putInt(int i);

Source Link

Usage

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

private void generateNodeLocalHash(StreamNode node, Hasher hasher, int id) {
    hasher.putInt(id);

    hasher.putInt(node.getParallelism());

    if (node.getOperator() instanceof AbstractUdfStreamOperator) {
        String udfClassName = ((AbstractUdfStreamOperator<?, ?>) node.getOperator()).getUserFunction()
                .getClass().getName();// w  ww.j a va2 s.com

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

From source file:org.openmicroscopy.shoola.keywords.ThumbnailCheckLibrary.java

/**
 * <table>//from  w ww  . j  a v  a  2  s .c o  m
 *   <td>Get Thumbnail Hash</td>
 *   <td>name of image whose thumbnail is queried</td>
 * </table>
 * @param imageFilename the name of the image
 * @return the hash of the thumbnail canvas image
 * @throws MultipleComponentsFoundException if multiple thumbnails exist for the given name
 * @throws ComponentNotFoundException if no thumbnails exist for the given name
 */
public String getThumbnailHash(String imageFilename)
        throws ComponentNotFoundException, MultipleComponentsFoundException {
    final RenderedImage image = captureImage("thumbnail", imageFilename);
    final IteratorIntPixel pixels = new IteratorIntPixel(image);
    final Hasher hasher = Hashing.goodFastHash(128).newHasher();
    while (pixels.hasNext()) {
        hasher.putInt(pixels.next());
    }
    return hasher.hash().toString();
}

From source file:com.google.javascript.jscomp.RandomNameGenerator.java

/**
 * Returns the {@code nameIdx}-th short name. This might be a reserved name.
 * A user-requested prefix is not included, but the first returned character
 * is supposed to go at position {@code position} in the final name
 *///www .j ava2s  . co m
private String generateSuffix(int position, int nameIdx) {
    StringBuilder name = new StringBuilder();
    int length = getNameLength(position, nameIdx);
    nameIdx++;
    do {
        nameIdx--;
        String alphabet;
        if (position == 0) {
            alphabet = shuffledFirst;
        } else {
            Hasher hasher = Hashing.murmur3_128().newHasher();
            hasher.putInt(length);
            hasher.putUnencodedChars(name);
            int alphabetIdx = (hasher.hash().asInt() & 0x7fffffff) % NUM_SHUFFLES;
            alphabet = shuffledNonFirst.get(alphabetIdx);
        }
        int alphabetSize = alphabet.length();
        char character = alphabet.charAt(nameIdx % alphabetSize);
        name.append(character);

        nameIdx /= alphabetSize;
        position++;
    } while (nameIdx > 0);
    return name.toString();
}

From source file:org.apache.kylin.engine.mr.steps.CalculateStatsFromBaseCuboidMapper.java

public void putRowKeyToHLLOld(String[] row) {
    //generate hash for each row key column
    byte[][] rowHashCodes = new byte[nRowKey][];
    for (int i = 0; i < nRowKey; i++) {
        Hasher hc = hf.newHasher();
        String colValue = row[i];
        if (colValue != null) {
            rowHashCodes[i] = hc.putString(colValue).hash().asBytes();
        } else {//from   w w w  .j a  v  a 2 s  .c  o m
            rowHashCodes[i] = hc.putInt(0).hash().asBytes();
        }
    }

    // use the row key column hash to get a consolidated hash for each cuboid
    for (int i = 0; i < cuboidIds.length; i++) {
        Hasher hc = hf.newHasher();
        for (int position = 0; position < allCuboidsBitSet[i].length; position++) {
            hc.putBytes(rowHashCodes[allCuboidsBitSet[i][position]]);
        }

        allCuboidsHLL[i].add(hc.hash().asBytes());
    }
}

From source file:com.eucalyptus.network.NetworkInfoBroadcaster.java

private static int fingerprint(final NetworkInfoSource source,
        final List<com.eucalyptus.cluster.Cluster> clusters, final Set<String> dirtyPublicAddresses,
        final String networkConfiguration) {
    final HashFunction hashFunction = goodFastHash(32);
    final Hasher hasher = hashFunction.newHasher();
    final Funnel<VersionedNetworkView> versionedItemFunnel = new Funnel<VersionedNetworkView>() {
        @Override//from  w w  w. j  a v a  2s .  c o m
        public void funnel(final VersionedNetworkView o, final PrimitiveSink primitiveSink) {
            primitiveSink.putString(o.getId());
            primitiveSink.putChar('=');
            primitiveSink.putInt(o.getVersion());
        }
    };
    for (final Map.Entry<String, Iterable<? extends VersionedNetworkView>> entry : source.getView()
            .entrySet()) {
        hasher.putString(entry.getKey());
        for (final VersionedNetworkView item : entry.getValue()) {
            hasher.putObject(item, versionedItemFunnel);
        }
    }
    hasher.putString(Joiner.on(',').join(Sets.newTreeSet(Iterables.transform(clusters, HasName.GET_NAME))));
    hasher.putString(Joiner.on(',').join(Sets.newTreeSet(dirtyPublicAddresses)));
    hasher.putInt(networkConfiguration.hashCode());
    return hasher.hash().asInt();
}

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

/**
 * Applies the {@link Hasher} to the {@link StreamNode} (only node local
 * attributes are taken into account). The hasher encapsulates the current
 * state of the hash.//from w  ww . java  2 s.  c  om
 *
 * <p>
 * <p>The specified ID is local to this node. We cannot use the
 * {@link StreamNode#id}, because it is incremented in a static counter.
 * Therefore, the IDs for identical jobs will otherwise be different.
 */
private void generateNodeLocalHash(StreamNode node, Hasher hasher, int id) {
    // This resolves conflicts for otherwise identical source nodes. BUT
    // the generated hash codes depend on the ordering of the nodes in the
    // stream graph.
    hasher.putInt(id);
}

From source file:org.apache.kylin.engine.mr.steps.fdc2.FactDistinctHiveColumnsMapper2.java

private void putRowKeyToHLL(String[] row) {

    //generate hash for each row key column
    for (int i = 0; i < nRowKey; i++) {
        Hasher hc = hf.newHasher();
        String colValue = row[intermediateTableDesc.getRowKeyColumnIndexes()[i]];
        if (colValue != null) {
            row_hashcodes[i].set(hc.putString(colValue).hash().asBytes());
        } else {//from w w w  .  j  a va2s.c  o m
            row_hashcodes[i].set(hc.putInt(0).hash().asBytes());
        }
    }

    // user the row key column hash to get a consolidated hash for each cuboid
    for (int i = 0, n = allCuboidsBitSet.length; i < n; i++) {
        Hasher hc = hf.newHasher();
        for (int position = 0; position < allCuboidsBitSet[i].length; position++) {
            hc.putBytes(row_hashcodes[allCuboidsBitSet[i][position]].array());
        }

        allCuboidsHLL[i].add(hc.hash().asBytes());
    }
}

From source file:org.apache.kylin.engine.mr.steps.FactDistinctColumnsMapper.java

private void putRowKeyToHLLOld(String[] row) {
    //generate hash for each row key column
    for (int i = 0; i < nRowKey; i++) {
        Hasher hc = hf.newHasher();
        String colValue = row[intermediateTableDesc.getRowKeyColumnIndexes()[i]];
        if (colValue != null) {
            row_hashcodes[i].set(hc.putString(colValue).hash().asBytes());
        } else {/* ww  w.  j a  v a2 s.  co  m*/
            row_hashcodes[i].set(hc.putInt(0).hash().asBytes());
        }
    }

    // user the row key column hash to get a consolidated hash for each cuboid
    for (int i = 0, n = allCuboidsBitSet.length; i < n; i++) {
        Hasher hc = hf.newHasher();
        for (int position = 0; position < allCuboidsBitSet[i].length; position++) {
            hc.putBytes(row_hashcodes[allCuboidsBitSet[i][position]].array());
        }

        allCuboidsHLL[i].add(hc.hash().asBytes());
    }
}

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

/**
 * ctor//from  w  w w  .j  a  va2 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.vmware.appfactory.common.base.AbstractApiController.java

/**
 * Sets an e-tag on the request calculated from the given DAO.
 *
 * If the request contains the same e-tag, this means the resource
 * has not been modified since the last request, and the browser cache
 * already contains the whole response.//from  www.jav a 2s .  com
 *
 * In this case, we'll set the HTTP headers to indicate that the
 * response has not been modified, and return true.
 *
 * @param request    - web request, used to get client's etag (if any)
 * @param response   - web response, used to set 304 response if necessary
 * @param objCollection - A collection of objects. If set, the e-tag is computed based on the hashCode
 *                      for each of the objects.
 * @param daoList    - one or more DAOs to query.  The e-tag is computed using the current state of each
 *                      of these tables, such that if any changes the computed e-tag should change.
 * @return
 * true - if the client already has a cached version of the resource.
 * When this is returned, this method has written an HTTP response,
 * and the caller should simply return immediately.
 *
 * false - if the E-Tag header has been set and the caller should
 * proceed to generate and write a normal response.
 *
 * @throws IOException
 * if the 304 response could not be written
 */
public boolean checkModified(@Nonnull HttpServletRequest request, @Nonnull HttpServletResponse response,
        Collection<? extends Object> objCollection, @Nonnull AfDao... daoList) throws IOException {

    String charSequence = "optimus-prime";
    Hasher hasher = hashFunction.newHasher().putString(charSequence);

    for (AfDao dao : daoList) {
        long lastModified = dao.lastModified();
        long size = dao.countAll();
        hasher.putLong(lastModified).putLong(size);
    }

    // If the objects are not present, put a 0 value indicating 0 sized objects.
    if (CollectionUtils.isNotEmpty(objCollection)) {
        for (Object o : objCollection) {
            if (o != null) {
                hasher.putInt(o.hashCode());
            } else {
                // Indicating that there was an object but null.
                hasher.putInt(0);
            }
        }
    }

    String newToken = hasher.hash().toString();

    String oldToken = applyETagCache(request, response, newToken, false);
    return newToken.equals(oldToken);
}