Search in sources :

Example 16 with HashFunction

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);
    }
}
Also used : Random(java.util.Random) HashFunction(com.google.common.hash.HashFunction) Test(org.junit.Test)

Example 17 with HashFunction

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;
}
Also used : HashCode(com.google.common.hash.HashCode) HashFunction(com.google.common.hash.HashFunction) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) NonNull(com.android.annotations.NonNull)

Example 18 with HashFunction

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());
        }
    }
}
Also used : GenericContainerResponse(org.everrest.core.GenericContainerResponse) Response(javax.ws.rs.core.Response) ApplicationContext(org.everrest.core.ApplicationContext) Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) HashFunction(com.google.common.hash.HashFunction) Request(javax.ws.rs.core.Request) List(java.util.List) EntityTag(javax.ws.rs.core.EntityTag)

Example 19 with HashFunction

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);
    }
}
Also used : NSObject(com.dd.plist.NSObject) NSData(com.dd.plist.NSData) NSArray(com.dd.plist.NSArray) NSDictionary(com.dd.plist.NSDictionary) NSString(com.dd.plist.NSString) NSString(com.dd.plist.NSString) HashCode(com.google.common.hash.HashCode) ImmutableSet(com.google.common.collect.ImmutableSet) NSDate(com.dd.plist.NSDate) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) Date(java.util.Date) NSDate(com.dd.plist.NSDate) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) HashFunction(com.google.common.hash.HashFunction) NSObject(com.dd.plist.NSObject)

Example 20 with HashFunction

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));
}
Also used : HashFunction(com.google.common.hash.HashFunction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

HashFunction (com.google.common.hash.HashFunction)23 Test (org.junit.Test)12 Random (java.util.Random)7 ByteBuffer (java.nio.ByteBuffer)5 HashCode (com.google.common.hash.HashCode)3 Hasher (com.google.common.hash.Hasher)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 BaseEncoding (com.google.common.io.BaseEncoding)2 IOException (java.io.IOException)2 ArrayDeque (java.util.ArrayDeque)2 HashSet (java.util.HashSet)2 SolrCore (org.apache.solr.core.SolrCore)2 Ignore (org.junit.Ignore)2 NonNull (com.android.annotations.NonNull)1 NSArray (com.dd.plist.NSArray)1 NSData (com.dd.plist.NSData)1 NSDate (com.dd.plist.NSDate)1 NSDictionary (com.dd.plist.NSDictionary)1 NSObject (com.dd.plist.NSObject)1