KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > util > DbSpace


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbSpace.java,v 1.23 2006/12/05 14:38:41 cwl Exp $
7  */

8
9 package com.sleepycat.je.util;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.PrintStream JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.SortedMap JavaDoc;
18
19 import com.sleepycat.je.DatabaseException;
20 import com.sleepycat.je.DbInternal;
21 import com.sleepycat.je.Environment;
22 import com.sleepycat.je.EnvironmentConfig;
23 import com.sleepycat.je.JEVersion;
24 import com.sleepycat.je.cleaner.FileSummary;
25 import com.sleepycat.je.cleaner.UtilizationProfile;
26 import com.sleepycat.je.dbi.EnvironmentImpl;
27 import com.sleepycat.je.log.UtilizationFileReader;
28 import com.sleepycat.je.utilint.CmdUtil;
29
30 public class DbSpace {
31
32     private static final String JavaDoc USAGE =
33     "usage: " + CmdUtil.getJavaCommand(DbSpace.class) + "\n" +
34         " -h <dir> # environment home directory\n" +
35         " [-q] # quiet, print grand totals only\n" +
36         " [-u] # sort by utilization\n" +
37         " [-d] # dump file summary details\n" +
38         " [-r] # recalculate utilization (reads entire log)\n" +
39         " [-V] # print JE version number";
40
41     public static void main(String JavaDoc argv[])
42     throws DatabaseException {
43
44     DbSpace space = new DbSpace();
45     space.parseArgs(argv);
46
47         EnvironmentConfig envConfig = new EnvironmentConfig();
48         envConfig.setReadOnly(true);
49         Environment env = new Environment(space.envHome, envConfig);
50         space.envImpl = DbInternal.envGetEnvironmentImpl(env);
51
52     try {
53         space.print(System.out);
54         System.exit(0);
55     } catch (Throwable JavaDoc e) {
56             e.printStackTrace(System.err);
57             System.exit(1);
58     } finally {
59             try {
60                 env.close();
61             } catch (Throwable JavaDoc e) {
62                 e.printStackTrace(System.err);
63                 System.exit(1);
64             }
65     }
66     }
67
68     private File JavaDoc envHome = null;
69     private EnvironmentImpl envImpl;
70     private boolean quiet = false;
71     private boolean sorted = false;
72     private boolean details = false;
73     private boolean recalc = false;
74
75     private DbSpace() {
76     }
77
78     public DbSpace(Environment env,
79            boolean quiet,
80                    boolean details,
81                    boolean sorted) {
82         this(DbInternal.envGetEnvironmentImpl(env), quiet, details, sorted);
83     }
84
85     public DbSpace(EnvironmentImpl envImpl,
86            boolean quiet,
87                    boolean details,
88                    boolean sorted) {
89     this.envImpl = envImpl;
90     this.quiet = quiet;
91         this.details = details;
92     this.sorted = sorted;
93     }
94
95     private void printUsage(String JavaDoc msg) {
96         if (msg != null) {
97             System.err.println(msg);
98         }
99     System.err.println(USAGE);
100     System.exit(-1);
101     }
102
103     private void parseArgs(String JavaDoc argv[]) {
104
105     int argc = 0;
106     int nArgs = argv.length;
107         
108         if (nArgs == 0) {
109         printUsage(null);
110             System.exit(0);
111         }
112
113     while (argc < nArgs) {
114         String JavaDoc thisArg = argv[argc++];
115         if (thisArg.equals("-q")) {
116         quiet = true;
117             } else if (thisArg.equals("-u")) {
118         sorted = true;
119             } else if (thisArg.equals("-d")) {
120         details = true;
121             } else if (thisArg.equals("-r")) {
122         recalc = true;
123         } else if (thisArg.equals("-V")) {
124         System.out.println(JEVersion.CURRENT_VERSION);
125         System.exit(0);
126         } else if (thisArg.equals("-h")) {
127         if (argc < nArgs) {
128             envHome = new File JavaDoc(argv[argc++]);
129         } else {
130             printUsage("-h requires an argument");
131         }
132         }
133     }
134
135     if (envHome == null) {
136         printUsage("-h is a required argument");
137     }
138     }
139
140     public void print(PrintStream JavaDoc out)
141     throws IOException JavaDoc, DatabaseException {
142
143         UtilizationProfile profile = envImpl.getUtilizationProfile();
144         SortedMap JavaDoc map = profile.getFileSummaryMap(false);
145         Map JavaDoc recalcMap = UtilizationFileReader.calcFileSummaryMap(envImpl);
146         int fileIndex = 0;
147
148         Summary totals = new Summary();
149         Summary[] summaries = null;
150         if (!quiet) {
151             summaries = new Summary[map.size()];
152         }
153
154     Iterator JavaDoc iter = map.entrySet().iterator();
155     while (iter.hasNext()) {
156         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
157         Long JavaDoc fileNum = (Long JavaDoc) entry.getKey();
158         FileSummary fs = (FileSummary) entry.getValue();
159             FileSummary recalcFs = null;
160             if (recalcMap != null) {
161                  recalcFs = (FileSummary) recalcMap.get(fileNum);
162             }
163             Summary summary = new Summary(fileNum, fs, recalcFs);
164             if (summaries != null) {
165                 summaries[fileIndex] = summary;
166             }
167             if (details) {
168                 out.println
169                     ("File 0x" + Long.toHexString(fileNum.longValue()) +
170                      ": " + fs);
171                 if (recalcMap != null) {
172                     out.println
173                         ("Recalculated File 0x" +
174                          Long.toHexString(fileNum.longValue()) +
175                          ": " + recalcFs);
176                 }
177             }
178             totals.add(summary);
179             fileIndex += 1;
180         }
181
182         if (details) {
183             out.println();
184         }
185         out.println(recalc ? Summary.RECALC_HEADER : Summary.HEADER);
186
187         if (summaries != null) {
188             if (sorted) {
189                 Arrays.sort(summaries);
190             }
191             for (int i = 0; i < summaries.length; i += 1) {
192                 summaries[i].print(out, recalc);
193             }
194         }
195
196         totals.print(out, recalc);
197     }
198
199     private static class Summary implements Comparable JavaDoc {
200
201         static final String JavaDoc HEADER = " File Size (KB) % Used\n" +
202                                      "-------- --------- ------";
203                                    // 12345678 123456789 123
204
// 12 12345
205
// TOTALS:
206

207         static final String JavaDoc RECALC_HEADER =
208                    " File Size (KB) % Used % Used (recalculated)\n" +
209                    "-------- --------- ------ ------";
210                  // 12345678 123456789 123 123
211
// 12 12345 12345
212
// TOTALS:
213

214         Long JavaDoc fileNum;
215         long totalSize;
216         long obsoleteSize;
217         long recalcObsoleteSize;
218
219         Summary() {}
220
221         Summary(Long JavaDoc fileNum, FileSummary summary, FileSummary recalcSummary)
222             throws DatabaseException {
223
224             this.fileNum = fileNum;
225             totalSize = summary.totalSize;
226             obsoleteSize = summary.getObsoleteSize();
227             if (recalcSummary != null) {
228                 recalcObsoleteSize = recalcSummary.getObsoleteSize();
229             }
230         }
231
232         public int compareTo(Object JavaDoc other) {
233             Summary o = (Summary) other;
234             return utilization() - o.utilization();
235         }
236
237     public boolean equals(Object JavaDoc o) {
238         if (o == null) {
239         return false;
240         }
241
242         if (o instanceof Summary) {
243         return utilization() == ((Summary) o).utilization();
244         } else {
245         return false;
246         }
247     }
248
249         void add(Summary o) {
250             totalSize += o.totalSize;
251             obsoleteSize += o.obsoleteSize;
252             recalcObsoleteSize += o.recalcObsoleteSize;
253         }
254
255         void print(PrintStream JavaDoc out, boolean recalc) {
256             if (fileNum != null) {
257                 pad(out, Long.toHexString(fileNum.longValue()), 8, '0');
258             } else {
259                 out.print(" TOTALS ");
260             }
261             int kb = (int) (totalSize / 1024);
262             out.print(" ");
263             pad(out, Integer.toString(kb), 9, ' ');
264             out.print(" ");
265             pad(out, Integer.toString(utilization()), 3, ' ');
266             if (recalc) {
267                 out.print(" ");
268                 pad(out, Integer.toString(recalcUtilization()), 3, ' ');
269             }
270             out.println();
271         }
272
273         int utilization() {
274             return UtilizationProfile.utilization(obsoleteSize, totalSize);
275         }
276
277         int recalcUtilization() {
278             return UtilizationProfile.utilization
279                     (recalcObsoleteSize, totalSize);
280         }
281
282         private void pad(PrintStream JavaDoc out, String JavaDoc val, int digits,
283                            char padChar) {
284             int padSize = digits - val.length();
285             for (int i = 0; i < padSize; i += 1) {
286                 out.print(padChar);
287             }
288             out.print(val);
289         }
290     }
291 }
292
Popular Tags