1 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 44 45 public class DBConglomerateDiagTool { 46 47 private static void diagnose(String path, String 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 conglomerate.open(name); 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 95 private static void printSyntax() { 96 System.out.println("DBConglomerateDiagTool -path [data directory] " + 97 "[-name [database name]]"); 98 } 99 100 103 public static void main(String [] args) { 104 CommandLine cl = new CommandLine(args); 105 106 String path = cl.switchArgument("-path"); 107 String 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 diagnose(path, name); 117 118 } 119 120 122 } 123 124 125 128 class ConglomerateViewPane extends JRootPane { 129 130 133 private TableDataConglomerate conglomerate; 134 135 138 private RawDiagnosticTable current_table; 139 140 145 148 private StoreViewPane view_pane; 149 150 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 view_pane = new StoreViewPane(); 171 172 final String [] 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 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 211 212 213 214 } 215 216 218 221 private class StoreViewPane extends JPanel { 222 223 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 239 void setDiagnosticModel(RawDiagnosticTable model) { 240 table.setModel(new DTableModel(model)); 241 } 242 243 } 244 245 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 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 getValueAt(int row, int col) { 277 try { 278 279 if (col == 0) { 280 return new Integer (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 e) { 305 return "#ERROR:" + e.getMessage() + "#"; 306 } 307 } 308 } 309 310 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 |