Example usage for com.google.common.hash Hasher putInt

List of usage examples for com.google.common.hash Hasher putInt

Introduction

In this page you can find the example usage for com.google.common.hash Hasher putInt.

Prototype

@Override
    Hasher putInt(int i);

Source Link

Usage

From source file:com.facebook.buck.hashing.StringHashing.java

/**
 * Encodes the string in UTF-8, then hashes the length of the encoded
 * UTF-8 bytes followed by the bytes themselves.
 *
 * Useful to ensure hash codes are different when multiple strings
 * are hashed in order ("foo" then "bar" should hash differently from "foobar").
 */// w  w  w. ja v a 2  s.co  m
public static void hashStringAndLength(Hasher hasher, String string) {
    byte[] utf8Bytes = string.getBytes(StandardCharsets.UTF_8);
    hasher.putInt(utf8Bytes.length);
    hasher.putBytes(utf8Bytes);
}

From source file:org.gradle.internal.classloader.DefaultHashingClassLoaderFactory.java

private static void addToHash(Hasher hasher, Set<String> items) {
    int count = items.size();
    hasher.putInt(count);
    if (count == 0) {
        return;/* ww w  .  j  a  v  a 2s .  c  o  m*/
    }
    String[] sortedItems = items.toArray(new String[count]);
    Arrays.sort(sortedItems);
    for (String item : sortedItems) {
        hasher.putInt(0);
        hasher.putString(item, Charsets.UTF_8);
    }
}

From source file:com.facebook.buck.util.hashing.StringHashing.java

/**
 * Encodes the length of the string in UTF-16 code units, then the UTF-16 code units of the
 * string./*from  w w  w  .  j a  v a 2  s .  c o  m*/
 *
 * <p>Useful to ensure hash codes are different when multiple strings are hashed in order ("foo"
 * then "bar" should hash differently from "foobar").
 */
public static void hashStringAndLength(Hasher hasher, String string) {
    // We used to hash the UTF-8 bytes of the string, but it takes
    // a lot of unnecessary CPU and memory to do so.
    hasher.putInt(string.length());
    hasher.putUnencodedChars(string);
}

From source file:de.nx42.maps4cim.map.texture.osm.OsmHash.java

protected static String getQueryHashEntities(List<EntityDef> entities) {
    Hasher h = hf.newHasher();

    // add query/*  w w  w . j a v a2  s .  com*/
    for (EntityDef def : entities) {
        h.putInt(def.hashCode());
    }

    // get hash, cap length
    byte[] hash = h.hash().asBytes();
    if (hash.length > 4) {
        hash = Arrays.copyOfRange(hash, 0, 3);
    }

    // return hash, max 4 byte (6 chars)
    return Base64.encodeBase64URLSafeString(hash);
}

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  ww  .j a  va2 s . c o m*/
        @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:nextflow.util.CacheHelper.java

public static Hasher hasher(Hasher hasher, Object value, HashMode mode) {

    if (value == null)
        return hasher;

    if (value instanceof Boolean)
        return hasher.putBoolean((Boolean) value);

    if (value instanceof Short)
        return hasher.putShort((Short) value);

    if (value instanceof Integer)
        return hasher.putInt((Integer) value);

    if (value instanceof Long)
        return hasher.putLong((Long) value);

    if (value instanceof Float)
        return hasher.putFloat((Float) value);

    if (value instanceof Double)
        return hasher.putDouble((Double) value);

    if (value instanceof Number)
        // reduce all other number types (BigInteger, BigDecimal, AtomicXxx, etc) to string equivalent
        return hasher.putUnencodedChars(value.toString());

    if (value instanceof Character)
        return hasher.putChar((Character) value);

    if (value instanceof CharSequence)
        return hasher.putUnencodedChars((CharSequence) value);

    if (value instanceof Byte)
        return hasher.putByte((Byte) value);

    if (value instanceof byte[])
        return hasher.putBytes((byte[]) value);

    if (value instanceof Object[]) {
        for (Object item : ((Object[]) value))
            hasher = CacheHelper.hasher(hasher, item, mode);
        return hasher;
    }/*w  w w .j av  a  2  s  .c  o m*/

    if (value instanceof Map) {
        // note: should map be order invariant as Set ?
        for (Object item : ((Map) value).values())
            hasher = CacheHelper.hasher(hasher, item, mode);
        return hasher;
    }

    if (value instanceof Bag || value instanceof Set)
        return hashUnorderedCollection(hasher, (Collection) value, mode);

    if (value instanceof Collection) {
        for (Object item : ((Collection) value))
            hasher = CacheHelper.hasher(hasher, item, mode);
        return hasher;
    }

    if (value instanceof FileHolder)
        return CacheHelper.hasher(hasher, ((FileHolder) value).getSourceObj(), mode);

    if (value instanceof Path)
        return hashFile(hasher, (Path) value, mode);

    if (value instanceof java.io.File)
        return hashFile(hasher, (java.io.File) value, mode);

    if (value instanceof UUID) {
        UUID uuid = (UUID) value;
        return hasher.putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits());
    }

    log.debug("[WARN] Unknown hashing type: {} -- {}", value.getClass(), value);
    return hasher.putInt(value.hashCode());
}

From source file:org.danilopianini.lang.HashUtils.java

private static void populateHasher(final Object data, final Hasher h) {
    if (data != null) {
        if (data instanceof Number) {
            final Number d = (Number) data;
            if (data instanceof Integer) {
                h.putInt(d.intValue());
            } else if (data instanceof Double) {
                h.putDouble(d.doubleValue());
            } else if (data instanceof Long) {
                h.putLong(d.longValue());
            } else if (data instanceof Float) {
                h.putFloat(d.floatValue());
            } else if (data instanceof Byte) {
                h.putByte(d.byteValue());
            } else if (data instanceof Short) {
                h.putShort(d.shortValue());
            } else {
                h.putInt(data.hashCode());
            }/*from ww  w .j a v  a  2s .c  om*/
        } else if (data instanceof CharSequence) {
            h.putString((CharSequence) data, CHARSET);
        } else if (data.getClass().isArray()) {
            final int size = Array.getLength(data);
            for (int i = 0; i < size; i++) {
                populateHasher(Array.get(data, i), h);
            }
        } else if (data instanceof Iterable) {
            for (final Object o : (Iterable<?>) data) {
                populateHasher(o, h);
            }
        } else {
            h.putInt(data.hashCode());
        }
    }
}

From source file:org.eclipse.epp.internal.logging.aeri.ui.model.Reports.java

public static String traceIdentityHash(ErrorReport report) {
    final Hasher hasher = Hashing.murmur3_128().newHasher();
    visit(report, new ModelSwitch<Hasher>() {

        @Override//www.j a v a2 s  .  c  om
        public Hasher caseStackTraceElement(StackTraceElement element) {
            hasher.putString(element.getClassName(), UTF_8);
            hasher.putString(element.getMethodName(), UTF_8);
            hasher.putInt(element.getLineNumber());
            return null;
        }
    });
    String hash = hasher.hash().toString();
    return hash;
}

From source file:com.spotify.heroic.metric.Point.java

@Override
public void hash(final Hasher hasher) {
    hasher.putInt(MetricType.POINT.ordinal());
    hasher.putDouble(value);
}

From source file:com.spotify.heroic.metric.Payload.java

@Override
public void hash(final Hasher hasher) {
    hasher.putInt(MetricType.EVENT.ordinal());
    hasher.putBytes(state);
}