KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.h2.message.Message;
10 import org.h2.store.DataHandler;
11 import org.h2.value.Value;
12 import org.h2.value.ValueNull;
13
14 public class ValueHashMap extends HashBase {
15
16     private Value[] keys;
17     private Object JavaDoc[] values;
18     private DataHandler database;
19     
20     public ValueHashMap(DataHandler database) {
21         this.database = database;
22     }
23     
24     protected void reset(int newLevel) {
25         super.reset(newLevel);
26         keys = new Value[len];
27         values = new Object JavaDoc[len];
28     }
29     
30     protected void rehash(int newLevel) throws SQLException JavaDoc {
31         Value[] oldKeys = keys;
32         Object JavaDoc[] oldValues = values;
33         reset(newLevel);
34         for(int i=0; i<oldKeys.length; i++) {
35             Value k = oldKeys[i];
36             if(k != null && k != ValueNull.DELETED) {
37                 put(k, oldValues[i]);
38             }
39         }
40     }
41     
42     private int getIndex(Value key) {
43         return key.hashCode() & mask;
44     }
45     
46     public void put(Value key, Object JavaDoc value) throws SQLException JavaDoc {
47         checkSizePut();
48         int index = getIndex(key);
49         int plus = 1;
50         int deleted = -1;
51         do {
52             Value k = keys[index];
53             if(k==null) {
54                 // found an empty record
55
if(deleted>=0) {
56                     index = deleted;
57                     deletedCount--;
58                 }
59                 size++;
60                 keys[index] = key;
61                 values[index] = value;
62                 return;
63             } else if(k==ValueNull.DELETED) {
64                 // found a deleted record
65
if(deleted<0) {
66                     deleted = index;
67                 }
68             } else if(database.compareTypeSave(k, key)==0) {
69                 // update existing
70
values[index] = value;
71                 return;
72             }
73             index = (index + plus++) & mask;
74         } while(plus <= len);
75         // no space
76
throw Message.getInternalError("hashmap is full");
77     }
78     
79     public void remove(Value key) throws SQLException JavaDoc {
80         checkSizeRemove();
81         int index = getIndex(key);
82         int plus = 1;
83         do {
84             Value k = keys[index];
85             if(k==null) {
86                 // found an empty record
87
return;
88             } else if(k==ValueNull.DELETED) {
89                 // found a deleted record
90
} else if(database.compareTypeSave(k, key)==0) {
91                 // found the record
92
keys[index] = ValueNull.DELETED;
93                 values[index] = null;
94                 deletedCount++;
95                 size--;
96                 return;
97             }
98             index = (index + plus++) & mask;
99             k = keys[index];
100         } while(plus <= len);
101         // not found
102
}
103     
104     public Object JavaDoc get(Value key) throws SQLException JavaDoc {
105         int index = getIndex(key);
106         int plus = 1;
107         do {
108             Value k = keys[index];
109             if(k==null) {
110                 // found an empty record
111
return null;
112             } else if(k == ValueNull.DELETED) {
113                 // found a deleted record
114
} else if(database.compareTypeSave(k, key)==0) {
115                 // found it
116
return values[index];
117             }
118             index = (index + plus++) & mask;
119         } while(plus <= len);
120         return null;
121     }
122     
123     public ObjectArray keys() {
124         ObjectArray list = new ObjectArray(size);
125         for(int i=0; i<keys.length; i++) {
126             Value k = keys[i];
127             if(k != null && k != ValueNull.DELETED) {
128                 list.add(k);
129             }
130         }
131         return list;
132     }
133
134     public ObjectArray values() {
135         ObjectArray list = new ObjectArray(size);
136         for(int i=0; i<keys.length; i++) {
137             Value k = keys[i];
138             if(k != null && k != ValueNull.DELETED) {
139                 list.add(values[i]);
140             }
141         }
142         return list;
143     }
144
145 }
146
Popular Tags