Source Code Cross Referenced for ViewIndex.java in  » Database-DBMS » h2database » org » h2 » index » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » h2database » org.h2.index 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.