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 org.h2.engine.Session; |
9 | import org.h2.message.DbException; |
10 | import org.h2.result.Row; |
11 | import org.h2.result.SearchRow; |
12 | import org.h2.result.SortOrder; |
13 | import org.h2.table.IndexColumn; |
14 | import org.h2.table.RangeTable; |
15 | import org.h2.table.TableFilter; |
16 | |
17 | /** |
18 | * An index for the SYSTEM_RANGE table. |
19 | * This index can only scan through all rows, search is not supported. |
20 | */ |
21 | public class RangeIndex extends BaseIndex { |
22 | |
23 | private final RangeTable rangeTable; |
24 | |
25 | public RangeIndex(RangeTable table, IndexColumn[] columns) { |
26 | initBaseIndex(table, 0, "RANGE_INDEX", columns, |
27 | IndexType.createNonUnique(true)); |
28 | this.rangeTable = table; |
29 | } |
30 | |
31 | @Override |
32 | public void close(Session session) { |
33 | // nothing to do |
34 | } |
35 | |
36 | @Override |
37 | public void add(Session session, Row row) { |
38 | throw DbException.getUnsupportedException("SYSTEM_RANGE"); |
39 | } |
40 | |
41 | @Override |
42 | public void remove(Session session, Row row) { |
43 | throw DbException.getUnsupportedException("SYSTEM_RANGE"); |
44 | } |
45 | |
46 | @Override |
47 | public Cursor find(Session session, SearchRow first, SearchRow last) { |
48 | long min = rangeTable.getMin(session), start = min; |
49 | long max = rangeTable.getMax(session), end = max; |
50 | long step = rangeTable.getStep(session); |
51 | try { |
52 | start = Math.max(min, first == null ? min : first.getValue(0).getLong()); |
53 | } catch (Exception e) { |
54 | // error when converting the value - ignore |
55 | } |
56 | try { |
57 | end = Math.min(max, last == null ? max : last.getValue(0).getLong()); |
58 | } catch (Exception e) { |
59 | // error when converting the value - ignore |
60 | } |
61 | return new RangeCursor(start, end, step); |
62 | } |
63 | |
64 | @Override |
65 | public double getCost(Session session, int[] masks, TableFilter filter, |
66 | SortOrder sortOrder) { |
67 | return 1; |
68 | } |
69 | |
70 | @Override |
71 | public String getCreateSQL() { |
72 | return null; |
73 | } |
74 | |
75 | @Override |
76 | public void remove(Session session) { |
77 | throw DbException.getUnsupportedException("SYSTEM_RANGE"); |
78 | } |
79 | |
80 | @Override |
81 | public void truncate(Session session) { |
82 | throw DbException.getUnsupportedException("SYSTEM_RANGE"); |
83 | } |
84 | |
85 | @Override |
86 | public boolean needRebuild() { |
87 | return false; |
88 | } |
89 | |
90 | @Override |
91 | public void checkRename() { |
92 | throw DbException.getUnsupportedException("SYSTEM_RANGE"); |
93 | } |
94 | |
95 | @Override |
96 | public boolean canGetFirstOrLast() { |
97 | return true; |
98 | } |
99 | |
100 | @Override |
101 | public Cursor findFirstOrLast(Session session, boolean first) { |
102 | long pos = first ? rangeTable.getMin(session) : rangeTable.getMax(session); |
103 | return new RangeCursor(pos, pos); |
104 | } |
105 | |
106 | @Override |
107 | public long getRowCount(Session session) { |
108 | return rangeTable.getRowCountApproximation(); |
109 | } |
110 | |
111 | @Override |
112 | public long getRowCountApproximation() { |
113 | return rangeTable.getRowCountApproximation(); |
114 | } |
115 | |
116 | @Override |
117 | public long getDiskSpaceUsed() { |
118 | return 0; |
119 | } |
120 | } |