KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.D_DiagnosticUtil
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.context.ContextService;
25 import org.apache.derby.iapi.services.diag.Diagnosticable;
26 import org.apache.derby.iapi.services.diag.DiagnosticUtil;
27 import org.apache.derby.iapi.services.monitor.Monitor;
28 import org.apache.derby.iapi.error.StandardException;
29 import org.apache.derby.iapi.store.access.AccessFactory;
30 import org.apache.derby.iapi.store.access.ConglomerateController;
31 import org.apache.derby.iapi.store.access.TransactionController;
32 import org.apache.derby.iapi.store.raw.ContainerHandle;
33 import org.apache.derby.iapi.store.raw.ContainerKey;
34 import org.apache.derby.iapi.store.raw.Page;
35 import org.apache.derby.iapi.store.raw.Transaction;
36 import org.apache.derby.iapi.store.raw.RawStoreFactory;
37
38
39 // import com.ibm.db2j.impl.BasicServices.TestService.TestTemplate.T_MultiIterations;
40
// import com.ibm.db2j.impl.BasicServices.TestService.TestTemplate.T_Fail;
41
import org.apache.derby.iapi.reference.Property;
42
43 // import java.util.Properties;
44

45 // DEBUGGING:
46

47 /**
48
49   This class provides some utility functions used to debug on disk structures
50   of the store.
51
52 **/

53
54
55 public class D_DiagnosticUtil
56 {
57
58
59     /* Constructors for This class: */
60
61     /**
62      * No arg Constructor.
63      **/

64     public D_DiagnosticUtil()
65     {
66     }
67
68     /* Private/Protected methods of This class: */
69
70     /**
71      * Given a database name come up with a module.
72      * <p>
73      *
74      * @return The store module associated with given database name.
75      *
76      * @param db_name name of the database.
77      *
78      * @exception StandardException Standard exception policy.
79      **/

80     private static Object JavaDoc getModuleFromDbName(String JavaDoc db_name)
81         throws StandardException
82     {
83         Object JavaDoc store_module = null;
84
85         Object JavaDoc db = Monitor.findService(Property.DATABASE_MODULE, db_name);
86
87         // RESOLVE (mikem) - find a single way to find the current
88
// AccessFactory that works both for ij and unit tests.
89
if (db == null)
90         {
91             // maybe it is a module test - try this hack:
92
store_module = Monitor.findService(AccessFactory.MODULE, db_name);
93         }
94         else
95         {
96             // Find the AccessFactory
97
store_module = Monitor.findServiceModule(db, AccessFactory.MODULE);
98         }
99
100         return(store_module);
101     }
102
103     /* Public Methods of This class: */
104     /**
105      * Given a Database name and conglomid print out diagnostic info.
106      * <p>
107      * Print diagnostic information about a particular conglomerate, can be
108      * called for either a btree or heap conglomerate. This routine
109      * prints out the string to "System.out"; "ij", depending on it's
110      * configuration, will only print out a fixed length (default 128 bytes),
111      * so having ij print the string can be a problem.
112      * <p>
113      *
114      * Can be called from ij to find out info about conglomid 19 in database
115      * 'msgdb' by using the following syntax:
116      *
117        maximumdisplaywidth 9000;
118
119        CREATE FUNCTION D_CONGLOMID_PRINT(DBNAME VARCHAR(128), CONGLOMID INT)
120        RETURNS VARCHAR(32000) RETURNS NULL ON NULL INPUT
121        EXTERNAL NAME
122        'org.apache.derby.impl.store.raw.data.D_DiagnosticUtil.diag_conglomid_print'
123        LANGUAGE JAVA PARAMETER STYLE JAVA;
124
125        values D_CONGLOMID_PRINT('msgdb', 19);
126           com.ibm.db2j.protocol.BasicServices.Diagnostic.T_Diagnosticable::
127           diag_conglomid_print('msgdb', 19);
128      *
129      * RESOLVE - An interface that takes a table name would be nice.
130      *
131      * @param db_name name of the database
132      * @param conglomid conglomerate id of the conglomerate to debug
133      *
134      * @exception StandardException Standard exception policy.
135      **/

136     public static String JavaDoc diag_conglomid_print(String JavaDoc db_name, long conglomid)
137         throws StandardException
138     {
139         try
140         {
141             System.out.println(diag_conglomid(db_name, conglomid));
142         }
143         catch (Throwable JavaDoc t)
144         {
145             t.printStackTrace();
146         }
147
148         return("");
149     }
150     
151     /**
152      * Given a Database name and conglomid, return diagnositic string.
153      * <p>
154      * Return a string with diagnostic information about a particular
155      * conglomerate, can be called for any type of conglomerate (some types
156      * may not return any info though).
157      * <p>
158      * Can be called from ij to find out info about conglomid 19 in database
159      * 'msgdb' by using the following syntax:
160      *
161      * values
162      * com.ibm.db2j.protocol.BasicServices.Diagnostic.T_Diagnosticable::
163      * diag_conglomid('msgdb', 19);
164        maximumdisplaywidth 9000;
165
166        CREATE FUNCTION DIAG_CONGLOMID(DBNAME VARCHAR(128), CONGLOMID INT)
167        RETURNS VARCHAR(32000) RETURNS NULL ON NULL INPUT
168        EXTERNAL NAME
169        'org.apache.derby.impl.store.raw.data.D_DiagnosticUtil.diag_conglomid'
170        LANGUAGE JAVA PARAMETER STYLE JAVA;
171
172        values DIAG_CONGLOMID('msgdb', 19);
173           com.ibm.db2j.protocol.BasicServices.Diagnostic.T_Diagnosticable::
174           diag_conglomid_print('msgdb', 19);
175      *
176      * RESOLVE - An interface that takes a table name would be nice.
177      *
178      * @param db_name name of the database
179      * @param conglomid conglomerate id of the conglomerate to debug
180      *
181      * @exception StandardException Standard exception policy.
182      **/

183     public static String JavaDoc diag_conglomid(String JavaDoc db_name, long conglomid)
184         throws StandardException
185     {
186         String JavaDoc ret_string = null;
187         AccessFactory store_module = null;
188
189         store_module = (AccessFactory) getModuleFromDbName(db_name);
190
191         if (store_module != null)
192         {
193
194             TransactionController tc =
195                 store_module.getTransaction(
196                     ContextService.getFactory().getCurrentContextManager());
197
198             ConglomerateController open_table =
199                 tc.openConglomerate(
200                     conglomid, false, 0, TransactionController.MODE_TABLE,
201                     TransactionController.ISOLATION_SERIALIZABLE);
202
203             open_table.debugConglomerate();
204
205             Diagnosticable diag_obj = DiagnosticUtil.findDiagnostic(open_table);
206
207             ret_string = diag_obj.diag();
208
209             open_table.close();
210         }
211         else
212         {
213             System.out.println(
214                 "Could not find module for database: " + db_name);
215         }
216
217         return(ret_string);
218     }
219
220     /**
221      * Dump raw contents of a page.
222      * <p>
223      * A utility routine that can be called from an ij session that will
224      * dump the raw contents of a page, in the raw store dump format.
225      *
226      * @param db_name name of the database
227      * @param segmentid segmentid of the table (usually 0)
228      * @param containerid containerid of the table (not conglomid)
229      * @param pagenumber pagenumber of page to dump.
230      *
231      **/

232     public static void diag_dump_page(
233     String JavaDoc db_name,
234     long segmentid,
235     long containerid,
236     long pagenumber)
237     {
238         Transaction xact = null;
239         try
240         {
241             Object JavaDoc module = getModuleFromDbName(db_name);
242
243             RawStoreFactory store_module = (RawStoreFactory)
244                 Monitor.findServiceModule(module, RawStoreFactory.MODULE);
245
246             xact = store_module.startInternalTransaction(ContextService.getFactory().getCurrentContextManager());
247
248             ContainerKey id = new ContainerKey(segmentid, containerid);
249             ContainerHandle container =
250                 xact.openContainer(id,
251                                    ContainerHandle.MODE_READONLY);
252             Page page = container.getPage(pagenumber);
253
254             if (page != null)
255             {
256                 System.out.println(page.toString());
257                 page.unlatch();
258             }
259             else
260             {
261                 System.out.println("page " + pagenumber + " not found");
262             }
263             xact.abort();
264             xact.close();
265             xact = null;
266         }
267         catch (StandardException se)
268         {
269             se.printStackTrace();
270         }
271         finally
272         {
273             if (xact != null)
274             {
275                 try
276                 {
277                     xact.abort();
278                     xact.close();
279                 }
280                 catch (StandardException se)
281                 {
282                 }
283             }
284         }
285     }
286
287     /**
288      * Given a Database name and conglomid, return container id.
289      * <p>
290      * Return the containerid of a given conglomerate id.
291      * <p>
292      * Can be called from ij to find out info about conglomid 19 in database
293      * 'msgdb' by using the following syntax:
294      *
295           values
296           com.ibm.db2j.protocol.BasicServices.Diagnostic.T_Diagnosticable).
297           diag_containerid_to_conglomid('msgdb', 924300359390);
298      *
299      * RESOLVE - An interface that takes a table name would be nice.
300      *
301      * @param db_name name of the database
302      * @param containerid container id of the conglomerate to look up
303      *
304      * @exception StandardException Standard exception policy.
305      **/

306     public static long diag_containerid_to_conglomid(
307     String JavaDoc db_name,
308     long containerid)
309         throws StandardException
310     {
311         // Find the AccessFactory
312
Object JavaDoc store_module = getModuleFromDbName(db_name);
313
314         return(diag_containerid_to_conglomid(store_module, containerid));
315     }
316
317     public static long diag_containerid_to_conglomid(
318     Object JavaDoc module,
319     long containerid)
320     {
321         String JavaDoc ret_string = null;
322         AccessFactory store_module = null;
323         long conglom_id = Long.MIN_VALUE;
324
325         // Find the AccessFactory
326
store_module = (AccessFactory)
327             Monitor.getServiceModule(module, AccessFactory.MODULE);
328
329         if (store_module != null)
330         {
331             try
332             {
333                 TransactionController tc =
334                     store_module.getTransaction(
335                         ContextService.getFactory().getCurrentContextManager());
336
337                 conglom_id = tc.findConglomid(containerid);
338             }
339             catch (Throwable JavaDoc t)
340             {
341                 t.printStackTrace();
342                 // on error just return the initialized bad value conglom_id
343
}
344         }
345         else
346         {
347             // during access boot this does not exist, assume for now that
348
// is why we got here. RESOLVE - it would be nice if we could
349
// actuallly figure that is why we failed.
350

351             /*
352             System.out.println(
353                 "Could not find module for module: " + module);
354             */

355         }
356
357         return(conglom_id);
358     }
359
360     /**
361      * Given a Database name and containerid, return conglomerate id.
362      * <p>
363      * Return the conglomerate id of a given conainer id.
364      * <p>
365      * Can be called from ij to find out info about conglomid 19 in database
366      * 'msgdb' by using the following syntax:
367      *
368           values
369           com.ibm.db2j.protocol.BasicServices.Diagnostic.T_Diagnosticable).
370           diag_conglomid_to_containerid('msgdb', 19);
371      *
372      * RESOLVE - An interface that takes a table name would be nice.
373      *
374      * @param db_name name of the database
375      * @param conglomid conglomerate id of the conglomerate to debug
376      *
377      * @exception StandardException Standard exception policy.
378      **/

379     public static long diag_conglomid_to_containerid(
380     String JavaDoc db_name,
381     long conglomid)
382         throws StandardException
383     {
384         String JavaDoc ret_string = null;
385         Object JavaDoc store_module = null;
386         long conglom_id = Long.MIN_VALUE;
387
388         // Find the AccessFactory
389
store_module = getModuleFromDbName(db_name);
390
391         return(diag_conglomid_to_containerid(store_module, conglomid));
392     }
393
394     public static long diag_conglomid_to_containerid(
395     Object JavaDoc module,
396     long conglomid)
397     {
398         String JavaDoc ret_string = null;
399         AccessFactory store_module = null;
400         long container_id = Long.MIN_VALUE;
401
402         // Find the AccessFactory
403
store_module = (AccessFactory)
404             Monitor.getServiceModule(module, AccessFactory.MODULE);
405
406         if (store_module != null)
407         {
408             try
409             {
410                 TransactionController tc =
411                     store_module.getTransaction(
412                         ContextService.getFactory().getCurrentContextManager());
413
414                 container_id = tc.findContainerid(conglomid);
415             }
416             catch (Throwable JavaDoc t)
417             {
418                 t.printStackTrace();
419                 // on error just return the initialized bad value conglom_id
420
}
421         }
422         else
423         {
424             // during access boot this does not exist, assume for now that
425
// is why we got here. RESOLVE - it would be nice if we could
426
// actuallly figure that is why we failed.
427

428             /*
429             System.out.println(
430                 "Could not find module for module: " + module);
431             */

432         }
433
434         return(container_id);
435     }
436
437 }
438
Popular Tags