KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > data > D_StoredPage


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.D_StoredPage
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.raw.data;
23
24 import org.apache.derby.iapi.services.diag.Diagnosticable;
25 import org.apache.derby.iapi.services.diag.DiagnosticableGeneric;
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27 import org.apache.derby.iapi.error.StandardException;
28
29 import org.apache.derby.iapi.store.access.Qualifier;
30
31 import org.apache.derby.iapi.store.raw.FetchDescriptor;
32 import org.apache.derby.iapi.store.raw.Page;
33 import org.apache.derby.iapi.store.raw.RecordHandle;
34
35 import org.apache.derby.iapi.types.DataValueDescriptor;
36
37 import java.util.Properties JavaDoc;
38 import java.io.PrintStream JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 import org.apache.derby.iapi.services.io.FormatableBitSet;
42 import org.apache.derby.iapi.services.io.CounterOutputStream;
43 import org.apache.derby.iapi.services.io.NullOutputStream;
44
45 /**
46
47 The D_StoredPage class provides diagnostic information about the StoredPage
48 class. Currently this info includes:
49   o a dump of the page.
50   o page size of the page.
51   o bytes free on the page.
52   o bytes reserved on the page.
53
54 **/

55
56 public class D_StoredPage implements Diagnosticable
57 {
58     protected StoredPage page;
59
60     public D_StoredPage()
61     {
62     }
63
64     /* Private/Protected methods of This class: */
65
66     /*
67     ** Methods of Diagnosticable
68     */

69     public void init(Object JavaDoc obj)
70     {
71         if (SanityManager.DEBUG)
72             SanityManager.ASSERT(obj instanceof StoredPage);
73
74         page = (StoredPage) obj;
75     }
76
77     /**
78      * Provide a string dump of the StoredPage.
79      * <p>
80      * RESOLVE - once the "Diagnostic" interface is accepted move the
81      * string dumping code into this routine from it's current place in
82      * the StoredPage code.
83      * <p>
84      *
85      * @return string dump of the StoredPage
86      *
87      * @exception StandardException Standard exception policy.
88      **/

89     public String JavaDoc diag()
90         throws StandardException
91     {
92         return(page.toString());
93     }
94
95     /**
96      * Provide detailed diagnostic information about a StoredPage.
97      * <p>
98      * Currently supports 3 types of information:
99      * Page.DIAG_PAGE_SIZE - page size.
100      * Page.DIAG_BTYES_FREE - # of free bytes on the page.
101      * Page.DIAG_BYTES_RESERVED - # of reserved bytes on the page.
102      * <p>
103      *
104      * @exception StandardException Standard exception policy.
105      **/

106     public void diag_detail(Properties JavaDoc prop)
107         throws StandardException
108     {
109         String JavaDoc prop_value = null;
110
111         // currently only support 2 properties - pageSize and freeBytes
112
if (prop.getProperty(Page.DIAG_PAGE_SIZE) != null)
113         {
114             // set the page size diag string
115
prop.put(Page.DIAG_PAGE_SIZE, Integer.toString(page.getPageSize()));
116         }
117
118         if (prop.getProperty(Page.DIAG_BYTES_FREE) != null)
119         {
120             int space_available = page.freeSpace;
121
122             // set the page free diag string
123
prop.put(Page.DIAG_BYTES_FREE, Integer.toString(space_available));
124         }
125
126         if (prop.getProperty(Page.DIAG_BYTES_RESERVED) != null)
127         {
128             int reservedSpace = (page.totalSpace * page.spareSpace / 100);
129             reservedSpace = Math.min(reservedSpace, page.freeSpace);
130             
131             // set the reserved space diag string.
132
prop.put(
133                 Page.DIAG_BYTES_RESERVED, Integer.toString(reservedSpace));
134         }
135
136         if (prop.getProperty(Page.DIAG_RESERVED_SPACE) != null)
137         {
138             // DIAG_RESERVED_SPACE is the % of the page to reserve during
139
// insert for expansion.
140

141             prop.put(
142                 Page.DIAG_RESERVED_SPACE, Integer.toString(page.spareSpace));
143         }
144
145         if (prop.getProperty(Page.DIAG_MINIMUM_REC_SIZE) != null)
146         {
147             // DIAG_MINIMUM_REC_SZE is the minimum number of bytes per row
148
// to reserve at insert time for a record.
149

150             prop.put(
151                 Page.DIAG_MINIMUM_REC_SIZE,
152                 Integer.toString(page.minimumRecordSize));
153         }
154
155         if (prop.getProperty(Page.DIAG_PAGEOVERHEAD) != null)
156         {
157             // DIAG_PAGEOVERHEAD is the amount of space needed by the page
158
// for it's internal info.
159

160             prop.put(
161                 Page.DIAG_PAGEOVERHEAD,
162                 Integer.toString(page.getPageSize() - page.getMaxFreeSpace()));
163         }
164
165         if (prop.getProperty(Page.DIAG_SLOTTABLE_SIZE) != null)
166         {
167             // DIAG_SLOTTABLE_SIZE is the amount of space needed by the page
168
// for the current slot table.
169

170             // RESOLVE - it would be better to call a StoredPage variable or
171
// interface.
172
int slotEntrySize = page.getSlotsInUse() * 3 *
173                 ((page.getPageSize() >= 65536) ?
174                     StoredPage.LARGE_SLOT_SIZE : StoredPage.SMALL_SLOT_SIZE);
175
176             prop.put(Page.DIAG_SLOTTABLE_SIZE, Integer.toString(slotEntrySize));
177         }
178
179         // loop through slot table and determine row size's and overflow recs.
180
int overflow_count = 0;
181         int row_size = 0;
182         long min_rowsize = 0;
183         long max_rowsize = 0;
184         long record_size = 0;
185
186         if (page.getSlotsInUse() > 0)
187         {
188             min_rowsize = Long.MAX_VALUE;
189             max_rowsize = Long.MIN_VALUE;
190
191             for (int slot = 0; slot < page.getSlotsInUse(); slot++)
192             {
193                 try
194                 {
195                     if (page.getIsOverflow(slot))
196                     {
197                         if (SanityManager.DEBUG)
198                             SanityManager.DEBUG_PRINT("OVER",
199                                 "Slot (" + slot + ") is overflow record of page:" +
200                                 page);
201                         overflow_count++;
202                     }
203                     record_size = page.getRecordLength(slot);
204                     row_size += record_size;
205
206                     min_rowsize = Math.min(min_rowsize, record_size);
207                     max_rowsize = Math.max(max_rowsize, record_size);
208                 }
209                 catch (Throwable JavaDoc t)
210                 {
211                     System.out.println("Got error from getIsOverflow().");
212                 }
213
214             }
215         }
216
217         if (prop.getProperty(Page.DIAG_NUMOVERFLOWED) != null)
218         {
219             // DIAG_NUMOVERFLOWED is the number of over flow rows on this page.
220

221             prop.put(Page.DIAG_NUMOVERFLOWED, Integer.toString(overflow_count));
222         }
223
224         if (prop.getProperty(Page.DIAG_ROWSIZE) != null)
225         {
226             // sum of the record lengths on this page.
227

228             prop.put(Page.DIAG_ROWSIZE, Integer.toString(row_size));
229         }
230
231         if (prop.getProperty(Page.DIAG_MINROWSIZE) != null)
232         {
233             // minimum length record on this page.
234

235             prop.put(Page.DIAG_MINROWSIZE, Long.toString(min_rowsize));
236         }
237
238         if (prop.getProperty(Page.DIAG_MAXROWSIZE) != null)
239         {
240             // maximum length record on this page.
241

242             prop.put(Page.DIAG_MAXROWSIZE, Long.toString(max_rowsize));
243         }
244     }
245
246
247
248     /**
249         Checks the slot table.
250         <p>
251
252         1) checks the number of slot entries matches the record count
253         2) checks the slot table lengths match the field lengths
254
255         @exception StandardException Standard exception policy.
256     */

257     public boolean checkSlotTable(PrintStream JavaDoc out) throws StandardException, IOException JavaDoc {
258
259         boolean ok = true;
260
261         int slotCount = page.getSlotsInUse();
262         int recordCount = page.recordCount();
263
264         if (slotCount != recordCount) {
265             out.println("CORRUPT PAGE: slot count mismatch: slot count " + slotCount
266                 + " record count " + recordCount);
267             ok = false;
268         }
269
270         for (int slot = 0; slot < slotCount; slot++) {
271
272             int recordLength = page.getRecordPortionLength(slot);
273
274
275             CounterOutputStream counter = new CounterOutputStream();
276             counter.setOutputStream(new NullOutputStream());
277
278             int recordId =
279                 page.fetchFromSlot(
280                     null,
281                     slot,
282                     new DataValueDescriptor[0],
283                     (FetchDescriptor) null, true).getId();
284
285             page.logRecord(slot, page.LOG_RECORD_DEFAULT, recordId,
286                            (FormatableBitSet) null, counter, (RecordHandle)null);
287
288             int actualLength = counter.getCount();
289
290             if (actualLength != recordLength) {
291                 out.println(
292                     "CORRUPT PAGE: record length mismatch at slot " + slot);
293                 out.println(" slot entry length " + recordLength);
294                 out.println(" actual length " + actualLength);
295                 ok = false;
296             }
297
298         }
299
300
301         return ok;
302
303     }
304
305     public String JavaDoc pageHeaderToString()
306     {
307         return "page id " + page.getIdentity() +
308                 " Overflow: " + page.isOverflowPage() +
309                 " PageVersion: " + page.getPageVersion() +
310                 " SlotsInUse: " + page.getSlotsInUse() +
311                 " PageStatus: " + page.getPageStatus() +
312                 " NextId: " + page.newRecordId() + "\n";
313     }
314
315 }
316
Popular Tags