Java Code Examples for com.google.common.hash.Hasher#putInt()

The following examples show how to use com.google.common.hash.Hasher#putInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NullHasherTest.java    From elastic-load-balancing-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasics() {
    Hasher hasher = NullHasher.INSTANCE;
    assertEquals(0, hasher.hash().asInt());
    hasher.putBoolean(false);
    hasher.putByte((byte) 3);
    hasher.putBytes(new byte[0]);
    hasher.putBytes(null, 3, 3);
    hasher.putChar('c');
    hasher.putDouble(3.3);
    hasher.putFloat(3.4f);
    hasher.putInt(7);
    hasher.putLong(3);
    hasher.putObject(null, null);
    hasher.putShort((short) 7);
    hasher.putString(null, null);
    hasher.putUnencodedChars(null);
}
 
Example 2
Source File: HeaderSearchPaths.java    From buck with Apache License 2.0 6 votes vote down vote up
private HashCode getHeaderSymlinkTreeHashCode(
    ImmutableSortedMap<Path, Path> contents,
    boolean shouldCreateHeadersSymlinks,
    boolean shouldCreateHeaderMap) {
  Hasher hasher = Hashing.sha1().newHasher();
  hasher.putBytes(ruleKeyConfiguration.getCoreKey().getBytes(Charsets.UTF_8));
  String symlinkState = shouldCreateHeadersSymlinks ? "symlinks-enabled" : "symlinks-disabled";
  byte[] symlinkStateValue = symlinkState.getBytes(Charsets.UTF_8);
  hasher.putInt(symlinkStateValue.length);
  hasher.putBytes(symlinkStateValue);
  String hmapState = shouldCreateHeaderMap ? "hmap-enabled" : "hmap-disabled";
  byte[] hmapStateValue = hmapState.getBytes(Charsets.UTF_8);
  hasher.putInt(hmapStateValue.length);
  hasher.putBytes(hmapStateValue);
  hasher.putInt(0);
  for (Map.Entry<Path, Path> entry : contents.entrySet()) {
    byte[] key = entry.getKey().toString().getBytes(Charsets.UTF_8);
    byte[] value = entry.getValue().toString().getBytes(Charsets.UTF_8);
    hasher.putInt(key.length);
    hasher.putBytes(key);
    hasher.putInt(value.length);
    hasher.putBytes(value);
  }
  return hasher.hash();
}
 
Example 3
Source File: StringWithMacrosConverter.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Expand the input given for the this macro to some string, which is intended to be written to a
 * file.
 */
private Arg expand(MacroContainer macroContainer) throws MacroException {
  Arg arg = expand(macroContainer.getMacro());

  // If specified, wrap this macro's output in a `WriteToFileArg`.
  if (macroContainer.isOutputToFile()) {
    // "prefix" should give a stable name, so that the same delegate with the same input can
    // output the same file. We won't optimise for this case, since it's actually unlikely to
    // happen within a single run, but using a random name would cause 'buck-out' to expand in an
    // uncontrolled manner.
    Hasher hasher = Hashing.sha256().newHasher();
    hasher.putString(macroContainer.getMacro().getMacroClass().getName(), UTF_8);
    hasher.putInt(macroContainer.getMacro().hashCode());
    String prefix = hasher.hash().toString();
    arg = new WriteToFileArg(getBuildTarget(), prefix, arg);
  }

  return arg;
}
 
Example 4
Source File: OutputModule.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String generateOutputFilename(int postfix) {
  HashFunction hashFunction = Hashing.murmur3_128();

  Hasher hasher = hashFunction.newHasher();
  hasher.putUnencodedChars(outputIdentifier);
  hasher.putInt(postfix);

  return protoTempDir.toString() + File.separator + hasher.hash() + ProtoSuffix;
}
 
Example 5
Source File: SerializableSaltedHasher.java    From CuckooFilter4J with Apache License 2.0 5 votes vote down vote up
/**
 * hashes the object with an additional salt. For purpose of the cuckoo
 * filter, this is used when the hash generated for an item is all zeros.
 * All zeros is the same as an empty bucket, so obviously it's not a valid
 * tag. 
 */
HashCode hashObjWithSalt(T object, int moreSalt) {
	Hasher hashInst = hasher.newHasher();
	hashInst.putObject(object, funnel);
	hashInst.putLong(seedNSalt);
	hashInst.putInt(moreSalt);
	return hashInst.hash();
}
 
Example 6
Source File: SkipScanFilter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(32);
    Hasher h = hf.newHasher();
    h.putInt(slots.size());
    for (int i=0; i<slots.size(); i++) {
        h.putInt(slots.get(i).size());
        for (int j=0; j<slots.size(); j++) {
            h.putBytes(slots.get(i).get(j).getLowerRange());
            h.putBytes(slots.get(i).get(j).getUpperRange());
        }
    }
    return h.hash().asInt();
}
 
Example 7
Source File: _Response.java    From tac-kbp-eal with MIT License 5 votes vote down vote up
private Hasher hasher2016() {
  final Hasher hasher = SHA1_HASHER.newHasher()
      .putString(docID().toString(), Charsets.UTF_8)
      .putString(type().toString(), Charsets.UTF_8)
      .putString(role().toString(), Charsets.UTF_8)
      .putString(canonicalArgument().string(), Charsets.UTF_8)
      .putInt(canonicalArgument().charOffsetSpan().startInclusive())
      .putInt(canonicalArgument().charOffsetSpan().endInclusive())
      .putInt(baseFiller().startInclusive())
      .putInt(baseFiller().endInclusive());

  // we put PJ_CODE and AAJ_CODE into the hash because without them,
  // observe that shifting a second PJ element to being the first AAJ
  // element results in the same hash
  hasher.putInt(PJ_CODE);
  for (final CharOffsetSpan pj : Ordering.natural().sortedCopy(predicateJustifications())) {
    hasher.putInt(pj.startInclusive()).putInt(pj.endInclusive());
  }

  hasher.putInt(AAJ_CODE);
  for (final CharOffsetSpan aaj : Ordering.natural().sortedCopy(
      additionalArgumentJustifications())) {
    hasher.putInt(aaj.startInclusive()).putInt(aaj.endInclusive());
  }

  hasher.putInt(realis().ordinal());

  return hasher;
}
 
Example 8
Source File: Example.java    From oryx with Apache License 2.0 5 votes vote down vote up
public Example(Feature target, Feature... features) {
  Preconditions.checkArgument(features != null);
  this.features = features;
  this.target = target;
  Hasher hasher = HASH.newHasher();
  for (Feature feature : features) {
    if (feature != null) {
      hasher.putInt(feature.hashCode());
    }
  }
  if (target != null) {
    hasher.putInt(target.hashCode());
  }
  cachedHashCode = hasher.hashCode();
}
 
Example 9
Source File: SkipScanFilter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(32);
    Hasher h = hf.newHasher();
    h.putInt(slots.size());
    for (int i=0; i<slots.size(); i++) {
        h.putInt(slots.get(i).size());
        for (int j=0; j<slots.size(); j++) {
            h.putBytes(slots.get(i).get(j).getLowerRange());
            h.putBytes(slots.get(i).get(j).getUpperRange());
        }
    }
    return h.hash().asInt();
}
 
Example 10
Source File: SkipScanFilter.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(32);
    Hasher h = hf.newHasher();
    h.putInt(slots.size());
    for (int i=0; i<slots.size(); i++) {
        h.putInt(slots.get(i).size());
        for (int j=0; j<slots.size(); j++) {
            h.putBytes(slots.get(i).get(j).getLowerRange());
            h.putBytes(slots.get(i).get(j).getUpperRange());
        }
    }
    return h.hash().asInt();
}
 
Example 11
Source File: Sha1HashCode.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the specified {@link Hasher} by putting the 20 bytes of this SHA-1 to it in order.
 *
 * @return The specified {@link Hasher}.
 */
public Hasher update(Hasher hasher) {
  hasher.putInt(firstFourBytes);
  hasher.putLong(nextEightBytes);
  hasher.putLong(lastEightBytes);
  return hasher;
}
 
Example 12
Source File: NestedRandom.java    From log-synth with Apache License 2.0 5 votes vote down vote up
private void hash(Hasher hasher) {
    if (content != null) {
        hasher.putString(content, Charsets.UTF_8);
    } else {
        hasher.putInt(seed);
    }
    if (parent != null) {
        parent.hash(hasher);
    }
}
 
Example 13
Source File: YQLType.java    From yql-plus with Apache License 2.0 4 votes vote down vote up
public void hashTo(Hasher digest) {
    digest.putInt(getCoreType().ordinal());
    annotations.hashTo(digest);
}
 
Example 14
Source File: ShardGroupCompactionImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Hash our data into a consistent long
 */
protected Hasher doHash( final ApplicationScope scope, final DirectedEdgeMeta directedEdgeMeta,
                         final ShardEntryGroup shardEntryGroup ) {

    final Hasher hasher = MURMUR_128.newHasher();


    addToHash( hasher, scope.getApplication() );


    for ( DirectedEdgeMeta.NodeMeta nodeMeta : directedEdgeMeta.getNodes() ) {
        addToHash( hasher, nodeMeta.getId() );
        hasher.putInt( nodeMeta.getNodeType().getStorageValue() );
    }


    /**
     * Add our edge type
     */
    for ( String type : directedEdgeMeta.getTypes() ) {
        hasher.putString( type, CHARSET );
    }


    return hasher;
}
 
Example 15
Source File: StringHashing.java    From buck with Apache License 2.0 3 votes vote down vote up
/**
 * Encodes the length of the string in UTF-16 code units, then the UTF-16 code units of the
 * string.
 *
 * <p>Useful to ensure hash codes are different when multiple strings are hashed in order ("foo"
 * then "bar" should hash differently from "foobar").
 */
public static void hashStringAndLength(Hasher hasher, String string) {
  // We used to hash the UTF-8 bytes of the string, but it takes
  // a lot of unnecessary CPU and memory to do so.
  hasher.putInt(string.length());
  hasher.putUnencodedChars(string);
}