The xor filter, a new algorithm that can replace a Bloom filter. It needs 1.23 log(1/fpp) bits per key. It is related to the BDZ algorithm [1] (a minimal perfect hash function algorithm). [1] paper: Simple and Space-Efficient Minimal Perfect Hash Functions - http://cmph.sourceforge.net/papers/wads
| 11 | * http://cmph.sourceforge.net/papers/wads07.pdf |
| 12 | */ |
| 13 | public class Xor8 { |
| 14 | |
| 15 | private static final int BITS_PER_FINGERPRINT = 8; |
| 16 | private static final int HASHES = 3; |
| 17 | private static final int OFFSET = 2; |
| 18 | private static final int FACTOR_TIMES_100 = 123; |
| 19 | private final int size; |
| 20 | private final int arrayLength; |
| 21 | private final int blockLength; |
| 22 | private long seed; |
| 23 | private byte[][] ciphertext; |
| 24 | private final int bitCount; |
| 25 | private static Random random = new Random(); |
| 26 | |
| 27 | |
| 28 | private static int getArrayLength(int size) { |
| 29 | return (int) (OFFSET + (long) FACTOR_TIMES_100 * size / 100); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | public Xor8(long[] keys, byte[][] ct) { |
| 34 | this.size = keys.length; |
| 35 | arrayLength = getArrayLength(size); |
| 36 | bitCount = arrayLength * BITS_PER_FINGERPRINT; |
| 37 | blockLength = arrayLength / HASHES; |
| 38 | int m = arrayLength; |
| 39 | ciphertext = new byte[m][]; |
| 40 | long[] reverseOrder = new long[arrayLength]; |
| 41 | byte[] reverseH = new byte[arrayLength]; |
| 42 | int reverseOrderPos; |
| 43 | long seed; |
| 44 | do { |
| 45 | seed = Hash.randomSeed(); |
| 46 | byte[] t2count = new byte[m]; |
| 47 | long[] t2 = new long[m]; |
| 48 | for (int i = 0; i < size; i++) { |
| 49 | long k = i; |
| 50 | for (int hi = 0; hi < HASHES; hi++) { |
| 51 | int h = getHash(keys[i], seed, hi); |
| 52 | t2[h] ^= k; |
| 53 | if (t2count[h] > 120) { |
| 54 | // probably something wrong with the hash function |
| 55 | // let us not crash the system: |
| 56 | throw new IllegalArgumentException(); |
| 57 | } |
| 58 | t2count[h]++; |
| 59 | } |
| 60 | } |
| 61 | reverseOrderPos = 0; |
| 62 | int[][] alone = new int[HASHES][blockLength]; |
| 63 | int[] alonePos = new int[HASHES]; |
| 64 | for (int nextAlone = 0; nextAlone < HASHES; nextAlone++) { |
| 65 | for (int i = 0; i < blockLength; i++) { |
| 66 | if (t2count[nextAlone * blockLength + i] == 1) { |
| 67 | alone[nextAlone][alonePos[nextAlone]++] = nextAlone * blockLength + i; |
| 68 | } |
| 69 | } |
| 70 | } |
nothing calls this directly
no outgoing calls
no test coverage detected