1 | /* ============================================================= |
2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
3 | * ============================================================= |
4 | * |
5 | * (C) Copyright 2004-2006, by Volker Berlin. |
6 | * |
7 | * Project Info: http://www.smallsql.de/ |
8 | * |
9 | * This library is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU Lesser General Public License as published by |
11 | * the Free Software Foundation; either version 2.1 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | * License for more details. |
18 | * |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
22 | * USA. |
23 | * |
24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
25 | * in the United States and other countries.] |
26 | * |
27 | * --------------- |
28 | * IndexDescription.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | * Created on 19.04.2005 |
33 | */ |
34 | package smallsql.database; |
35 | |
36 | import java.io.File; |
37 | import java.io.RandomAccessFile; |
38 | import java.sql.SQLException; |
39 | |
40 | |
41 | |
42 | final class IndexDescription { |
43 | |
44 | static final int MAGIC_INDEX = 'S' << 24 | 'Q' << 16 | 'L' << 8 | 'I'; |
45 | static final int INDEX_VERSION = 1; |
46 | |
47 | private String name; |
48 | final private int constraintType; //PRIMARY, UNIQUE, FOREIGIN, INDEX |
49 | final private Strings columns; |
50 | private int[] matrix; |
51 | final private Expressions expressions; |
52 | private Index index; |
53 | |
54 | |
55 | /** |
56 | * |
57 | * @param constraintType one of SQLTokenizer.PRIMARY, SQLTokenizer.UNIQUE, SQLTokenizer.FOREIGN or SQLTokenizer.INDEX. |
58 | * @param columns the Expressions that build the index. For example one or more database columns. |
59 | */ |
60 | IndexDescription(int constraintType, Expressions expressions, Strings columns){ |
61 | this.constraintType = constraintType; |
62 | this.expressions = expressions; |
63 | this.columns = columns; |
64 | } |
65 | |
66 | |
67 | final void setName(String name){ |
68 | this.name = name; |
69 | } |
70 | |
71 | |
72 | final String getName(){ |
73 | return name; |
74 | } |
75 | |
76 | |
77 | final boolean isPrimary(){ |
78 | return constraintType == SQLTokenizer.PRIMARY; |
79 | } |
80 | |
81 | |
82 | final boolean isUnique(){ |
83 | return constraintType == SQLTokenizer.PRIMARY || constraintType == SQLTokenizer.UNIQUE; |
84 | } |
85 | |
86 | |
87 | final Strings getColumns(){ |
88 | return columns; |
89 | } |
90 | |
91 | |
92 | /** |
93 | * Descript how well the index match to the column list. |
94 | * @param strings a list of columns that should match |
95 | * @return Integer.MAX_VALUE does not match; 0 - 100% match |
96 | */ |
97 | final int matchFactor(Strings strings){ |
98 | if(strings.size() < columns.size()) |
99 | return Integer.MAX_VALUE; //does not map |
100 | |
101 | nextColumn: |
102 | for(int c=0; c<columns.size(); c++){ |
103 | String colName = columns.get(c); |
104 | for(int s=0; s<strings.size(); s++){ |
105 | if(colName.equalsIgnoreCase(strings.get(s)) ) |
106 | continue nextColumn; |
107 | } |
108 | return Integer.MAX_VALUE; //No Strin found for colName |
109 | } |
110 | return strings.size() - columns.size(); |
111 | } |
112 | |
113 | |
114 | /** |
115 | * Create a binding of the columns form this index to the columns of the table. |
116 | * @param database |
117 | * @param tableView |
118 | * @see IndexDescriptions#setTableView |
119 | */ |
120 | final void init(Database database, TableView tableView)/* throws Exception*/{ |
121 | int size = tableView.columns.size(); |
122 | matrix = new int[size]; |
123 | for(int i=0; i<matrix.length; i++){ |
124 | matrix[i] = -1; |
125 | } |
126 | |
127 | for(int i=0; i<columns.size(); i++){ |
128 | matrix[tableView.findColumnIdx(columns.get(i))] = i; |
129 | } |
130 | if(name == null){ |
131 | name = tableView.name + "_" + Long.toHexString(System.currentTimeMillis()) + Integer.toHexString(hashCode()); |
132 | } |
133 | |
134 | } |
135 | |
136 | |
137 | /** |
138 | * Create the index. A file for storing the index data is saved. |
139 | */ |
140 | final void create(Database database, TableView tableView) throws Exception{ |
141 | init( database, tableView ); |
142 | createFile( database ).close(); |
143 | } |
144 | |
145 | |
146 | static File getFile(Database database, String name) throws Exception{ |
147 | return new File( Utils.createIdxFileName( database, name ) ); |
148 | } |
149 | |
150 | |
151 | private RandomAccessFile createFile(Database database) throws Exception{ |
152 | File file = getFile( database, name ); |
153 | boolean ok = file.createNewFile(); |
154 | if(!ok) throw Utils.createSQLException("Index '" + name + "' allready exists."); |
155 | RandomAccessFile raFile = new RandomAccessFile( file, "rw" ); |
156 | writeMagic(raFile); |
157 | return raFile; |
158 | } |
159 | |
160 | |
161 | public void drop(Database database) throws Exception { |
162 | boolean ok = getFile( database, name).delete(); |
163 | if(!ok) throw Utils.createSQLException("Table '" + name + "' can't drop."); |
164 | } |
165 | |
166 | private final void writeMagic(RandomAccessFile raFile) throws Exception{ |
167 | raFile.writeInt(MAGIC_INDEX); |
168 | raFile.writeInt(INDEX_VERSION); |
169 | } |
170 | |
171 | |
172 | /** |
173 | * This is call if a single colum of the table is changed. |
174 | * @param columnIdx The column position in the table |
175 | * @param valueExpression the new value of the current row. |
176 | */ |
177 | final void writeExpression( int columnIdx, Expression valueExpression) { |
178 | int idx = matrix[columnIdx]; |
179 | if(idx >= 0) //set only if the column part of this index |
180 | expressions.set(idx, valueExpression); |
181 | } |
182 | |
183 | |
184 | /** |
185 | * This is call if the row is finsch written. |
186 | * @param con the connection for a later commit or rollback. |
187 | */ |
188 | final void writeFinsh(SSConnection con) { |
189 | //TODO |
190 | //index.addValues(expressions); |
191 | } |
192 | |
193 | |
194 | /** |
195 | * Save this IndexDescription in the Table definition. |
196 | */ |
197 | final void save(StoreImpl store) throws SQLException{ |
198 | store.writeInt(constraintType); |
199 | store.writeInt(columns.size()); |
200 | for(int c=0; c<columns.size(); c++){ |
201 | store.writeString( columns.get(c) ); |
202 | } |
203 | store.writeString(name); |
204 | } |
205 | |
206 | |
207 | /** |
208 | * Restore a IndexDescription from a saved Table. |
209 | */ |
210 | final static IndexDescription load(Database database, TableView tableView, StoreImpl store) throws SQLException{ |
211 | int constraintType = store.readInt(); |
212 | int count = store.readInt(); |
213 | Strings columns = new Strings(); |
214 | Expressions expressions = new Expressions(); |
215 | SQLParser sqlParser = new SQLParser(); |
216 | for(int c=0; c<count; c++){ |
217 | String column = store.readString(); |
218 | columns.add( column ); |
219 | expressions.add( sqlParser.parseExpression(column)); |
220 | } |
221 | IndexDescription indexDesc = new IndexDescription(constraintType, expressions, columns); |
222 | indexDesc.setName( store.readString() ); |
223 | indexDesc.init( database, tableView ); |
224 | return indexDesc; |
225 | } |
226 | |
227 | |
228 | } |