001: /* BloomFilter32bit
002: *
003: * $Id: BloomFilter32bit.java 4644 2006-09-20 22:40:21Z paul_jack $
004: *
005: * Created on Jun 21, 2005
006: *
007: * Copyright (C) 2005 Internet Archive; a slight adaptation of
008: * LGPL work (C) Sebastiano Vigna
009: *
010: * This file is part of the Heritrix web crawler (crawler.archive.org).
011: *
012: * Heritrix is free software; you can redistribute it and/or modify
013: * it under the terms of the GNU Lesser Public License as published by
014: * the Free Software Foundation; either version 2.1 of the License, or
015: * any later version.
016: *
017: * Heritrix is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU Lesser Public License for more details.
021: *
022: * You should have received a copy of the GNU Lesser Public License
023: * along with Heritrix; if not, write to the Free Software
024: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: */
026:
027: package org.archive.util;
028:
029: import java.io.Serializable;
030: import java.security.SecureRandom;
031:
032: /** A Bloom filter.
033: *
034: * SLIGHTLY ADAPTED VERSION OF MG4J it.unimi.dsi.mg4j.util.BloomFilter
035: *
036: * <p>KEY CHANGES:
037: *
038: * <ul>
039: * <li>Adapted to use 32bit ops as much as possible... may be slightly
040: * faster on 32bit hardware/OS</li>
041: * <li>NUMBER_OF_WEIGHTS is 2083, to better avoid collisions between
042: * similar strings</li>
043: * <li>Removed dependence on cern.colt MersenneTwister (replaced with
044: * SecureRandom) and QuickBitVector (replaced with local methods).</li>
045: * </ul>
046: *
047: * <hr>
048: *
049: * <P>Instances of this class represent a set of character sequences (with false positives)
050: * using a Bloom filter. Because of the way Bloom filters work,
051: * you cannot remove elements.
052: *
053: * <P>Bloom filters have an expected error rate, depending on the number
054: * of hash functions used, on the filter size and on the number of elements in the filter. This implementation
055: * uses a variable optimal number of hash functions, depending on the expected
056: * number of elements. More precisely, a Bloom
057: * filter for <var>n</var> character sequences with <var>d</var> hash functions will use
058: * ln 2 <var>d</var><var>n</var> ≈ 1.44 <var>d</var><var>n</var> bits;
059: * false positives will happen with probability 2<sup>-<var>d</var></sup>.
060: *
061: * <P>Hash functions are generated at creation time using universal hashing. Each hash function
062: * uses {@link #NUMBER_OF_WEIGHTS} random integers, which are cyclically multiplied by
063: * the character codes in a character sequence. The resulting integers are XOR-ed together.
064: *
065: * <P>This class exports access methods that are very similar to those of {@link java.util.Set},
066: * but it does not implement that interface, as too many non-optional methods
067: * would be unimplementable (e.g., iterators).
068: *
069: * @author Sebastiano Vigna
070: */
071: public class BloomFilter32bit implements Serializable, BloomFilter {
072:
073: private static final long serialVersionUID = -1567837798979475689L;
074:
075: /** The number of weights used to create hash functions. */
076: final public static int NUMBER_OF_WEIGHTS = 2083; // CHANGED FROM 16
077: /** The number of bits in this filter. */
078: final public long m;
079: /** The number of hash functions used by this filter. */
080: final public int d;
081: /** The underlying bit vectorS. */
082: final private int[] bits;
083: /** The random integers used to generate the hash functions. */
084: final private int[][] weight;
085:
086: /** The number of elements currently in the filter. It may be
087: * smaller than the actual number of additions of distinct character
088: * sequences because of false positives.
089: */
090: private int size;
091:
092: /** The natural logarithm of 2, used in the computation of the number of bits. */
093: private final static double NATURAL_LOG_OF_2 = Math.log(2);
094:
095: private final static boolean DEBUG = false;
096:
097: /** Creates a new Bloom filter with given number of hash functions and expected number of elements.
098: *
099: * @param n the expected number of elements.
100: * @param d the number of hash functions; if the filter add not more than <code>n</code> elements,
101: * false positives will happen with probability 2<sup>-<var>d</var></sup>.
102: */
103: public BloomFilter32bit(final int n, final int d) {
104: this .d = d;
105: int len = (int) Math
106: .ceil(((long) n * (long) d / NATURAL_LOG_OF_2) / 32);
107: this .m = len * 32L;
108: if (m >= 1L << 32) {
109: throw new IllegalArgumentException(
110: "This filter would require " + m + " bits");
111: }
112: bits = new int[len];
113:
114: if (DEBUG)
115: System.err.println("Number of bits: " + m);
116:
117: // seeded for reproduceable behavior in repeated runs; BUT:
118: // SecureRandom's default implementation (as of 1.5)
119: // seems to mix in its own seeding.
120: final SecureRandom random = new SecureRandom(new byte[] { 19,
121: 96 });
122: weight = new int[d][];
123: for (int i = 0; i < d; i++) {
124: weight[i] = new int[NUMBER_OF_WEIGHTS];
125: for (int j = 0; j < NUMBER_OF_WEIGHTS; j++)
126: weight[i][j] = random.nextInt();
127: }
128: }
129:
130: /** The number of character sequences in the filter.
131: *
132: * @return the number of character sequences in the filter (but see {@link #contains(CharSequence)}).
133: */
134:
135: public int size() {
136: return size;
137: }
138:
139: /** Hashes the given sequence with the given hash function.
140: *
141: * @param s a character sequence.
142: * @param l the length of <code>s</code>.
143: * @param k a hash function index (smaller than {@link #d}).
144: * @return the position in the filter corresponding to <code>s</code> for the hash function <code>k</code>.
145: */
146: private long hash(final CharSequence s, final int l, final int k) {
147: final int[] w = weight[k];
148: int h = 0, i = l;
149: while (i-- != 0)
150: h ^= s.charAt(i) * w[i % NUMBER_OF_WEIGHTS];
151: return ((long) h - Integer.MIN_VALUE) % m;
152: }
153:
154: /** Checks whether the given character sequence is in this filter.
155: *
156: * <P>Note that this method may return true on a character sequence that is has
157: * not been added to the filter. This will happen with probability 2<sub>-<var>d</var></sub>,
158: * where <var>d</var> is the number of hash functions specified at creation time, if
159: * the number of the elements in the filter is less than <var>n</var>, the number
160: * of expected elements specified at creation time.
161: *
162: * @param s a character sequence.
163: * @return true if the sequence is in the filter (or if a sequence with the
164: * same hash sequence is in the filter).
165: */
166:
167: public boolean contains(final CharSequence s) {
168: int i = d, l = s.length();
169: while (i-- != 0)
170: if (!getBit(hash(s, l, i)))
171: return false;
172: return true;
173: }
174:
175: /** Adds a character sequence to the filter.
176: *
177: * @param s a character sequence.
178: * @return true if the character sequence was not in the filter (but see {@link #contains(CharSequence)}).
179: */
180:
181: public boolean add(final CharSequence s) {
182: boolean result = false;
183: int i = d, l = s.length();
184: long h;
185: while (i-- != 0) {
186: h = hash(s, l, i);
187: if (!getBit(h))
188: result = true;
189: setBit(h);
190: }
191: if (result)
192: size++;
193: return result;
194: }
195:
196: protected final static long ADDRESS_BITS_PER_UNIT = 5; // 32=2^5
197: protected final static long BIT_INDEX_MASK = 31; // = BITS_PER_UNIT - 1;
198:
199: /**
200: * Returns from the local bitvector the value of the bit with
201: * the specified index. The value is <tt>true</tt> if the bit
202: * with the index <tt>bitIndex</tt> is currently set; otherwise,
203: * returns <tt>false</tt>.
204: *
205: * (adapted from cern.colt.bitvector.QuickBitVector)
206: *
207: * @param bitIndex the bit index.
208: * @return the value of the bit with the specified index.
209: */
210: protected boolean getBit(long bitIndex) {
211: return ((bits[(int) (bitIndex >> ADDRESS_BITS_PER_UNIT)] & (1 << (bitIndex & BIT_INDEX_MASK))) != 0);
212: }
213:
214: /**
215: * Changes the bit with index <tt>bitIndex</tt> in local bitvector.
216: *
217: * (adapted from cern.colt.bitvector.QuickBitVector)
218: *
219: * @param bitIndex the index of the bit to be set.
220: */
221: protected void setBit(long bitIndex) {
222: bits[(int) (bitIndex >> ADDRESS_BITS_PER_UNIT)] |= 1 << (bitIndex & BIT_INDEX_MASK);
223: }
224:
225: /* (non-Javadoc)
226: * @see org.archive.util.BloomFilter#getSizeBytes()
227: */
228: public long getSizeBytes() {
229: return bits.length * 4;
230: }
231: }
|