KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > sleepycat > SleepycatDBUsage


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.persistence.sleepycat;
5
6 import com.sleepycat.je.Cursor;
7 import com.sleepycat.je.CursorConfig;
8 import com.sleepycat.je.Database;
9 import com.sleepycat.je.DatabaseConfig;
10 import com.sleepycat.je.DatabaseEntry;
11 import com.sleepycat.je.DatabaseException;
12 import com.sleepycat.je.Environment;
13 import com.sleepycat.je.EnvironmentConfig;
14 import com.sleepycat.je.LockMode;
15 import com.sleepycat.je.OperationStatus;
16
17 import java.io.File JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 public class SleepycatDBUsage {
22
23   private static final int LEFT = 1;
24   private static final int RIGHT = 2;
25   private static final int CENTER = 3;
26
27   private EnvironmentConfig enc;
28   private Environment env;
29   private long grandTotal;
30   private long totalCount;
31   private DatabaseConfig dbc;
32   private boolean header = true;
33   private long keyTotal;
34   private long valuesTotal;
35
36   public SleepycatDBUsage(File JavaDoc dir) throws Exception JavaDoc {
37     enc = new EnvironmentConfig();
38     enc.setReadOnly(true);
39     env = new Environment(dir, enc);
40     dbc = new DatabaseConfig();
41     dbc.setReadOnly(true);
42   }
43
44   public void report() throws DatabaseException {
45     List JavaDoc dbs = env.getDatabaseNames();
46     log("Databases in the enviroment : " + dbs);
47
48     log("\nReport on individual databases :\n================================\n");
49     for (Iterator JavaDoc i = dbs.iterator(); i.hasNext();) {
50       String JavaDoc dbNAme = (String JavaDoc) i.next();
51       Database db = env.openDatabase(null, dbNAme, dbc);
52       DBStats stats = calculate(db);
53       db.close();
54       report(stats);
55     }
56     reportGrandTotals();
57   }
58
59   private void reportGrandTotals() {
60     log("\n");
61     log(" TOTAL : ", String.valueOf(totalCount), "", "", "", String.valueOf(keyTotal), "", "", "", String
62         .valueOf(valuesTotal), String.valueOf(grandTotal));
63   }
64
65   private DBStats calculate(Database db) throws DatabaseException {
66     CursorConfig config = new CursorConfig();
67     Cursor c = db.openCursor(null, config);
68     DBStats stats = new DBStats(db.getDatabaseName());
69     DatabaseEntry key = new DatabaseEntry();
70     DatabaseEntry value = new DatabaseEntry();
71     while (OperationStatus.SUCCESS.equals(c.getNext(key, value, LockMode.DEFAULT))) {
72       stats.record(key.getData().length, value.getData().length);
73     }
74     c.close();
75     return stats;
76   }
77
78   private void report(DBStats stats) {
79     if (header) {
80       log("DBName", "# Records", "Keys(Bytes)", "Values(Bytes)", "Total(Bytes)");
81       log("", "", "min", "max", "avg", "total", "min", "max", "avg", "total", "");
82       log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
83       header = false;
84     }
85     log(stats.getDatabaseName(), stats.getRecordCount(), stats.getKeyMin(), stats.getKeyMax(), stats.getKeyAvg(), stats
86         .getTotalKeySize(), stats.getValueMin(), stats.getValueMax(), stats.getValueAvg(), stats.getTotalValueSize(),
87         stats.getTotalSize());
88     this.keyTotal += stats.getTotalKeySize();
89     this.valuesTotal += stats.getTotalValueSize();
90     this.grandTotal += stats.getTotalSize();
91     this.totalCount += stats.getRecordCount();
92   }
93
94   private void log(String JavaDoc databaseName, long recordCount, long keyMin, long keyMax, long keyAvg, long totalKeySize,
95                    long valueMin, long valueMax, long valueAvg, long totalValueSize, long totalSize) {
96     log(databaseName, String.valueOf(recordCount), String.valueOf(keyMin), String.valueOf(keyMax), String
97         .valueOf(keyAvg), String.valueOf(totalKeySize), String.valueOf(valueMin), String.valueOf(valueMax), String
98         .valueOf(valueAvg), String.valueOf(totalValueSize), String.valueOf(totalSize));
99   }
100
101   private static void log(String JavaDoc nameHeader, String JavaDoc countHeader, String JavaDoc keyHeader, String JavaDoc valueHeader, String JavaDoc sizeHeader) {
102     log(format(nameHeader, 20, LEFT) + format(countHeader, 10, RIGHT) + format(keyHeader, 30, CENTER)
103         + format(valueHeader, 30, CENTER) + format(sizeHeader, 15, RIGHT));
104   }
105
106   private void log(String JavaDoc databaseName, String JavaDoc count, String JavaDoc kmin, String JavaDoc kmax, String JavaDoc kavg, String JavaDoc kTot, String JavaDoc vmin,
107                    String JavaDoc vmax, String JavaDoc vavg, String JavaDoc vTot, String JavaDoc totalSize) {
108     log(format(databaseName, 20, LEFT) + format(count, 10, RIGHT) + format(kmin, 5, RIGHT) + format(kmax, 10, RIGHT)
109         + format(kavg, 5, RIGHT) + format(kTot, 10, RIGHT) + format(vmin, 5, RIGHT) + format(vmax, 10, RIGHT)
110         + format(vavg, 5, RIGHT) + format(vTot, 10, RIGHT) + format(totalSize, 15, RIGHT));
111   }
112
113   private static String JavaDoc format(String JavaDoc s, int size, int justification) {
114     if (s == null || s.length() >= size) { return s; }
115     int diff = size - s.length();
116     if (justification == LEFT) {
117       return s + createSpaces(diff);
118     } else if (justification == RIGHT) {
119       return createSpaces(diff) + s;
120     } else {
121       return createSpaces(diff / 2) + s + createSpaces(diff - (diff / 2));
122     }
123   }
124
125   private static String JavaDoc createSpaces(int i) {
126     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
127     while (i-- > 0) {
128       sb.append(' ');
129     }
130     return sb.toString();
131   }
132
133   public static void main(String JavaDoc[] args) {
134     if (args == null || args.length < 1) {
135       usage();
136       System.exit(1);
137     }
138
139     try {
140       File JavaDoc dir = new File JavaDoc(args[0]);
141       validateDir(dir);
142       SleepycatDBUsage reporter = new SleepycatDBUsage(dir);
143       reporter.report();
144     } catch (Exception JavaDoc e) {
145       e.printStackTrace();
146       System.exit(2);
147     }
148   }
149
150   private static void validateDir(File JavaDoc dir) {
151     if (!dir.exists() || !dir.isDirectory()) { throw new RuntimeException JavaDoc("Not a valid directory : " + dir); }
152   }
153
154   private static void usage() {
155     log("Usage: SleepycatDBUsage <environment home directory>");
156   }
157
158   private static void log(String JavaDoc message) {
159     System.out.println(message);
160   }
161
162   private static final class DBStats {
163
164     private long count;
165     private long keySize;
166     private long valueSize;
167     private long minKey;
168     private long maxKey;
169     private long minValue;
170     private long maxValue;
171     private final String JavaDoc databaseName;
172
173     public DBStats(String JavaDoc databaseName) {
174       this.databaseName = databaseName;
175     }
176
177     public long getValueAvg() {
178       return (count == 0 ? 0 : valueSize / count);
179     }
180
181     public long getValueMax() {
182       return maxValue;
183     }
184
185     public long getValueMin() {
186       return minValue;
187     }
188
189     public long getKeyAvg() {
190       return (count == 0 ? 0 : keySize / count);
191     }
192
193     public long getKeyMax() {
194       return maxKey;
195     }
196
197     public long getKeyMin() {
198       return minKey;
199     }
200
201     public long getTotalValueSize() {
202       return valueSize;
203     }
204
205     public long getTotalKeySize() {
206       return keySize;
207     }
208
209     public long getTotalSize() {
210       return keySize + valueSize;
211     }
212
213     public String JavaDoc getValueStats() {
214       return valueSize + "(" + minValue + "/" + maxValue + "/" + getValueAvg() + ")";
215     }
216
217     public String JavaDoc getKeyStats() {
218       return keySize + "(" + minKey + "/" + maxKey + "/" + getKeyAvg() + ")";
219     }
220
221     public long getRecordCount() {
222       return count;
223     }
224
225     public String JavaDoc getDatabaseName() {
226       return databaseName;
227     }
228
229     public void record(int kSize, int vSize) {
230       count++;
231       keySize += kSize;
232       valueSize += vSize;
233       if (minKey == 0 || minKey > kSize) {
234         minKey = kSize;
235       }
236       if (maxKey < kSize) {
237         maxKey = kSize;
238       }
239       if (minValue == 0 || minValue > vSize) {
240         minValue = vSize;
241       }
242       if (maxValue < vSize) {
243         maxValue = vSize;
244       }
245     }
246
247   }
248
249 }
250
Popular Tags