--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.catalog.types.StatisticsImpl\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.catalog.types;\r
+\r
+import org.apache.derby.catalog.Statistics;\r
+import org.apache.derby.iapi.services.io.Formatable;\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+import org.apache.derby.iapi.services.io.FormatableHashtable;\r
+import org.apache.derby.iapi.services.io.FormatableLongHolder;\r
+\r
+import java.io.ObjectOutput;\r
+import java.io.ObjectInput;\r
+import java.io.IOException;\r
+\r
+public class StatisticsImpl implements Statistics, Formatable\r
+{\r
+ /* total count of rows for which this statistic was created-- this\r
+ is not the same as the total number of rows in the conglomerate\r
+ currently, but a snapshot; i.e the number of rows when this\r
+ statistic was created/updated.\r
+ */\r
+\r
+ private long numRows;\r
+ \r
+ /* total count of unique values for the keys \r
+ */\r
+ private long numUnique;\r
+\r
+ /**\r
+ * Constructor for StatisticsImpl.\r
+ * \r
+ * @param numRows number of rows in the conglomerate for which\r
+ * this statistic is being created.\r
+ * @param numUnique number of unique values in the key for which\r
+ * this statistic is being created.\r
+ */\r
+ public StatisticsImpl(long numRows, long numUnique)\r
+ {\r
+ this.numRows = numRows;\r
+ this.numUnique = numUnique;\r
+ }\r
+\r
+ /** Zero argument constructor for Formatable Interface */\r
+ public StatisticsImpl()\r
+ {}\r
+\r
+ /** @see Statistics#selectivity */\r
+ public double selectivity(Object[] predicates)\r
+ {\r
+ if (numRows == 0.0)\r
+ return 0.1;\r
+\r
+ /* xxxSTATresolve: for small values of numRows, should we do something\r
+ * special? \r
+ */\r
+ return (double)(1/(double)numUnique);\r
+ }\r
+\r
+ /*------------------ Externalizable Interface ------------------*/\r
+ \r
+ /**\r
+ * @see java.io.Externalizable#readExternal\r
+ */\r
+ public void readExternal(ObjectInput in) \r
+ throws IOException, ClassNotFoundException\r
+ {\r
+ FormatableHashtable fh = (FormatableHashtable)in.readObject();\r
+ numRows = fh.getLong("numRows");\r
+ numUnique = fh.getLong("numUnique");\r
+ }\r
+\r
+ /**\r
+ * Write this object to a stream of stored objects.\r
+ *\r
+ * @param out write bytes here.\r
+ *\r
+ * @exception IOException thrown on error\r
+ */\r
+ public void writeExternal(ObjectOutput out)\r
+ throws IOException\r
+ {\r
+ FormatableHashtable fh = new FormatableHashtable();\r
+ fh.putLong("numRows", numRows);\r
+ fh.putLong("numUnique", numUnique);\r
+ out.writeObject(fh);\r
+ }\r
+ \r
+ /*------------------- Formatable Interface ------------------*/\r
+ /**\r
+ * @return the format id which corresponds to this class.\r
+ */\r
+ public int getTypeFormatId()\r
+ {\r
+ return StoredFormatIds.STATISTICS_IMPL_V01_ID;\r
+ }\r
+\r
+ \r
+ /** @see java.lang.Object#toString */\r
+ public String toString()\r
+ {\r
+ return "numunique= " + numUnique + " numrows= " + numRows;\r
+ }\r
+ \r
+}\r