@Test
 public void testModuloMethod() throws Exception {
   HashFunction h = new HashFunction.ModuloMethod();
   for (int i = 0; i < 10; i++) {
     assertEquals(i, h.compute(i, 10));
   }
 }
Beispiel #2
0
 public int hashCode() {
   int hash = this.bits;
   for (HashFunction function : this.functions) {
     hash ^= function.hashCode();
   }
   return hash;
 }
 @Test
 public void updateFile() throws Exception {
   final Method method = _utilsType.getDeclaredMethod(_methodName, File.class);
   final HashFunction function = newInstanceOf(_constructor);
   final File file = testFileOf("testFile1.txt");
   assertThat(method.invoke(null, file), is((Object) function.update(file)));
 }
 public void testGoodFastHash() {
   for (int i = 1; i < 200; i += 17) {
     HashFunction hasher = Hashing.goodFastHash(i);
     assertTrue(hasher.bits() >= i);
     HashTestUtils.assertInvariants(hasher);
   }
 }
 @Test
 public void testMultiplicationMethod() throws Exception {
   HashFunction h = new HashFunction.MutiplicationMethod();
   for (int i = 0; i < 10000; i++) {
     int p = h.compute(i, 10);
     assertTrue(0 <= p && p < 10);
   }
 }
 @Test
 public void updateByteRanges() throws Exception {
   final Method method =
       _utilsType.getDeclaredMethod(_methodName, byte[].class, int.class, int.class);
   final HashFunction function = newInstanceOf(_constructor);
   assertThat(
       method.invoke(null, TEST_BYTES_1, 1, 4), is((Object) function.update(TEST_BYTES_1, 1, 4)));
 }
 @Test
 public void updateStringWithCharset() throws Exception {
   final Method method = _utilsType.getDeclaredMethod(_methodName, String.class, Charset.class);
   final HashFunction function = newInstanceOf(_constructor);
   assertThat(
       method.invoke(null, TEST_CONTENT_1, CHARSET),
       is((Object) function.update(TEST_CONTENT_1, CHARSET)));
 }
Beispiel #8
0
 ConcatenatedHashFunction(HashFunction... functions) {
   super();
   int bitSum = 0;
   for (HashFunction function : functions) {
     bitSum += function.bits();
   }
   this.bits = bitSum;
 }
 @Test
 public void updateInputStream() throws Exception {
   final Method method = _utilsType.getDeclaredMethod(_methodName, InputStream.class);
   final HashFunction function = newInstanceOf(_constructor);
   try (final InputStream is1 = streamOf("testFile1.txt")) {
     try (final InputStream is2 = streamOf("testFile1.txt")) {
       assertThat(method.invoke(null, is1), is((Object) function.update(is2)));
     }
   }
 }
 @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();
 }
 /**
  * The main method required for a Bloom filter which, given a value determines set membership.
  * Unlike a conventional set, the fuzzy set returns NO or MAYBE rather than true or false.
  *
  * @return NO or MAYBE
  */
 public ContainsResult contains(BytesRef value) {
   int hash = hashFunction.hash(value);
   if (hash < 0) {
     hash = hash * -1;
   }
   return mayContainValue(hash);
 }
 @Deprecated
 protected int hash(HashFunction hashFunction, String type, String id) {
   if (type == null || "_all".equals(type)) {
     throw new IllegalArgumentException(
         "Can't route an operation with no type and having type part of the routing (for backward comp)");
   }
   return hashFunction.hash(type, id);
 }
 /**
  * Records a value in the set. The referenced bytes are hashed and then modulo n'd where n is the
  * chosen size of the internal bitset.
  *
  * @param value the key value to be hashed
  * @throws IOException If there is a low-level I/O error
  */
 public void addValue(BytesRef value) throws IOException {
   int hash = hashFunction.hash(value);
   if (hash < 0) {
     hash = hash * -1;
   }
   // Bitmasking using bloomSize is effectively a modulo operation.
   int bloomPos = hash & bloomSize;
   filter.set(bloomPos);
 }
Beispiel #14
0
  /**
   * Add a node to the overlay network topology.
   *
   * @param data unused for this hash ring; nodes are placed evenly based on current topology
   *     characteristics. You may safely pass 'null' to this method.
   * @return the location of the new node in the hash space.
   */
  @Override
  public BigInteger addNode(T data) throws HashTopologyException, HashException {
    /* Edge case: when there are no entries in the hash ring yet. */
    if (entryMap.values().size() == 0) {
      BigInteger pos;

      if (randomize) {
        /* Find a random location to start with */
        pos = function.randomHash();
      } else {
        pos = BigInteger.ZERO;
      }

      HashRingEntry firstEntry = new HashRingEntry(pos);
      entryMap.put(pos, firstEntry);

      return pos;
    }

    /* Edge case: only one entry in the hash ring */
    if (entryMap.values().size() == 1) {
      HashRingEntry firstEntry = entryMap.values().iterator().next();
      BigInteger halfSize = maxHash.divide(BigInteger.valueOf(2));
      BigInteger secondPos = firstEntry.position.add(halfSize);

      if (secondPos.compareTo(maxHash) > 0) {
        secondPos = secondPos.subtract(maxHash);
      }

      HashRingEntry secondEntry = new HashRingEntry(secondPos, firstEntry);
      firstEntry.neighbor = secondEntry;
      entryMap.put(secondPos, secondEntry);

      return secondPos;
    }

    /* Find the largest empty span of hash space */
    BigInteger largestSpan = BigInteger.ZERO;
    HashRingEntry largestEntry = null;
    for (HashRingEntry entry : entryMap.values()) {
      BigInteger len = lengthBetween(entry, entry.neighbor);
      if (len.compareTo(largestSpan) > 0) {
        largestSpan = len;
        largestEntry = entry;
      }
    }

    if (largestEntry == null) {
      return BigInteger.ONE.negate();
    }

    /* Put the new node in the middle of the largest span */
    BigInteger half = half(largestEntry, largestEntry.neighbor);
    addRingEntry(half, largestEntry);
    return half;
  }
Beispiel #15
0
 public T get(Object key) {
   if (circle.isEmpty()) {
     return null;
   }
   int hash = hashFunction.hash(key);
   if (!circle.containsKey(hash)) {
     SortedMap<Integer, T> tailMap = circle.tailMap(hash);
     hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
   }
   return circle.get(hash);
 }
Beispiel #16
0
  @Override
  public BigInteger locate(T data) throws HashException {
    BigInteger hashLocation = function.hash(data);
    BigInteger node = entryMap.ceilingKey(hashLocation);

    /* Wraparound edge case */
    if (node == null) {
      node = entryMap.ceilingKey(BigInteger.ZERO);
    }

    return node;
  }
Beispiel #17
0
  /**
   * insert {@code key}
   *
   * @return {@code true} if this caused a collision
   */
  boolean insert(T key) {
    long h = hash_.hash(key);
    int idx = (int) (h % table_.size());
    SList<T> bucket = table_.at(idx);
    if (bucket.empty()) {
      ++n_;
      bucket.push_front(key);
      recordInsert(idx);
      if (verbose) System.err.println("SimpleHashTable: insert " + key + " @ " + idx);
      return false;
    } else if (onCollision_ == null) { // separate chaining
      ++n_;
      bucket.push_front(key);
      recordInsert(idx);
      ++nCollisions_;
      if (verbose)
        System.err.println(
            "SimpleHashTable: insert "
                + key
                + " @ "
                + idx
                + " (bucket size="
                + bucket.size()
                + ")");
      return true;
    } else {
      for (int j = 1; j < table_.size(); ++j) {
        if (verbose) System.err.println("SimpleHashTable: insert " + key + " with " + onCollision_);

        h = onCollision_.newHash(this, key, h, j);

        if (verbose) System.err.print("SimpleHashTable: " + idx + " occupied => ");

        idx = (int) (h % table_.size());

        if (verbose) System.err.println(idx);

        ++nCollisions_;

        if ((bucket = table_.at(idx)).empty()) {
          ++n_;
          bucket.push_front(key);
          recordInsert(idx);
          return true;
        }
      }
      // give up after N tries !
      throw new RuntimeException("collision handling failed!");
    }
  }
Beispiel #18
0
 public void remove(T node) {
   for (int i = 0; i < numberOfReplicas; i++) {
     circle.remove(hashFunction.hash(node.toString() + i));
   }
 }
 @Test
 public void updateByte() throws Exception {
   final Method method = _utilsType.getDeclaredMethod(_methodName, byte.class);
   final HashFunction function = newInstanceOf(_constructor);
   assertThat(method.invoke(null, (byte) 'X'), is((Object) function.update((byte) 'X')));
 }
Beispiel #20
0
 public void add(T node) {
   for (int i = 0; i < numberOfReplicas; i++) {
     circle.put(hashFunction.hash(node.toString() + i), node);
   }
 }
 protected int hash(HashFunction hashFunction, String routing) {
   return hashFunction.hash(routing);
 }
Beispiel #22
0
 /**
  * Creates a BalancedHashRing using the provided hash function. The function will determine the
  * size of the hash space and where nodes will be placed in the topology.
  *
  * @param function HashFunction that defines the hash space being used.
  */
 public BalancedHashRing(HashFunction<T> function, boolean randomize) {
   this.function = function;
   this.randomize = randomize;
   maxHash = function.maxValue();
 }
 public HashingOutputStream(HashFunction hashfunction, OutputStream outputstream) {
   super((OutputStream) Preconditions.checkNotNull(outputstream));
   hasher = (Hasher) Preconditions.checkNotNull(hashfunction.newHasher());
 }