KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > index > HashIndex


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.index;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Session;
10 import org.h2.message.Message;
11 import org.h2.result.Row;
12 import org.h2.result.SearchRow;
13 import org.h2.table.Column;
14 import org.h2.table.TableData;
15 import org.h2.util.IntIntHashMap;
16 import org.h2.util.ValueHashMap;
17 import org.h2.value.Value;
18 import org.h2.value.ValueArray;
19
20 /**
21  * @author Thomas
22  */

23 public class HashIndex extends Index {
24
25     private ValueHashMap rows;
26     private IntIntHashMap intMap;
27     private TableData tableData;
28
29     public HashIndex(TableData table, int id, String JavaDoc indexName, Column[] columns, IndexType indexType) {
30         super(table, id, indexName, columns, indexType);
31         this.tableData = table;
32         reset();
33     }
34
35     private void reset() {
36         if(columns.length == 1 && columns[0].getType() == Value.INT) {
37             intMap = new IntIntHashMap();
38         } else {
39             rows = new ValueHashMap(table.getDatabase());
40         }
41     }
42
43     public void close(Session session) {
44         // nothing to do
45
}
46
47     public void truncate(Session session) throws SQLException JavaDoc {
48         reset();
49     }
50
51     public void remove(Session session) throws SQLException JavaDoc {
52         // nothing to do
53
}
54
55     public void add(Session session, Row row) throws SQLException JavaDoc {
56         if(intMap!=null) {
57             int key = row.getValue(columns[0].getColumnId()).getInt();
58             intMap.put(key, row.getPos());
59         } else {
60             Value key = getKey(row);
61             Object JavaDoc old = rows.get(key);
62             if (old != null) {
63                 // TODO index duplicate key for hash indexes: is this allowed?
64
throw getDuplicateKeyException();
65             }
66             Integer JavaDoc pos = new Integer JavaDoc(row.getPos());
67             rows.put(getKey(row), pos);
68         }
69         rowCount++;
70     }
71
72     public void remove(Session session, Row row) throws SQLException JavaDoc {
73         if(intMap!=null) {
74             int key = row.getValue(columns[0].getColumnId()).getInt();
75             intMap.remove(key);
76         } else {
77             rows.remove(getKey(row));
78         }
79         rowCount--;
80     }
81
82     private Value getKey(SearchRow row) {
83         if (columns.length == 1) {
84             Column column = columns[0];
85             int index = column.getColumnId();
86             Value v = row.getValue(index);
87             return v;
88         }
89         Value[] list = new Value[columns.length];
90         for (int i = 0; i < columns.length; i++) {
91             Column column = columns[i];
92             int index = column.getColumnId();
93             list[i] = row.getValue(index);
94         }
95         return ValueArray.get(list);
96     }
97
98     public Cursor find(Session session, SearchRow first, SearchRow last) throws SQLException JavaDoc {
99         if(first == null || last == null) {
100             // TODO hash index: should additionally check if values are the same
101
throw Message.getInternalError();
102         }
103         Row result;
104         if(intMap!=null) {
105             int key = first.getValue(columns[0].getColumnId()).getInt();
106             int pos = intMap.get(key);
107             if(pos != IntIntHashMap.NOT_FOUND) {
108                 result = tableData.getRow(pos);
109             } else {
110                 result = null;
111             }
112         } else {
113             Integer JavaDoc pos = (Integer JavaDoc) rows.get(getKey(first));
114             if (pos == null) {
115                 result = null;
116             } else {
117                 result = tableData.getRow(pos.intValue());
118             }
119         }
120         return new HashCursor(result);
121     }
122
123     public int getCost(int[] masks) {
124         for (int i = 0; i < columns.length; i++) {
125             Column column = columns[i];
126             int index = column.getColumnId();
127             int mask = masks[index];
128             if ((mask & IndexCondition.EQUALITY) != IndexCondition.EQUALITY) {
129                 return Integer.MAX_VALUE;
130             }
131         }
132         return 2;
133     }
134
135     public void checkRename() throws SQLException JavaDoc {
136         // ok
137
}
138
139     public boolean needRebuild() {
140         return true;
141     }
142
143     public boolean canGetFirstOrLast(boolean first) {
144         return false;
145     }
146
147     public Value findFirstOrLast(Session session, boolean first) throws SQLException JavaDoc {
148         throw Message.getUnsupportedException();
149     }
150
151 }
152
Popular Tags