001: /*
002:
003: Derby - Class org.apache.derby.impl.store.access.conglomerate.TemplateRow
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.store.access.conglomerate;
023:
024: import org.apache.derby.iapi.reference.SQLState;
025:
026: import org.apache.derby.iapi.services.monitor.Monitor;
027: import org.apache.derby.iapi.services.sanity.SanityManager;
028: import org.apache.derby.iapi.services.io.Storable;
029:
030: import org.apache.derby.iapi.error.StandardException;
031: import org.apache.derby.iapi.store.access.RowUtil;
032:
033: import org.apache.derby.iapi.types.DataValueDescriptor;
034:
035: import org.apache.derby.iapi.types.SQLLongint;
036:
037: import org.apache.derby.iapi.services.io.FormatableBitSet;
038:
039: public final class TemplateRow {
040:
041: /*
042: ** Constructors of TemplateRow
043: */
044:
045: private TemplateRow() {
046: }
047:
048: /* Private/Protected methods of This class: */
049:
050: /**
051: * Allocate new objects to array based on format id's and column_list.
052: * <p>
053: *
054: * @param num_cols_to_allocate The number of columns to allocate for array.
055: * @param column_list description of partial set of columns to
056: * built as described in RowUtil. If null do
057: * all the columns.
058: * @param format_ids An array of format ids representing every
059: * column in the table. column_list describes
060: * which of these columns to populate into the
061: * columns array.
062: *
063: * @exception StandardException Standard exception policy.
064: **/
065: private static DataValueDescriptor[] allocate_objects(
066: int num_cols_to_allocate, FormatableBitSet column_list,
067: int[] format_ids) throws StandardException {
068: int dest_pos = 0;
069:
070: DataValueDescriptor[] ret_row = new DataValueDescriptor[num_cols_to_allocate];
071: int num_cols = (column_list == null ? format_ids.length
072: : column_list.size());
073:
074: for (int i = 0; i < num_cols; i++) {
075: // does caller want this column?
076: if ((column_list != null) && (!column_list.get(i))) {
077: // no - column should be skipped.
078: } else {
079: // yes - create the column
080:
081: // get empty instance of object identified by the format id.
082: ret_row[i] = (DataValueDescriptor) Monitor
083: .newInstanceFromIdentifier(format_ids[i]);
084:
085: if (SanityManager.DEBUG) {
086: DataValueDescriptor o = ret_row[i];
087:
088: if (o == null) {
089: SanityManager
090: .THROWASSERT("obj from Monitor.newInstanceFromIdentifier() null."
091: + ";src column position = "
092: + i
093: + ";dest column position = "
094: + i
095: + ";num_cols = "
096: + num_cols
097: + ";format_ids.length = "
098: + format_ids.length);
099:
100: }
101:
102: if (!(o instanceof Storable))
103: SanityManager.THROWASSERT("object:("
104: + o.getClass() + "):(" + o
105: + ") not an instanceof Storable");
106: }
107: }
108: }
109:
110: return (ret_row);
111: }
112:
113: /* Public Methods of This class: */
114:
115: /**
116: Constuctor for creating a template row which stores n SQLLongint's
117: **/
118: public static DataValueDescriptor[] newU8Row(int nkeys) {
119: DataValueDescriptor[] columns = new DataValueDescriptor[nkeys];
120:
121: for (int i = 0; i < columns.length; i++) {
122: columns[i] = new SQLLongint(Long.MIN_VALUE);
123: }
124:
125: return columns;
126: }
127:
128: /**
129: * Generate an "empty" row to match the format id specification.
130: * <p>
131: * Generate an array of new'd objects matching the format id specification
132: * passed in. This routine is mostly used by the btree code to generate
133: * temporary rows needed during operations like split. It is more
134: * efficient to allocate new objects based on the old object vs. calling
135: * the Monitor.
136: * <p>
137: *
138: *
139: * @param template An array which represents a row as described in
140: * RowUtil.
141: *
142: * @exception StandardException Standard exception policy.
143: *
144: * @return The new row.
145: *
146: * @see RowUtil
147: **/
148: public static DataValueDescriptor[] newRow(
149: DataValueDescriptor[] template) throws StandardException {
150: DataValueDescriptor[] columns = new DataValueDescriptor[template.length];
151:
152: try {
153: for (int i = template.length; i-- > 0;) {
154: // get empty instance of object identified by the format id.
155: columns[i] = (DataValueDescriptor) template[i]
156: .getClass().newInstance();
157: }
158: } catch (Throwable t) {
159: // RESOLVE - Dan is investigating ways to change the monitor
160: // so that it provides the functionality required here, when
161: // that happens I will just all the monitor and let any
162: // StandardError that come back just go on up.
163: throw (StandardException
164: .newException(SQLState.CONGLOMERATE_TEMPLATE_CREATE_ERROR));
165: }
166:
167: return columns;
168: }
169:
170: /**
171: * Generate an "empty" row to match the format id specification.
172: * <p>
173: * Generate an array of new'd objects matching the format id specification
174: * passed in. This routine is mostly used by the btree code to generate
175: * temporary rows needed during operations like split.
176: * <p>
177: *
178: * @return The new row.
179: *
180: * @param format_ids an array of format id's, one per column in row.
181: *
182: * @exception StandardException Standard exception policy.
183: **/
184: public static DataValueDescriptor[] newRow(
185: FormatableBitSet column_list, int[] format_ids)
186: throws StandardException {
187: return (allocate_objects(format_ids.length, column_list,
188: format_ids));
189: }
190:
191: /**
192: * Generate an "empty" row to match the format id + coluumn specification.
193: * <p>
194: * Generate an array of new'd objects matching the format id specification
195: * passed in, and the column passed in. The new row is first made up of
196: * columns matching the format ids, and then followed by one other column
197: * matching the column passed in. This routine is mostly used by the
198: * btree code to generate temporary branch rows needed during operations
199: * like split.
200: * <p>
201: *
202: * @return The new row.
203: *
204: * @param format_ids an array of format id's, one per column in row.
205: * @param page_ptr The object to place in the last column of the template.
206: *
207: * @exception StandardException Standard exception policy.
208: **/
209: public static DataValueDescriptor[] newBranchRow(int[] format_ids,
210: DataValueDescriptor page_ptr) throws StandardException {
211: // allocate an object array with the number of columns in the template
212: // row (ie. number of columns in the leaf row) + one column to hold
213: // the page pointer in the branch row.
214: DataValueDescriptor[] columns = allocate_objects(
215: format_ids.length + 1, (FormatableBitSet) null,
216: format_ids);
217:
218: // tack on the page pointer to the extra column allocated onto the
219: // end of the row built from a leafrow template.
220: columns[format_ids.length] = page_ptr;
221:
222: return (columns);
223: }
224:
225: /**
226: * Check that columns in the row conform to a set of format id's,
227: * both in number and type.
228: *
229: * @return boolean indicating if template matches format id's
230: *
231: * @param format_ids array of format ids which are the types of cols in row
232: * @param row the array of columns that make up the row.
233: *
234: * @exception StandardException Standard exception policy.
235: **/
236: static public boolean checkColumnTypes(int[] format_ids,
237: DataValueDescriptor[] row) throws StandardException {
238: boolean ret_val = true;
239:
240: while (true) {
241: int nCols = row.length;
242: if (format_ids.length != row.length) {
243: if (SanityManager.DEBUG) {
244: SanityManager.THROWASSERT("format_ids[] length ("
245: + format_ids.length
246: + ") expected to be = row length ("
247: + row.length + ")");
248: }
249: ret_val = false;
250: break;
251: }
252:
253: if (SanityManager.DEBUG) {
254: Object column;
255: Object column_template;
256:
257: for (int colid = 0; colid < nCols; colid++) {
258: column = row[colid];
259:
260: if (column == null) {
261: SanityManager.THROWASSERT("column[" + colid
262: + "] is null");
263: }
264:
265: column_template = Monitor
266: .newInstanceFromIdentifier(format_ids[colid]);
267:
268: // is this the right check?
269: if (column.getClass() != column_template.getClass()) {
270: SanityManager.DEBUG_PRINT("check", "row = "
271: + RowUtil.toString(row));
272:
273: SanityManager
274: .THROWASSERT("column["
275: + colid
276: + "] ("
277: + column.getClass()
278: + ") expected to be instanceof column_tempate() ("
279: + column_template.getClass()
280: + ")" + "column = " + column
281: + "row[colid] = " + row[colid]);
282: }
283: }
284: }
285: break;
286: }
287:
288: return (ret_val);
289: }
290:
291: /**
292: * Check that columns in the row conform to a set of format id's,
293: * both in number and type.
294: *
295: * @return boolean indicating if template matches format id's
296: *
297: * @param format_ids array of format ids which are the types of cols in row
298: * @param row the array of columns that make up the row.
299: *
300: * @exception StandardException Standard exception policy.
301: **/
302: static public boolean checkPartialColumnTypes(int[] format_ids,
303: FormatableBitSet validColumns, int[] fieldStates,
304: DataValueDescriptor[] row) throws StandardException {
305: boolean ret_val = true;
306:
307: return (ret_val);
308: }
309: }
|