KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.CompositeTable 28 Oct 2001
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 com.mckoi.util.IntegerVector;
28
29 /**
30  * A composite of two or more datasets used to implement UNION, INTERSECTION,
31  * and DIFFERENCE.
32  *
33  * @author Tobias Downer
34  */

35
36 public class CompositeTable extends Table implements RootTable {
37
38   // ---------- Statics ----------
39

40   /**
41    * The composite function for finding the union of the tables.
42    */

43   public static int UNION = 1;
44
45   /**
46    * The composite function for finding the interestion of the tables.
47    */

48   public static int INTERSECT = 2;
49
50   /**
51    * The composite function for finding the difference of the tables.
52    */

53   public static int EXCEPT = 3;
54
55
56   // ---------- Members ----------
57

58   /**
59    * The 'master table' used to resolve information about this table such as
60    * fields and field types.
61    */

62   private Table master_table;
63
64   /**
65    * The tables being made a composite of.
66    */

67   private Table[] composite_tables;
68
69   /**
70    * The list of indexes of rows to include in each table.
71    */

72   private IntegerVector[] table_indexes;
73
74   /**
75    * The schemes to describe the entity relation in the given column.
76    */

77   private SelectableScheme[] column_scheme;
78
79   /**
80    * The number of root locks on this table.
81    */

82   private int roots_locked;
83
84   /**
85    * Constructs the composite table given the 'master_table' (the field
86    * structure this composite dataset is based on), and a list of tables to
87    * be the composite of this table. The 'master_table' must be one of the
88    * elements of the 'composite_list' array.
89    * <p>
90    * NOTE: This does not set up table indexes for a composite function.
91    */

92   public CompositeTable(Table master_table, Table[] composite_list) {
93     super();
94     this.master_table = master_table;
95     this.composite_tables = composite_list;
96     this.column_scheme = new SelectableScheme[master_table.getColumnCount()];
97   }
98
99   /**
100    * Consturcts the composite table assuming the first item in the list is the
101    * master table.
102    */

103   public CompositeTable(Table[] composite_list) {
104     this(composite_list[0], composite_list);
105   }
106
107
108   /**
109    * Removes duplicate rows from the table. If 'pre_sorted' is true then each
110    * composite index is already in sorted order.
111    */

112   private void removeDuplicates(boolean pre_sorted) {
113     throw new Error JavaDoc("PENDING");
114   }
115
116   /**
117    * Sets up the indexes in this composite table by performing for composite
118    * function on the tables. If the 'all' parameter is true then duplicate
119    * rows are removed.
120    */

121   public void setupIndexesForCompositeFunction(int function, boolean all) {
122     int size = composite_tables.length;
123     table_indexes = new IntegerVector[size];
124
125     if (function == UNION) {
126       // Include all row sets in all tables
127
for (int i = 0; i < size; ++i) {
128         table_indexes[i] = composite_tables[i].selectAll();
129       }
130       if (!all) {
131         removeDuplicates(false);
132       }
133     }
134     else {
135       throw new Error JavaDoc("Unrecognised composite function");
136     }
137
138   }
139
140   // ---------- Implemented from Table ----------
141

142   public Database getDatabase() {
143     return master_table.getDatabase();
144   }
145
146   public int getColumnCount() {
147     return master_table.getColumnCount();
148   }
149
150   public int getRowCount() {
151     int row_count = 0;
152     for (int i = 0; i < table_indexes.length; ++i) {
153       row_count += table_indexes[i].size();
154     }
155     return row_count;
156   }
157
158   public int findFieldName(Variable v) {
159     return master_table.findFieldName(v);
160   }
161
162   public DataTableDef getDataTableDef() {
163     return master_table.getDataTableDef();
164   }
165
166   public Variable getResolvedVariable(int column) {
167     return master_table.getResolvedVariable(column);
168   }
169
170   SelectableScheme getSelectableSchemeFor(int column,
171                                           int original_column, Table table) {
172
173     SelectableScheme scheme = column_scheme[column];
174     if (scheme == null) {
175       scheme = new BlindSearch(this, column);
176       column_scheme[column] = scheme;
177     }
178
179     // If we are getting a scheme for this table, simple return the information
180
// from the column_trees Vector.
181
if (table == this) {
182       return scheme;
183     }
184     // Otherwise, get the scheme to calculate a subset of the given scheme.
185
else {
186       return scheme.getSubsetScheme(table, original_column);
187     }
188   }
189
190   void setToRowTableDomain(int column, IntegerVector row_set,
191                            TableDataSource ancestor) {
192     if (ancestor != this) {
193       throw new RuntimeException JavaDoc("Method routed to incorrect table ancestor.");
194     }
195   }
196
197   RawTableInformation resolveToRawTable(RawTableInformation info) {
198     System.err.println("Efficiency Warning in DataTable.resolveToRawTable.");
199     IntegerVector row_set = new IntegerVector();
200     RowEnumeration e = rowEnumeration();
201     while (e.hasMoreRows()) {
202       row_set.addInt(e.nextRowIndex());
203     }
204     info.add(this, row_set);
205     return info;
206   }
207
208   public TObject getCellContents(int column, int row) {
209     for (int i = 0; i < table_indexes.length; ++i) {
210       IntegerVector ivec = table_indexes[i];
211       int sz = ivec.size();
212       if (row < sz) {
213         return composite_tables[i].getCellContents(column, ivec.intAt(row));
214       }
215       else {
216         row -= sz;
217       }
218     }
219     throw new Error JavaDoc("Row '" + row + "' out of bounds.");
220   }
221
222   public RowEnumeration rowEnumeration() {
223     return new SimpleRowEnumeration(getRowCount());
224   }
225
226   void addDataTableListener(DataTableListener listener) {
227     for (int i = 0; i < composite_tables.length; ++i) {
228       composite_tables[i].addDataTableListener(listener);
229     }
230   }
231
232   void removeDataTableListener(DataTableListener listener) {
233     for (int i = 0; i < composite_tables.length; ++i) {
234       composite_tables[i].removeDataTableListener(listener);
235     }
236   }
237
238   public void lockRoot(int lock_key) {
239     // For each table, recurse.
240
roots_locked++;
241     for (int i = 0; i < composite_tables.length; ++i) {
242       composite_tables[i].lockRoot(lock_key);
243     }
244   }
245
246   public void unlockRoot(int lock_key) {
247     // For each table, recurse.
248
roots_locked--;
249     for (int i = 0; i < composite_tables.length; ++i) {
250       composite_tables[i].unlockRoot(lock_key);
251     }
252   }
253
254   public boolean hasRootsLocked() {
255     return roots_locked != 0;
256   }
257
258   // ---------- Implemented from RootTable ----------
259

260   public boolean typeEquals(RootTable table) {
261     return (this == table);
262 // return true;
263
}
264
265 }
266
Popular Tags