KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > HashBase


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 import java.sql.SQLException JavaDoc;
8
9 public abstract class HashBase {
10     protected int mask, len, size, deletedCount, level;
11     protected boolean zeroKey;
12     private int maxSize, minSize, maxDeleted;
13     private static final int MAX_LOAD = 90;
14
15     public HashBase() {
16         reset(2);
17     }
18
19     public int size() {
20         return size + (zeroKey ? 1 : 0);
21     }
22
23     protected void checkSizePut() throws SQLException JavaDoc {
24         if(deletedCount > size) {
25             rehash(level);
26         }
27         if(size + deletedCount >= maxSize) {
28             rehash(level+1);
29         }
30     }
31
32     protected void checkSizeRemove() throws SQLException JavaDoc {
33         if(size < minSize && level>0) {
34             rehash(level-1);
35         } else if(deletedCount > maxDeleted) {
36             rehash(level);
37         }
38     }
39
40     protected abstract void rehash(int newLevel) throws SQLException JavaDoc;
41
42     protected void reset(int newLevel) {
43         minSize = size * 3 / 4;
44         size = 0;
45         level = newLevel;
46         len = 2 << level;
47         mask = len - 1;
48         maxSize = (int)(len * MAX_LOAD / 100L);
49         deletedCount = 0;
50         maxDeleted = 20 + len / 2;
51     }
52
53     protected int getIndex(int hash) {
54         return hash & mask;
55     }
56
57 }
58
Popular Tags