use of com.google.common.hash.HashFunction in project hive by apache.
the class TestMurmur3 method testHashCodesM3_32_ints.
@Test
public void testHashCodesM3_32_ints() {
int seed = 123;
Random rand = new Random(seed);
HashFunction hf = Hashing.murmur3_32(seed);
for (int i = 0; i < 1000; i++) {
int val = rand.nextInt();
byte[] data = ByteBuffer.allocate(4).putInt(val).array();
int hc1 = hf.hashBytes(data).asInt();
int hc2 = Murmur3.hash32(data, data.length, seed);
assertEquals(hc1, hc2);
}
}
use of com.google.common.hash.HashFunction in project bazel by bazelbuild.
the class FileUtils method getDirectoryNameForJar.
/**
* Chooses a directory name, based on a JAR file name, considering exploded-aar and classes.jar.
*/
@NonNull
public static String getDirectoryNameForJar(@NonNull File inputFile) {
// add a hash of the original file path.
HashFunction hashFunction = Hashing.sha1();
HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath(), Charsets.UTF_16LE);
String name = Files.getNameWithoutExtension(inputFile.getName());
if (name.equals("classes") && inputFile.getAbsolutePath().contains("exploded-aar")) {
// This naming scheme is coming from DependencyManager#computeArtifactPath.
File versionDir = inputFile.getParentFile().getParentFile();
File artifactDir = versionDir.getParentFile();
File groupDir = artifactDir.getParentFile();
name = Joiner.on('-').join(groupDir.getName(), artifactDir.getName(), versionDir.getName());
}
name = name + "_" + hashCode.toString();
return name;
}
use of com.google.common.hash.HashFunction in project che by eclipse.
the class ETagResponseFilter method doFilter.
/**
* Filter the given container response
*
* @param containerResponse
* the response to use
*/
public void doFilter(GenericContainerResponse containerResponse) {
// get entity of the response
Object entity = containerResponse.getEntity();
// no entity, skip
if (entity == null) {
return;
}
// Only handle JSON content
if (!MediaType.APPLICATION_JSON_TYPE.equals(containerResponse.getContentType())) {
return;
}
// Get the request
ApplicationContext applicationContext = ApplicationContext.getCurrent();
Request request = applicationContext.getRequest();
// manage only GET requests
if (!HttpMethod.GET.equals(request.getMethod())) {
return;
}
// calculate hash with MD5
HashFunction hashFunction = Hashing.md5();
Hasher hasher = hashFunction.newHasher();
boolean hashingSuccess = true;
// Manage a list
if (entity instanceof List) {
List<?> entities = (List) entity;
for (Object simpleEntity : entities) {
hashingSuccess = addHash(simpleEntity, hasher);
if (!hashingSuccess) {
break;
}
}
} else {
hashingSuccess = addHash(entity, hasher);
}
// if we're able to handle the hash
if (hashingSuccess) {
// get result of the hash
HashCode hashCode = hasher.hash();
// Create the entity tag
EntityTag entityTag = new EntityTag(hashCode.toString());
// Check the etag
Response.ResponseBuilder builder = request.evaluatePreconditions(entityTag);
// not modified ?
if (builder != null) {
containerResponse.setResponse(builder.tag(entityTag).build());
} else {
// it has been changed, so send response with new ETag and entity
Response.ResponseBuilder responseBuilder = Response.fromResponse(containerResponse.getResponse()).tag(entityTag);
containerResponse.setResponse(responseBuilder.build());
}
}
}
use of com.google.common.hash.HashFunction in project buck by facebook.
the class AbstractProvisioningProfileMetadata method fromProvisioningProfilePath.
public static ProvisioningProfileMetadata fromProvisioningProfilePath(ProcessExecutor executor, ImmutableList<String> readCommand, Path profilePath) throws IOException, InterruptedException {
Set<ProcessExecutor.Option> options = EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT);
// Extract the XML from its signed message wrapper.
ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(readCommand).addCommand(profilePath.toString()).build();
ProcessExecutor.Result result;
result = executor.launchAndExecute(processExecutorParams, options, /* stdin */
Optional.empty(), /* timeOutMs */
Optional.empty(), /* timeOutHandler */
Optional.empty());
if (result.getExitCode() != 0) {
throw new IOException(result.getMessageForResult("Invalid provisioning profile: " + profilePath));
}
try {
NSDictionary plist = (NSDictionary) PropertyListParser.parse(result.getStdout().get().getBytes());
Date expirationDate = ((NSDate) plist.get("ExpirationDate")).getDate();
String uuid = ((NSString) plist.get("UUID")).getContent();
ImmutableSet.Builder<HashCode> certificateFingerprints = ImmutableSet.builder();
NSArray certificates = (NSArray) plist.get("DeveloperCertificates");
HashFunction hasher = Hashing.sha1();
if (certificates != null) {
for (NSObject item : certificates.getArray()) {
certificateFingerprints.add(hasher.hashBytes(((NSData) item).bytes()));
}
}
ImmutableMap.Builder<String, NSObject> builder = ImmutableMap.builder();
NSDictionary entitlements = ((NSDictionary) plist.get("Entitlements"));
for (String key : entitlements.keySet()) {
builder = builder.put(key, entitlements.objectForKey(key));
}
String appID = entitlements.get("application-identifier").toString();
ProvisioningProfileMetadata.Builder provisioningProfileMetadata = ProvisioningProfileMetadata.builder();
if (plist.get("Platform") != null) {
for (Object platform : (Object[]) plist.get("Platform").toJavaObject()) {
provisioningProfileMetadata.addPlatforms((String) platform);
}
}
return provisioningProfileMetadata.setAppID(ProvisioningProfileMetadata.splitAppID(appID)).setExpirationDate(expirationDate).setUUID(uuid).setProfilePath(profilePath).setEntitlements(builder.build()).setDeveloperCertificateFingerprints(certificateFingerprints.build()).build();
} catch (Exception e) {
throw new IllegalArgumentException("Malformed embedded plist: " + e);
}
}
use of com.google.common.hash.HashFunction in project stream-lib by addthis.
the class TestHyperLogLog method testPrecise.
@Test
@Ignore
public void testPrecise() throws CardinalityMergeException {
int cardinality = 1000000000;
int b = 12;
HyperLogLog baseline = new HyperLogLog(b);
HyperLogLog guava128 = new HyperLogLog(b);
HashFunction hf128 = Hashing.murmur3_128();
for (int j = 0; j < cardinality; j++) {
Double val = Math.random();
String valString = val.toString();
baseline.offer(valString);
guava128.offerHashed(hf128.hashString(valString, Charsets.UTF_8).asLong());
if (j > 0 && j % 1000000 == 0) {
System.out.println("current count: " + j);
}
}
long baselineEstimate = baseline.cardinality();
long g128Estimate = guava128.cardinality();
double se = cardinality * (1.04 / Math.sqrt(Math.pow(2, b)));
double baselineError = (baselineEstimate - cardinality) / (double) cardinality;
double g128Error = (g128Estimate - cardinality) / (double) cardinality;
System.out.format("b: %f g128 %f", baselineError, g128Error);
assertTrue("baseline estimate bigger than expected", baselineEstimate >= cardinality - (2 * se));
assertTrue("baseline estimate smaller than expected", baselineEstimate <= cardinality + (2 * se));
assertTrue("g128 estimate bigger than expected", g128Estimate >= cardinality - (2 * se));
assertTrue("g128 estimate smaller than expected", g128Estimate <= cardinality + (2 * se));
}
Aggregations