--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.catalog.SYSSTATISTICSRowFactory\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.catalog;\r
+\r
+import org.apache.derby.iapi.sql.dictionary.SystemColumn;\r
+import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.DataDictionary;\r
+import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;\r
+import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.sql.execute.ExecRow;\r
+import org.apache.derby.iapi.sql.execute.ExecIndexRow;\r
+import org.apache.derby.iapi.sql.execute.ExecutionFactory;\r
+import org.apache.derby.iapi.types.TypeId;\r
+import org.apache.derby.iapi.types.DataValueFactory;\r
+import org.apache.derby.iapi.types.RowLocation;\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+import org.apache.derby.iapi.services.uuid.UUIDFactory;\r
+import org.apache.derby.catalog.UUID;\r
+import org.apache.derby.catalog.Statistics;\r
+import org.apache.derby.iapi.types.*;\r
+\r
+import java.sql.Timestamp;\r
+import java.sql.Types;\r
+\r
+/**\r
+ * Factory for creating a SYSSTATISTICS row.\r
+ *\r
+ * @version 0.1\r
+ *\r
+ */\r
+\r
+public class SYSSTATISTICSRowFactory extends CatalogRowFactory\r
+{\r
+ static final String TABLENAME_STRING = "SYSSTATISTICS";\r
+\r
+ /* column #s for sysstatistics (1 based) */\r
+ \r
+ /* unique UUID of this entry in statistics. \r
+ */\r
+ protected static final int SYSSTATISTICS_ID = 1;\r
+\r
+ /* reference id from sysconglomerates... */\r
+ protected static final int SYSSTATISTICS_REFERENCEID = 2;\r
+\r
+ /* table id--table for which this statistic is created */\r
+ protected static final int SYSSTATISTICS_TABLEID = 3;\r
+ \r
+ /* time when this statistic was created/updated */\r
+ protected static final int SYSSTATISTICS_TIMESTAMP = 4;\r
+\r
+ /* type of statistics-- we only have index (I) statistics right now but\r
+ * later on we might have table or column statistics.\r
+ */\r
+ protected static final int SYSSTATISTICS_TYPE = 5; \r
+\r
+ /* whether the statistics are valid or not; currently this is not used, but\r
+ * in the future the optimizer might be smart enough to recognize that a\r
+ * statistic has gone stale and then mark it as invalid (as opposed to\r
+ * dropping it which is a more drastic measure?)\r
+ */\r
+ protected static final int SYSSTATISTICS_VALID = 6;\r
+\r
+ /* the number of columns in this statistics */\r
+ protected static final int SYSSTATISTICS_COLCOUNT = 7;\r
+\r
+ /* and finally the statistics */\r
+ protected static final int SYSSTATISTICS_STAT = 8;\r
+\r
+ protected static final int SYSSTATISTICS_COLUMN_COUNT = 8;\r
+\r
+ /* first index on tableUUID, conglomerate UUID */\r
+ protected static final int SYSSTATISTICS_INDEX1_ID = 0;\r
+\r
+ private static final boolean[] uniqueness = {false};\r
+\r
+ private static final int[][] indexColumnPositions =\r
+ {\r
+ {SYSSTATISTICS_TABLEID, SYSSTATISTICS_REFERENCEID}\r
+ };\r
+\r
+ private static final String[] uuids =\r
+ {\r
+ "f81e0010-00e3-6612-5a96-009e3a3b5e00", // catalog UUID\r
+ "08264012-00e3-6612-5a96-009e3a3b5e00", // heap UUID.\r
+ "c013800d-00e3-ffbe-37c6-009e3a3b5e00", // _INDEX1 UUID\r
+ };\r
+ /*\r
+ * STATE\r
+ */\r
+\r
+ /*\r
+ * CONSTRUCTORS\r
+ */\r
+ SYSSTATISTICSRowFactory(UUIDFactory uuidf, \r
+ ExecutionFactory ef, \r
+ DataValueFactory dvf)\r
+ {\r
+ super(uuidf,ef,dvf);\r
+ \r
+ initInfo(SYSSTATISTICS_COLUMN_COUNT, TABLENAME_STRING, \r
+ indexColumnPositions, uniqueness, uuids);\r
+ }\r
+\r
+\r
+ /**\r
+ * Make a SYSSTATISTICS row\r
+ *\r
+ *\r
+ * @return Row suitable for inserting into SYSSTATISTICS.\r
+ *\r
+ * @exception StandardException thrown on failure\r
+ */\r
+\r
+ public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)\r
+ throws StandardException \r
+ {\r
+ String myID = null, referenceID = null, tableID = null;\r
+ String statName = null, colMap = null, statType = null;\r
+ Timestamp updateTime = null;\r
+ int columnCount = 0;\r
+ Statistics statisticsObject = null;\r
+ boolean validStat = false;\r
+ ExecRow row = getExecutionFactory().getValueRow(SYSSTATISTICS_COLUMN_COUNT);\r
+ \r
+ if (td != null)\r
+ {\r
+ StatisticsDescriptor statDesc = (StatisticsDescriptor)td;\r
+ myID = statDesc.getUUID().toString();\r
+ tableID = statDesc.getTableUUID().toString();\r
+ referenceID = statDesc.getReferenceID().toString();\r
+ updateTime = statDesc.getUpdateTimestamp();\r
+ statType = statDesc.getStatType();\r
+ validStat = statDesc.isValid();\r
+ statisticsObject = statDesc.getStatistic();\r
+ columnCount = statDesc.getColumnCount();\r
+ }\r
+\r
+ row.setColumn(1, new SQLChar(myID));\r
+ row.setColumn(2, new SQLChar(referenceID));\r
+ row.setColumn(3, new SQLChar(tableID));\r
+ row.setColumn(4, new SQLTimestamp(updateTime));\r
+ row.setColumn(5, new SQLChar(statType));\r
+ row.setColumn(6, dvf.getDataValue(validStat));\r
+ row.setColumn(7, dvf.getDataValue(columnCount));\r
+ row.setColumn(8, dvf.getDataValue(statisticsObject));\r
+ return row;\r
+ }\r
+ \r
+ public TupleDescriptor buildDescriptor(\r
+ ExecRow row,\r
+ TupleDescriptor parentDesc,\r
+ DataDictionary dd)\r
+ throws StandardException\r
+ \r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ SanityManager.ASSERT(\r
+ row.nColumns() == SYSSTATISTICS_COLUMN_COUNT, \r
+ "Wrong number of columns for a SYSSTATISTICS row");\r
+ }\r
+\r
+ DataValueDescriptor col;\r
+ String scratch;\r
+ UUIDFactory uuidFactory = getUUIDFactory();\r
+ UUID statUUID, statReferenceUUID, statTableUUID;\r
+ String statName;\r
+ \r
+ /* 1st column is UUID */\r
+ col = row.getColumn(SYSSTATISTICS_ID);\r
+ scratch = col.getString();\r
+ statUUID = uuidFactory.recreateUUID(scratch);\r
+\r
+ /* 2nd column is reference UUID */\r
+ col = row.getColumn(SYSSTATISTICS_REFERENCEID);\r
+ scratch = col.getString();\r
+ statReferenceUUID = uuidFactory.recreateUUID(scratch);\r
+\r
+ /* 3rd column is table UUID */\r
+ col = row.getColumn(SYSSTATISTICS_TABLEID);\r
+ scratch = col.getString();\r
+ statTableUUID = uuidFactory.recreateUUID(scratch);\r
+\r
+ /* 4th column is timestamp */\r
+ col = row.getColumn(SYSSTATISTICS_TIMESTAMP);\r
+ Timestamp updateTime = (Timestamp) col.getObject();\r
+\r
+ /* 5th column is stat type -- string */\r
+ col = row.getColumn(SYSSTATISTICS_TYPE);\r
+ String statType = col.getString();\r
+\r
+ /* 6th column is stat valid -- boolean */\r
+ col = row.getColumn(SYSSTATISTICS_VALID);\r
+ boolean valid = col.getBoolean();\r
+\r
+ /* 7th column is column count */\r
+ col = row.getColumn(SYSSTATISTICS_COLCOUNT);\r
+ int columnCount = col.getInt();\r
+\r
+ /* 8th column is statistics itself */\r
+ col = row.getColumn(SYSSTATISTICS_STAT);\r
+ Statistics stat = (Statistics)col.getObject();\r
+\r
+ return new StatisticsDescriptor(dd, statUUID, statReferenceUUID,\r
+ statTableUUID, // statName, colMap,\r
+ statType, stat, columnCount);\r
+ } \r
+\r
+ /**\r
+ * Builds a list of columns suitable for creating this Catalog.\r
+ *\r
+ *\r
+ * @return array of SystemColumn suitable for making this catalog.\r
+ */\r
+ public SystemColumn[] buildColumnList()\r
+ {\r
+ return new SystemColumn[] {\r
+ SystemColumnImpl.getUUIDColumn("STATID", false),\r
+ SystemColumnImpl.getUUIDColumn("REFERENCEID", false),\r
+ SystemColumnImpl.getUUIDColumn("TABLEID", false),\r
+ SystemColumnImpl.getColumn("CREATIONTIMESTAMP", Types.TIMESTAMP, false),\r
+ SystemColumnImpl.getIndicatorColumn("TYPE"),\r
+ SystemColumnImpl.getColumn("VALID", Types.BOOLEAN, false),\r
+ SystemColumnImpl.getColumn("COLCOUNT", Types.INTEGER, false),\r
+ SystemColumnImpl.getJavaColumn("STATISTICS",\r
+ "org.apache.derby.catalog.Statistics", false)\r
+ };\r
+ }\r
+} \r