1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.mvstore.db; |
7 | |
8 | import java.util.List; |
9 | |
10 | import org.h2.engine.Session; |
11 | import org.h2.index.BaseIndex; |
12 | import org.h2.index.Cursor; |
13 | import org.h2.index.IndexType; |
14 | import org.h2.message.DbException; |
15 | import org.h2.result.Row; |
16 | import org.h2.result.SearchRow; |
17 | import org.h2.result.SortOrder; |
18 | import org.h2.table.Column; |
19 | import org.h2.table.IndexColumn; |
20 | import org.h2.table.TableFilter; |
21 | import org.h2.value.ValueLong; |
22 | |
23 | /** |
24 | * An index that delegates indexing to another index. |
25 | */ |
26 | public class MVDelegateIndex extends BaseIndex implements MVIndex { |
27 | |
28 | private final MVPrimaryIndex mainIndex; |
29 | |
30 | public MVDelegateIndex(MVTable table, int id, String name, |
31 | MVPrimaryIndex mainIndex, |
32 | IndexType indexType) { |
33 | IndexColumn[] cols = IndexColumn.wrap(new Column[] { table |
34 | .getColumn(mainIndex.getMainIndexColumn()) }); |
35 | this.initBaseIndex(table, id, name, cols, indexType); |
36 | this.mainIndex = mainIndex; |
37 | if (id < 0) { |
38 | throw DbException.throwInternalError("" + name); |
39 | } |
40 | } |
41 | |
42 | @Override |
43 | public void addRowsToBuffer(List<Row> rows, String bufferName) { |
44 | throw DbException.throwInternalError(); |
45 | } |
46 | |
47 | @Override |
48 | public void addBufferedRows(List<String> bufferNames) { |
49 | throw DbException.throwInternalError(); |
50 | } |
51 | |
52 | @Override |
53 | public void add(Session session, Row row) { |
54 | // nothing to do |
55 | } |
56 | |
57 | @Override |
58 | public boolean canGetFirstOrLast() { |
59 | return true; |
60 | } |
61 | |
62 | @Override |
63 | public void close(Session session) { |
64 | // nothing to do |
65 | } |
66 | |
67 | @Override |
68 | public Cursor find(Session session, SearchRow first, SearchRow last) { |
69 | ValueLong min = mainIndex.getKey(first, |
70 | MVPrimaryIndex.MIN, MVPrimaryIndex.MIN); |
71 | // ifNull is MIN_VALUE as well, because the column is never NULL |
72 | // so avoid returning all rows (returning one row is OK) |
73 | ValueLong max = mainIndex.getKey(last, |
74 | MVPrimaryIndex.MAX, MVPrimaryIndex.MIN); |
75 | return mainIndex.find(session, min, max); |
76 | } |
77 | |
78 | @Override |
79 | public Cursor findFirstOrLast(Session session, boolean first) { |
80 | return mainIndex.findFirstOrLast(session, first); |
81 | } |
82 | |
83 | @Override |
84 | public int getColumnIndex(Column col) { |
85 | if (col.getColumnId() == mainIndex.getMainIndexColumn()) { |
86 | return 0; |
87 | } |
88 | return -1; |
89 | } |
90 | |
91 | @Override |
92 | public double getCost(Session session, int[] masks, TableFilter filter, |
93 | SortOrder sortOrder) { |
94 | return 10 * getCostRangeIndex(masks, |
95 | mainIndex.getRowCountApproximation(), filter, sortOrder); |
96 | } |
97 | |
98 | @Override |
99 | public boolean needRebuild() { |
100 | return false; |
101 | } |
102 | |
103 | @Override |
104 | public void remove(Session session, Row row) { |
105 | // nothing to do |
106 | } |
107 | |
108 | @Override |
109 | public void remove(Session session) { |
110 | mainIndex.setMainIndexColumn(-1); |
111 | } |
112 | |
113 | @Override |
114 | public void truncate(Session session) { |
115 | // nothing to do |
116 | } |
117 | |
118 | @Override |
119 | public void checkRename() { |
120 | // ok |
121 | } |
122 | |
123 | @Override |
124 | public long getRowCount(Session session) { |
125 | return mainIndex.getRowCount(session); |
126 | } |
127 | |
128 | @Override |
129 | public long getRowCountApproximation() { |
130 | return mainIndex.getRowCountApproximation(); |
131 | } |
132 | |
133 | @Override |
134 | public long getDiskSpaceUsed() { |
135 | return 0; |
136 | } |
137 | |
138 | } |