File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\index\HashIndex.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.index;
8
9 import org.h2.engine.Session;
10 import org.h2.message.DbException;
11 import org.h2.result.Row;
12 import org.h2.result.SearchRow;
13 import org.h2.table.Column;
14 import org.h2.table.IndexColumn;
15 import org.h2.table.RegularTable;
16 import org.h2.util.ValueHashMap;
17 import org.h2.value.Value;
18
19 /**
20 * An unique index based on an in-memory hash map.
21 */
22 public class HashIndex extends BaseIndex {
23
24 /**
25 * The index of the indexed column.
26 */
27 protected final int indexColumn;
28
29 private final RegularTable tableData;
30 private ValueHashMap<Long> rows;
31
32 public HashIndex(RegularTable table, int id, String indexName, IndexColumn[] columns, IndexType indexType) {
33 initBaseIndex(table, id, indexName, columns, indexType);
34 this.indexColumn = columns[0].column.getColumnId();
35 this.tableData = table;
36 reset();
37 }
38
39 private void reset() {
40 rows = ValueHashMap.newInstance();
41 }
42
43 public void truncate(Session session) {
44 reset();
45 }
46
47 public void add(Session session, Row row) {
48 Value key = row.getValue(indexColumn);
49 Object old = rows.get(key);
50 if (old != null) {
51 // TODO index duplicate key for hash indexes: is this allowed?
52 throw getDuplicateKeyException();
53 }
54 rows.put(key, row.getKey());
55 }
56
57 public void remove(Session session, Row row) {
58 rows.remove(row.getValue(indexColumn));
59 }
60
61 public Cursor find(Session session, SearchRow first, SearchRow last) {
62 if (first == null || last == null) {
63 // TODO hash index: should additionally check if values are the same
64 throw DbException.throwInternalError();
65 }
66 Row result;
67 Long pos = rows.get(first.getValue(indexColumn));
68 if (pos == null) {
69 result = null;
70 } else {
71 result = tableData.getRow(session, pos.intValue());
72 }
73 return new SingleRowCursor(result);
74 }
75
76 public long getRowCount(Session session) {
77 return getRowCountApproximation();
78 }
79
80 public long getRowCountApproximation() {
81 return rows.size();
82 }
83
84 public void close(Session session) {
85 // nothing to do
86 }
87
88 public void remove(Session session) {
89 // nothing to do
90 }
91
92 public double getCost(Session session, int[] masks) {
93 for (Column column : columns) {
94 int index = column.getColumnId();
95 int mask = masks[index];
96 if ((mask & IndexCondition.EQUALITY) != IndexCondition.EQUALITY) {
97 return Long.MAX_VALUE;
98 }
99 }
100 return 2;
101 }
102
103 public void checkRename() {
104 // ok
105 }
106
107 public boolean needRebuild() {
108 return true;
109 }
110
111 public boolean canGetFirstOrLast() {
112 return false;
113 }
114
115 public Cursor findFirstOrLast(Session session, boolean first) {
116 throw DbException.getUnsupportedException("HASH");
117 }
118
119 public boolean canScan() {
120 return false;
121 }
122
123 }
124