1 | /** |
---|
2 | * |
---|
3 | * Copyright (c) 2005, European Commission project OneLab under contract 034819 |
---|
4 | * (http://www.one-lab.org) |
---|
5 | * |
---|
6 | * All rights reserved. |
---|
7 | * Redistribution and use in source and binary forms, with or |
---|
8 | * without modification, are permitted provided that the following |
---|
9 | * conditions are met: |
---|
10 | * - Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * - Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in |
---|
14 | * the documentation and/or other materials provided with the distribution. |
---|
15 | * - Neither the name of the University Catholique de Louvain - UCL |
---|
16 | * nor the names of its contributors may be used to endorse or |
---|
17 | * promote products derived from this software without specific prior |
---|
18 | * written permission. |
---|
19 | * |
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
---|
23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
---|
24 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
---|
26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
27 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
---|
28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
---|
30 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
31 | * POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | /** |
---|
35 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
36 | * or more contributor license agreements. See the NOTICE file |
---|
37 | * distributed with this work for additional information |
---|
38 | * regarding copyright ownership. The ASF licenses this file |
---|
39 | * to you under the Apache License, Version 2.0 (the |
---|
40 | * "License"); you may not use this file except in compliance |
---|
41 | * with the License. You may obtain a copy of the License at |
---|
42 | * |
---|
43 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
44 | * |
---|
45 | * Unless required by applicable law or agreed to in writing, software |
---|
46 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
47 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
48 | * See the License for the specific language governing permissions and |
---|
49 | * limitations under the License. |
---|
50 | */ |
---|
51 | package org.apache.hadoop.util.bloom; |
---|
52 | |
---|
53 | import java.io.DataInput; |
---|
54 | import java.io.DataOutput; |
---|
55 | import java.io.IOException; |
---|
56 | import java.util.Collection; |
---|
57 | import java.util.List; |
---|
58 | |
---|
59 | import org.apache.hadoop.io.Writable; |
---|
60 | import org.apache.hadoop.util.hash.Hash; |
---|
61 | |
---|
62 | /** |
---|
63 | * Defines the general behavior of a filter. |
---|
64 | * <p> |
---|
65 | * A filter is a data structure which aims at offering a lossy summary of a set <code>A</code>. The |
---|
66 | * key idea is to map entries of <code>A</code> (also called <i>keys</i>) into several positions |
---|
67 | * in a vector through the use of several hash functions. |
---|
68 | * <p> |
---|
69 | * Typically, a filter will be implemented as a Bloom filter (or a Bloom filter extension). |
---|
70 | * <p> |
---|
71 | * It must be extended in order to define the real behavior. |
---|
72 | * |
---|
73 | * @see Key The general behavior of a key |
---|
74 | * @see HashFunction A hash function |
---|
75 | */ |
---|
76 | public abstract class Filter implements Writable { |
---|
77 | private static final int VERSION = -1; // negative to accommodate for old format |
---|
78 | /** The vector size of <i>this</i> filter. */ |
---|
79 | protected int vectorSize; |
---|
80 | |
---|
81 | /** The hash function used to map a key to several positions in the vector. */ |
---|
82 | protected HashFunction hash; |
---|
83 | |
---|
84 | /** The number of hash function to consider. */ |
---|
85 | protected int nbHash; |
---|
86 | |
---|
87 | /** Type of hashing function to use. */ |
---|
88 | protected int hashType; |
---|
89 | |
---|
90 | protected Filter() {} |
---|
91 | |
---|
92 | /** |
---|
93 | * Constructor. |
---|
94 | * @param vectorSize The vector size of <i>this</i> filter. |
---|
95 | * @param nbHash The number of hash functions to consider. |
---|
96 | * @param hashType type of the hashing function (see {@link Hash}). |
---|
97 | */ |
---|
98 | protected Filter(int vectorSize, int nbHash, int hashType) { |
---|
99 | this.vectorSize = vectorSize; |
---|
100 | this.nbHash = nbHash; |
---|
101 | this.hashType = hashType; |
---|
102 | this.hash = new HashFunction(this.vectorSize, this.nbHash, this.hashType); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Adds a key to <i>this</i> filter. |
---|
107 | * @param key The key to add. |
---|
108 | */ |
---|
109 | public abstract void add(Key key); |
---|
110 | |
---|
111 | /** |
---|
112 | * Determines wether a specified key belongs to <i>this</i> filter. |
---|
113 | * @param key The key to test. |
---|
114 | * @return boolean True if the specified key belongs to <i>this</i> filter. |
---|
115 | * False otherwise. |
---|
116 | */ |
---|
117 | public abstract boolean membershipTest(Key key); |
---|
118 | |
---|
119 | /** |
---|
120 | * Peforms a logical AND between <i>this</i> filter and a specified filter. |
---|
121 | * <p> |
---|
122 | * <b>Invariant</b>: The result is assigned to <i>this</i> filter. |
---|
123 | * @param filter The filter to AND with. |
---|
124 | */ |
---|
125 | public abstract void and(Filter filter); |
---|
126 | |
---|
127 | /** |
---|
128 | * Peforms a logical OR between <i>this</i> filter and a specified filter. |
---|
129 | * <p> |
---|
130 | * <b>Invariant</b>: The result is assigned to <i>this</i> filter. |
---|
131 | * @param filter The filter to OR with. |
---|
132 | */ |
---|
133 | public abstract void or(Filter filter); |
---|
134 | |
---|
135 | /** |
---|
136 | * Peforms a logical XOR between <i>this</i> filter and a specified filter. |
---|
137 | * <p> |
---|
138 | * <b>Invariant</b>: The result is assigned to <i>this</i> filter. |
---|
139 | * @param filter The filter to XOR with. |
---|
140 | */ |
---|
141 | public abstract void xor(Filter filter); |
---|
142 | |
---|
143 | /** |
---|
144 | * Performs a logical NOT on <i>this</i> filter. |
---|
145 | * <p> |
---|
146 | * The result is assigned to <i>this</i> filter. |
---|
147 | */ |
---|
148 | public abstract void not(); |
---|
149 | |
---|
150 | /** |
---|
151 | * Adds a list of keys to <i>this</i> filter. |
---|
152 | * @param keys The list of keys. |
---|
153 | */ |
---|
154 | public void add(List<Key> keys){ |
---|
155 | if(keys == null) { |
---|
156 | throw new IllegalArgumentException("ArrayList<Key> may not be null"); |
---|
157 | } |
---|
158 | |
---|
159 | for(Key key: keys) { |
---|
160 | add(key); |
---|
161 | } |
---|
162 | }//end add() |
---|
163 | |
---|
164 | /** |
---|
165 | * Adds a collection of keys to <i>this</i> filter. |
---|
166 | * @param keys The collection of keys. |
---|
167 | */ |
---|
168 | public void add(Collection<Key> keys){ |
---|
169 | if(keys == null) { |
---|
170 | throw new IllegalArgumentException("Collection<Key> may not be null"); |
---|
171 | } |
---|
172 | for(Key key: keys) { |
---|
173 | add(key); |
---|
174 | } |
---|
175 | }//end add() |
---|
176 | |
---|
177 | /** |
---|
178 | * Adds an array of keys to <i>this</i> filter. |
---|
179 | * @param keys The array of keys. |
---|
180 | */ |
---|
181 | public void add(Key[] keys){ |
---|
182 | if(keys == null) { |
---|
183 | throw new IllegalArgumentException("Key[] may not be null"); |
---|
184 | } |
---|
185 | for(int i = 0; i < keys.length; i++) { |
---|
186 | add(keys[i]); |
---|
187 | } |
---|
188 | }//end add() |
---|
189 | |
---|
190 | // Writable interface |
---|
191 | |
---|
192 | public void write(DataOutput out) throws IOException { |
---|
193 | out.writeInt(VERSION); |
---|
194 | out.writeInt(this.nbHash); |
---|
195 | out.writeByte(this.hashType); |
---|
196 | out.writeInt(this.vectorSize); |
---|
197 | } |
---|
198 | |
---|
199 | public void readFields(DataInput in) throws IOException { |
---|
200 | int ver = in.readInt(); |
---|
201 | if (ver > 0) { // old unversioned format |
---|
202 | this.nbHash = ver; |
---|
203 | this.hashType = Hash.JENKINS_HASH; |
---|
204 | } else if (ver == VERSION) { |
---|
205 | this.nbHash = in.readInt(); |
---|
206 | this.hashType = in.readByte(); |
---|
207 | } else { |
---|
208 | throw new IOException("Unsupported version: " + ver); |
---|
209 | } |
---|
210 | this.vectorSize = in.readInt(); |
---|
211 | this.hash = new HashFunction(this.vectorSize, this.nbHash, this.hashType); |
---|
212 | } |
---|
213 | }//end class |
---|