001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.index;
007:
008: import java.sql.SQLException;
009: import org.h2.command.dml.Query;
010: import org.h2.engine.Constants;
011: import org.h2.engine.Session;
012: import org.h2.expression.Comparison;
013: import org.h2.expression.Parameter;
014: import org.h2.message.Message;
015: import org.h2.result.LocalResult;
016: import org.h2.result.Row;
017: import org.h2.result.SearchRow;
018: import org.h2.table.Column;
019: import org.h2.table.TableView;
020: import org.h2.util.IntArray;
021: import org.h2.util.ObjectArray;
022: import org.h2.util.SmallLRUCache;
023: import org.h2.value.Value;
024:
025: /**
026: * This object represents a virtual index for a query.
027: * Actually it only represents a prepared SELECT statement.
028: */
029: public class ViewIndex extends BaseIndex {
030:
031: private String querySQL;
032: private ObjectArray originalParameters;
033: private SmallLRUCache costCache = new SmallLRUCache(
034: Constants.VIEW_INDEX_CACHE_SIZE);
035: private boolean recursive;
036: private int[] masks;
037: private String planSQL;
038: private Query query;
039: private Session session;
040:
041: public ViewIndex(TableView view, String querySQL,
042: ObjectArray originalParameters, boolean recursive) {
043: super (view, 0, null, null, IndexType.createNonUnique(false));
044: this .querySQL = querySQL;
045: this .originalParameters = originalParameters;
046: this .recursive = recursive;
047: columns = new Column[0];
048: }
049:
050: public ViewIndex(TableView view, ViewIndex index, Session session,
051: int[] masks) throws SQLException {
052: super (view, 0, null, null, IndexType.createNonUnique(false));
053: this .querySQL = index.querySQL;
054: this .originalParameters = index.originalParameters;
055: this .recursive = index.recursive;
056: this .masks = masks;
057: this .session = session;
058: columns = new Column[0];
059: query = getQuery(session, masks);
060: planSQL = query.getPlanSQL();
061: }
062:
063: public Session getSession() {
064: return session;
065: }
066:
067: public String getPlanSQL() {
068: return planSQL;
069: }
070:
071: public void close(Session session) throws SQLException {
072: }
073:
074: public void add(Session session, Row row) throws SQLException {
075: throw Message.getUnsupportedException();
076: }
077:
078: public void remove(Session session, Row row) throws SQLException {
079: throw Message.getUnsupportedException();
080: }
081:
082: private static class CostElement {
083: long evaluatedAt;
084: double cost;
085: }
086:
087: public double getCost(Session session, int[] masks)
088: throws SQLException {
089: IntArray masksArray = new IntArray(masks == null ? new int[0]
090: : masks);
091: CostElement cachedCost = (CostElement) costCache
092: .get(masksArray);
093: if (cachedCost != null) {
094: long time = System.currentTimeMillis();
095: if (time < cachedCost.evaluatedAt
096: + Constants.VIEW_COST_CACHE_MAX_AGE) {
097: return cachedCost.cost;
098: }
099: }
100: Query query = (Query) session.prepare(querySQL, true);
101: if (masks == null) {
102: columns = new Column[0];
103: } else {
104: IntArray paramIndex = new IntArray();
105: for (int i = 0; i < masks.length; i++) {
106: int mask = masks[i];
107: if (mask == 0) {
108: continue;
109: }
110: paramIndex.add(i);
111: }
112: int len = paramIndex.size();
113: columns = new Column[len];
114: for (int i = 0; i < len; i++) {
115: int idx = paramIndex.get(i);
116: Column col = table.getColumn(idx);
117: columns[i] = col;
118: int mask = masks[idx];
119: int nextParamIndex = query.getParameters().size();
120: if ((mask & IndexCondition.EQUALITY) != 0) {
121: Parameter param = new Parameter(nextParamIndex);
122: query.addGlobalCondition(param, idx,
123: Comparison.EQUAL);
124: } else {
125: if ((mask & IndexCondition.START) != 0) {
126: Parameter param = new Parameter(nextParamIndex);
127: query.addGlobalCondition(param, idx,
128: Comparison.BIGGER_EQUAL);
129: }
130: if ((mask & IndexCondition.END) != 0) {
131: Parameter param = new Parameter(nextParamIndex);
132: query.addGlobalCondition(param, idx,
133: Comparison.SMALLER_EQUAL);
134: }
135: }
136: }
137: if (recursive) {
138: return 10;
139: }
140: String sql = query.getPlanSQL();
141: query = (Query) session.prepare(sql);
142: }
143: double cost = query.getCost();
144: cachedCost = new CostElement();
145: cachedCost.evaluatedAt = System.currentTimeMillis();
146: cachedCost.cost = cost;
147: costCache.put(masksArray, cachedCost);
148: return cost;
149: }
150:
151: public Cursor find(Session session, SearchRow first, SearchRow last)
152: throws SQLException {
153: ObjectArray paramList = query.getParameters();
154: int idx = 0;
155: for (int i = 0; originalParameters != null
156: && i < originalParameters.size(); i++) {
157: Parameter orig = (Parameter) originalParameters.get(i);
158: Value value = orig.getValue(session);
159: Parameter param = (Parameter) paramList.get(idx++);
160: param.setValue(value);
161: }
162: int len;
163: if (first != null) {
164: len = first.getColumnCount();
165: } else if (last != null) {
166: len = last.getColumnCount();
167: } else {
168: len = 0;
169: }
170: for (int i = 0; i < len; i++) {
171: if (first != null) {
172: Value v = first.getValue(i);
173: if (v != null) {
174: Parameter param = (Parameter) paramList.get(idx++);
175: param.setValue(v);
176: }
177: }
178: // for equality, only one parameter is used (first == last)
179: if (last != null && masks[i] != IndexCondition.EQUALITY) {
180: Value v = last.getValue(i);
181: if (v != null) {
182: Parameter param = (Parameter) paramList.get(idx++);
183: param.setValue(v);
184: }
185: }
186: }
187: LocalResult result = query.query(0);
188: return new ViewCursor(table, result);
189: }
190:
191: private Query getQuery(Session session, int[] masks)
192: throws SQLException {
193: Query query = (Query) session.prepare(querySQL, true);
194: if (masks == null) {
195: return query;
196: }
197: int firstIndexParam = query.getParameters().size();
198: IntArray paramIndex = new IntArray();
199: for (int i = 0; i < masks.length; i++) {
200: int mask = masks[i];
201: if (mask == 0) {
202: continue;
203: }
204: paramIndex.add(i);
205: if ((mask & IndexCondition.RANGE) == IndexCondition.RANGE) {
206: // two parameters for range queries: >= x AND <= y
207: paramIndex.add(i);
208: }
209: }
210: int len = paramIndex.size();
211: columns = new Column[len];
212: for (int i = 0; i < len;) {
213: int idx = paramIndex.get(i);
214: Column col = table.getColumn(idx);
215: columns[i] = col;
216: int mask = masks[idx];
217: if ((mask & IndexCondition.EQUALITY) == IndexCondition.EQUALITY) {
218: Parameter param = new Parameter(firstIndexParam + i);
219: query.addGlobalCondition(param, idx, Comparison.EQUAL);
220: i++;
221: } else {
222: if ((mask & IndexCondition.START) == IndexCondition.START) {
223: Parameter param = new Parameter(firstIndexParam + i);
224: query.addGlobalCondition(param, idx,
225: Comparison.BIGGER_EQUAL);
226: i++;
227: }
228: if ((mask & IndexCondition.END) == IndexCondition.END) {
229: Parameter param = new Parameter(firstIndexParam + i);
230: query.addGlobalCondition(param, idx,
231: Comparison.SMALLER_EQUAL);
232: i++;
233: }
234: }
235: }
236: String sql = query.getPlanSQL();
237: query = (Query) session.prepare(sql, true);
238: return query;
239: }
240:
241: public void remove(Session session) throws SQLException {
242: throw Message.getUnsupportedException();
243: }
244:
245: public void truncate(Session session) throws SQLException {
246: throw Message.getUnsupportedException();
247: }
248:
249: public void checkRename() throws SQLException {
250: throw Message.getUnsupportedException();
251: }
252:
253: public boolean needRebuild() {
254: return false;
255: }
256:
257: public boolean canGetFirstOrLast() {
258: return false;
259: }
260:
261: public SearchRow findFirstOrLast(Session session, boolean first)
262: throws SQLException {
263: throw Message.getUnsupportedException();
264: }
265:
266: public void setRecursive(boolean value) {
267: this.recursive = value;
268: }
269:
270: }
|