1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/*
* File........: c:/arc/prog/java/misc/dbtest/DirDB.java
* Package.....: Default
* Created.....: 98/10/02, Guido Krueger
* RCS.........: $Revision$
* $Date$ $Author$
*
* Copyright (c) 1998 Guido Krueger. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for NON-COMMERCIAL purposes
* and without fee is hereby granted provided that this
* copyright notice appears in all copies.
*
* THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
* SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY
* DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
import java.util.*;
import java.io.*;
import java.sql.*;
import java.text.*;
import gk.util.*;
public class DirDB
{
//---Constants-----------------------------------------------
static int INSTANT185 = 1;
static int ACCESS95 = 2;
static int HSQLDB = 3;
//---Pseudo constants----------------------------------------
static String FILESEP = System.getProperty("file.separator");
//---Static Variables----------------------------------------
static int db = INSTANT185;
static Connection con;
static Statement stmt;
static Statement stmt1;
static DatabaseMetaData dmd;
static int nextdid = 1;
static int nextfid = 1;
/**
* �ffnet die Datenbank.
*/
public static void open()
throws Exception
{
//Treiber laden und Connection erzeugen
if (db == INSTANT185) {
Class.forName("jdbc.idbDriver");
con = DriverManager.getConnection(
"jdbc:idb=dirdb.prp",
new Properties()
);
} else if (db == HSQLDB) {
Class.forName("org.hsqldb.jdbcDriver");
con = DriverManager.getConnection(
"jdbc:hsqldb:hsqldbtest",
"SA",
""
);
} else {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:DirDB");
}
//Metadaten ausgeben
dmd = con.getMetaData();
System.out.println("");
System.out.println("Connection URL: " + dmd.getURL());
System.out.println("Driver Name: " + dmd.getDriverName());
System.out.println("Driver Version: " + dmd.getDriverVersion());
System.out.println("");
//Statementobjekte erzeugen
stmt = con.createStatement();
stmt1 = con.createStatement();
}
/**
* Legt die Tabellen an.
*/
public static void createTables()
throws SQLException
{
//Anlegen der Tabelle dir
try {
stmt.executeUpdate("DROP TABLE dir");
} catch (SQLException e) {
//Nichts zu tun
}
stmt.executeUpdate("CREATE TABLE dir (" +
"did INT," +
"dname CHAR(100)," +
"fatherdid INT," +
"entries INT)"
);
stmt.executeUpdate("CREATE INDEX idir1 ON dir ( did )");
stmt.executeUpdate("CREATE INDEX idir2 ON dir ( fatherdid )");
//Anlegen der Tabelle file
try {
stmt.executeUpdate("DROP TABLE file");
} catch (SQLException e) {
//Nichts zu tun
}
stmt.executeUpdate("CREATE TABLE file (" +
"fid INT ," +
"did INT," +
"fname CHAR(100)," +
"fsize INT," +
"fdate DATE," +
"ftime CHAR(5))"
);
stmt.executeUpdate("CREATE INDEX ifile1 ON file ( fid )");
}
/**
* Durchl�uft den Verzeichnisbaum rekursiv und schreibt
* Verzeichnis- und Dateinamen in die Datenbank.
*/
public static void populate(String dir)
throws Exception
{
addDirectory(0, "", dir);
}
/**
* F�gt das angegebene Verzeichnis und alle
* Unterverzeichnisse mit allen darin enthaltenen
* Dateien zur Datenbank hinzu.
*/
public static void addDirectory(
int fatherdid, String parent, String name
)
throws Exception
{
String dirname = "";
if (parent.length() > 0) {
dirname = parent;
if (!parent.endsWith(FILESEP)) {
dirname += FILESEP;
}
}
dirname += name;
System.out.println("processing " + dirname);
File dir = new File(dirname);
if (!dir.isDirectory()) {
throw new Exception("not a directory: " + dirname);
}
//Verzeichnis anlegen
int did = nextdid++;
stmt.executeUpdate(
"INSERT INTO dir VALUES (" +
did + "," +
"\'" + name + "\'," +
fatherdid + "," +
"0)"
);
//Verzeichniseintr�ge lesen
File entries[] = dir.listFiles();
//Verzeichnis durchlaufen
for (int i = 0; i < entries.length; ++i) {
if (entries[i].isDirectory()) {
addDirectory(did, dirname, entries[i].getName());
} else {
java.util.Date d = new java.util.Date(
entries[i].lastModified()
);
SimpleDateFormat sdf;
//Datum
sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(d);
//Zeit
sdf = new SimpleDateFormat("HH:mm");
String time = sdf.format(d);
//Satz anh�ngen
stmt.executeUpdate(
"INSERT INTO file VALUES (" +
(nextfid++) + "," +
did + "," +
"\'" + entries[i].getName() + "\'," +
entries[i].length() + "," +
"{d \'" + date + "\'}," +
"\'" + time + "\')"
);
System.out.println(" " + entries[i].getName());
}
}
//Anzahl der Eintr�ge aktualisieren
stmt.executeUpdate(
"UPDATE dir SET entries = " + entries.length +
" WHERE did = " + did
);
}
/**
* Gibt die Anzahl der Dateien und Verzeichnisse aus.
*/
public static void countRecords()
throws SQLException
{
ResultSet rs = stmt.executeQuery(
"SELECT count(*) FROM dir"
);
if (!rs.next()) {
throw new SQLException("SELECT COUNT(*): no result");
}
System.out.println("Directories: " + rs.getInt(1));
rs = stmt.executeQuery("SELECT count(*) FROM file");
if (!rs.next()) {
throw new SQLException("SELECT COUNT(*): no result");
}
System.out.println("Files: " + rs.getInt(1));
rs.close();
}
/**
* Liefert den Pfadnamen zu dem Verzeichnis mit dem
* angegebenen Schl�ssel.
*/
public static String getDirPath(int did)
throws SQLException
{
String ret = "";
while (true) {
ResultSet rs = stmt1.executeQuery(
"SELECT * FROM dir WHERE did = " + did
);
if (!rs.next()) {
throw new SQLException(
"no dir record found with did = " + did
);
}
ret = rs.getString("dname").trim() +
(ret.length() > 0 ? FILESEP + ret : "");
if ((did = rs.getInt("fatherdid")) == 0) {
break;
}
}
return ret;
}
/**
* Gibt eine Liste aller Files auf dem Bildschirm aus,
* die zu dem angegebenen Dateinamen passen. Darin d�rfen
* die �blichen SQL-Wildcards % und _ enthalten sein.
*/
public static void findFile(String name)
throws SQLException
{
String query = "SELECT * FROM file " +
"WHERE fname LIKE \'" + name + "\'";
if (db == INSTANT185) {
query += " IGNORE CASE";
}
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String path = getDirPath(rs.getInt("did"));
System.out.println(
path + FILESEP +
rs.getString("fname").trim()
);
}
rs.close();
}
/**
* Gibt eine Liste aller Verzeichnisse auf dem Bildschirm
* aus, die zu dem angegebenen Verzeichnisnamen passen.
* Darin d�rfen die �blichen SQL-Wildcards % und _
* enthalten sein.
*/
public static void findDir(String name)
throws SQLException
{
String query = "SELECT * FROM dir " +
"WHERE dname LIKE \'" + name + "\'";
if (db == INSTANT185) {
query += " IGNORE CASE";
}
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
System.out.println(
getDirPath(rs.getInt("did")) +
" (" + rs.getInt("entries") + " entries)"
);
}
rs.close();
}
/**
* Gibt die howmany gr��ten Dateien aus.
*/
public static void biggestFiles(int howmany)
throws SQLException
{
ResultSet rs = stmt.executeQuery(
"SELECT * FROM file ORDER BY fsize DESC"
);
for (int i = 0; i < howmany; ++i) {
if (rs.next()) {
System.out.print(
getDirPath(rs.getInt("did")) +
FILESEP + rs.getString("fname").trim()
);
System.out.println(
Str.getFormatted("%10d", rs.getInt("fsize"))
);
}
}
rs.close();
}
/**
* Summiert einerseits die tats�chliche Gr��e aller
* Dateien und andererseits die Gr��e, die sie durch
* das Clustering mit der angegebenen Clustergr��e
* belegen. Zus�tzlich wird der durch das Clustering
* "verschwendete" Speicherplatz ausgegeben.
*/
public static void clustering(int clustersize)
throws SQLException
{
int truesize = 0;
int clusteredsize = 0;
double wasted;
ResultSet rs = stmt.executeQuery(
"SELECT * FROM file"
);
while (rs.next()) {
int fsize = rs.getInt("fsize");
truesize += fsize;
if (fsize % clustersize == 0) {
clusteredsize += fsize;
} else {
clusteredsize += ((fsize / clustersize) + 1) * clustersize;
}
}
System.out.println("true size = " + truesize);
System.out.println("clustered size = " + clusteredsize);
wasted = 100 * (1 - ((double)truesize / clusteredsize));
System.out.println("wasted space = " + wasted + " %");
}
/**
* Schlie�t die Datenbank.
*/
public static void close()
throws SQLException
{
stmt.close();
stmt1.close();
con.close();
}
//---main-------------------------------------------------
public static void main(String args[])
{
if (args.length < 1) {
System.out.println("usage: java DirDB [A|I|H] <command> [<options>]");
System.out.println("");
System.out.println("command options");
System.out.println("-----------------------------------------");
System.out.println("POPULATE <directory>");
System.out.println("COUNT");
System.out.println("FINDFILE <name>");
System.out.println("FINDDIR <name>");
System.out.println("BIGGESTFILES <howmany>");
System.out.println("CLUSTERING <clustersize>");
System.exit(1);
}
if (args[0].equalsIgnoreCase("A")) {
db = ACCESS95;
} else if (args[0].equalsIgnoreCase("H")) {
db = HSQLDB;
}
try {
if (args[1].equalsIgnoreCase("populate")) {
open();
createTables();
populate(args[2]);
close();
} else if (args[1].equalsIgnoreCase("count")) {
open();
countRecords();
close();
} else if (args[1].equalsIgnoreCase("findfile")) {
open();
findFile(args[2]);
close();
} else if (args[1].equalsIgnoreCase("finddir")) {
open();
findDir(args[2]);
close();
} else if (args[1].equalsIgnoreCase("biggestfiles")) {
open();
biggestFiles(Integer.parseInt(args[2]));
close();
} else if (args[1].equalsIgnoreCase("clustering")) {
open();
clustering(Integer.parseInt(args[2]));
close();
}
} catch (SQLException e) {
while (e != null) {
System.err.println(e.toString());
System.err.println("SQL-State: " + e.getSQLState());
System.err.println("ErrorCode: " + e.getErrorCode());
e = e.getNextException();
}
System.exit(1);
} catch (Exception e) {
System.err.println(e.toString());
System.exit(1);
}
}
}
|