KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > DataIndexSetDef


1 /**
2  * com.mckoi.database.DataIndexSetDef 08 Sep 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.io.IOException JavaDoc;
28 import java.io.DataInput JavaDoc;
29 import java.io.DataOutput JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 /**
33  * Represents the meta-data for a set of indexes of a table.
34  *
35  * @author Tobias Downer
36  */

37
38 public class DataIndexSetDef {
39
40   /**
41    * The TableName this index set meta data is for.
42    */

43   private TableName table_name;
44   
45   /**
46    * The list of indexes in the table.
47    */

48   private ArrayList JavaDoc index_list;
49   
50   /**
51    * True if this object is immutable.
52    */

53   private boolean immutable;
54   
55   /**
56    * Constructor.
57    */

58   public DataIndexSetDef(TableName table_name) {
59     this.table_name = table_name;
60     index_list = new ArrayList JavaDoc();
61     immutable = false;
62   }
63
64   public DataIndexSetDef(DataIndexSetDef def) {
65     this.table_name = def.table_name;
66     index_list = new ArrayList JavaDoc();
67     for (int i = 0; i < def.indexCount(); ++i) {
68       index_list.add(new DataIndexDef(def.indexAt(i)));
69     }
70     immutable = false;
71   }
72   
73   /**
74    * Sets the immutable flag.
75    */

76   public void setImmutable() {
77     this.immutable = true;
78   }
79   
80   /**
81    * Adds a DataIndexDef to this table.
82    */

83   public void addDataIndexDef(DataIndexDef def) {
84     if (!immutable) {
85       index_list.add(def);
86     }
87     else {
88       throw new RuntimeException JavaDoc("Tried to add index to immutable def.");
89     }
90   }
91   
92   /**
93    * Removes a DataIndexDef to this table.
94    */

95   public void removeDataIndexDef(int i) {
96     if (!immutable) {
97       index_list.remove(i);
98     }
99     else {
100       throw new RuntimeException JavaDoc("Tried to add index to immutable def.");
101     }
102   }
103
104   /**
105    * Returns the total number of index in this table.
106    */

107   public int indexCount() {
108     return index_list.size();
109   }
110   
111   /**
112    * Returns the DataIndexDef at the given index in this list.
113    */

114   public DataIndexDef indexAt(int i) {
115     return (DataIndexDef) index_list.get(i);
116   }
117
118   /**
119    * Finds the index with the given name and returns the index in the list of
120    * the index (confusing comment!). Returns -1 if the name wasn't found.
121    */

122   public int findIndexWithName(String JavaDoc index_name) {
123     int sz = indexCount();
124     for (int i = 0; i < sz; ++i) {
125       if (indexAt(i).getName().equals(index_name)) {
126         return i;
127       }
128     }
129     return -1;
130   }
131   
132   /**
133    * Finds the first index for the given column name list. Returns -1 if an
134    * index over the given composite columns was not found.
135    */

136   public int findIndexForColumns(String JavaDoc[] cols) {
137     int sz = indexCount();
138     for (int i = 0; i < sz; ++i) {
139       String JavaDoc[] t_cols = indexAt(i).getColumnNames();
140       if (t_cols.length == cols.length) {
141         boolean passed = true;
142         for (int n = 0; n < t_cols.length && passed; ++n) {
143           if (!t_cols[n].equals(cols[n])) {
144             passed = false;
145           }
146         }
147         if (passed) {
148           return i;
149         }
150       }
151     }
152     return -1;
153   }
154   
155   /**
156    * Returns the DataIndexDef with the given name or null if it couldn't be
157    * found.
158    */

159   public DataIndexDef indexWithName(String JavaDoc index_name) {
160     int i = findIndexWithName(index_name);
161     if (i != -1) {
162       return indexAt(i);
163     }
164     else {
165       return null;
166     }
167   }
168   
169   /**
170    * Attempts to resolve the given index name from the index in this table.
171    * If 'ignore_case' is true, then we return the correct case of the index
172    * name.
173    */

174   public String JavaDoc resolveIndexName(String JavaDoc index_name, boolean ignore_case)
175                                                     throws DatabaseException {
176     int sz = indexCount();
177     String JavaDoc found = null;
178     for (int i = 0; i < sz; ++i) {
179       boolean passed;
180       String JavaDoc cur_index_name = indexAt(i).getName();
181       if (ignore_case) {
182         passed = cur_index_name.equalsIgnoreCase(index_name);
183       }
184       else {
185         passed = cur_index_name.equals(index_name);
186       }
187       if (passed) {
188         if (found != null) {
189           throw new DatabaseException("Ambigious index name '" +
190                                       index_name + "'");
191         }
192         found = cur_index_name;
193       }
194     }
195     if (found == null) {
196       throw new DatabaseException("Index '" + index_name + "' not found.");
197     }
198     return found;
199   }
200
201   /**
202    * Writes this DataIndexSetDef object to the given DataOutput.
203    */

204   public void write(DataOutput JavaDoc dout) throws IOException JavaDoc {
205     dout.writeInt(1);
206     dout.writeUTF(table_name.getSchema());
207     dout.writeUTF(table_name.getName());
208     dout.writeInt(index_list.size());
209     for (int i = 0; i < index_list.size(); ++i) {
210       ((DataIndexDef) index_list.get(i)).write(dout);
211     }
212   }
213
214   /**
215    * Reads the DataIndexSetDef object from the given DataInput.
216    */

217   public static DataIndexSetDef read(DataInput JavaDoc din) throws IOException JavaDoc {
218     int version = din.readInt();
219     if (version != 1) {
220       throw new IOException JavaDoc("Don't understand version.");
221     }
222     String JavaDoc schema = din.readUTF();
223     String JavaDoc name = din.readUTF();
224     int sz = din.readInt();
225     DataIndexSetDef index_set =
226                              new DataIndexSetDef(new TableName(schema, name));
227     for (int i = 0; i < sz; ++i) {
228       index_set.addDataIndexDef(DataIndexDef.read(din));
229     }
230
231     return index_set;
232   }
233   
234
235 }
236
237
Popular Tags