/**
  * 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);
 }
示例#4
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);
 }
示例#5
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;
  }
示例#6
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!");
    }
  }
示例#7
0
 public void remove(T node) {
   for (int i = 0; i < numberOfReplicas; i++) {
     circle.remove(hashFunction.hash(node.toString() + i));
   }
 }
示例#8
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);
 }