001: /* BloomFilter
002: *
003: * $Id: BloomFilter64bit.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>NUMBER_OF_WEIGHTS is 2083, to better avoid collisions between
040: * similar strings</li>
041: * <li>Removed dependence on cern.colt MersenneTwister (replaced with
042: * SecureRandom) and QuickBitVector (replaced with local methods).</li>
043: * <li>Adapted to allow long bit indices so long as the index/64 (used
044: * an array index in bit vector) fits within Integer.MAX_VALUE</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 BloomFilter64bit implements Serializable, BloomFilter {
072:
073: private static final long serialVersionUID = 2317000663009608403L;
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 vector. */
082: final private long[] bits;
083: /** The random integers used to generate the hash functions. */
084: final private long[][] 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 BloomFilter64bit(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) / 64L);
107: if (len / 64 > Integer.MAX_VALUE)
108: throw new IllegalArgumentException(
109: "This filter would require " + len * 64L + " bits");
110: bits = new long[len];
111: m = bits.length * 64;
112:
113: if (DEBUG)
114: System.err.println("Number of bits: " + m);
115:
116: // seeded for reproduceable behavior in repeated runs; BUT:
117: // SecureRandom's default implementation (as of 1.5)
118: // seems to mix in its own seeding.
119: final SecureRandom random = new SecureRandom(new byte[] { 19,
120: 96 });
121: weight = new long[d][];
122: for (int i = 0; i < d; i++) {
123: weight[i] = new long[NUMBER_OF_WEIGHTS];
124: for (int j = 0; j < NUMBER_OF_WEIGHTS; j++)
125: weight[i][j] = random.nextLong();
126: }
127: }
128:
129: /** The number of character sequences in the filter.
130: *
131: * @return the number of character sequences in the filter (but see {@link #contains(CharSequence)}).
132: */
133:
134: public int size() {
135: return size;
136: }
137:
138: /** Hashes the given sequence with the given hash function.
139: *
140: * @param s a character sequence.
141: * @param l the length of <code>s</code>.
142: * @param k a hash function index (smaller than {@link #d}).
143: * @return the position in the filter corresponding to <code>s</code> for the hash function <code>k</code>.
144: */
145:
146: private long hash(final CharSequence s, final int l, final int k) {
147: final long[] w = weight[k];
148: long h = 0;
149: int i = l;
150: while (i-- != 0)
151: h ^= s.charAt(i) * w[i % NUMBER_OF_WEIGHTS];
152: return (h & 0x7FFFFFFFFFFFFFFFL) % m;
153: }
154:
155: /** Checks whether the given character sequence is in this filter.
156: *
157: * <P>Note that this method may return true on a character sequence that is has
158: * not been added to the filter. This will happen with probability 2<sub>-<var>d</var></sub>,
159: * where <var>d</var> is the number of hash functions specified at creation time, if
160: * the number of the elements in the filter is less than <var>n</var>, the number
161: * of expected elements specified at creation time.
162: *
163: * @param s a character sequence.
164: * @return true if the sequence is in the filter (or if a sequence with the
165: * same hash sequence is in the filter).
166: */
167:
168: public boolean contains(final CharSequence s) {
169: int i = d, l = s.length();
170: while (i-- != 0)
171: if (!getBit(hash(s, l, i)))
172: return false;
173: return true;
174: }
175:
176: /** Adds a character sequence to the filter.
177: *
178: * @param s a character sequence.
179: * @return true if the character sequence was not in the filter (but see {@link #contains(CharSequence)}).
180: */
181:
182: public boolean add(final CharSequence s) {
183: boolean result = false;
184: int i = d, l = s.length();
185: long h;
186: while (i-- != 0) {
187: h = hash(s, l, i);
188: if (!getBit(h))
189: result = true;
190: setBit(h);
191: }
192: if (result)
193: size++;
194: return result;
195: }
196:
197: protected final static long ADDRESS_BITS_PER_UNIT = 6; // 64=2^6
198: protected final static long BIT_INDEX_MASK = 63; // = BITS_PER_UNIT - 1;
199:
200: /**
201: * Returns from the local bitvector the value of the bit with
202: * the specified index. The value is <tt>true</tt> if the bit
203: * with the index <tt>bitIndex</tt> is currently set; otherwise,
204: * returns <tt>false</tt>.
205: *
206: * (adapted from cern.colt.bitvector.QuickBitVector)
207: *
208: * @param bitIndex the bit index.
209: * @return the value of the bit with the specified index.
210: */
211: protected boolean getBit(long bitIndex) {
212: return ((bits[(int) (bitIndex >> ADDRESS_BITS_PER_UNIT)] & (1L << (bitIndex & BIT_INDEX_MASK))) != 0);
213: }
214:
215: /**
216: * Changes the bit with index <tt>bitIndex</tt> in local bitvector.
217: *
218: * (adapted from cern.colt.bitvector.QuickBitVector)
219: *
220: * @param bitIndex the index of the bit to be set.
221: */
222: protected void setBit(long bitIndex) {
223: bits[(int) (bitIndex >> ADDRESS_BITS_PER_UNIT)] |= 1L << (bitIndex & BIT_INDEX_MASK);
224: }
225:
226: /* (non-Javadoc)
227: * @see org.archive.util.BloomFilter#getSizeBytes()
228: */
229: public long getSizeBytes() {
230: return bits.length * 8;
231: }
232: }
|