KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > access > conglomerate > TemplateRow


1 /*
2
3    Derby - Class org.apache.derby.impl.store.access.conglomerate.TemplateRow
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.access.conglomerate;
23
24 import org.apache.derby.iapi.reference.SQLState;
25
26 import org.apache.derby.iapi.services.monitor.Monitor;
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28 import org.apache.derby.iapi.services.io.Storable;
29
30 import org.apache.derby.iapi.error.StandardException;
31 import org.apache.derby.iapi.store.access.RowUtil;
32
33 import org.apache.derby.iapi.types.DataValueDescriptor;
34
35 import org.apache.derby.iapi.types.SQLLongint;
36
37 import org.apache.derby.iapi.services.io.FormatableBitSet;
38
39 public final class TemplateRow
40 {
41
42     /*
43     ** Constructors of TemplateRow
44     */

45
46     private TemplateRow() {
47     }
48
49     /* Private/Protected methods of This class: */
50
51
52     /**
53      * Allocate new objects to array based on format id's and column_list.
54      * <p>
55      *
56      * @param num_cols_to_allocate The number of columns to allocate for array.
57      * @param column_list description of partial set of columns to
58      * built as described in RowUtil. If null do
59      * all the columns.
60      * @param format_ids An array of format ids representing every
61      * column in the table. column_list describes
62      * which of these columns to populate into the
63      * columns array.
64      *
65      * @exception StandardException Standard exception policy.
66      **/

67     private static DataValueDescriptor[] allocate_objects(
68     int num_cols_to_allocate,
69     FormatableBitSet column_list,
70     int[] format_ids)
71         throws StandardException
72     {
73         int dest_pos = 0;
74
75         DataValueDescriptor[] ret_row =
76             new DataValueDescriptor[num_cols_to_allocate];
77         int num_cols =
78             (column_list == null ? format_ids.length : column_list.size());
79
80         for (int i = 0; i < num_cols; i++)
81         {
82             // does caller want this column?
83
if ((column_list != null) && (!column_list.get(i)))
84             {
85                 // no - column should be skipped.
86
}
87             else
88             {
89                 // yes - create the column
90

91                 // get empty instance of object identified by the format id.
92
ret_row[i] = (DataValueDescriptor)
93                     Monitor.newInstanceFromIdentifier(format_ids[i]);
94
95                 if (SanityManager.DEBUG)
96                 {
97                     DataValueDescriptor o = ret_row[i];
98
99                     if (o == null)
100                     {
101                         SanityManager.THROWASSERT(
102                         "obj from Monitor.newInstanceFromIdentifier() null." +
103                         ";src column position = " + i +
104                         ";dest column position = " + i +
105                         ";num_cols = " + num_cols +
106                         ";format_ids.length = " + format_ids.length);
107
108                     }
109
110                     if ( ! (o instanceof Storable))
111                         SanityManager.THROWASSERT(
112                             "object:(" + o.getClass() +"):(" + o +
113                             ") not an instanceof Storable");
114                 }
115             }
116         }
117
118         return(ret_row);
119     }
120
121     /* Public Methods of This class: */
122
123     /**
124     Constuctor for creating a template row which stores n SQLLongint's
125     **/

126     public static DataValueDescriptor[] newU8Row(int nkeys)
127     {
128         DataValueDescriptor[] columns = new DataValueDescriptor[nkeys];
129
130         for (int i = 0; i < columns.length; i++)
131         {
132             columns[i] = new SQLLongint(Long.MIN_VALUE);
133         }
134
135         return columns;
136     }
137
138     /**
139      * Generate an "empty" row to match the format id specification.
140      * <p>
141      * Generate an array of new'd objects matching the format id specification
142      * passed in. This routine is mostly used by the btree code to generate
143      * temporary rows needed during operations like split. It is more
144      * efficient to allocate new objects based on the old object vs. calling
145      * the Monitor.
146      * <p>
147      *
148      *
149      * @param template An array which represents a row as described in
150      * RowUtil.
151      *
152      * @exception StandardException Standard exception policy.
153      *
154      * @return The new row.
155      *
156      * @see RowUtil
157      **/

158     public static DataValueDescriptor[] newRow(
159     DataValueDescriptor[] template)
160         throws StandardException
161     {
162         DataValueDescriptor[] columns =
163             new DataValueDescriptor[template.length];
164
165         try
166         {
167             for (int i = template.length; i-- > 0 ;)
168             {
169                 // get empty instance of object identified by the format id.
170
columns[i] =
171                     (DataValueDescriptor) template[i].getClass().newInstance();
172             }
173         }
174         catch (Throwable JavaDoc t)
175         {
176             // RESOLVE - Dan is investigating ways to change the monitor
177
// so that it provides the functionality required here, when
178
// that happens I will just all the monitor and let any
179
// StandardError that come back just go on up.
180
throw(StandardException.newException(
181                     SQLState.CONGLOMERATE_TEMPLATE_CREATE_ERROR));
182         }
183
184         return columns;
185     }
186
187     /**
188      * Generate an "empty" row to match the format id specification.
189      * <p>
190      * Generate an array of new'd objects matching the format id specification
191      * passed in. This routine is mostly used by the btree code to generate
192      * temporary rows needed during operations like split.
193      * <p>
194      *
195      * @return The new row.
196      *
197      * @param format_ids an array of format id's, one per column in row.
198      *
199      * @exception StandardException Standard exception policy.
200      **/

201     public static DataValueDescriptor[] newRow(
202     FormatableBitSet column_list,
203     int[] format_ids)
204         throws StandardException
205     {
206         return(allocate_objects(format_ids.length, column_list, format_ids));
207     }
208
209     /**
210      * Generate an "empty" row to match the format id + coluumn specification.
211      * <p>
212      * Generate an array of new'd objects matching the format id specification
213      * passed in, and the column passed in. The new row is first made up of
214      * columns matching the format ids, and then followed by one other column
215      * matching the column passed in. This routine is mostly used by the
216      * btree code to generate temporary branch rows needed during operations
217      * like split.
218      * <p>
219      *
220      * @return The new row.
221      *
222      * @param format_ids an array of format id's, one per column in row.
223      * @param page_ptr The object to place in the last column of the template.
224      *
225      * @exception StandardException Standard exception policy.
226      **/

227     public static DataValueDescriptor[] newBranchRow(
228     int[] format_ids,
229     DataValueDescriptor page_ptr)
230         throws StandardException
231     {
232         // allocate an object array with the number of columns in the template
233
// row (ie. number of columns in the leaf row) + one column to hold
234
// the page pointer in the branch row.
235
DataValueDescriptor[] columns =
236             allocate_objects(
237                 format_ids.length + 1, (FormatableBitSet) null, format_ids);
238
239         // tack on the page pointer to the extra column allocated onto the
240
// end of the row built from a leafrow template.
241
columns[format_ids.length] = page_ptr;
242
243         return(columns);
244     }
245
246     /**
247      * Check that columns in the row conform to a set of format id's,
248      * both in number and type.
249      *
250      * @return boolean indicating if template matches format id's
251      *
252      * @param format_ids array of format ids which are the types of cols in row
253      * @param row the array of columns that make up the row.
254      *
255      * @exception StandardException Standard exception policy.
256      **/

257     static public boolean checkColumnTypes(
258     int[] format_ids,
259     DataValueDescriptor[] row)
260         throws StandardException
261     {
262         boolean ret_val = true;
263
264         while (true)
265         {
266             int nCols = row.length;
267             if (format_ids.length != row.length)
268             {
269                 if (SanityManager.DEBUG)
270                 {
271                     SanityManager.THROWASSERT(
272                         "format_ids[] length (" + format_ids.length +
273                         ") expected to be = row length (" + row.length + ")");
274                 }
275                 ret_val = false;
276                 break;
277             }
278
279             if (SanityManager.DEBUG)
280             {
281                 Object JavaDoc column;
282                 Object JavaDoc column_template;
283
284                 for (int colid = 0; colid < nCols; colid++)
285                 {
286                     column = row[colid];
287
288                     if (column == null)
289                     {
290                         SanityManager.THROWASSERT(
291                             "column[" + colid + "] is null");
292                     }
293
294                     column_template =
295                         Monitor.newInstanceFromIdentifier(format_ids[colid]);
296
297
298                     // is this the right check?
299
if (column.getClass() != column_template.getClass())
300                     {
301                         SanityManager.DEBUG_PRINT(
302                             "check", "row = " + RowUtil.toString(row));
303
304                         SanityManager.THROWASSERT(
305                             "column["+colid+"] (" + column.getClass() +
306                             ") expected to be instanceof column_tempate() (" +
307                             column_template.getClass() + ")" +
308                             "column = " + column +
309                             "row[colid] = " + row[colid]);
310                     }
311                 }
312             }
313             break;
314         }
315
316         return(ret_val);
317     }
318
319     /**
320      * Check that columns in the row conform to a set of format id's,
321      * both in number and type.
322      *
323      * @return boolean indicating if template matches format id's
324      *
325      * @param format_ids array of format ids which are the types of cols in row
326      * @param row the array of columns that make up the row.
327      *
328      * @exception StandardException Standard exception policy.
329      **/

330     static public boolean checkPartialColumnTypes(
331     int[] format_ids,
332     FormatableBitSet validColumns,
333     int[] fieldStates,
334     DataValueDescriptor[] row)
335         throws StandardException
336     {
337         boolean ret_val = true;
338
339         return(ret_val);
340     }
341 }
342
Popular Tags