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.index; |
7 | |
8 | import java.util.ArrayList; |
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.result.SortOrder; |
14 | import org.h2.table.Column; |
15 | import org.h2.table.IndexColumn; |
16 | import org.h2.table.MetaTable; |
17 | import org.h2.table.TableFilter; |
18 | |
19 | /** |
20 | * The index implementation for meta data tables. |
21 | */ |
22 | public class MetaIndex extends BaseIndex { |
23 | |
24 | private final MetaTable meta; |
25 | private final boolean scan; |
26 | |
27 | public MetaIndex(MetaTable meta, IndexColumn[] columns, boolean scan) { |
28 | initBaseIndex(meta, 0, null, columns, IndexType.createNonUnique(true)); |
29 | this.meta = meta; |
30 | this.scan = scan; |
31 | } |
32 | |
33 | @Override |
34 | public void close(Session session) { |
35 | // nothing to do |
36 | } |
37 | |
38 | @Override |
39 | public void add(Session session, Row row) { |
40 | throw DbException.getUnsupportedException("META"); |
41 | } |
42 | |
43 | @Override |
44 | public void remove(Session session, Row row) { |
45 | throw DbException.getUnsupportedException("META"); |
46 | } |
47 | |
48 | @Override |
49 | public Cursor find(Session session, SearchRow first, SearchRow last) { |
50 | ArrayList<Row> rows = meta.generateRows(session, first, last); |
51 | return new MetaCursor(rows); |
52 | } |
53 | |
54 | @Override |
55 | public double getCost(Session session, int[] masks, TableFilter filter, |
56 | SortOrder sortOrder) { |
57 | if (scan) { |
58 | return 10 * MetaTable.ROW_COUNT_APPROXIMATION; |
59 | } |
60 | return getCostRangeIndex(masks, MetaTable.ROW_COUNT_APPROXIMATION, |
61 | filter, sortOrder); |
62 | } |
63 | |
64 | @Override |
65 | public void truncate(Session session) { |
66 | throw DbException.getUnsupportedException("META"); |
67 | } |
68 | |
69 | @Override |
70 | public void remove(Session session) { |
71 | throw DbException.getUnsupportedException("META"); |
72 | } |
73 | |
74 | @Override |
75 | public int getColumnIndex(Column col) { |
76 | if (scan) { |
77 | // the scan index cannot use any columns |
78 | return -1; |
79 | } |
80 | return super.getColumnIndex(col); |
81 | } |
82 | |
83 | @Override |
84 | public void checkRename() { |
85 | throw DbException.getUnsupportedException("META"); |
86 | } |
87 | |
88 | @Override |
89 | public boolean needRebuild() { |
90 | return false; |
91 | } |
92 | |
93 | @Override |
94 | public String getCreateSQL() { |
95 | return null; |
96 | } |
97 | |
98 | @Override |
99 | public boolean canGetFirstOrLast() { |
100 | return false; |
101 | } |
102 | |
103 | @Override |
104 | public Cursor findFirstOrLast(Session session, boolean first) { |
105 | throw DbException.getUnsupportedException("META"); |
106 | } |
107 | |
108 | @Override |
109 | public long getRowCount(Session session) { |
110 | return MetaTable.ROW_COUNT_APPROXIMATION; |
111 | } |
112 | |
113 | @Override |
114 | public long getRowCountApproximation() { |
115 | return MetaTable.ROW_COUNT_APPROXIMATION; |
116 | } |
117 | |
118 | @Override |
119 | public long getDiskSpaceUsed() { |
120 | return meta.getDiskSpaceUsed(); |
121 | } |
122 | |
123 | @Override |
124 | public String getPlanSQL() { |
125 | return "meta"; |
126 | } |
127 | |
128 | } |