KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > tools > DBConglomerateDiagTool


1 /**
2  * com.mckoi.tools.DBConglomerateDiagTool 30 Nov 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.tools;
26
27 import com.mckoi.database.*;
28 import com.mckoi.database.control.*;
29 import com.mckoi.util.CommandLine;
30 import com.mckoi.debug.Lvl;
31 import javax.swing.*;
32 import javax.swing.event.*;
33 import javax.swing.table.*;
34 import java.awt.*;
35 import java.awt.event.*;
36 import java.io.*;
37
38 /**
39  * An interactive tool for diagnosing the contents of a TableDataConglomerate
40  * object.
41  *
42  * @author Tobias Downer
43  */

44
45 public class DBConglomerateDiagTool {
46
47   private static void diagnose(String JavaDoc path, String JavaDoc name) {
48     try {
49       TransactionSystem system = new TransactionSystem();
50       DefaultDBConfig config = new DefaultDBConfig();
51       config.setDatabasePath(path);
52       config.setLogPath("");
53       config.setMinimumDebugLevel(50000);
54       config.setReadOnly(true);
55       system.setDebugOutput(new StringWriter());
56       system.init(config);
57       system.setDebugLevel(Lvl.INFORMATION);
58
59       final TableDataConglomerate conglomerate =
60                     new TableDataConglomerate(system, system.storeSystem());
61       // Open it.
62
conglomerate.open(name); //false);
63
// Open the main window
64
final JFrame frame = new JFrame("Mckoi SQL Database Diagnostic Tool");
65       frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
66       Container c = frame.getContentPane();
67       c.setLayout(new BorderLayout());
68       c.add(new ConglomerateViewPane(conglomerate), BorderLayout.CENTER);
69
70       frame.addWindowListener(new WindowAdapter() {
71         public void windowClosing(WindowEvent evt) {
72           try {
73             frame.dispose();
74             conglomerate.close();
75             System.exit(0);
76           }
77           catch (IOException e) {
78             e.printStackTrace();
79           }
80         }
81       });
82
83       frame.pack();
84       frame.setVisible(true);
85
86     }
87     catch (IOException e) {
88       e.printStackTrace();
89     }
90   }
91
92   /**
93    * Prints the syntax.
94    */

95   private static void printSyntax() {
96     System.out.println("DBConglomerateDiagTool -path [data directory] " +
97                        "[-name [database name]]");
98   }
99
100   /**
101    * Application start point.
102    */

103   public static void main(String JavaDoc[] args) {
104     CommandLine cl = new CommandLine(args);
105
106     String JavaDoc path = cl.switchArgument("-path");
107     String JavaDoc name = cl.switchArgument("-name", "DefaultDatabase");
108
109     if (path == null) {
110       printSyntax();
111       System.out.println("Error: -path not found on command line.");
112       System.exit(-1);
113     }
114
115     // Start the conversion.
116
diagnose(path, name);
117
118   }
119
120   // ---------- Inner classes ----------
121

122 }
123
124
125 /**
126  * The pane the shows the contents of a conglomerate.
127  */

128 class ConglomerateViewPane extends JRootPane {
129
130   /**
131    * The conglomerate.
132    */

133   private TableDataConglomerate conglomerate;
134
135   /**
136    * The current selected table.
137    */

138   private RawDiagnosticTable current_table;
139
140 // /**
141
// * The JDesktopPane.
142
// */
143
// private JDesktopPane desktop;
144

145   /**
146    * The view of the store.
147    */

148   private StoreViewPane view_pane;
149
150   /**
151    * Constructor.
152    */

153   public ConglomerateViewPane(TableDataConglomerate conglom) {
154     setPreferredSize(new Dimension(750, 550));
155
156     this.conglomerate = conglom;
157
158     JMenu info = new JMenu("Info");
159     info.add(average_row_count_action);
160
161     JMenuBar menubar = new JMenuBar();
162     menubar.add(info);
163
164     setJMenuBar(menubar);
165
166     Container c = getContentPane();
167     c.setLayout(new BorderLayout());
168
169     // The view pane.
170
view_pane = new StoreViewPane();
171
172     // Add the table list view to the desktop.
173
final String JavaDoc[] str_t_list = conglomerate.getAllTableFileNames();
174     final JList table_list = new JList(str_t_list);
175     table_list.addListSelectionListener(new ListSelectionListener() {
176       public void valueChanged(ListSelectionEvent evt) {
177         int index = table_list.getSelectedIndex();
178         current_table = conglomerate.getDiagnosticTable(str_t_list[index]);
179         view_pane.setDiagnosticModel(current_table);
180       }
181     });
182     JScrollPane scrolly_table_list = new JScrollPane(table_list);
183
184     // The split pane.
185
JSplitPane split_pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
186     split_pane.setLeftComponent(scrolly_table_list);
187     split_pane.setRightComponent(view_pane);
188
189     c.add(split_pane, BorderLayout.CENTER);
190
191 // // The frame that shows the contents of the store.
192
// JInternalFrame frame = new JInternalFrame("Store content", true);
193
// frame.setPreferredSize(new Dimension(570, 520));
194
// c = frame.getContentPane();
195
// c.add(view_pane);
196
// desktop.add(frame);
197
// frame.setLocation(new Point(170, 10));
198
// frame.pack();
199
// frame.setVisible(true);
200
//
201
//
202
// frame = new JInternalFrame("Store List", true);
203
// frame.setPreferredSize(new Dimension(150, 520));
204
// c = frame.getContentPane();
205
// c.add(new JScrollPane(table_list));
206
// desktop.add(frame);
207
// frame.setLocation(new Point(10, 10));
208
// frame.pack();
209
// frame.setVisible(true);
210

211
212
213
214   }
215
216   // ---------- Inner classes ----------
217

218   /**
219    * The view of the content of a store.
220    */

221   private class StoreViewPane extends JPanel {
222
223     /**
224      * The JTable that contains the information.
225      */

226     private JTable table;
227
228
229     public StoreViewPane() {
230       setLayout(new BorderLayout());
231
232       table = new JTable();
233       add(new JScrollPane(table));
234     }
235
236     /**
237      * Sets this store with the given RaDiagnosticTable model.
238      */

239     void setDiagnosticModel(RawDiagnosticTable model) {
240       table.setModel(new DTableModel(model));
241     }
242
243   }
244
245   /**
246    * A TableModel for displaying the contents of a RawDiagnosticTable.
247    */

248   private class DTableModel extends AbstractTableModel {
249
250     private RawDiagnosticTable diag_table;
251
252     public DTableModel(RawDiagnosticTable diag_table) {
253       this.diag_table = diag_table;
254     }
255
256     public int getRowCount() {
257       return diag_table.physicalRecordCount();
258     }
259
260     public int getColumnCount() {
261       return diag_table.getDataTableDef().columnCount() + 2;
262     }
263
264     public String JavaDoc getColumnName(int col) {
265       if (col == 0) {
266         return "Num";
267       }
268       else if (col == 1) {
269         return "State";
270       }
271       else {
272         return diag_table.getDataTableDef().columnAt(col - 2).getName();
273       }
274     }
275
276     public Object JavaDoc getValueAt(int row, int col) {
277       try {
278
279         if (col == 0) {
280           return new Integer JavaDoc(row);
281         }
282         else if (col == 1) {
283           int type = diag_table.recordState(row);
284           if (type == diag_table.UNCOMMITTED) {
285             return "U";
286           }
287           else if (type == diag_table.COMMITTED_ADDED) {
288             return "CA";
289           }
290           else if (type == diag_table.COMMITTED_REMOVED) {
291             return "CR";
292           }
293           else if (type == diag_table.DELETED) {
294             return "D";
295           }
296           else {
297             return "#ERROR#";
298           }
299         }
300         else {
301           return diag_table.getCellContents(col - 2, row).getObject();
302         }
303       }
304       catch (Throwable JavaDoc e) {
305         return "#ERROR:" + e.getMessage() + "#";
306       }
307     }
308   }
309
310   /**
311    * An action for calculating the average row size in the table.
312    */

313   private Action average_row_count_action =
314                         new AbstractAction("Table Statistics") {
315
316     public void actionPerformed(ActionEvent evt) {
317       if (current_table == null || current_table.physicalRecordCount() < 10) {
318         return;
319       }
320       int row_count = current_table.physicalRecordCount();
321       int[] store = new int[row_count];
322       long total_size = 0;
323
324       for (int i = 0; i < row_count; ++i) {
325         int record_size = current_table.recordSize(i);
326         store[i] = record_size;
327         total_size += record_size;
328       }
329
330       int avg = (int) (total_size / row_count);
331       System.out.println("Average row size: " + avg);
332
333       double best_score = 100000000000.0;
334       int best_ss = 45;
335       int best_size = Integer.MAX_VALUE;
336
337       for (int ss = 19; ss < avg + 128; ++ss) {
338         int total_sectors = 0;
339         for (int n = 0; n < store.length; ++n) {
340           int sz = store[n];
341           total_sectors += (sz / ( ss + 1 )) + 1;
342         }
343
344         int file_size = ((ss + 5) * total_sectors);
345         double average_sec = (double) total_sectors / row_count;
346         double score = file_size * (((average_sec - 1.0) / 20.0) + 1.0);
347
348         System.out.println(" (" + score + " : " + file_size +
349                            " : " + ss + " : " + average_sec + ") ");
350         if (average_sec < 2.8 && score < best_score) {
351           best_score = score;
352           best_size = file_size;
353           best_ss = ss;
354         }
355       }
356
357       System.out.println("Best sector size: " + best_ss +
358                          " Best file size: " + best_size );
359
360
361
362     }
363
364   };
365
366 }
367
368
369
Popular Tags