Search in sources :

Example 11 with HashFunction

use of com.google.common.hash.HashFunction in project lucene-solr by apache.

the class StatsComponentTest method testCardinality.

/** @see #testHllOptions */
public void testCardinality() throws Exception {
    SolrCore core = h.getCore();
    // insure we have the same hasher a_l would use
    HashFunction hasher = HllOptions.parseHllOptions(params("cardinality", "true"), core.getLatestSchema().getField("a_l")).getHasher();
    String[] baseParams = new String[] { "q", "*:*", "stats", "true", "indent", "true", "rows", "0" };
    assertQ("empty cardinalities", req(params("stats.field", "{!key=a cardinality=true}a_l", "stats.field", "{!key=pa cardinality=true}prehashed_a_l", "stats.field", "{!key=b cardinality=true}b_l", "stats.field", "{!key=c cardinality=true}c_l"), baseParams), cardinalityXpath("a", 0), cardinalityXpath("pa", 0), cardinalityXpath("b", 0), cardinalityXpath("c", 0));
    int id = 0;
    // add trivial docs to test basic cardinality
    for (int i = 0; i < 100; i++) {
        // add the same values multiple times (diff docs)
        for (int j = 0; j < 5; j++) {
            ++id;
            assertU(adoc("id", "" + id, "a_l", "" + i, "prehashed_a_l", "" + hasher.hashLong((long) i).asLong(), "b_l", "" + (i % 7), "c_l", "" + id));
        }
    }
    assertU(commit());
    assertQ("various cardinalities", req(params("stats.field", "{!key=a cardinality=true}a_l", "stats.field", "{!key=pa hllPreHashed=true cardinality=true}prehashed_a_l", "stats.field", "{!key=b cardinality=true}b_l", "stats.field", "{!key=c cardinality=true}c_l"), baseParams), cardinalityXpath("a", 100), cardinalityXpath("pa", 100), cardinalityXpath("b", 7), cardinalityXpath("c", 500));
    // various ways of explicitly saying "don't bother to compute cardinality"
    for (SolrParams p : new SolrParams[] { params("stats.field", "{!key=a min=true cardinality=false}a_l"), params("stats.field", "{!key=a min=true cardinality=$doit}a_l", "doit", "false"), // missing doit param
    params("stats.field", "{!key=a min=true cardinality=$doit}a_l"), // other tunning options shouldn't change things
    params("stats.field", "{!key=a min=true hllPreHashed=true cardinality=false}a_l"), params("stats.field", "{!key=a min=true hllRegwidth=4 cardinality=$doit}a_l", "doit", "false"), // missing doit param
    params("stats.field", "{!key=a min=true hllLog2m=18 cardinality=$doit}a_l") }) {
        assertQ("min w/cardinality explicitly disabled", req(p, baseParams), "count(//lst[@name='stats_fields']/lst[@name='a']/double[@name='min'])=1", "count(//lst[@name='stats_fields']/lst[@name='a']/long[@name='cardinality'])=0");
    }
}
Also used : HashFunction(com.google.common.hash.HashFunction) SolrCore(org.apache.solr.core.SolrCore) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams)

Example 12 with HashFunction

use of com.google.common.hash.HashFunction in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method open.

@Override
public String open(StorageObject object, Map<Option, ?> options) {
    try {
        Insert req = storage.objects().insert(object.getBucket(), object);
        GenericUrl url = req.buildHttpRequest().getUrl();
        String scheme = url.getScheme();
        String host = url.getHost();
        String path = "/upload" + url.getRawPath();
        url = new GenericUrl(scheme + "://" + host + path);
        url.set("uploadType", "resumable");
        url.set("name", object.getName());
        for (Option option : options.keySet()) {
            Object content = option.get(options);
            if (content != null) {
                url.set(option.value(), content.toString());
            }
        }
        JsonFactory jsonFactory = storage.getJsonFactory();
        HttpRequestFactory requestFactory = storage.getRequestFactory();
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object));
        HttpHeaders requestHeaders = httpRequest.getHeaders();
        requestHeaders.set("X-Upload-Content-Type", firstNonNull(object.getContentType(), "application/octet-stream"));
        String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options);
        if (key != null) {
            BaseEncoding base64 = BaseEncoding.base64();
            HashFunction hashFunction = Hashing.sha256();
            requestHeaders.set("x-goog-encryption-algorithm", "AES256");
            requestHeaders.set("x-goog-encryption-key", key);
            requestHeaders.set("x-goog-encryption-key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
        }
        HttpResponse response = httpRequest.execute();
        if (response.getStatusCode() != 200) {
            GoogleJsonError error = new GoogleJsonError();
            error.setCode(response.getStatusCode());
            error.setMessage(response.getStatusMessage());
            throw translate(error);
        }
        return response.getHeaders().getLocation();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) Insert(com.google.api.services.storage.Storage.Objects.Insert) BaseEncoding(com.google.common.io.BaseEncoding) HashFunction(com.google.common.hash.HashFunction) StorageObject(com.google.api.services.storage.model.StorageObject) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 13 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 14 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 15 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)

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