001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.catalog.SYSSTATISTICSRowFactory
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.sql.catalog;
023:
024: import org.apache.derby.iapi.sql.dictionary.SystemColumn;
025: import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
026: import org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor;
027: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
028: import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
029: import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
030:
031: import org.apache.derby.iapi.error.StandardException;
032:
033: import org.apache.derby.iapi.services.sanity.SanityManager;
034: import org.apache.derby.iapi.sql.execute.ExecRow;
035: import org.apache.derby.iapi.sql.execute.ExecIndexRow;
036: import org.apache.derby.iapi.sql.execute.ExecutionFactory;
037: import org.apache.derby.iapi.types.TypeId;
038: import org.apache.derby.iapi.types.DataValueFactory;
039: import org.apache.derby.iapi.types.RowLocation;
040: import org.apache.derby.iapi.types.DataValueDescriptor;
041: import org.apache.derby.iapi.services.uuid.UUIDFactory;
042: import org.apache.derby.catalog.UUID;
043: import org.apache.derby.catalog.Statistics;
044: import org.apache.derby.iapi.types.*;
045:
046: import java.sql.Timestamp;
047:
048: /**
049: * Factory for creating a SYSSTATISTICS row.
050: *
051: * @version 0.1
052: * @author manish
053: *
054: */
055:
056: public class SYSSTATISTICSRowFactory extends CatalogRowFactory {
057: static final String TABLENAME_STRING = "SYSSTATISTICS";
058:
059: /* column #s for sysstatistics (1 based) */
060:
061: /* unique UUID of this entry in statistics.
062: */
063: protected static final int SYSSTATISTICS_ID = 1;
064:
065: /* reference id from sysconglomerates... */
066: protected static final int SYSSTATISTICS_REFERENCEID = 2;
067:
068: /* table id--table for which this statistic is created */
069: protected static final int SYSSTATISTICS_TABLEID = 3;
070:
071: /* time when this statistic was created/updated */
072: protected static final int SYSSTATISTICS_TIMESTAMP = 4;
073:
074: /* type of statistics-- we only have index (I) statistics right now but
075: * later on we might have table or column statistics.
076: */
077: protected static final int SYSSTATISTICS_TYPE = 5;
078:
079: /* whether the statistics are valid or not; currently this is not used, but
080: * in the future the optimizer might be smart enough to recognize that a
081: * statistic has gone stale and then mark it as invalid (as opposed to
082: * dropping it which is a more drastic measure?)
083: */
084: protected static final int SYSSTATISTICS_VALID = 6;
085:
086: /* the number of columns in this statistics */
087: protected static final int SYSSTATISTICS_COLCOUNT = 7;
088:
089: /* and finally the statistics */
090: protected static final int SYSSTATISTICS_STAT = 8;
091:
092: protected static final int SYSSTATISTICS_COLUMN_COUNT = 8;
093:
094: /* first index on tableUUID, conglomerate UUID */
095: protected static final int SYSSTATISTICS_INDEX1_ID = 0;
096:
097: private static final boolean[] uniqueness = { false };
098:
099: private static final int[][] indexColumnPositions = { {
100: SYSSTATISTICS_TABLEID, SYSSTATISTICS_REFERENCEID } };
101:
102: private static final String[] uuids = {
103: "f81e0010-00e3-6612-5a96-009e3a3b5e00", // catalog UUID
104: "08264012-00e3-6612-5a96-009e3a3b5e00", // heap UUID.
105: "c013800d-00e3-ffbe-37c6-009e3a3b5e00", // _INDEX1 UUID
106: };
107: /*
108: * STATE
109: */
110: private SystemColumn[] columnList;
111:
112: /*
113: * CONSTRUCTORS
114: */
115: public SYSSTATISTICSRowFactory(UUIDFactory uuidf,
116: ExecutionFactory ef, DataValueFactory dvf,
117: boolean convertIdToLower) {
118: super (uuidf, ef, dvf, convertIdToLower);
119:
120: initInfo(SYSSTATISTICS_COLUMN_COUNT, TABLENAME_STRING,
121: indexColumnPositions, uniqueness, uuids);
122: }
123:
124: /**
125: * Make a SYSSTATISTICS row
126: *
127: *
128: * @return Row suitable for inserting into SYSSTATISTICS.
129: *
130: * @exception StandardException thrown on failure
131: */
132:
133: public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)
134: throws StandardException {
135: String myID = null, referenceID = null, tableID = null;
136: String statName = null, colMap = null, statType = null;
137: Timestamp updateTime = null;
138: int columnCount = 0;
139: Statistics statisticsObject = null;
140: boolean validStat = false;
141: ExecRow row = getExecutionFactory().getValueRow(
142: SYSSTATISTICS_COLUMN_COUNT);
143:
144: if (td != null) {
145: StatisticsDescriptor statDesc = (StatisticsDescriptor) td;
146: myID = statDesc.getUUID().toString();
147: tableID = statDesc.getTableUUID().toString();
148: referenceID = statDesc.getReferenceID().toString();
149: updateTime = statDesc.getUpdateTimestamp();
150: statType = statDesc.getStatType();
151: validStat = statDesc.isValid();
152: statisticsObject = statDesc.getStatistic();
153: columnCount = statDesc.getColumnCount();
154: }
155:
156: row.setColumn(1, dvf.getCharDataValue(myID));
157: row.setColumn(2, dvf.getCharDataValue(referenceID));
158: row.setColumn(3, dvf.getCharDataValue(tableID));
159: row.setColumn(4, new SQLTimestamp(updateTime));
160: row.setColumn(5, dvf.getCharDataValue(statType));
161: row.setColumn(6, dvf.getDataValue(validStat));
162: row.setColumn(7, dvf.getDataValue(columnCount));
163: row.setColumn(8, dvf.getDataValue(statisticsObject));
164: return row;
165: }
166:
167: public TupleDescriptor buildDescriptor(ExecRow row,
168: TupleDescriptor parentDesc, DataDictionary dd)
169: throws StandardException
170:
171: {
172: if (SanityManager.DEBUG) {
173: SanityManager.ASSERT(
174: row.nColumns() == SYSSTATISTICS_COLUMN_COUNT,
175: "Wrong number of columns for a SYSSTATISTICS row");
176: }
177:
178: DataValueDescriptor col;
179: String scratch;
180: UUIDFactory uuidFactory = getUUIDFactory();
181: UUID statUUID, statReferenceUUID, statTableUUID;
182: String statName;
183:
184: /* 1st column is UUID */
185: col = row.getColumn(SYSSTATISTICS_ID);
186: scratch = col.getString();
187: statUUID = uuidFactory.recreateUUID(scratch);
188:
189: /* 2nd column is reference UUID */
190: col = row.getColumn(SYSSTATISTICS_REFERENCEID);
191: scratch = col.getString();
192: statReferenceUUID = uuidFactory.recreateUUID(scratch);
193:
194: /* 3rd column is table UUID */
195: col = row.getColumn(SYSSTATISTICS_TABLEID);
196: scratch = col.getString();
197: statTableUUID = uuidFactory.recreateUUID(scratch);
198:
199: /* 4th column is timestamp */
200: col = row.getColumn(SYSSTATISTICS_TIMESTAMP);
201: Timestamp updateTime = (Timestamp) col.getObject();
202:
203: /* 5th column is stat type -- string */
204: col = row.getColumn(SYSSTATISTICS_TYPE);
205: String statType = col.getString();
206:
207: /* 6th column is stat valid -- boolean */
208: col = row.getColumn(SYSSTATISTICS_VALID);
209: boolean valid = col.getBoolean();
210:
211: /* 7th column is column count */
212: col = row.getColumn(SYSSTATISTICS_COLCOUNT);
213: int columnCount = col.getInt();
214:
215: /* 8th column is statistics itself */
216: col = row.getColumn(SYSSTATISTICS_STAT);
217: Statistics stat = (Statistics) col.getObject();
218:
219: return new StatisticsDescriptor(dd, statUUID,
220: statReferenceUUID, statTableUUID, // statName, colMap,
221: statType, stat, columnCount);
222: }
223:
224: /**
225: * Builds a list of columns suitable for creating this Catalog.
226: *
227: *
228: * @return array of SystemColumn suitable for making this catalog.
229: */
230: public SystemColumn[] buildColumnList() {
231: if (columnList != null)
232: return columnList;
233:
234: columnList = new SystemColumn[SYSSTATISTICS_COLUMN_COUNT];
235:
236: columnList[0] = new SystemColumnImpl(convertIdCase("STATID"), // column name
237: SYSSTATISTICS_ID, // column number
238: 0, // precision
239: 0, // scale
240: false, // nullability
241: "CHAR", // dataType
242: true, // built-in type
243: 36 // maxLength
244: );
245:
246: columnList[1] = new SystemColumnImpl(
247: convertIdCase("REFERENCEID"), // column name
248: SYSSTATISTICS_REFERENCEID, // column number
249: 0, // precision
250: 0, // scale
251: false, // nullability
252: "CHAR", // dataType
253: true, // built-in type
254: 36 // maxLength
255: );
256:
257: columnList[2] = new SystemColumnImpl(convertIdCase("TABLEID"), // column name
258: SYSSTATISTICS_TABLEID, // column number
259: 0, // precision
260: 0, // scale
261: false, // nullability
262: "CHAR", // dataType
263: true, // built-in type
264: 36 // maxLength
265: );
266:
267: columnList[3] = new SystemColumnImpl(
268: convertIdCase("CREATIONTIMESTAMP"), // name
269: SYSSTATISTICS_TIMESTAMP, // column number
270: 0, // precision
271: 0, // scale
272: false, // nullability
273: "TIMESTAMP", // dataType
274: true, // built-in type
275: TypeId.TIMESTAMP_MAXWIDTH // maxLength
276: );
277:
278: columnList[4] = new SystemColumnImpl(convertIdCase("TYPE"), // name
279: SYSSTATISTICS_TYPE, // column number
280: 0, // precision
281: 0, // scale
282: false, // nullability
283: "CHAR", // dataType
284: true, // built-in type
285: 1 // maxLength
286: );
287:
288: columnList[5] = new SystemColumnImpl(convertIdCase("VALID"), // name
289: SYSSTATISTICS_VALID, // column number
290: 0, // precision
291: 0, // scale
292: false, // nullability
293: "BOOLEAN", // dataType
294: true, // built-in type
295: 1 // maxLength
296: );
297:
298: columnList[6] = new SystemColumnImpl(convertIdCase("COLCOUNT"), // name
299: SYSSTATISTICS_COLCOUNT, // column number
300: 0, // precision
301: 0, // scale
302: false, // nullability
303: "INTEGER", // data type
304: true, // built in type
305: 4 //maxlength
306: );
307: columnList[7] = new SystemColumnImpl(
308: convertIdCase("STATISTICS"), // name
309: SYSSTATISTICS_STAT, // column number
310: 0, // precision
311: 0, // scale
312: false, // nullability
313: "org.apache.derby.catalog.Statistics", // dataType
314: false, // built-in type
315: 12 // maxLength
316: );
317:
318: return columnList;
319: }
320:
321: }
|