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

The following examples show how to use com.google.common.hash.Hasher#putUnencodedChars() . 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: FastPaths.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Adds the Path to the hasher as unencoded chars. Roughly equivalent to {@code
 * hasher.putUnencodedChars(path.toString())}.
 */
public static Hasher hashPathFast(Hasher hasher, Path path) {
  if (!(path instanceof BuckUnixPath)) {
    return hasher.putUnencodedChars(path.toString());
  }
  if (path.isAbsolute()) {
    hasher.putChar('/');
  }
  for (int i = 0; i < path.getNameCount(); i++) {
    if (i != 0) {
      hasher.putChar('/');
    }
    hasher.putUnencodedChars(FastPaths.getNameString(path, i));
  }
  return hasher;
}
 
Example 2
Source File: HashUtils.java    From MHAP with Apache License 2.0 6 votes vote down vote up
public static long[] computeHashes(String item, int numWords, int seed)
{
	long[] hashes = new long[numWords];

	for (int word = 0; word < numWords; word += 2)
	{
		HashFunction hashFunc = Hashing.murmur3_128(seed + word);
		Hasher hasher = hashFunc.newHasher();
		hasher.putUnencodedChars(item);

		// get the two longs out
		HashCode hc = hasher.hash();
		ByteBuffer bb = ByteBuffer.wrap(hc.asBytes());
		hashes[word] = bb.getLong(0);
		if (word + 1 < numWords)
			hashes[word + 1] = bb.getLong(8);
	}

	return hashes;
}
 
Example 3
Source File: AbstractStreamingFingerprintComputer.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Generate a fingerprint for the target object using its URI.
 *
 * @param target
 *          The target object
 * @param context
 *          The object containing the reference
 * @param hasher
 *          hasher to stream to
 */
private void fingerprintEObject(final EObject target, final EObject context, final Hasher hasher) {
  if (target == null) {
    hasher.putUnencodedChars(NULL_STRING);
  } else if (target.eIsProxy()) {
    if (context.eResource() instanceof LazyLinkingResource) {
      final URI proxyUri = ((InternalEObject) target).eProxyURI();
      if (!((LazyLinkingResource) context.eResource()).getEncoder().isCrossLinkFragment(context.eResource(), proxyUri.fragment())) {
        hasher.putUnencodedChars(proxyUri.toString());
        return;
      }
    }
    hasher.putUnencodedChars(UNRESOLVED_STRING);
  } else {
    hasher.putUnencodedChars(EcoreUtil.getURI(target).toString());
  }
}
 
Example 4
Source File: ScopeResourceDescriptionStrategy.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean createEObjectDescriptions(final EObject eObject, final IAcceptor<IEObjectDescription> acceptor) {
  if (getQualifiedNameProvider() == null || !(eObject instanceof ScopeModel)) {
    return false;
  }
  ScopeModel model = (ScopeModel) eObject;
  try {
    QualifiedName qualifiedName = getQualifiedNameProvider().getFullyQualifiedName(model);
    if (qualifiedName != null) {
      Hasher hasher = Hashing.murmur3_32().newHasher(HASHER_CAPACITY);
      hasher.putUnencodedChars(getSourceText(model));
      for (ScopeModel include : model.getIncludedScopes()) {
        hasher.putUnencodedChars(getSourceText(include));
      }
      acceptor.accept(EObjectDescription.create(qualifiedName, model, Collections.singletonMap("fingerprint", hasher.hash().toString())));
    }
    // CHECKSTYLE:CHECK-OFF IllegalCatch
  } catch (RuntimeException e) {
    // CHECKSTYLE:CHECK-ON
    LOG.error(e.getMessage(), e);
  }
  return false;
}
 
Example 5
Source File: Annotations.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
public void hashTo(Hasher digest) {
    for (Map.Entry<String, Object> e : annotations.entrySet()) {
        digest.putUnencodedChars(e.getKey());
        // we're going to assume value is a String or Number because the creation code enforces that
        digest.putUnencodedChars(e.getValue().toString());
    }
}
 
Example 6
Source File: YQLEnumType.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
@Override
public void hashTo(Hasher digest) {
    super.hashTo(digest);
    for (String sym : symbols) {
        digest.putUnencodedChars(sym);
    }
}
 
Example 7
Source File: ModuleWithDependenciesTest.java    From buck with Apache License 2.0 5 votes vote down vote up
private static String hash(String... content) {
  Hasher hasher = Hashing.murmur3_128().newHasher();
  for (String data : content) {
    hasher.putUnencodedChars(data);
  }
  return hasher.hash().toString();
}
 
Example 8
Source File: AstyanaxEventId.java    From emodb with Apache License 2.0 5 votes vote down vote up
/** Computes a 16-bit checksum of the contents of the specific ByteBuffer and channel name. */
private static int computeChecksum(byte[] buf, int offset, int length, String channel) {
    Hasher hasher = Hashing.murmur3_32().newHasher();
    hasher.putBytes(buf, offset, length);
    hasher.putUnencodedChars(channel);
    return hasher.hash().asInt() & 0xffff;
}
 
Example 9
Source File: BinaryParser.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
public Hasher init(final URI url) {
	final Hasher hasher = hashFunction.newHasher();
	if (url != null) {
		// Note that we need to go directly to the hasher to encode explicit IP addresses
		hasher.putUnencodedChars(url.getHost());
		hasher.putByte((byte)0);
	}
	return hasher;
}
 
Example 10
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 11
Source File: Sketches.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Compute the hash of the direct action key for the given action, including the names of its
 * output files.
 */
public static BigInteger computeActionKey(
    ActionAnalysisMetadata action, ActionKeyContext keyContext) {
  Hasher hasher = newHasher().putUnencodedChars(action.getKey(keyContext));
  for (Artifact output : action.getOutputs()) {
    hasher.putUnencodedChars(output.getExecPath().getPathString());
  }
  return fromHashCode(hasher.hash());
}
 
Example 12
Source File: ConfigFeatureFlagConfiguration.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Converts the given flag values into a string hash for use as an output directory fragment. */
private static String hashFlags(SortedMap<Label, String> flagValues) {
  // This hash function is relatively fast and stable between JVM invocations.
  Hasher hasher = Hashing.murmur3_128().newHasher();

  for (Map.Entry<Label, String> flag : flagValues.entrySet()) {
    hasher.putUnencodedChars(flag.getKey().toString());
    hasher.putByte((byte) 0);
    hasher.putUnencodedChars(flag.getValue());
    hasher.putByte((byte) 0);
  }
  return hasher.hash().toString();
}
 
Example 13
Source File: AbstractNonStreamingHashFunctionTest.java    From zetasketch with Apache License 2.0 5 votes vote down vote up
private static void assertPutString(char[] chars) {
  Hasher h1 = new NonStreamingVersion().newHasher();
  Hasher h2 = new NonStreamingVersion().newHasher();
  String s = new String(chars);
  // this is the correct implementation of the spec
  for (int i = 0; i < s.length(); i++) {
    h1.putChar(s.charAt(i));
  }
  h2.putUnencodedChars(s);
  assertEquals(h1.hash(), h2.hash());
}
 
Example 14
Source File: ModuleClassTest.java    From buck with Apache License 2.0 5 votes vote down vote up
private static void testBuckModuleHashProvider(TestExtension testExtension) {
  PluginManager pluginManager = BuckPluginManagerFactory.createPluginManager();
  BuckModuleJarHashProvider hashProvider = new BuckModuleJarHashProvider();
  BuckModuleManager moduleManager = new DefaultBuckModuleManager(pluginManager, hashProvider);
  Hasher hasher = Hashing.murmur3_128().newHasher();
  hasher.putUnencodedChars("some content that never changes");
  assertEquals(hasher.hash().toString(), moduleManager.getModuleHash(testExtension.getClass()));

  assertTrue(moduleManager.isClassInModule(testExtension.getClass()));
  assertFalse(moduleManager.isClassInModule(ModuleClassTest.class));
}
 
Example 15
Source File: YQLBaseType.java    From yql-plus with Apache License 2.0 4 votes vote down vote up
@Override
public void hashTo(Hasher digest) {
    digest.putUnencodedChars(YQLBaseType.class.getName());
    digest.putUnencodedChars(getCoreType().name());
}
 
Example 16
Source File: YQLNamedValueType.java    From yql-plus with Apache License 2.0 4 votes vote down vote up
public void hashTo(Hasher digest) {
    super.hashTo(digest);
    digest.putUnencodedChars(getName());
}
 
Example 17
Source File: Path.java    From bazel with Apache License 2.0 4 votes vote down vote up
/**
 * Return a string representation, as hexadecimal digits, of some hash of the directory.
 *
 * <p>The hash itself is computed according to the design document
 * https://github.com/bazelbuild/proposals/blob/master/designs/2018-07-13-repository-hashing.md
 * and takes enough information into account, to detect the typical non-reproducibility
 * of source-like repository rules, while leaving out what will change from invocation to
 * invocation of a repository rule (in particular file owners) and can reasonably be ignored
 * when considering if a repository is "the same source tree".
 *
 * @return a string representation of the bash of the directory
 * @throws IOException if the digest could not be computed for any reason
 */
public String getDirectoryDigest() throws IOException {
  List<String> entries = new ArrayList<String>(fileSystem.getDirectoryEntries(this));
  Collections.sort(entries);
  Hasher hasher = fileSystem.getDigestFunction().getHashFunction().newHasher();
  for (String entry : entries) {
    Path path = this.getChild(entry);
    FileStatus stat = path.stat(Symlinks.NOFOLLOW);
    hasher.putUnencodedChars(entry);
    if (stat.isFile()) {
      if (path.isExecutable()) {
        hasher.putChar('x');
      } else {
        hasher.putChar('-');
      }
      hasher.putBytes(path.getDigest());
    } else if (stat.isDirectory()) {
      hasher.putChar('d').putUnencodedChars(path.getDirectoryDigest());
    } else if (stat.isSymbolicLink()) {
      PathFragment link = path.readSymbolicLink();
      if (link.isAbsolute()) {
        try {
          Path resolved = path.resolveSymbolicLinks();
          if (resolved.isFile()) {
            if (resolved.isExecutable()) {
              hasher.putChar('x');
            } else {
              hasher.putChar('-');
            }
            hasher.putBytes(resolved.getDigest());
          } else {
            // link to a non-file: include the link itself in the hash
            hasher.putChar('l').putUnencodedChars(link.toString());
          }
        } catch (IOException e) {
          // dangling link: include the link itself in the hash
          hasher.putChar('l').putUnencodedChars(link.toString());
        }
      } else {
        // relative link: include the link itself in the hash
        hasher.putChar('l').putUnencodedChars(link.toString());
      }
    } else {
      // Neither file, nor directory, nor symlink. So do not include further information
      // in the hash, asuming it will not be used during the BUILD anyway.
      hasher.putChar('s');
    }
  }
  return hasher.hash().toString();
}
 
Example 18
Source File: AbstractStreamingFingerprintComputer.java    From dsl-devkit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Generate a fingerprint of an EReference.
 *
 * @param target
 *          The target object the EReference is linked to. May be a proxy or null. If null, the
 *          fingerprint tries to include the parse tree node value of the reference.
 * @param context
 *          The object containing the EReference
 * @param hasher
 *          hasher to stream to
 */
private void fingerprintEReferenceValue(final EObject target, final EObject context, final Hasher hasher) {
  if (target == null) {
    hasher.putUnencodedChars(NULL_STRING);
  } else {
    fingerprintEObject(target, context, hasher);
  }
}
 
Example 19
Source File: AbstractStreamingFingerprintComputer.java    From dsl-devkit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Computes a fingerprint for a general {@link Object}.
 * <p>
 * If the object is an {@link Iterable} then the fingerprint is computed by concatenating the fingerprints of its elements. If the object is neither an
 * {@link Iterable} nor an {@link EObject} then its fingerprint is its {@link String} value. If the object is an {@link EObject} then its fingerprint depends
 * on the {@code context} and {@code indirection}.
 * </p>
 *
 * @see #fingerprintIterable(Iterable, EObject, FingerprintOrder, FingerprintIndirection, Hasher)
 * @param obj
 *          the object to fingerprint, may be {@code null}
 * @param context
 *          the context eObject if the expression is a reference, may be {@code null}
 * @param order
 *          if the object is an {@link Iterable} then indicates whether the order within the iterable is significant for the fingerprint, must not be
 *          {@code null}
 * @param indirection
 *          if the object is an EObject or an Iterable of EObjects then indicates whether the fingerprint should be built using
 *          the URIs or by calling a generated function, must not be {@code null}
 * @param hasher
 *          hasher to stream to
 */
@SuppressWarnings("unchecked")
protected void fingerprintExpr(final Object obj, final EObject context, final FingerprintOrder order, final FingerprintIndirection indirection, final Hasher hasher) {
  if (obj instanceof EObject) {
    if (indirection == FingerprintIndirection.INDIRECT) {
      fingerprintIndirection((EObject) obj, context, null, hasher);
    } else {
      fingerprintEObject((EObject) obj, context, hasher);
    }
  } else if (obj instanceof Iterable) {
    fingerprintIterable((Iterable<? extends Object>) obj, context, order, indirection, hasher); // Unchecked
  } else {
    hasher.putUnencodedChars(String.valueOf(obj));
  }
}
 
Example 20
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);
}