Source Code Cross Referenced for HashIndex.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:
010:        import org.h2.engine.Session;
011:        import org.h2.message.Message;
012:        import org.h2.result.Row;
013:        import org.h2.result.SearchRow;
014:        import org.h2.table.Column;
015:        import org.h2.table.IndexColumn;
016:        import org.h2.table.TableData;
017:        import org.h2.util.IntIntHashMap;
018:        import org.h2.util.ObjectUtils;
019:        import org.h2.util.ValueHashMap;
020:        import org.h2.value.Value;
021:        import org.h2.value.ValueArray;
022:
023:        /**
024:         * An index based on an in-memory hash map.
025:         */
026:        public class HashIndex extends BaseIndex {
027:
028:            private ValueHashMap rows;
029:            private IntIntHashMap intMap;
030:            private TableData tableData;
031:
032:            public HashIndex(TableData table, int id, String indexName,
033:                    IndexColumn[] columns, IndexType indexType) {
034:                super (table, id, indexName, columns, indexType);
035:                this .tableData = table;
036:                reset();
037:            }
038:
039:            private void reset() {
040:                if (columns.length == 1 && columns[0].getType() == Value.INT) {
041:                    intMap = new IntIntHashMap();
042:                } else {
043:                    rows = new ValueHashMap(table.getDatabase());
044:                }
045:            }
046:
047:            public void close(Session session) {
048:                // nothing to do
049:            }
050:
051:            public void truncate(Session session) throws SQLException {
052:                reset();
053:            }
054:
055:            public void remove(Session session) throws SQLException {
056:                // nothing to do
057:            }
058:
059:            public void add(Session session, Row row) throws SQLException {
060:                if (intMap != null) {
061:                    int key = row.getValue(columns[0].getColumnId()).getInt();
062:                    intMap.put(key, row.getPos());
063:                } else {
064:                    Value key = getKey(row);
065:                    Object old = rows.get(key);
066:                    if (old != null) {
067:                        // TODO index duplicate key for hash indexes: is this allowed?
068:                        throw getDuplicateKeyException();
069:                    }
070:                    Integer pos = ObjectUtils.getInteger(row.getPos());
071:                    rows.put(getKey(row), pos);
072:                }
073:                rowCount++;
074:            }
075:
076:            public void remove(Session session, Row row) throws SQLException {
077:                if (intMap != null) {
078:                    int key = row.getValue(columns[0].getColumnId()).getInt();
079:                    intMap.remove(key);
080:                } else {
081:                    rows.remove(getKey(row));
082:                }
083:                rowCount--;
084:            }
085:
086:            private Value getKey(SearchRow row) {
087:                if (columns.length == 1) {
088:                    Column column = columns[0];
089:                    int index = column.getColumnId();
090:                    Value v = row.getValue(index);
091:                    return v;
092:                }
093:                Value[] list = new Value[columns.length];
094:                for (int i = 0; i < columns.length; i++) {
095:                    Column column = columns[i];
096:                    int index = column.getColumnId();
097:                    list[i] = row.getValue(index);
098:                }
099:                return ValueArray.get(list);
100:            }
101:
102:            public Cursor find(Session session, SearchRow first, SearchRow last)
103:                    throws SQLException {
104:                if (first == null || last == null) {
105:                    // TODO hash index: should additionally check if values are the same
106:                    throw Message.getInternalError();
107:                }
108:                Row result;
109:                if (intMap != null) {
110:                    int key = first.getValue(columns[0].getColumnId()).getInt();
111:                    int pos = intMap.get(key);
112:                    if (pos != IntIntHashMap.NOT_FOUND) {
113:                        result = tableData.getRow(session, pos);
114:                    } else {
115:                        result = null;
116:                    }
117:                } else {
118:                    Integer pos = (Integer) rows.get(getKey(first));
119:                    if (pos == null) {
120:                        result = null;
121:                    } else {
122:                        result = tableData.getRow(session, pos.intValue());
123:                    }
124:                }
125:                return new HashCursor(result);
126:            }
127:
128:            public double getCost(Session session, int[] masks) {
129:                for (int i = 0; i < columns.length; i++) {
130:                    Column column = columns[i];
131:                    int index = column.getColumnId();
132:                    int mask = masks[index];
133:                    if ((mask & IndexCondition.EQUALITY) != IndexCondition.EQUALITY) {
134:                        return Long.MAX_VALUE;
135:                    }
136:                }
137:                return 2;
138:            }
139:
140:            public void checkRename() throws SQLException {
141:                // ok
142:            }
143:
144:            public boolean needRebuild() {
145:                return true;
146:            }
147:
148:            public boolean canGetFirstOrLast() {
149:                return false;
150:            }
151:
152:            public SearchRow findFirstOrLast(Session session, boolean first)
153:                    throws SQLException {
154:                throw Message.getUnsupportedException();
155:            }
156:
157:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.