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.omnigon.aem.common.utils.ResourceUtils.java

/**
 *
 * @param path -//from   w  ww .  ja v  a2 s.co m
 * @return identifier based on path
 */
public static String generateId(String path) {
    if (org.apache.commons.lang3.StringUtils.isNotBlank(path)) {
        return Hashing.murmur3_32().hashString(path, Charsets.UTF_8).toString();
    } else {
        return null;
    }
}

From source file:org.apache.jackrabbit.oak.plugins.segment.standby.codec.SegmentDecoder.java

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }/*  www  . j  av  a2  s  .  c om*/
    int len = frame.readInt();
    byte type = frame.readByte();
    long msb = frame.readLong();
    long lsb = frame.readLong();
    long hash = frame.readLong();
    byte[] segment = new byte[len - 25];
    frame.getBytes(29, segment);
    Hasher hasher = Hashing.murmur3_32().newHasher();
    long check = hasher.putBytes(segment).hash().padToLong();
    if (hash == check) {
        SegmentId id = new SegmentId(store.getTracker(), msb, lsb);
        Segment s = new Segment(store.getTracker(), id, ByteBuffer.wrap(segment));
        log.debug("received type {} with id {} and size {}", type, id, s.size());
        return s;
    }
    log.debug("received corrupted segment {}, ignoring", new UUID(msb, lsb));
    return null;

}

From source file:org.dcache.poolmanager.RendezvousPoolManagerHandler.java

private static int hash(PnfsId pnfsId, CellAddressCore address) {
    return Hashing.murmur3_32().newHasher().putObject(pnfsId, PnfsId.funnel())
            .putString(address.toString(), US_ASCII).hash().asInt();
}

From source file:org.apache.bookkeeper.metastore.Value.java

@Override
public int hashCode() {
    HashFunction hf = Hashing.murmur3_32();
    Hasher hc = hf.newHasher();//from   ww w.j a v  a2s.co m
    for (String key : fields.keySet()) {
        hc.putString(key);
    }
    return hc.hash().asInt();
}

From source file:org.apache.fluo.recipes.export.ExportQueue.java

public void addAll(TransactionBase tx, Iterator<Export<K, V>> exports) {

    Set<Integer> bucketsNotified = new HashSet<>();
    while (exports.hasNext()) {
        Export<K, V> export = exports.next();

        byte[] k = serializer.serialize(export.getKey());
        byte[] v = serializer.serialize(export.getValue());

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

        ExportBucket bucket = new ExportBucket(tx, queueId, bucketId, numBuckets);
        bucket.add(tx.getStartTimestamp(), k, v);

        if (!bucketsNotified.contains(bucketId)) {
            bucket.notifyExportObserver();
            bucketsNotified.add(bucketId);
        }/*w  ww  .j a v a  2 s  .  co m*/
    }
}

From source file:org.apache.fluo.recipes.data.RowHasher.java

private static String genHash(Bytes row) {
    int hash = Hashing.murmur3_32().hashBytes(row.toArray()).asInt();
    hash = hash & 0x7fffffff;//from   w  w w . ja  v  a 2  s. c o m
    // base 36 gives a lot more bins in 4 bytes than hex, but it is still human readable which is
    // nice for debugging.
    String hashString = Strings.padStart(Integer.toString(hash, Character.MAX_RADIX), HASH_LEN, '0');
    hashString = hashString.substring(hashString.length() - HASH_LEN);

    return hashString;
}

From source file:org.apache.accumulo.core.sample.AbstractHashSampler.java

/**
 * Subclasses with options should override this method and call {@code super.init(config)}.
 *//*from   w w w. j  a  va 2s  .c o  m*/

@Override
public void init(SamplerConfiguration config) {
    String hasherOpt = config.getOptions().get("hasher");
    String modulusOpt = config.getOptions().get("modulus");

    requireNonNull(hasherOpt, "Hasher not specified");
    requireNonNull(modulusOpt, "Modulus not specified");

    for (String option : config.getOptions().keySet()) {
        checkArgument(isValidOption(option), "Unknown option : %s", option);
    }

    switch (hasherOpt) {
    case "murmur3_32":
        hashFunction = Hashing.murmur3_32();
        break;
    case "md5":
        hashFunction = Hashing.md5();
        break;
    case "sha1":
        hashFunction = Hashing.sha1();
        break;
    default:
        throw new IllegalArgumentException("Uknown hahser " + hasherOpt);
    }

    modulus = Integer.parseInt(modulusOpt);
}

From source file:com.globo.galeb.consistenthash.HashAlgorithm.java

/**
 * Calc Hash./*  w  w  w.  j  ava 2  s . com*/
 *
 * @param key the key
 * @return int hash
 */
public HashAlgorithm hash(Object key) {
    HashFunction hashAlgorithm;
    switch (hashType) {
    case MD5:
        hashAlgorithm = Hashing.md5();
        break;
    case MURMUR3_32:
        hashAlgorithm = Hashing.murmur3_32();
        break;
    case SHA256:
        hashAlgorithm = Hashing.sha256();
        break;
    case SIP24:
        hashAlgorithm = Hashing.sipHash24();
        break;
    default:
        hashAlgorithm = Hashing.sipHash24();
        break;
    }
    if (key instanceof String) {
        hashCode = hashAlgorithm.newHasher().putString((String) key, Charsets.UTF_8).hash();
    } else if (key instanceof Long) {
        hashCode = hashAlgorithm.newHasher().putLong((Long) key).hash();
    } else {
        hashCode = hashAlgorithm.newHasher().hash();
    }
    return this;
}

From source file:org.apache.kylin.cube.util.CubingUtils.java

public static Map<Long, HLLCounter> sampling(CubeDesc cubeDesc, IJoinedFlatTableDesc flatDescIn,
        Iterable<List<String>> streams) {
    final CubeJoinedFlatTableEnrich flatDesc = new CubeJoinedFlatTableEnrich(flatDescIn, cubeDesc);
    final int rowkeyLength = cubeDesc.getRowkey().getRowKeyColumns().length;
    final List<Long> allCuboidIds = new CuboidScheduler(cubeDesc).getAllCuboidIds();
    final long baseCuboidId = Cuboid.getBaseCuboidId(cubeDesc);
    final Map<Long, Integer[]> allCuboidsBitSet = Maps.newHashMap();

    Lists.transform(allCuboidIds, new Function<Long, Integer[]>() {
        @Nullable//from w w  w.  ja v a2s . c  om
        @Override
        public Integer[] apply(@Nullable Long cuboidId) {
            Integer[] result = new Integer[Long.bitCount(cuboidId)];

            long mask = Long.highestOneBit(baseCuboidId);
            int position = 0;
            for (int i = 0; i < rowkeyLength; i++) {
                if ((mask & cuboidId) > 0) {
                    result[position] = i;
                    position++;
                }
                mask = mask >> 1;
            }
            return result;
        }
    });
    final Map<Long, HLLCounter> result = Maps.newHashMapWithExpectedSize(allCuboidIds.size());
    for (Long cuboidId : allCuboidIds) {
        result.put(cuboidId, new HLLCounter(cubeDesc.getConfig().getCubeStatsHLLPrecision()));
        Integer[] cuboidBitSet = new Integer[Long.bitCount(cuboidId)];

        long mask = Long.highestOneBit(baseCuboidId);
        int position = 0;
        for (int i = 0; i < rowkeyLength; i++) {
            if ((mask & cuboidId) > 0) {
                cuboidBitSet[position] = i;
                position++;
            }
            mask = mask >> 1;
        }
        allCuboidsBitSet.put(cuboidId, cuboidBitSet);
    }

    HashFunction hf = Hashing.murmur3_32();
    ByteArray[] row_hashcodes = new ByteArray[rowkeyLength];
    for (int i = 0; i < rowkeyLength; i++) {
        row_hashcodes[i] = new ByteArray();
    }
    for (List<String> row : streams) {
        //generate hash for each row key column
        for (int i = 0; i < rowkeyLength; i++) {
            Hasher hc = hf.newHasher();
            final String cell = row.get(flatDesc.getRowKeyColumnIndexes()[i]);
            if (cell != null) {
                row_hashcodes[i].set(hc.putString(cell).hash().asBytes());
            } else {
                row_hashcodes[i].set(hc.putInt(0).hash().asBytes());
            }
        }

        for (Map.Entry<Long, HLLCounter> longHyperLogLogPlusCounterNewEntry : result.entrySet()) {
            Long cuboidId = longHyperLogLogPlusCounterNewEntry.getKey();
            HLLCounter counter = longHyperLogLogPlusCounterNewEntry.getValue();
            Hasher hc = hf.newHasher();
            final Integer[] cuboidBitSet = allCuboidsBitSet.get(cuboidId);
            for (int position = 0; position < cuboidBitSet.length; position++) {
                hc.putBytes(row_hashcodes[cuboidBitSet[position]].array());
            }
            counter.add(hc.hash().asBytes());
        }
    }
    return result;
}

From source file:org.apache.accumulo.core.client.sample.AbstractHashSampler.java

/**
 * Subclasses with options should override this method and call {@code super.init(config)}.
 *//*  w  w  w . j a v a 2 s. c o  m*/
@SuppressFBWarnings(value = "UNSAFE_HASH_EQUALS", justification = "these hashes don't protect any secrets, just used for binning")
@Override
public void init(SamplerConfiguration config) {
    String hasherOpt = config.getOptions().get("hasher");
    String modulusOpt = config.getOptions().get("modulus");

    requireNonNull(hasherOpt, "Hasher not specified");
    requireNonNull(modulusOpt, "Modulus not specified");

    for (String option : config.getOptions().keySet()) {
        checkArgument(isValidOption(option), "Unknown option : %s", option);
    }

    switch (hasherOpt) {
    case "murmur3_32":
        hashFunction = Hashing.murmur3_32();
        break;
    case "md5":
        @SuppressWarnings("deprecation")
        HashFunction deprecatedMd5 = Hashing.md5();
        hashFunction = deprecatedMd5;
        break;
    case "sha1":
        @SuppressWarnings("deprecation")
        HashFunction deprecatedSha1 = Hashing.sha1();
        hashFunction = deprecatedSha1;
        break;
    default:
        throw new IllegalArgumentException("Unknown hahser " + hasherOpt);
    }

    modulus = Integer.parseInt(modulusOpt);
}