KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > access > heap > D_HeapController


1 /*
2
3    Derby - Class org.apache.derby.impl.store.access.heap.D_HeapController
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.store.access.heap;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25
26 import org.apache.derby.iapi.error.StandardException;
27 import org.apache.derby.iapi.store.raw.ContainerHandle;
28 import org.apache.derby.iapi.store.raw.Page;
29 import org.apache.derby.iapi.services.diag.Diagnosticable;
30 import org.apache.derby.iapi.services.diag.DiagnosticableGeneric;
31 import org.apache.derby.iapi.services.diag.DiagnosticUtil;
32
33
34 import java.util.Properties JavaDoc;
35
36
37 /**
38
39   The HeapController_D class implements the Diagnostics protocol for the
40   HeapController class.
41
42 **/

43
44 class TableStats
45 {
46     public int num_pages = 0; // number of pages in heap.
47
public int num_overflow_pgs = 0; // number of overflow pages heap.
48
public int num_entries = 0; // number recs on page
49
public int num_deleted = 0; // number of recs on page marked deleted.
50
public long max_pageno = 0; // biggest page number allocated
51
public long num_free_bytes = 0; // number of free bytes on the pages.
52
public long num_res_bytes = 0; // number of reserved bytes on the pages.
53
public long num_overflow_rows = 0; // number of over flow rows on page.
54
public long num_rowsize_bytes = 0; // number of bytes in rows.
55
public long min_rowsize_bytes = Long.MAX_VALUE; // length of shortest row.
56
public long max_rowsize_bytes = Long.MIN_VALUE; // length of longest row.
57
}
58
59 public class D_HeapController extends DiagnosticableGeneric
60 {
61     /* Private/Protected methods of This class: */
62     private static void diag_page(
63     Page page,
64     Properties JavaDoc prop,
65     TableStats stat)
66         throws StandardException
67     {
68         stat.num_pages++;
69         stat.num_entries += page.recordCount();
70         stat.num_deleted +=
71             (page.recordCount() - page.nonDeletedRecordCount());
72         stat.max_pageno = Math.max(stat.max_pageno, page.getPageNumber());
73
74         DiagnosticUtil.findDiagnostic(page).diag_detail(prop);
75
76         // number of free bytes on page.
77
int free_bytes =
78             Integer.parseInt(prop.getProperty(Page.DIAG_BYTES_FREE));
79
80         stat.num_free_bytes += free_bytes;
81
82         // number of bytes reserved on page.
83
int res_bytes =
84             Integer.parseInt(prop.getProperty(Page.DIAG_BYTES_RESERVED));
85
86         stat.num_res_bytes += res_bytes;
87
88         // overflow rows.
89
int overflow =
90             Integer.parseInt(prop.getProperty(Page.DIAG_NUMOVERFLOWED));
91
92         stat.num_overflow_rows += overflow;
93
94         // size of rows.
95
int rowsize =
96             Integer.parseInt(prop.getProperty(Page.DIAG_ROWSIZE));
97
98         stat.num_rowsize_bytes += rowsize;
99
100         // minimum row size.
101
int min_rowsize =
102             Integer.parseInt(prop.getProperty(Page.DIAG_MINROWSIZE));
103
104         if (min_rowsize != 0)
105             stat.min_rowsize_bytes =
106                 Math.min(stat.min_rowsize_bytes, min_rowsize);
107
108         // maximum row size.
109
int max_rowsize =
110             Integer.parseInt(prop.getProperty(Page.DIAG_MAXROWSIZE));
111
112         stat.max_rowsize_bytes = Math.max(stat.max_rowsize_bytes, max_rowsize);
113     }
114
115     private static String JavaDoc out_summary(
116     String JavaDoc hdr,
117     long value,
118     double ratio,
119     String JavaDoc ratio_desc)
120     {
121         String JavaDoc double_str = "" + ratio;
122         String JavaDoc short_str;
123
124         if (ratio > 0.001)
125         {
126             short_str = double_str.substring(
127                 0,
128                 Math.min(double_str.lastIndexOf(".") + 3, double_str.length()));
129         }
130         else
131         {
132             short_str = "NA";
133         }
134
135         return(
136             "\t" + hdr + value + ".\t(" + short_str +
137             " " + ratio_desc + ").\n");
138     }
139             
140
141     private static String JavaDoc diag_tabulate(
142     Properties JavaDoc prop,
143     TableStats stat)
144     {
145         String JavaDoc ret_string = new String JavaDoc();
146
147         // Totals:
148
ret_string +=
149             "Heap conglom has:\n" +
150             "\t" + prop.getProperty(Page.DIAG_PAGE_SIZE) + " bytes per page\n" +
151             "\t" + stat.num_pages + " total used pages (" +
152                 (Integer.parseInt(prop.getProperty(Page.DIAG_PAGE_SIZE)) *
153                      stat.num_pages) +
154                 " bytes)\n" +
155             "\tmaximum page number = " + stat.max_pageno + ".\n" +
156             "\treserved space % = " + prop.getProperty(Page.DIAG_RESERVED_SPACE) + "%.\n" +
157             "\tminimum record size = " + prop.getProperty(Page.DIAG_MINIMUM_REC_SIZE) + ".\n" +
158             "\tminimum record length = " + stat.min_rowsize_bytes + ".\n" +
159             "\tmaximum record length = " + stat.max_rowsize_bytes + ".\n" +
160             "\t# of bytes in rows = " + stat.num_rowsize_bytes + "." +
161                 "\t(" +
162                     (stat.num_entries == 0 ?
163                          0 : (stat.num_rowsize_bytes / stat.num_entries)) +
164                 " bytes/row).\n" +
165             out_summary(
166                 "# of reserved bytes = ",
167                 stat.num_res_bytes,
168                 (stat.num_res_bytes / stat.num_pages),
169                 "reserved bytes/page") +
170             out_summary(
171                 "# of free bytes = ",
172                 stat.num_free_bytes,
173                 (stat.num_free_bytes / stat.num_pages),
174                 "free bytes/page") +
175             out_summary(
176                 "# of total records = ",
177                 stat.num_entries,
178                 (((double) stat.num_entries) / stat.num_pages),
179                 "records/page") +
180             out_summary(
181                 "# of overflow records = ",
182                 stat.num_overflow_rows,
183                 (((double) stat.num_overflow_rows) / stat.num_pages),
184                 "overflow records/page") +
185             out_summary(
186                 "# of deleted records = ",
187                 stat.num_deleted,
188                 (((double) stat.num_deleted) / stat.num_pages),
189                 "deleted records/page");
190
191         return(ret_string);
192     }
193
194     /*
195     ** Methods of Diagnosticable
196     */

197     public void init(Object JavaDoc obj)
198     {
199         if (SanityManager.DEBUG)
200             SanityManager.ASSERT(obj instanceof HeapController);
201
202         super.init(obj);
203     }
204
205
206     /**
207      * Default implementation of diagnostic on the object.
208      * <p>
209      * This routine returns a string with whatever diagnostic information
210      * you would like to provide about this object.
211      * <p>
212      * This routine should be overriden by a real implementation of the
213      * diagnostic information you would like to provide.
214      * <p>
215      *
216      * @return A string with diagnostic information about the object.
217      *
218      * @exception StandardException Standard cloudscape exception policy
219      **/

220     public String JavaDoc diag()
221         throws StandardException
222     {
223         long pageid;
224         ContainerHandle container =
225             ((HeapController) this.diag_object).getOpenConglom().getContainer();
226
227         TableStats stat = new TableStats();
228
229         // ask page to provide diag info:
230
Properties JavaDoc prop = new Properties JavaDoc();
231         prop.put(Page.DIAG_PAGE_SIZE, "");
232         prop.put(Page.DIAG_BYTES_FREE, "");
233         prop.put(Page.DIAG_BYTES_RESERVED, "");
234         prop.put(Page.DIAG_RESERVED_SPACE, "");
235         prop.put(Page.DIAG_MINIMUM_REC_SIZE, "");
236         prop.put(Page.DIAG_NUMOVERFLOWED, "");
237         prop.put(Page.DIAG_ROWSIZE, "");
238         prop.put(Page.DIAG_MINROWSIZE, "");
239         prop.put(Page.DIAG_MAXROWSIZE, "");
240
241         // scan all pages in the heap gathering summary stats in stat
242
Page page = container.getFirstPage();
243
244         while (page != null)
245         {
246             this.diag_page(page, prop, stat);
247             pageid = page.getPageNumber();
248             page.unlatch();
249             page = container.getNextPage(pageid);
250         }
251
252         return(this.diag_tabulate(prop, stat));
253     }
254 }
255
Popular Tags