Example #1
0
  // Search the key in the hashcode sorted array
  private IMapEntry binarySearch(byte[] key) throws IOException {
    int hashCode = Arrays.hashCode(key);
    int lo = 0;
    int slo = lo;
    int hi = this.getAppendedSize() - 1;
    int shi = hi;
    while (lo <= hi) {
      int mid = lo + (hi - lo) / 2;
      IMapEntry mapEntry = this.getMapEntry(mid);
      int midHashCode = mapEntry.getKeyHash();
      if (hashCode < midHashCode) hi = mid - 1;
      else if (hashCode > midHashCode) lo = mid + 1;
      else {
        if (BytesUtil.compare(key, mapEntry.getKey()) == 0) {
          return mapEntry;
        }
        // find left
        int index = mid - 1;
        while (index >= slo) {
          mapEntry = this.getMapEntry(index);
          if (hashCode != mapEntry.getKeyHash()) break;
          if (BytesUtil.compare(key, mapEntry.getKey()) == 0) {
            return mapEntry;
          }
          index--;
        }
        // find right
        index = mid + 1;
        while (index <= shi) {
          mapEntry = this.getMapEntry(index);
          if (hashCode != mapEntry.getKeyHash()) break;
          if (BytesUtil.compare(key, mapEntry.getKey()) == 0) {
            return mapEntry;
          }
          index++;
        }

        return null;
      }
    }
    return null;
  }