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

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

Introduction

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

Prototype

public static HashFunction murmur3_32() 

Source Link

Document

Returns a hash function implementing the <a href="http://smhasher.googlecode.com/svn/trunk/MurmurHash3.cpp"> 32-bit murmur3 algorithm, x86 variant</a> (little-endian variant), using a seed value of zero.

Usage

From source file:com.dcits.govsbu.southernbase.baseproject2.modules.utils.Digests.java

/**
 * murmur32
 */
public static int murmur32(String input) {
    return Hashing.murmur3_32().hashString(input, Charsets.UTF8).asInt();
}

From source file:io.atomix.primitive.partition.impl.HashBasedPrimaryElection.java

/**
 * Recomputes the current term.//from   ww  w .ja  v a2s . co  m
 */
private synchronized void recomputeTerm(PartitionGroupMembership membership) {
    if (membership == null) {
        return;
    }

    // Create a list of candidates based on the availability of members in the group.
    List<GroupMember> candidates = new ArrayList<>();
    for (MemberId memberId : membership.members()) {
        Member member = clusterMembershipService.getMember(memberId);
        if (member != null && member.isReachable()) {
            candidates.add(new GroupMember(memberId, MemberGroupId.from(memberId.id())));
        }
    }

    // Sort the candidates by a hash of their member ID.
    candidates.sort((a, b) -> {
        int aoffset = Hashing.murmur3_32().hashString(a.memberId().id(), StandardCharsets.UTF_8).asInt()
                % partitionId.id();
        int boffset = Hashing.murmur3_32().hashString(b.memberId().id(), StandardCharsets.UTF_8).asInt()
                % partitionId.id();
        return aoffset - boffset;
    });

    // Store the current term in a local variable avoid repeated volatile reads.
    PrimaryTerm currentTerm = this.currentTerm;

    // Compute the primary from the sorted candidates list.
    GroupMember primary = candidates.isEmpty() ? null : candidates.get(0);

    // Remove the primary from the candidates list.
    candidates = candidates.isEmpty() ? Collections.emptyList() : candidates.subList(1, candidates.size());

    // If the primary has changed, increment the term. Otherwise, use the current term from the replicated counter.
    long term = currentTerm != null && Objects.equals(currentTerm.primary(), primary)
            && Objects.equals(currentTerm.candidates(), candidates) ? currentTerm() : incrementTerm();

    // Create the new primary term. If the term has changed update the term and trigger an event.
    PrimaryTerm newTerm = new PrimaryTerm(term, primary, candidates);
    if (!Objects.equals(currentTerm, newTerm)) {
        this.currentTerm = newTerm;
        LOGGER.debug("{} - Recomputed term for partition {}: {}",
                clusterMembershipService.getLocalMember().id(), partitionId, newTerm);
        post(new PrimaryElectionEvent(PrimaryElectionEvent.Type.CHANGED, partitionId, newTerm));
        broadcastCounters();
    }
}

From source file:com.dcits.govsbu.southernbase.baseproject2.modules.utils.Digests.java

/**
 * murmur32/*from  ww w  .java2 s  .  co  m*/
 */
public static int murmur32(String input, Charset charset) {
    return Hashing.murmur3_32().hashString(input, charset).asInt();
}

From source file:org.jenkinsci.plugins.github_branch_source.Connector.java

/**
 * @param config url and creds id to be hashed
 *
 * @return unique id for folder name to create cache inside of base cache dir
 *//*from   w w w . j  a  v a2  s .co m*/
private static String hashed(GitHubServerConfig config) {
    return Hashing.murmur3_32().newHasher().putString(trimToEmpty(config.getApiUrl()))
            .putString(trimToEmpty(config.getCredentialsId())).hash().toString();
}

From source file:org.apache.accumulo.core.summary.Gatherer.java

/**
 * @param fileSelector/*  ww  w  .ja va 2 s. c  o  m*/
 *          only returns files that match this predicate
 * @return A map of the form : {@code map<tserver location, map<path, list<range>>} . The ranges
 *         associated with a file represent the tablets that use the file.
 */
private Map<String, Map<String, List<TRowRange>>> getFilesGroupedByLocation(Predicate<String> fileSelector) {

    Iterable<TabletMetadata> tmi = TabletsMetadata.builder().forTable(tableId).overlapping(startRow, endRow)
            .fetchFiles().fetchLocation().fetchLast().fetchPrev().build(ctx);

    // get a subset of files
    Map<String, List<TabletMetadata>> files = new HashMap<>();

    for (TabletMetadata tm : tmi) {
        for (String file : tm.getFiles()) {
            if (fileSelector.test(file)) {
                // TODO push this filtering to server side and possibly use batch scanner
                files.computeIfAbsent(file, s -> new ArrayList<>()).add(tm);
            }
        }
    }

    // group by location, then file

    Map<String, Map<String, List<TRowRange>>> locations = new HashMap<>();

    List<String> tservers = null;

    for (Entry<String, List<TabletMetadata>> entry : files.entrySet()) {

        String location = entry.getValue().stream().filter(tm -> tm.getLocation() != null) // filter
                // tablets
                // w/o a
                // location
                .map(tm -> tm.getLocation().getHostAndPort().toString()) // convert to host:port strings
                .min(String::compareTo) // find minimum host:port
                .orElse(entry.getValue().stream().filter(tm -> tm.getLast() != null) // if no locations,
                        // then look at last
                        // locations
                        .map(tm -> tm.getLast().getHostAndPort().toString()) // convert to host:port strings
                        .min(String::compareTo).orElse(null)); // find minimum last location or return null

        if (location == null) {
            if (tservers == null) {
                tservers = ctx.instanceOperations().getTabletServers();
                Collections.sort(tservers);
            }

            // When no location, the approach below will consistently choose the same tserver for the
            // same file (as long as the set of tservers is stable).
            int idx = Math.abs(Hashing.murmur3_32().hashString(entry.getKey(), UTF_8).asInt())
                    % tservers.size();
            location = tservers.get(idx);
        }

        // merge contiguous ranges
        List<Range> merged = Range
                .mergeOverlapping(Lists.transform(entry.getValue(), tm -> tm.getExtent().toDataRange()));
        List<TRowRange> ranges = merged.stream().map(r -> toClippedExtent(r).toThrift())
                .collect(Collectors.toList()); // clip ranges to queried range

        locations.computeIfAbsent(location, s -> new HashMap<>()).put(entry.getKey(), ranges);
    }

    return locations;
}

From source file:org.apache.fluo.recipes.map.CollisionFreeMap.java

/**
 * This method will retrieve the current value for key and any outstanding updates and combine
 * them using the configured {@link Combiner}. The result from the combiner is returned.
 *//*from w ww .  jav a2s  . c om*/
public V get(SnapshotBase tx, K key) {

    byte[] k = serializer.serialize(key);

    int hash = Hashing.murmur3_32().hashBytes(k).asInt();
    String bucketId = BucketUtil.genBucketId(Math.abs(hash % numBuckets), numBuckets);

    BytesBuilder rowBuilder = Bytes.newBuilder();
    rowBuilder.append(mapId).append(":u:").append(bucketId).append(":").append(k);

    ScannerConfiguration sc = new ScannerConfiguration();
    sc.setSpan(Span.prefix(rowBuilder.toBytes()));

    RowIterator iter = tx.get(sc);

    Iterator<V> ui;

    if (iter.hasNext()) {
        ui = Iterators.transform(iter, e -> deserVal(e.getValue().next().getValue()));
    } else {
        ui = Collections.<V>emptyList().iterator();
    }

    rowBuilder.setLength(mapId.length());
    rowBuilder.append(":d:").append(bucketId).append(":").append(k);

    Bytes dataRow = rowBuilder.toBytes();

    Bytes cv = tx.get(dataRow, DATA_COLUMN);

    if (!ui.hasNext()) {
        if (cv == null) {
            return null;
        } else {
            return deserVal(cv);
        }
    }

    return combiner.combine(key, concat(ui, cv)).orElse(null);
}

From source file:org.lightjason.agentspeak.language.CCommon.java

/**
 * returns the hasing function for term data
 *
 * @return hasher
 */
public static Hasher getTermHashing() {
    return Hashing.murmur3_32().newHasher();
}

From source file:org.apache.fluo.recipes.map.CollisionFreeMap.java

/**
 * Queues updates for a collision free map. These updates will be made by an Observer executing
 * another transaction. This method will not collide with other transaction queuing updates for
 * the same keys./*  w w w.  j a v  a 2  s  .  c  o  m*/
 *
 * @param tx This transaction will be used to make the updates.
 * @param updates The keys in the map should correspond to keys in the collision free map being
 *        updated. The values in the map will be queued for updating.
 */
public void update(TransactionBase tx, Map<K, V> updates) {
    Preconditions.checkState(numBuckets > 0, "Not initialized");

    Set<String> buckets = new HashSet<>();

    BytesBuilder rowBuilder = Bytes.newBuilder();
    rowBuilder.append(mapId).append(":u:");
    int prefixLength = rowBuilder.getLength();

    byte[] startTs = encSeq(tx.getStartTimestamp());

    for (Entry<K, V> entry : updates.entrySet()) {
        byte[] k = serializer.serialize(entry.getKey());
        int hash = Hashing.murmur3_32().hashBytes(k).asInt();
        String bucketId = BucketUtil.genBucketId(Math.abs(hash % numBuckets), numBuckets);

        // reset to the common row prefix
        rowBuilder.setLength(prefixLength);

        Bytes row = rowBuilder.append(bucketId).append(":").append(k).append(startTs).toBytes();
        Bytes val = Bytes.of(serializer.serialize(entry.getValue()));

        // TODO set if not exists would be comforting here.... but
        // collisions on bucketId+key+uuid should never occur
        tx.set(row, UPDATE_COL, val);

        buckets.add(bucketId);
    }

    for (String bucketId : buckets) {
        rowBuilder.setLength(prefixLength);
        rowBuilder.append(bucketId).append(":");

        Bytes row = rowBuilder.toBytes();

        tx.setWeakNotification(row, new Column("fluoRecipes", "cfm:" + mapId));
    }
}

From source file:com.addthis.meshy.MeshyServer.java

public boolean shouldBeConnector(String newUuid) {
    int myHash = Hashing.murmur3_32().hashUnencodedChars(getUUID()).asInt();
    int peerHash = Hashing.murmur3_32().hashUnencodedChars(newUuid).asInt();
    boolean useGreater = ((myHash ^ peerHash) & 0x1) == 0;

    int compareUuidStrings = getUUID().compareTo(newUuid);
    if (useGreater) {
        return compareUuidStrings > 0;
    } else {/*  w ww  .jav a 2s. c  o m*/
        return compareUuidStrings < 0;
    }
}

From source file:org.apache.beam.sdk.io.HadoopWriteFiles.java

private static <DestinationT> int hashDestination(DestinationT destination,
        Coder<DestinationT> destinationCoder) throws IOException {
    return Hashing.murmur3_32().hashBytes(CoderUtils.encodeToByteArray(destinationCoder, destination)).asInt();
}