Example usage for com.google.common.hash Hashing sha1

List of usage examples for com.google.common.hash Hashing sha1

Introduction

In this page you can find the example usage for com.google.common.hash Hashing sha1.

Prototype

public static HashFunction sha1() 

Source Link

Document

Returns a hash function implementing the SHA-1 algorithm (160 hash bits) by delegating to the SHA-1 MessageDigest .

Usage

From source file:org.glowroot.central.repo.AggregateDao.java

public void store(String agentId, long captureTime, List<OldAggregatesByType> aggregatesByTypeList,
        List<Aggregate.SharedQueryText> initialSharedQueryTexts) throws Exception {
    if (aggregatesByTypeList.isEmpty()) {
        agentDao.updateLastCaptureTime(agentId, captureTime).get();
        return;/*from  w w  w  . ja  va  2  s  . c  om*/
    }
    List<String> agentRollupIds = agentDao.readAgentRollupIds(agentId);
    int adjustedTTL = getAdjustedTTL(getTTLs().get(0), captureTime, clock);
    List<ResultSetFuture> futures = Lists.newArrayList();
    List<Aggregate.SharedQueryText> sharedQueryTexts = Lists.newArrayList();
    for (Aggregate.SharedQueryText sharedQueryText : initialSharedQueryTexts) {
        String fullTextSha1 = sharedQueryText.getFullTextSha1();
        if (fullTextSha1.isEmpty()) {
            String fullText = sharedQueryText.getFullText();
            if (fullText.length() > StorageConfig.AGGREGATE_QUERY_TEXT_TRUNCATE) {
                fullTextSha1 = Hashing.sha1().hashString(fullText, Charsets.UTF_8).toString();
                futures.addAll(fullQueryTextDao.store(agentId, fullTextSha1, fullText));
                sharedQueryTexts.add(Aggregate.SharedQueryText.newBuilder()
                        .setTruncatedText(fullText.substring(0, StorageConfig.AGGREGATE_QUERY_TEXT_TRUNCATE))
                        .setFullTextSha1(fullTextSha1).build());
            } else {
                sharedQueryTexts.add(sharedQueryText);
            }
        } else {
            futures.addAll(fullQueryTextDao.updateTTL(agentId, fullTextSha1));
            sharedQueryTexts.add(sharedQueryText);
        }
    }

    // wait for success before proceeding in order to ensure cannot end up with orphaned
    // fullTextSha1
    MoreFutures.waitForAll(futures);
    futures.clear();

    for (OldAggregatesByType aggregatesByType : aggregatesByTypeList) {
        String transactionType = aggregatesByType.getTransactionType();
        Aggregate overallAggregate = aggregatesByType.getOverallAggregate();
        futures.addAll(storeOverallAggregate(agentId, transactionType, captureTime, overallAggregate,
                sharedQueryTexts, adjustedTTL));
        for (OldTransactionAggregate transactionAggregate : aggregatesByType.getTransactionAggregateList()) {
            futures.addAll(storeTransactionAggregate(agentId, transactionType,
                    transactionAggregate.getTransactionName(), captureTime, transactionAggregate.getAggregate(),
                    sharedQueryTexts, adjustedTTL));
        }
        futures.addAll(transactionTypeDao.store(agentRollupIds, transactionType));
    }
    futures.add(agentDao.updateLastCaptureTime(agentId, captureTime));
    // wait for success before inserting "needs rollup" records
    MoreFutures.waitForAll(futures);
    futures.clear();

    List<RollupConfig> rollupConfigs = configRepository.getRollupConfigs();
    // TODO report checker framework issue that occurs without this suppression
    @SuppressWarnings("assignment.type.incompatible")
    Set<String> transactionTypes = aggregatesByTypeList.stream().map(OldAggregatesByType::getTransactionType)
            .collect(Collectors.toSet());

    int needsRollupAdjustedTTL = getNeedsRollupAdjustedTTL(adjustedTTL, rollupConfigs);
    if (agentRollupIds.size() > 1) {
        BoundStatement boundStatement = insertNeedsRollupFromChild.bind();
        int i = 0;
        boundStatement.setString(i++, agentRollupIds.get(1));
        boundStatement.setTimestamp(i++, new Date(captureTime));
        boundStatement.setUUID(i++, UUIDs.timeBased());
        boundStatement.setString(i++, agentId);
        boundStatement.setSet(i++, transactionTypes);
        boundStatement.setInt(i++, needsRollupAdjustedTTL);
        futures.add(session.executeAsync(boundStatement));
    }
    // insert into aggregate_needs_rollup_1
    long intervalMillis = rollupConfigs.get(1).intervalMillis();
    long rollupCaptureTime = Utils.getRollupCaptureTime(captureTime, intervalMillis);
    BoundStatement boundStatement = insertNeedsRollup.get(0).bind();
    int i = 0;
    boundStatement.setString(i++, agentId);
    boundStatement.setTimestamp(i++, new Date(rollupCaptureTime));
    boundStatement.setUUID(i++, UUIDs.timeBased());
    boundStatement.setSet(i++, transactionTypes);
    boundStatement.setInt(i++, needsRollupAdjustedTTL);
    futures.add(session.executeAsync(boundStatement));
    MoreFutures.waitForAll(futures);
}

From source file:it.sayservice.platform.smartplanner.cache.CacheManager.java

private String getEqString(String eqs, String agencyId) {
    String eq = eqs;/*  w  w w .  j a  va2s .co m*/
    eq = eq.replaceAll(agencyId, "").replaceAll("[_ ]", "").replaceAll(",", ";").replaceAll("[\\[\\]]", "");
    return Hashing.sha1().hashString(eq, Charset.forName("UTF-8")).toString();
}

From source file:it.sayservice.platform.smartplanner.cache.CacheManager.java

private String getEqString(List<String> eqs, String agencyId) {
    List<String> sEqus = new ArrayList<String>(eqs);
    Collections.sort(sEqus);//from   w w w.  j  ava2  s  . c o m
    String eq = sEqus.toString();
    eq = eq.replaceAll(agencyId, "").replaceAll("[_ ]", "").replaceAll(",", ";").replaceAll("[\\[\\]]", "");
    return Hashing.sha1().hashString(eq, Charset.forName("UTF-8")).toString();
}

From source file:org.apache.druid.metadata.IndexerSQLMetadataStorageCoordinator.java

/**
 * Compare-and-swap dataSource metadata in a transaction. This will only modify dataSource metadata if it equals
 * oldCommitMetadata when this function is called (based on T.equals). This method is idempotent in that if
 * the metadata already equals newCommitMetadata, it will return true.
 *
 * @param handle        database handle/*  ww w.j a v  a 2 s  . c o m*/
 * @param dataSource    druid dataSource
 * @param startMetadata dataSource metadata pre-insert must match this startMetadata according to
 *                      {@link DataSourceMetadata#matches(DataSourceMetadata)}
 * @param endMetadata   dataSource metadata post-insert will have this endMetadata merged in with
 *                      {@link DataSourceMetadata#plus(DataSourceMetadata)}
 *
 * @return SUCCESS if dataSource metadata was updated from matching startMetadata to matching endMetadata, FAILURE or
 * TRY_AGAIN if it definitely was not updated. This guarantee is meant to help
 * {@link #announceHistoricalSegments(Set, DataSourceMetadata, DataSourceMetadata)}
 * achieve its own guarantee.
 *
 * @throws RuntimeException if state is unknown after this call
 */
protected DataSourceMetadataUpdateResult updateDataSourceMetadataWithHandle(final Handle handle,
        final String dataSource, final DataSourceMetadata startMetadata, final DataSourceMetadata endMetadata)
        throws IOException {
    Preconditions.checkNotNull(dataSource, "dataSource");
    Preconditions.checkNotNull(startMetadata, "startMetadata");
    Preconditions.checkNotNull(endMetadata, "endMetadata");

    final byte[] oldCommitMetadataBytesFromDb = getDataSourceMetadataWithHandleAsBytes(handle, dataSource);
    final String oldCommitMetadataSha1FromDb;
    final DataSourceMetadata oldCommitMetadataFromDb;

    if (oldCommitMetadataBytesFromDb == null) {
        oldCommitMetadataSha1FromDb = null;
        oldCommitMetadataFromDb = null;
    } else {
        oldCommitMetadataSha1FromDb = BaseEncoding.base16()
                .encode(Hashing.sha1().hashBytes(oldCommitMetadataBytesFromDb).asBytes());
        oldCommitMetadataFromDb = jsonMapper.readValue(oldCommitMetadataBytesFromDb, DataSourceMetadata.class);
    }

    final boolean startMetadataMatchesExisting = oldCommitMetadataFromDb == null ? startMetadata.isValidStart()
            : startMetadata.matches(oldCommitMetadataFromDb);

    if (!startMetadataMatchesExisting) {
        // Not in the desired start state.
        log.info("Not updating metadata, existing state is not the expected start state.");
        log.debug("Existing database state [%s], request's start metadata [%s]", oldCommitMetadataFromDb,
                startMetadata);
        return DataSourceMetadataUpdateResult.FAILURE;
    }

    final DataSourceMetadata newCommitMetadata = oldCommitMetadataFromDb == null ? endMetadata
            : oldCommitMetadataFromDb.plus(endMetadata);
    final byte[] newCommitMetadataBytes = jsonMapper.writeValueAsBytes(newCommitMetadata);
    final String newCommitMetadataSha1 = BaseEncoding.base16()
            .encode(Hashing.sha1().hashBytes(newCommitMetadataBytes).asBytes());

    final DataSourceMetadataUpdateResult retVal;
    if (oldCommitMetadataBytesFromDb == null) {
        // SELECT -> INSERT can fail due to races; callers must be prepared to retry.
        final int numRows = handle.createStatement(StringUtils.format(
                "INSERT INTO %s (dataSource, created_date, commit_metadata_payload, commit_metadata_sha1) "
                        + "VALUES (:dataSource, :created_date, :commit_metadata_payload, :commit_metadata_sha1)",
                dbTables.getDataSourceTable())).bind("dataSource", dataSource)
                .bind("created_date", DateTimes.nowUtc().toString())
                .bind("commit_metadata_payload", newCommitMetadataBytes)
                .bind("commit_metadata_sha1", newCommitMetadataSha1).execute();

        retVal = numRows == 1 ? DataSourceMetadataUpdateResult.SUCCESS
                : DataSourceMetadataUpdateResult.TRY_AGAIN;
    } else {
        // Expecting a particular old metadata; use the SHA1 in a compare-and-swap UPDATE
        final int numRows = handle.createStatement(StringUtils.format(
                "UPDATE %s SET " + "commit_metadata_payload = :new_commit_metadata_payload, "
                        + "commit_metadata_sha1 = :new_commit_metadata_sha1 "
                        + "WHERE dataSource = :dataSource AND commit_metadata_sha1 = :old_commit_metadata_sha1",
                dbTables.getDataSourceTable())).bind("dataSource", dataSource)
                .bind("old_commit_metadata_sha1", oldCommitMetadataSha1FromDb)
                .bind("new_commit_metadata_payload", newCommitMetadataBytes)
                .bind("new_commit_metadata_sha1", newCommitMetadataSha1).execute();

        retVal = numRows == 1 ? DataSourceMetadataUpdateResult.SUCCESS
                : DataSourceMetadataUpdateResult.TRY_AGAIN;
    }

    if (retVal == DataSourceMetadataUpdateResult.SUCCESS) {
        log.info("Updated metadata from[%s] to[%s].", oldCommitMetadataFromDb, newCommitMetadata);
    } else {
        log.info("Not updating metadata, compare-and-swap failure.");
    }

    return retVal;
}

From source file:org.apache.druid.metadata.IndexerSQLMetadataStorageCoordinator.java

@Override
public boolean resetDataSourceMetadata(final String dataSource, final DataSourceMetadata dataSourceMetadata)
        throws IOException {
    final byte[] newCommitMetadataBytes = jsonMapper.writeValueAsBytes(dataSourceMetadata);
    final String newCommitMetadataSha1 = BaseEncoding.base16()
            .encode(Hashing.sha1().hashBytes(newCommitMetadataBytes).asBytes());

    return connector.retryWithHandle(new HandleCallback<Boolean>() {
        @Override/*from   ww w  .  j a v a2s .  c  om*/
        public Boolean withHandle(Handle handle) {
            final int numRows = handle
                    .createStatement(StringUtils.format(
                            "UPDATE %s SET " + "commit_metadata_payload = :new_commit_metadata_payload, "
                                    + "commit_metadata_sha1 = :new_commit_metadata_sha1 "
                                    + "WHERE dataSource = :dataSource",
                            dbTables.getDataSourceTable()))
                    .bind("dataSource", dataSource).bind("new_commit_metadata_payload", newCommitMetadataBytes)
                    .bind("new_commit_metadata_sha1", newCommitMetadataSha1).execute();
            return numRows == 1;
        }
    });
}

From source file:org.apache.druid.metadata.IndexerSQLMetadataStorageCoordinator.java

@Override
public boolean insertDataSourceMetadata(String dataSource, DataSourceMetadata metadata) {
    return 1 == connector.getDBI().inTransaction((handle, status) -> handle.createStatement(StringUtils.format(
            "INSERT INTO %s (dataSource, created_date, commit_metadata_payload, commit_metadata_sha1) VALUES"
                    + " (:dataSource, :created_date, :commit_metadata_payload, :commit_metadata_sha1)",
            dbTables.getDataSourceTable())).bind("dataSource", dataSource)
            .bind("created_date", DateTimes.nowUtc().toString())
            .bind("commit_metadata_payload", jsonMapper.writeValueAsBytes(metadata))
            .bind("commit_metadata_sha1",
                    BaseEncoding.base16()
                            .encode(Hashing.sha1().hashBytes(jsonMapper.writeValueAsBytes(metadata)).asBytes()))
            .execute());/*from www.  j  av  a  2  s  . com*/
}

From source file:com.facebook.buck.apple.ProjectGenerator.java

private HashCode getHeaderSymlinkTreeHashCode(ImmutableSortedMap<Path, Path> contents) {
    Hasher hasher = Hashing.sha1().newHasher();
    hasher.putBytes(BuckVersion.getVersion().getBytes(Charsets.UTF_8));
    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);/*from  ww  w.j  a  v  a  2 s. c om*/
        hasher.putBytes(key);
        hasher.putInt(value.length);
        hasher.putBytes(value);
    }
    return hasher.hash();
}

From source file:com.facebook.buck.apple.project_generator.ProjectGenerator.java

private HashCode getHeaderSymlinkTreeHashCode(ImmutableSortedMap<Path, Path> contents,
        boolean shouldCreateHeadersSymlinks) {
    Hasher hasher = Hashing.sha1().newHasher();
    hasher.putBytes(BuckVersion.getVersion().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);/*w  w w .j a  v a2  s  .  co  m*/
    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();
}

From source file:com.facebook.buck.apple.project_generator.ProjectGenerator.java

private Path getPathToHeaderSymlinkTree(TargetNode<? extends CxxLibraryDescription.Arg, ?> targetNode,
        HeaderVisibility headerVisibility) {
    String hashedPath = BaseEncoding.base64Url().omitPadding()
            .encode(Hashing.sha1()
                    .hashString(targetNode.getBuildTarget().getUnflavoredBuildTarget().getFullyQualifiedName(),
                            Charsets.UTF_8)
                    .asBytes());//w w w  .j a v  a  2 s.  c  o  m
    return projectFilesystem.getBuckPaths().getGenDir().resolve("_project")
            .resolve(hashedPath + AppleHeaderVisibilities.getHeaderSymlinkTreeSuffix(headerVisibility));
}

From source file:com.facebook.buck.features.apple.project.ProjectGenerator.java

private HashCode getHeaderSymlinkTreeHashCode(ImmutableSortedMap<Path, Path> contents,
        Optional<String> moduleName, 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);//from   www.  j  av a 2 s .  c  o  m
    String hmapState = shouldCreateHeaderMap ? "hmap-enabled" : "hmap-disabled";
    byte[] hmapStateValue = hmapState.getBytes(Charsets.UTF_8);
    hasher.putInt(hmapStateValue.length);
    hasher.putBytes(hmapStateValue);
    if (moduleName.isPresent()) {
        byte[] moduleNameValue = moduleName.get().getBytes(Charsets.UTF_8);
        hasher.putInt(moduleNameValue.length);
        hasher.putBytes(moduleNameValue);
    }
    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();
}