KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > catalog > types > StatisticsImpl


1 /*
2
3    Derby - Class org.apache.derby.catalog.types.StatisticsImpl
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.catalog.types;
23
24 import org.apache.derby.catalog.Statistics;
25 import org.apache.derby.iapi.services.io.Formatable;
26 import org.apache.derby.iapi.services.io.StoredFormatIds;
27 import org.apache.derby.iapi.services.io.FormatableHashtable;
28 import org.apache.derby.iapi.services.io.FormatableLongHolder;
29
30 import java.io.ObjectOutput JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 public class StatisticsImpl implements Statistics, Formatable
35 {
36     /* total count of rows for which this statistic was created-- this
37        is not the same as the total number of rows in the conglomerate
38        currently, but a snapshot; i.e the number of rows when this
39        statistic was created/updated.
40     */

41
42     private long numRows;
43     
44     /* total count of unique values for the keys
45      */

46     private long numUnique;
47
48     /**
49      * Constructor for StatisticsImpl.
50      *
51      * @param numRows number of rows in the conglomerate for which
52      * this statistic is being created.
53      * @param numUnique number of unique values in the key for which
54      * this statistic is being created.
55      */

56     public StatisticsImpl(long numRows, long numUnique)
57     {
58         this.numRows = numRows;
59         this.numUnique = numUnique;
60     }
61
62     /** Zero argument constructor for Formatable Interface */
63     public StatisticsImpl()
64     {}
65
66     /** @see Statistics#selectivity */
67     public double selectivity(Object JavaDoc[] predicates)
68     {
69         if (numRows == 0.0)
70             return 0.1;
71
72         /* xxxSTATresolve: for small values of numRows, should we do something
73          * special?
74          */

75         return (double)(1/(double)numUnique);
76     }
77
78     /*------------------ Externalizable Interface ------------------*/
79     
80     /**
81      * @see java.io.Externalizable#readExternal
82      */

83     public void readExternal(ObjectInput JavaDoc in)
84         throws IOException JavaDoc, ClassNotFoundException JavaDoc
85     {
86         FormatableHashtable fh = (FormatableHashtable)in.readObject();
87         numRows = fh.getLong("numRows");
88         numUnique = fh.getLong("numUnique");
89     }
90
91     /**
92      * Write this object to a stream of stored objects.
93      *
94      * @param out write bytes here.
95      *
96      * @exception IOException thrown on error
97      */

98     public void writeExternal(ObjectOutput JavaDoc out)
99          throws IOException JavaDoc
100     {
101         FormatableHashtable fh = new FormatableHashtable();
102         fh.putLong("numRows", numRows);
103         fh.putLong("numUnique", numUnique);
104         out.writeObject(fh);
105     }
106         
107     /*------------------- Formatable Interface ------------------*/
108     /**
109      * @return the format id which corresponds to this class.
110      */

111     public int getTypeFormatId()
112     {
113         return StoredFormatIds.STATISTICS_IMPL_V01_ID;
114     }
115
116     
117     /** @see java.lang.Object#toString */
118     public String JavaDoc toString()
119     {
120         return "numunique= " + numUnique + " numrows= " + numRows;
121     }
122     
123 }
124
Popular Tags