KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > diag > SpaceTable


1 /*
2
3    Derby - Class org.apache.derby.diag.SpaceTable
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.diag;
23
24 import org.apache.derby.iapi.services.monitor.Monitor;
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26
27 import org.apache.derby.iapi.error.StandardException;
28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
29 import org.apache.derby.iapi.sql.conn.ConnectionUtil;
30 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
31 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
32 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
33 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
34 import org.apache.derby.iapi.store.access.AccessFactory;
35 import org.apache.derby.iapi.store.access.TransactionController;
36 import org.apache.derby.iapi.store.access.ConglomerateController;
37 import org.apache.derby.iapi.store.access.SpaceInfo;
38 import org.apache.derby.iapi.error.PublicAPI;
39
40 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
41 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData;
42
43 import java.sql.ResultSetMetaData JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.sql.Types JavaDoc;
46 import org.apache.derby.vti.VTITemplate;
47 import org.apache.derby.vti.VTICosting;
48 import org.apache.derby.vti.VTIEnvironment;
49
50 /**
51     SpaceTable is a virtual table that shows the space usage of a particular
52     table and its indexes.
53     
54     This virtual table can be invoked by calling it
55     directly, and supplying the schema name and table name as arguments.
56     <PRE> select * from new org.apache.derby.diag.SpaceTable('MYSCHEMA','MYTABLE') t; </PRE>
57     If the schema name is not supplied, the default schema is used.
58     <PRE> select * from new org.apache.derby.diag.SpaceTable('MYTABLE') t; </PRE>
59     Alternatively, the table can be invoked through the system alias SpaceTable
60     <PRE> select * from new SPACETABLE('MYTABLE') t; </PRE>
61     <P>
62     NOTE: Both the schema name and the table name must be any expression that evaluates to a
63     string data type. If you created a schema or table name as a non-delimited identifier,
64     you must present their names in all upper case.
65
66
67     <P>The SpaceTable virtual table can be used to estimate whether space
68     might be saved by compressing a table and its indexes.
69
70     <P>The SpaceTable virtual table has the following columns:
71     <UL>
72     <LI>CONGLOMERATENAME varchar(128) - nullable. The name of the conglomerate,
73     which is either the table name or the index name. (Unlike the
74     SYSCONGLOMERATES column of the same name, table ID's do not appear
75     here).</LI>
76     <LI>ISINDEX SMALLINT - not nullable. Is not zero if the conglomerate is an
77     index, 0 otherwise.</LI>
78     <LI>NUMALLOCATEDPAGES bigint - not nullable. The number of pages actively
79     linked into the table. The total number of pages in the file is the
80     sum of NUMALLOCATEDPAGES + NUMFREEPAGES.</LI>
81     <LI>NUMFREEPAGES bigint - not nullable. The number of free pages that
82     belong to the table. When a new page is to be linked into the table the
83     system will move a page from the NUMFREEPAGES list to the NUMALLOCATEDPAGES
84     list. The total number of pages in the file is the sum of
85     NUMALLOCATEDPAGES + NUMFREEPAGES.</LI>
86     <LI>NUMUNFILLEDPAGES bigint - not nullable. The number of unfilled pages
87     that belong to the table. Unfilled pages are allocated pages that are not
88     completely full. Note that the number of unfilled pages is an estimate and
89     is not exact. Running the same query twice can give different results on
90     this column. </LI>
91     <LI>PAGESIZE integer - not nullable. The size of the page in bytes for
92     that conglomerate.
93     </LI>
94     <LI>ESTIMSPACESAVING bigint - not nullable. The estimated space which
95     could possibly be saved by compressing the conglomerate, in bytes.</LI>
96     </UL>
97
98
99     <P>
100     To get space information on all schemas and tables, use a query such as
101     <PRE>
102     select v.*
103     from SYS.SYSSCHEMAS s,
104          SYS.SYSTABLES t,
105          new org.apache.derby.diag.SpaceTable(SCHEMANAME,TABLENAME) v
106     where s.SCHEMAID = t.SCHEMAID;
107     </PRE>
108 */

109 public class SpaceTable extends VTITemplate implements VTICosting {
110
111     private ConglomInfo[] conglomTable;
112     boolean initialized;
113     int currentRow;
114     private boolean wasNull;
115     private String JavaDoc schemaName;
116     private String JavaDoc tableName;
117     private SpaceInfo spaceInfo;
118     private TransactionController tc;
119
120
121     public SpaceTable(String JavaDoc schemaName, String JavaDoc tableName)
122     {
123         this.schemaName = schemaName;
124         this.tableName = tableName;
125     }
126
127     public SpaceTable(String JavaDoc tableName)
128     {
129         this.tableName = tableName;
130     }
131
132     private void getConglomInfo(LanguageConnectionContext lcc)
133         throws StandardException
134     {
135         DataDictionary dd = lcc.getDataDictionary();
136         
137         if (schemaName == null)
138             schemaName = lcc.getCurrentSchemaName();
139
140         // if schemaName is null, it gets the default schema
141
SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, lcc.getTransactionExecute(), true);
142         TableDescriptor td = dd.getTableDescriptor(tableName,sd);
143         if (td == null) // table does not exist
144
{
145             conglomTable = new ConglomInfo[0]; // make empty conglom table
146
return;
147         }
148         ConglomerateDescriptor[] cds = td.getConglomerateDescriptors();
149         // initialize spaceTable
150
conglomTable = new ConglomInfo[cds.length];
151         for (int i = 0; i < cds.length; i++)
152             conglomTable[i] = new ConglomInfo(
153                 cds[i].getConglomerateNumber(),
154                 cds[i].isIndex() ? cds[i].getConglomerateName() : tableName,
155                 cds[i].isIndex());
156     }
157
158
159     private void getSpaceInfo(int index)
160         throws StandardException
161     {
162             ConglomerateController cc = tc.openConglomerate(
163                 conglomTable[index].getConglomId(),
164                 false,
165                 0, // not for update
166
TransactionController.MODE_RECORD,
167                 TransactionController.ISOLATION_READ_COMMITTED
168                 );
169             spaceInfo = cc.getSpaceInfo();
170             cc.close();
171             cc = null;
172     }
173
174
175     /**
176         @see java.sql.ResultSet#getMetaData
177      */

178     public ResultSetMetaData getMetaData()
179     {
180         return metadata;
181     }
182
183     /**
184         @see java.sql.ResultSet#next
185         @exception SQLException if no transaction context can be found
186      */

187     public boolean next() throws SQLException JavaDoc
188     {
189         try
190         {
191             if (!initialized)
192             {
193                 LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
194                getConglomInfo(lcc);
195                 tc = lcc.getTransactionExecute();
196                 initialized = true;
197                 currentRow = -1;
198             }
199             if (conglomTable == null)
200                 return false;
201             currentRow++;
202             if (currentRow >= conglomTable.length)
203                 return false;
204             spaceInfo = null;
205             getSpaceInfo(currentRow);
206             return true;
207         }
208         catch (StandardException se)
209         {
210             throw PublicAPI.wrapStandardException(se);
211         }
212     }
213
214
215     /**
216         @see java.sql.ResultSet#close
217      */

218     public void close()
219     {
220         conglomTable = null;
221         spaceInfo = null;
222         tc = null;
223     }
224
225     /**
226         @see java.sql.ResultSet#getString
227      */

228     public String JavaDoc getString(int columnNumber)
229     {
230         ConglomInfo conglomInfo = conglomTable[currentRow];
231         String JavaDoc str = conglomInfo.getConglomName();
232         wasNull = (str == null);
233         return str;
234     }
235
236     /**
237     @see java.sql.ResultSet#getLong
238     */

239     public long getLong(int columnNumber)
240     {
241         long longval;
242         ConglomInfo conglomInfo = conglomTable[currentRow];
243         switch(columnNumber)
244         {
245             case 3:
246                 longval = spaceInfo.getNumAllocatedPages();
247                 break;
248             case 4:
249                 longval = spaceInfo.getNumFreePages();
250                 break;
251             case 5:
252                 longval = spaceInfo.getNumUnfilledPages();
253                 break;
254             case 7:
255                 int psize = spaceInfo.getPageSize();
256                 longval = (spaceInfo.getNumFreePages() * psize);
257                 // unfilled page estimate is not reproducible/too unstable
258
// + ((spaceInfo.getNumUnfilledPages() * psize) / 2);
259
break;
260             default:
261                 longval = -1;
262         }
263         wasNull = false;
264         if (SanityManager.DEBUG)
265             if (longval < 0)
266                 SanityManager.THROWASSERT("SpaceTable column number " + columnNumber +
267                     " has a negative value at row " + currentRow);
268         return longval;
269     }
270
271     /**
272     @see java.sql.ResultSet#getShort
273     */

274     public short getShort(int columnNumber)
275     {
276         ConglomInfo conglomInfo = conglomTable[currentRow];
277         wasNull = false;
278         return (short) (conglomInfo.getIsIndex() ? 1 : 0);
279     }
280
281
282     /**
283     @see java.sql.ResultSet#getInt
284     */

285     public int getInt(int columnNumber)
286     {
287         return spaceInfo.getPageSize();
288     }
289
290
291     /**
292         @see java.sql.ResultSet#wasNull
293      */

294     public boolean wasNull()
295     {
296         return wasNull;
297     }
298
299
300     /** VTI costing interface */
301
302     /**
303         @see VTICosting#getEstimatedRowCount
304      */

305     public double getEstimatedRowCount(VTIEnvironment vtiEnvironment)
306     {
307         return VTICosting.defaultEstimatedRowCount;
308     }
309
310     /**
311         @see VTICosting#getEstimatedCostPerInstantiation
312      */

313     public double getEstimatedCostPerInstantiation(VTIEnvironment vtiEnvironment)
314     {
315         return VTICosting.defaultEstimatedCost;
316     }
317
318     /**
319         @return true
320         @see VTICosting#supportsMultipleInstantiations
321      */

322     public boolean supportsMultipleInstantiations(VTIEnvironment vtiEnvironment)
323     {
324         return true;
325     }
326
327     /*
328     ** Metadata
329     */

330     private static final ResultColumnDescriptor[] columnInfo = {
331
332         EmbedResultSetMetaData.getResultColumnDescriptor("CONGLOMERATENAME", Types.VARCHAR, true, 128),
333         EmbedResultSetMetaData.getResultColumnDescriptor("ISINDEX", Types.SMALLINT, false),
334         EmbedResultSetMetaData.getResultColumnDescriptor("NUMALLOCATEDPAGES", Types.BIGINT, false),
335         EmbedResultSetMetaData.getResultColumnDescriptor("NUMFREEPAGES", Types.BIGINT, false),
336         EmbedResultSetMetaData.getResultColumnDescriptor("NUMUNFILLEDPAGES", Types.BIGINT, false),
337         EmbedResultSetMetaData.getResultColumnDescriptor("PAGESIZE", Types.INTEGER, false),
338         EmbedResultSetMetaData.getResultColumnDescriptor("ESTIMSPACESAVING", Types.BIGINT, false),
339     };
340     
341     private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(columnInfo);
342
343 }
344
345 class ConglomInfo
346 {
347     private long conglomId;
348     private String JavaDoc conglomName;
349     private boolean isIndex;
350
351     public ConglomInfo(long conglomId, String JavaDoc conglomName, boolean isIndex)
352     {
353         this.conglomId = conglomId;
354         this.conglomName = conglomName;
355         this.isIndex = isIndex;
356     }
357
358     public long getConglomId()
359     {
360         return conglomId;
361     }
362
363     public String JavaDoc getConglomName()
364     {
365         return conglomName;
366     }
367
368     public boolean getIsIndex()
369     {
370         return isIndex;
371     }
372 }
373
374
375
376
377
Popular Tags