Package com.google.common.hash

Examples of com.google.common.hash.Hasher


   * @since 12.0
   */
  public static HashCode hash(
      InputSupplier<? extends InputStream> supplier, HashFunction hashFunction)
      throws IOException {
    Hasher hasher = hashFunction.newHasher();
    copy(supplier, Funnels.asOutputStream(hasher));
    return hasher.hash();
  }
View Full Code Here


    @Test
    public void testZoneKeyData()
            throws Exception
    {
        Hasher hasher = Hashing.murmur3_128().newHasher();

        SortedSet<TimeZoneKey> timeZoneKeysSortedByKey = ImmutableSortedSet.copyOf(new Comparator<TimeZoneKey>()
        {
            @Override
            public int compare(TimeZoneKey left, TimeZoneKey right)
            {
                return Short.compare(left.getKey(), right.getKey());
            }
        }, TimeZoneKey.getTimeZoneKeys());

        for (TimeZoneKey timeZoneKey : timeZoneKeysSortedByKey) {
            hasher.putShort(timeZoneKey.getKey());
            hasher.putString(timeZoneKey.getId(), StandardCharsets.UTF_8);
        }
        // Zone file should not (normally) be changed, so let's make is more difficult
        assertEquals(hasher.hash().asLong(), 7273867108989103529L, "zone-index.properties file contents changed!");
    }
View Full Code Here

    }

    @Override
    public int hashCode() {
        HashFunction hf = Hashing.murmur3_32();
        Hasher hc = hf.newHasher();
        for (String key : fields.keySet()) {
            hc.putString(key);
        }
        return hc.hash().asInt();
    }
View Full Code Here

  private static final HashFunction hashFn = Hashing.murmur3_128();
  public static final char SEPARATOR = '\u0001';

  protected static void hashRow(List<DimensionSelector> selectorList, HyperLogLogCollector collector)
  {
    final Hasher hasher = hashFn.newHasher();
    for (int k = 0; k < selectorList.size(); ++k) {
      if (k != 0) {
        hasher.putByte((byte) 0);
      }
      final DimensionSelector selector = selectorList.get(k);
      final IndexedInts row = selector.getRow();
      final int size = row.size();
      // nothing to add to hasher if size == 0, only handle size == 1 and size != 0 cases.
      if (size == 1) {
        final String value = selector.lookupName(row.get(0));
        hasher.putUnencodedChars(value != null ? value : NULL_STRING);
      } else if (size != 0) {
        final String[] values = new String[size];
        for (int i = 0; i < size; ++i) {
          final String value = selector.lookupName(row.get(i));
          values[i] = value != null ? value : NULL_STRING;
        }
        // Values need to be sorted to ensure consistent multi-value ordering across different segments
        Arrays.sort(values);
        for (int i = 0; i < size; ++i) {
          if (i != 0) {
            hasher.putChar(SEPARATOR);
          }
          hasher.putUnencodedChars(values[i]);
        }
      }
    }
    collector.add(hasher.hash().asBytes());
  }
View Full Code Here

    }

    @Override
    public int hashCode() {
        HashFunction hf = Hashing.goodFastHash(32);
        Hasher h = hf.newHasher();
        h.putInt(slots.size());
        for (int i=0; i<slots.size(); i++) {
            h.putInt(slots.get(i).size());
            for (int j=0; j<slots.size(); j++) {
                h.putBytes(slots.get(i).get(j).getLowerRange());
                h.putBytes(slots.get(i).get(j).getUpperRange());
            }
        }
        return h.hash().asInt();
    }
View Full Code Here

    if (buf.length() == 0) {
      return null;
    }

    LOG.debug("Creating jar file for coprocessor classes: " + buf.toString());
    final Hasher hasher = Hashing.md5().newHasher();
    final byte[] buffer = new byte[COPY_BUFFER_SIZE];

    final Map<String, URL> dependentClasses = new HashMap<String, URL>();
    for (Class<? extends Coprocessor> clz : classes) {
      Dependencies.findClassDependencies(clz.getClassLoader(), new Dependencies.ClassAcceptor() {
        @Override
        public boolean accept(String className, final URL classUrl, URL classPathUrl) {
          // Assuming the endpoint and protocol class doesn't have dependencies
          // other than those comes with HBase and Java.
          if (className.startsWith("co.cask")) {
            if (!dependentClasses.containsKey(className)) {
              dependentClasses.put(className, classUrl);
            }
            return true;
          }
          return false;
        }
      }, clz.getName());
    }

    if (!dependentClasses.isEmpty()) {
      LOG.debug("Adding " + dependentClasses.size() + " classes to jar");
      File jarFile = File.createTempFile(filePrefix, ".jar");
      try {
        JarOutputStream jarOutput = null;
        try {
          jarOutput = new JarOutputStream(new FileOutputStream(jarFile));
          for (Map.Entry<String, URL> entry : dependentClasses.entrySet()) {
            try {
              jarOutput.putNextEntry(new JarEntry(entry.getKey().replace('.', File.separatorChar) + ".class"));
              InputStream inputStream = entry.getValue().openStream();

              try {
                int len = inputStream.read(buffer);
                while (len >= 0) {
                  hasher.putBytes(buffer, 0, len);
                  jarOutput.write(buffer, 0, len);
                  len = inputStream.read(buffer);
                }
              } finally {
                inputStream.close();
              }
            } catch (IOException e) {
              LOG.info("Error writing to jar", e);
              throw Throwables.propagate(e);
            }
          }
        } finally {
          if (jarOutput != null) {
            jarOutput.close();
          }
        }

        // Copy jar file into HDFS
        // Target path is the jarDir + jarMD5.jar
        final Location targetPath = jarDir.append("coprocessor" + hasher.hash().toString() + ".jar");

        // If the file exists and having same since, assume the file doesn't changed
        if (targetPath.exists() && targetPath.length() == jarFile.length()) {
          return targetPath;
        }
View Full Code Here

    }

    @Override
    public int hashCode() {
        HashFunction hf = Hashing.goodFastHash(32);
        Hasher h = hf.newHasher();
        h.putInt(slots.size());
        for (int i=0; i<slots.size(); i++) {
            h.putInt(slots.get(i).size());
            for (int j=0; j<slots.size(); j++) {
                h.putBytes(slots.get(i).get(j).getLowerRange());
                h.putBytes(slots.get(i).get(j).getUpperRange());
            }
        }
        return h.hash().asInt();
    }
View Full Code Here

  //

  /** {@inheritDoc} */
  @Override
  public synchronized String getFingerprint() {
    Hasher hash = Hashing.sha256().newHasher();
    hash.putString(profile.getFingerprint());

    hash.putString("Posts(");
    for (Post post : getPosts()) {
      hash.putString("Post(").putString(post.getId()).putString(")");
    }
    hash.putString(")");

    List<PostReply> replies = new ArrayList<PostReply>(getReplies());
    Collections.sort(replies, Reply.TIME_COMPARATOR);
    hash.putString("Replies(");
    for (PostReply reply : replies) {
      hash.putString("Reply(").putString(reply.getId()).putString(")");
    }
    hash.putString(")");

    List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
    Collections.sort(likedPostIds);
    hash.putString("LikedPosts(");
    for (String likedPostId : likedPostIds) {
      hash.putString("Post(").putString(likedPostId).putString(")");
    }
    hash.putString(")");

    List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
    Collections.sort(likedReplyIds);
    hash.putString("LikedReplies(");
    for (String likedReplyId : likedReplyIds) {
      hash.putString("Reply(").putString(likedReplyId).putString(")");
    }
    hash.putString(")");

    hash.putString("Albums(");
    for (Album album : rootAlbum.getAlbums()) {
      if (!Album.NOT_EMPTY.apply(album)) {
        continue;
      }
      hash.putString(album.getFingerprint());
    }
    hash.putString(")");

    return hash.hash().toString();
  }
View Full Code Here

  // FINGERPRINTABLE METHODS
  //

  @Override
  public String getFingerprint() {
    Hasher hash = Hashing.sha256().newHasher();
    hash.putString("Image(");
    hash.putString("ID(").putString(id).putString(")");
    hash.putString("Title(").putString(title).putString(")");
    hash.putString("Description(").putString(description).putString(")");
    hash.putString(")");
    return hash.hash().toString();
  }
View Full Code Here

  /**
   * {@inheritDoc}
   */
  @Override
  public String getFingerprint() {
    Hasher hash = Hashing.sha256().newHasher();
    hash.putString("Profile(");
    if (firstName != null) {
      hash.putString("FirstName(").putString(firstName).putString(")");
    }
    if (middleName != null) {
      hash.putString("MiddleName(").putString(middleName).putString(")");
    }
    if (lastName != null) {
      hash.putString("LastName(").putString(lastName).putString(")");
    }
    if (birthDay != null) {
      hash.putString("BirthDay(").putInt(birthDay).putString(")");
    }
    if (birthMonth != null) {
      hash.putString("BirthMonth(").putInt(birthMonth).putString(")");
    }
    if (birthYear != null) {
      hash.putString("BirthYear(").putInt(birthYear).putString(")");
    }
    if (avatar != null) {
      hash.putString("Avatar(").putString(avatar).putString(")");
    }
    hash.putString("ContactInformation(");
    for (Field field : fields) {
      hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
    }
    hash.putString(")");
    hash.putString(")");

    return hash.hash().toString();
  }
View Full Code Here

TOP

Related Classes of com.google.common.hash.Hasher

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.