Debian Codegraph @

jdbm1/src/main/java/jdbm/htree/HashBucket.java

raw
1 /**
2 * JDBM LICENSE v1.00
3 *
4 * Redistribution and use of this software and associated documentation
5 * ("Software"), with or without modification, are permitted provided
6 * that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain copyright
9 * statements and notices. Redistributions must also contain a
10 * copy of this document.
11 *
12 * 2. Redistributions in binary form must reproduce the
13 * above copyright notice, this list of conditions and the
14 * following disclaimer in the documentation and/or other
15 * materials provided with the distribution.
16 *
17 * 3. The name "JDBM" must not be used to endorse or promote
18 * products derived from this Software without prior written
19 * permission of Cees de Groot. For written permission,
20 * please contact [email protected].
21 *
22 * 4. Products derived from this Software may not be called "JDBM"
23 * nor may "JDBM" appear in their names without prior written
24 * permission of Cees de Groot.
25 *
26 * 5. Due credit should be given to the JDBM Project
27 * (http://jdbm.sourceforge.net/).
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
31 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
43 * Contributions are Copyright (C) 2000 by their associated contributors.
44 *
45 */
46
47 package jdbm.htree;
48
49
50 import java.io.Externalizable;
51 import java.io.IOException;
52 import java.io.ObjectInput;
53 import java.io.ObjectOutput;
54 import java.util.ArrayList;
55
56 import jdbm.I18n;
57
58
59 /**
60 * A bucket is a placeholder for multiple (key, value) pairs. Buckets
61 * are used to store collisions (same hash value) at all levels of an
62 * H*tree.
63 *
64 * There are two types of buckets: leaf and non-leaf.
65 *
66 * Non-leaf buckets are buckets which hold collisions which happen
67 * when the H*tree is not fully expanded. Keys in a non-leaf buckets
68 * can have different hash codes. Non-leaf buckets are limited to an
69 * arbitrary size. When this limit is reached, the H*tree should create
70 * a new Directory page and distribute keys of the non-leaf buckets into
71 * the newly created Directory.
72 *
73 * A leaf bucket is a bucket which contains keys which all have
74 * the same <code>hashCode()</code>. Leaf buckets stand at the
75 * bottom of an H*tree because the hashing algorithm cannot further
76 * discriminate between different keys based on their hash code.
77 *
78 * @author <a href="mailto:[email protected]">Alex Boisvert</a>
79 */
80 final class HashBucket
81 extends HashNode
82 implements Externalizable
83 {
84
85 final static long serialVersionUID = 1L;
86
87 /**
88 * The maximum number of elements (key, value) a non-leaf bucket
89 * can contain.
90 */
91 public static final int OVERFLOW_SIZE = 8;
92
93 /**
94 * Depth of this bucket.
95 */
96 private int _depth;
97
98 /**
99 * Keys in this bucket. Keys are ordered to match their respective
100 * value in <code>_values</code>.
101 */
102 private ArrayList _keys;
103
104 /**
105 * Values in this bucket. Values are ordered to match their respective
106 * key in <code>_keys</code>.
107 */
108 private ArrayList _values;
109
110
111 /**
112 * Public constructor for serialization.
113 */
114 public HashBucket()
115 {
116 // empty
117 }
118
119
120 /**
121 * Construct a bucket with a given depth level. Depth level is the
122 * number of <code>HashDirectory</code> above this bucket.
123 */
124 public HashBucket( int level )
125 {
126 if ( level > HashDirectory.MAX_DEPTH + 1 )
127 {
128 throw new IllegalArgumentException( I18n.err( I18n.ERR_534, level ) );
129 }
130 _depth = level;
131 _keys = new ArrayList( OVERFLOW_SIZE );
132 _values = new ArrayList( OVERFLOW_SIZE );
133 }
134
135
136 /**
137 * Returns the number of elements contained in this bucket.
138 */
139 public int getElementCount()
140 {
141 return _keys.size();
142 }
143
144
145 /**
146 * Returns whether or not this bucket is a "leaf bucket".
147 */
148 public boolean isLeaf()
149 {
150 return ( _depth > HashDirectory.MAX_DEPTH );
151 }
152
153
154 /**
155 * Returns true if bucket can accept at least one more element.
156 */
157 public boolean hasRoom()
158 {
159 if ( isLeaf() )
160 {
161 return true; // leaf buckets are never full
162 }
163 else
164 {
165 // non-leaf bucket
166 return ( _keys.size() < OVERFLOW_SIZE );
167 }
168 }
169
170
171 /**
172 * Add an element (key, value) to this bucket. If an existing element
173 * has the same key, it is replaced silently.
174 *
175 * @return Object which was previously associated with the given key
176 * or <code>null</code> if no association existed.
177 */
178 public Object addElement( Object key, Object value )
179 {
180 int existing = _keys.indexOf( key );
181 if ( existing != -1 )
182 {
183 // replace existing element
184 Object before = _values.get( existing );
185 _values.set( existing, value );
186 return before;
187 }
188 else
189 {
190 // add new (key, value) pair
191 _keys.add( key );
192 _values.add( value );
193 return null;
194 }
195 }
196
197
198 /**
199 * Remove an element, given a specific key.
200 *
201 * @param key Key of the element to remove
202 *
203 * @return Removed element value, or <code>null</code> if not found
204 */
205 public Object removeElement( Object key )
206 {
207 int existing = _keys.indexOf( key );
208 if ( existing != -1 )
209 {
210 Object obj = _values.get( existing );
211 _keys.remove( existing );
212 _values.remove( existing );
213 return obj;
214 }
215 else
216 {
217 // not found
218 return null;
219 }
220 }
221
222
223 /**
224 * Returns the value associated with a given key. If the given key
225 * is not found in this bucket, returns <code>null</code>.
226 */
227 public Object getValue( Object key )
228 {
229 int existing = _keys.indexOf( key );
230 if ( existing != -1 )
231 {
232 return _values.get( existing );
233 }
234 else
235 {
236 // key not found
237 return null;
238 }
239 }
240
241
242 /**
243 * Obtain keys contained in this buckets. Keys are ordered to match
244 * their values, which be be obtained by calling <code>getValues()</code>.
245 *
246 * As an optimization, the Vector returned is the instance member
247 * of this class. Please don't modify outside the scope of this class.
248 */
249 ArrayList getKeys()
250 {
251 return this._keys;
252 }
253
254
255 /**
256 * Obtain values contained in this buckets. Values are ordered to match
257 * their keys, which be be obtained by calling <code>getKeys()</code>.
258 *
259 * As an optimization, the Vector returned is the instance member
260 * of this class. Please don't modify outside the scope of this class.
261 */
262 ArrayList getValues()
263 {
264 return this._values;
265 }
266
267
268 /**
269 * Implement Externalizable interface.
270 */
271 public void writeExternal( ObjectOutput out )
272 throws IOException
273 {
274 out.writeInt( _depth );
275
276 int entries = _keys.size();
277 out.writeInt( entries );
278
279 // write keys
280 for ( int i = 0; i < entries; i++ )
281 {
282 out.writeObject( _keys.get( i ) );
283 }
284 // write values
285 for ( int i = 0; i < entries; i++ )
286 {
287 out.writeObject( _values.get( i ) );
288 }
289 }
290
291
292 /**
293 * Implement Externalizable interface.
294 */
295 public void readExternal( ObjectInput in )
296 throws IOException, ClassNotFoundException
297 {
298 _depth = in.readInt();
299
300 int entries = in.readInt();
301
302 // prepare array lists
303 int size = Math.max( entries, OVERFLOW_SIZE );
304 _keys = new ArrayList( size );
305 _values = new ArrayList( size );
306
307 // read keys
308 for ( int i = 0; i < entries; i++ )
309 {
310 _keys.add( in.readObject() );
311 }
312 // read values
313 for ( int i = 0; i < entries; i++ )
314 {
315 _values.add( in.readObject() );
316 }
317 }
318
319
320 public String toString()
321 {
322 StringBuffer buf = new StringBuffer();
323 buf.append( "HashBucket {depth=" );
324 buf.append( _depth );
325 buf.append( ", keys=" );
326 buf.append( _keys );
327 buf.append( ", values=" );
328 buf.append( _values );
329 buf.append( "}" );
330 return buf.toString();
331 }
332 }