KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > tools > Statistics


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.tools;
22
23 import com.db4o.*;
24 import com.db4o.ext.*;
25 import com.db4o.foundation.*;
26
27 /**
28  * prints statistics about a database file to System.out.
29  * <br><br>Pass the database file path as an argument.
30  * <br><br><b>This class is not part of db4o.jar!</b><br>
31  * It is delivered as sourcecode in the
32  * path ../com/db4o/tools/<br><br>
33  */

34 public class Statistics {
35
36     /**
37      * the main method that runs the statistics.
38      * @param args a String array of length 1, with the name of the database
39      * file as element 0.
40      */

41     public static void main(String JavaDoc[] args) {
42         Db4o.configure().messageLevel(-1);
43         if (args == null || args.length != 1) {
44             System.out.println("Usage: java com.db4o.tools.Statistics <database filename>");
45         } else {
46             new Statistics().run(args[0]);
47         }
48     }
49
50     public void run(String JavaDoc filename) {
51         if (new java.io.File JavaDoc(filename).exists()) {
52             ObjectContainer con = null;
53             try {
54                 con = Db4o.openFile(filename);
55                 printHeader("STATISTICS");
56                 System.out.println("File: " + filename);
57                 printStats(con, filename);
58                 con.close();
59             } catch (Exception JavaDoc e) {
60                 System.out.println("Statistics failed for file: '" + filename + "'");
61                 System.out.println(e.getMessage());
62                 e.printStackTrace();
63             }
64         } else {
65             System.out.println("File not found: '" + filename + "'");
66         }
67     }
68
69     private void printStats(ObjectContainer con, String JavaDoc filename) {
70
71         Tree unavailable = new TreeString(REMOVE);
72         Tree noConstructor = new TreeString(REMOVE);
73
74         // one element too many, substract one in the end
75
StoredClass[] internalClasses = con.ext().storedClasses();
76         for (int i = 0; i < internalClasses.length; i++) {
77             try {
78                 Class JavaDoc clazz = Class.forName(internalClasses[i].getName());
79                 try {
80                     clazz.newInstance();
81                 } catch (Throwable JavaDoc th) {
82                     noConstructor =
83                         noConstructor.add(new TreeString(internalClasses[i].getName()));
84                 }
85             } catch (Throwable JavaDoc t) {
86                 unavailable = unavailable.add(new TreeString(internalClasses[i].getName()));
87             }
88         }
89         unavailable = unavailable.removeLike(new TreeString(REMOVE));
90         noConstructor = noConstructor.removeLike(new TreeString(REMOVE));
91         if (unavailable != null) {
92             printHeader("UNAVAILABLE");
93             unavailable.traverse(new Visitor4() {
94                 public void visit(Object JavaDoc obj) {
95                     System.out.println(((TreeString) obj)._key);
96                 }
97             });
98         }
99         if (noConstructor != null) {
100             printHeader("NO PUBLIC CONSTRUCTOR");
101             noConstructor.traverse(new Visitor4() {
102                 public void visit(Object JavaDoc obj) {
103                     System.out.println(((TreeString) obj)._key);
104                 }
105             });
106         }
107
108         printHeader("CLASSES");
109         System.out.println("Number of objects per class:");
110         
111         final Tree.ByRef ids = new Tree.ByRef(new TreeInt(0));
112
113         if (internalClasses.length > 0) {
114             Tree all = new TreeStringObject(internalClasses[0].getName(), internalClasses[0]);
115             for (int i = 1; i < internalClasses.length; i++) {
116                 all =
117                     all.add(
118                         new TreeStringObject(internalClasses[i].getName(), internalClasses[i]));
119             }
120             all.traverse(new Visitor4() {
121                 public void visit(Object JavaDoc obj) {
122                     TreeStringObject node = (TreeStringObject) obj;
123                     long[] newIDs = ((StoredClass)node._object).getIDs();
124                     for (int j = 0; j < newIDs.length; j++) {
125                         if (ids.value.find(new TreeInt((int) newIDs[j])) == null) {
126                             ids.value = (TreeInt) ids.value.add(new TreeInt((int) newIDs[j]));
127                         }
128                     }
129                     System.out.println(node._key + ": " + newIDs.length);
130                 }
131             });
132
133         }
134
135         printHeader("SUMMARY");
136         System.out.println("File: " + filename);
137         System.out.println("Stored classes: " + internalClasses.length);
138         if (unavailable != null) {
139             System.out.println("Unavailable classes: " + unavailable.size());
140         }
141         if (noConstructor != null) {
142             System.out.println("Classes without public constructors: " + noConstructor.size());
143         }
144         System.out.println("Total number of objects: " + (ids.value.size() - 1));
145     }
146     
147     private void printHeader(String JavaDoc str){
148         int stars = (39 - str.length()) / 2;
149         System.out.println("\n");
150         for (int i = 0; i < stars; i++) {
151             System.out.print("*");
152         }
153         System.out.print(" " + str + " ");
154         for (int i = 0; i < stars; i++) {
155             System.out.print("*");
156         }
157         System.out.println();
158     }
159
160     private static final String JavaDoc REMOVE = "XXxxREMOVExxXX";
161
162 }
163
Popular Tags