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.FunctionTable; |
14 | import org.h2.table.IndexColumn; |
15 | import org.h2.table.TableFilter; |
16 | |
17 | /** |
18 | * An index for a function that returns a result set. This index can only scan |
19 | * through all rows, search is not supported. |
20 | */ |
21 | public class FunctionIndex extends BaseIndex { |
22 | |
23 | private final FunctionTable functionTable; |
24 | |
25 | public FunctionIndex(FunctionTable functionTable, IndexColumn[] columns) { |
26 | initBaseIndex(functionTable, 0, null, columns, IndexType.createNonUnique(true)); |
27 | this.functionTable = functionTable; |
28 | } |
29 | |
30 | @Override |
31 | public void close(Session session) { |
32 | // nothing to do |
33 | } |
34 | |
35 | @Override |
36 | public void add(Session session, Row row) { |
37 | throw DbException.getUnsupportedException("ALIAS"); |
38 | } |
39 | |
40 | @Override |
41 | public void remove(Session session, Row row) { |
42 | throw DbException.getUnsupportedException("ALIAS"); |
43 | } |
44 | |
45 | @Override |
46 | public Cursor find(Session session, SearchRow first, SearchRow last) { |
47 | if (functionTable.isBufferResultSetToLocalTemp()) { |
48 | return new FunctionCursor(functionTable.getResult(session)); |
49 | } |
50 | return new FunctionCursorResultSet(session, |
51 | functionTable.getResultSet(session)); |
52 | } |
53 | |
54 | @Override |
55 | public double getCost(Session session, int[] masks, TableFilter filter, |
56 | SortOrder sortOrder) { |
57 | if (masks != null) { |
58 | throw DbException.getUnsupportedException("ALIAS"); |
59 | } |
60 | long expectedRows; |
61 | if (functionTable.canGetRowCount()) { |
62 | expectedRows = functionTable.getRowCountApproximation(); |
63 | } else { |
64 | expectedRows = database.getSettings().estimatedFunctionTableRows; |
65 | } |
66 | return expectedRows * 10; |
67 | } |
68 | |
69 | @Override |
70 | public void remove(Session session) { |
71 | throw DbException.getUnsupportedException("ALIAS"); |
72 | } |
73 | |
74 | @Override |
75 | public void truncate(Session session) { |
76 | throw DbException.getUnsupportedException("ALIAS"); |
77 | } |
78 | |
79 | @Override |
80 | public boolean needRebuild() { |
81 | return false; |
82 | } |
83 | |
84 | @Override |
85 | public void checkRename() { |
86 | throw DbException.getUnsupportedException("ALIAS"); |
87 | } |
88 | |
89 | @Override |
90 | public boolean canGetFirstOrLast() { |
91 | return false; |
92 | } |
93 | |
94 | @Override |
95 | public Cursor findFirstOrLast(Session session, boolean first) { |
96 | throw DbException.getUnsupportedException("ALIAS"); |
97 | } |
98 | |
99 | @Override |
100 | public long getRowCount(Session session) { |
101 | return functionTable.getRowCount(session); |
102 | } |
103 | |
104 | @Override |
105 | public long getRowCountApproximation() { |
106 | return functionTable.getRowCountApproximation(); |
107 | } |
108 | |
109 | @Override |
110 | public long getDiskSpaceUsed() { |
111 | return 0; |
112 | } |
113 | |
114 | @Override |
115 | public String getPlanSQL() { |
116 | return "function"; |
117 | } |
118 | |
119 | @Override |
120 | public boolean canScan() { |
121 | return false; |
122 | } |
123 | |
124 | } |