/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.util.bloom;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.BitSet;

import org.roaringbitmap.RoaringBitmap;

/**
 * It is the extendable class to hadoop bloomfilter, it is extendable to implement compressed bloom
 * and fast serialize and deserialize of bloom.
 */
public class CarbonBloomFilter extends BloomFilter {

  private RoaringBitmap bitmap;

  private boolean compress;

  private int blockletNo;

  // used for building blocklet when query
  private String shardName;

  public CarbonBloomFilter() {
  }

  public CarbonBloomFilter(int vectorSize, int nbHash, int hashType, boolean compress) {
    super(vectorSize, nbHash, hashType);
    this.compress = compress;
  }

  @Override
  public boolean membershipTest(Key key) {
    if (key == null) {
      throw new NullPointerException("key cannot be null");
    }

    int[] h = hash.hash(key);
    hash.clear();
    if (compress) {
      // If it is compressed check in roaring bitmap
      for (int i = 0; i < nbHash; i++) {
        if (!bitmap.contains(h[i])) {
          return false;
        }
      }
    } else {
      for (int i = 0; i < nbHash; i++) {
        if (!bits.get(h[i])) {
          return false;
        }
      }
    }
    return true;
  }

  @Override
  public void write(DataOutput out) throws IOException {
    out.writeInt(blockletNo);
    out.writeInt(this.nbHash);
    out.writeByte(this.hashType);
    out.writeInt(this.vectorSize);
    out.writeBoolean(compress);
    BitSet bits = getBitSet();
    if (!compress) {
      byte[] bytes = bits.toByteArray();
      out.writeInt(bytes.length);
      out.write(bytes);
    } else {
      RoaringBitmap bitmap = new RoaringBitmap();
      int length = bits.cardinality();
      int nextSetBit = bits.nextSetBit(0);
      for (int i = 0; i < length; ++i) {
        bitmap.add(nextSetBit);
        nextSetBit = bits.nextSetBit(nextSetBit + 1);
      }
      bitmap.serialize(out);
    }
  }

  @Override
  public void readFields(DataInput in) throws IOException {
    this.blockletNo = in.readInt();
    this.nbHash = in.readInt();
    this.hashType = in.readByte();
    this.vectorSize = in.readInt();
    this.compress = in.readBoolean();
    if (!compress) {
      int len = in.readInt();
      byte[] bytes = new byte[len];
      in.readFully(bytes);
      setBitSet(BitSet.valueOf(bytes));
    } else {
      this.bitmap = new RoaringBitmap();
      bitmap.deserialize(in);
    }
    this.hash = new HashFunction(this.vectorSize, this.nbHash, this.hashType);
  }

  public int getSize() {
    int size = 14; // size of nbHash,hashType, vectorSize, compress
    if (compress) {
      size += bitmap.getSizeInBytes();
    } else {
      try {
        size += getBitSet().toLongArray().length * 8;
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
    return size;
  }

  /**
   * Get bitset from super class using reflection, in some cases java cannot access the fields if
   * jars are loaded in separte class loaders.
   *
   * @return
   * @throws IOException
   */
  private BitSet getBitSet() throws IOException {
    try {
      Field field = BloomFilter.class.getDeclaredField("bits");
      field.setAccessible(true);
      return (BitSet)field.get(this);
    } catch (Exception e) {
      throw new IOException(e);
    }
  }

  /**
   * Set bitset from super class using reflection, in some cases java cannot access the fields if
   * jars are loaded in separte class loaders.
   * @param bitSet
   * @throws IOException
   */
  private void setBitSet(BitSet bitSet) throws IOException {
    try {
      Field field = BloomFilter.class.getDeclaredField("bits");
      field.setAccessible(true);
      field.set(this, bitSet);
    } catch (Exception e) {
      throw new IOException(e);
    }
  }

  public void setBlockletNo(int blockletNo) {
    this.blockletNo = blockletNo;
  }

  public int getBlockletNo() {
    return blockletNo;
  }

  public String getShardName() {
    return shardName;
  }

  public void setShardName(String shardName) {
    this.shardName = shardName;
  }

}

@@ -1636,24 +1636,99 @@
 (Key key) %7B%0A
+    if (compress) %7B%0A      // If it is compressed check in roaring bitmap%0A  
     if (key 
@@ -1736,32 +1736,34 @@
 = null) %7B%0A      
+  
 throw new NullPo
@@ -1808,15 +1808,18 @@
     
+  
 %7D
-%0A
 %0A    
+  
 int%5B
@@ -1844,16 +1844,18 @@
 y);%0A    
+  
 hash.cle
@@ -1864,81 +1864,8 @@
 ();%0A
-    if (compress) %7B%0A      // If it is compressed check in roaring bitmap%0A
     
@@ -1989,153 +1989,153 @@
     
-%7D else %7B%0A      for (int i = 0; i %3C nbHash; i++) %7B%0A        if (!bits.get(h%5Bi%5D)) %7B%0A  
+  return true;%0A    %7D else %7B%0A      // call super method to avoid IllegalAccessError for %60bits%60 field%0A
       
-  
 return 
-false;%0A        %7D%0A      %7D%0A    %7D%0A    return true;
+super.membershipTest(key);%0A    %7D
 %0A  %7D

Fix access field error for BitSet in bloom filter