KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > catalog > SYSSTATISTICSRowFactory


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.SYSSTATISTICSRowFactory
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.sql.catalog;
23
24 import org.apache.derby.iapi.sql.dictionary.SystemColumn;
25 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
26 import org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor;
27 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
28 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
29 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
30
31 import org.apache.derby.iapi.error.StandardException;
32
33 import org.apache.derby.iapi.services.sanity.SanityManager;
34 import org.apache.derby.iapi.sql.execute.ExecRow;
35 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
36 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
37 import org.apache.derby.iapi.types.TypeId;
38 import org.apache.derby.iapi.types.DataValueFactory;
39 import org.apache.derby.iapi.types.RowLocation;
40 import org.apache.derby.iapi.types.DataValueDescriptor;
41 import org.apache.derby.iapi.services.uuid.UUIDFactory;
42 import org.apache.derby.catalog.UUID;
43 import org.apache.derby.catalog.Statistics;
44 import org.apache.derby.iapi.types.*;
45
46 import java.sql.Timestamp JavaDoc;
47
48 /**
49  * Factory for creating a SYSSTATISTICS row.
50  *
51  * @version 0.1
52  * @author manish
53  *
54  */

55
56 public class SYSSTATISTICSRowFactory extends CatalogRowFactory
57 {
58     static final String JavaDoc TABLENAME_STRING = "SYSSTATISTICS";
59
60     /* column #s for sysstatistics (1 based) */
61     
62     /* unique UUID of this entry in statistics.
63     */

64     protected static final int SYSSTATISTICS_ID = 1;
65
66     /* reference id from sysconglomerates... */
67     protected static final int SYSSTATISTICS_REFERENCEID = 2;
68
69     /* table id--table for which this statistic is created */
70     protected static final int SYSSTATISTICS_TABLEID = 3;
71     
72     /* time when this statistic was created/updated */
73     protected static final int SYSSTATISTICS_TIMESTAMP = 4;
74
75     /* type of statistics-- we only have index (I) statistics right now but
76      * later on we might have table or column statistics.
77      */

78     protected static final int SYSSTATISTICS_TYPE = 5;
79
80     /* whether the statistics are valid or not; currently this is not used, but
81      * in the future the optimizer might be smart enough to recognize that a
82      * statistic has gone stale and then mark it as invalid (as opposed to
83      * dropping it which is a more drastic measure?)
84      */

85     protected static final int SYSSTATISTICS_VALID = 6;
86
87     /* the number of columns in this statistics */
88     protected static final int SYSSTATISTICS_COLCOUNT = 7;
89
90     /* and finally the statistics */
91     protected static final int SYSSTATISTICS_STAT = 8;
92
93     protected static final int SYSSTATISTICS_COLUMN_COUNT = 8;
94
95     /* first index on tableUUID, conglomerate UUID */
96     protected static final int SYSSTATISTICS_INDEX1_ID = 0;
97
98     private static final boolean[] uniqueness = {false};
99
100     private static final int[][] indexColumnPositions =
101     {
102         {SYSSTATISTICS_TABLEID, SYSSTATISTICS_REFERENCEID}
103     };
104
105     private static final String JavaDoc[] uuids =
106     {
107         "f81e0010-00e3-6612-5a96-009e3a3b5e00", // catalog UUID
108
"08264012-00e3-6612-5a96-009e3a3b5e00", // heap UUID.
109
"c013800d-00e3-ffbe-37c6-009e3a3b5e00", // _INDEX1 UUID
110
};
111     /*
112      * STATE
113      */

114     private SystemColumn[] columnList;
115
116     /*
117      * CONSTRUCTORS
118      */

119     public SYSSTATISTICSRowFactory(UUIDFactory uuidf,
120                                     ExecutionFactory ef,
121                                     DataValueFactory dvf,
122                                     boolean convertIdToLower)
123     {
124         super(uuidf,ef,dvf,convertIdToLower);
125         
126         initInfo(SYSSTATISTICS_COLUMN_COUNT, TABLENAME_STRING,
127                  indexColumnPositions, uniqueness, uuids);
128     }
129
130
131   /**
132      * Make a SYSSTATISTICS row
133      *
134      *
135      * @return Row suitable for inserting into SYSSTATISTICS.
136      *
137      * @exception StandardException thrown on failure
138      */

139
140     public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)
141                     throws StandardException
142     {
143         String JavaDoc myID = null, referenceID = null, tableID = null;
144         String JavaDoc statName = null, colMap = null, statType = null;
145         Timestamp JavaDoc updateTime = null;
146         int columnCount = 0;
147         Statistics statisticsObject = null;
148         boolean validStat = false;
149         ExecRow row = getExecutionFactory().getValueRow(SYSSTATISTICS_COLUMN_COUNT);
150         
151         if (td != null)
152         {
153             StatisticsDescriptor statDesc = (StatisticsDescriptor)td;
154             myID = statDesc.getUUID().toString();
155             tableID = statDesc.getTableUUID().toString();
156             referenceID = statDesc.getReferenceID().toString();
157             updateTime = statDesc.getUpdateTimestamp();
158             statType = statDesc.getStatType();
159             validStat = statDesc.isValid();
160             statisticsObject = statDesc.getStatistic();
161             columnCount = statDesc.getColumnCount();
162         }
163
164         row.setColumn(1, dvf.getCharDataValue(myID));
165         row.setColumn(2, dvf.getCharDataValue(referenceID));
166         row.setColumn(3, dvf.getCharDataValue(tableID));
167         row.setColumn(4, new SQLTimestamp(updateTime));
168         row.setColumn(5, dvf.getCharDataValue(statType));
169         row.setColumn(6, dvf.getDataValue(validStat));
170         row.setColumn(7, dvf.getDataValue(columnCount));
171         row.setColumn(8, dvf.getDataValue(statisticsObject));
172         return row;
173     }
174     
175     public TupleDescriptor buildDescriptor(
176          ExecRow row,
177          TupleDescriptor parentDesc,
178          DataDictionary dd)
179         throws StandardException
180          
181     {
182         if (SanityManager.DEBUG)
183         {
184             SanityManager.ASSERT(
185                 row.nColumns() == SYSSTATISTICS_COLUMN_COUNT,
186                 "Wrong number of columns for a SYSSTATISTICS row");
187         }
188
189         DataValueDescriptor col;
190         String JavaDoc scratch;
191         UUIDFactory uuidFactory = getUUIDFactory();
192         UUID statUUID, statReferenceUUID, statTableUUID;
193         String JavaDoc statName;
194         
195         /* 1st column is UUID */
196         col = row.getColumn(SYSSTATISTICS_ID);
197         scratch = col.getString();
198         statUUID = uuidFactory.recreateUUID(scratch);
199
200         /* 2nd column is reference UUID */
201         col = row.getColumn(SYSSTATISTICS_REFERENCEID);
202         scratch = col.getString();
203         statReferenceUUID = uuidFactory.recreateUUID(scratch);
204
205         /* 3rd column is table UUID */
206         col = row.getColumn(SYSSTATISTICS_TABLEID);
207         scratch = col.getString();
208         statTableUUID = uuidFactory.recreateUUID(scratch);
209
210         /* 4th column is timestamp */
211         col = row.getColumn(SYSSTATISTICS_TIMESTAMP);
212         Timestamp JavaDoc updateTime = (Timestamp JavaDoc) col.getObject();
213
214         /* 5th column is stat type -- string */
215         col = row.getColumn(SYSSTATISTICS_TYPE);
216         String JavaDoc statType = col.getString();
217
218         /* 6th column is stat valid -- boolean */
219         col = row.getColumn(SYSSTATISTICS_VALID);
220         boolean valid = col.getBoolean();
221
222         /* 7th column is column count */
223         col = row.getColumn(SYSSTATISTICS_COLCOUNT);
224         int columnCount = col.getInt();
225
226         /* 8th column is statistics itself */
227         col = row.getColumn(SYSSTATISTICS_STAT);
228         Statistics stat = (Statistics)col.getObject();
229
230         return new StatisticsDescriptor(dd, statUUID, statReferenceUUID,
231                                            statTableUUID, // statName, colMap,
232
statType, stat, columnCount);
233     }
234
235     /**
236      * Builds a list of columns suitable for creating this Catalog.
237      *
238      *
239      * @return array of SystemColumn suitable for making this catalog.
240      */

241     public SystemColumn[] buildColumnList()
242     {
243         if (columnList != null)
244             return columnList;
245
246         columnList = new SystemColumn[SYSSTATISTICS_COLUMN_COUNT];
247         
248         columnList[0] = new SystemColumnImpl(
249                            convertIdCase( "STATID"), // column name
250
SYSSTATISTICS_ID, // column number
251
0, // precision
252
0, // scale
253
false, // nullability
254
"CHAR", // dataType
255
true, // built-in type
256
36 // maxLength
257
);
258         
259         columnList[1] = new SystemColumnImpl(
260                            convertIdCase( "REFERENCEID"), // column name
261
SYSSTATISTICS_REFERENCEID, // column number
262
0, // precision
263
0, // scale
264
false, // nullability
265
"CHAR", // dataType
266
true, // built-in type
267
36 // maxLength
268
);
269
270         columnList[2] = new SystemColumnImpl(
271                            convertIdCase( "TABLEID"), // column name
272
SYSSTATISTICS_TABLEID, // column number
273
0, // precision
274
0, // scale
275
false, // nullability
276
"CHAR", // dataType
277
true, // built-in type
278
36 // maxLength
279
);
280
281         columnList[3] =
282                     new SystemColumnImpl(
283                             convertIdCase( "CREATIONTIMESTAMP"), // name
284
SYSSTATISTICS_TIMESTAMP, // column number
285
0, // precision
286
0, // scale
287
false, // nullability
288
"TIMESTAMP", // dataType
289
true, // built-in type
290
TypeId.TIMESTAMP_MAXWIDTH // maxLength
291
);
292
293         columnList[4] =
294                     new SystemColumnImpl(
295                             convertIdCase( "TYPE"), // name
296
SYSSTATISTICS_TYPE, // column number
297
0, // precision
298
0, // scale
299
false, // nullability
300
"CHAR", // dataType
301
true, // built-in type
302
1 // maxLength
303
);
304
305         columnList[5] =
306                     new SystemColumnImpl(
307                             convertIdCase( "VALID"), // name
308
SYSSTATISTICS_VALID, // column number
309
0, // precision
310
0, // scale
311
false, // nullability
312
"BOOLEAN", // dataType
313
true, // built-in type
314
1 // maxLength
315
);
316
317
318         columnList[6] =
319                    new SystemColumnImpl(
320                             convertIdCase( "COLCOUNT"), // name
321
SYSSTATISTICS_COLCOUNT, // column number
322
0, // precision
323
0, // scale
324
false, // nullability
325
"INTEGER", // data type
326
true, // built in type
327
4 //maxlength
328
);
329         columnList[7] =
330                     new SystemColumnImpl(
331                             convertIdCase( "STATISTICS"), // name
332
SYSSTATISTICS_STAT, // column number
333
0, // precision
334
0, // scale
335
false, // nullability
336
"org.apache.derby.catalog.Statistics", // dataType
337
false, // built-in type
338
12 // maxLength
339
);
340
341
342
343         return columnList;
344     }
345
346
347 }
348
Popular Tags