KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CattellImpl


1 // $Id: CattellImpl.java,v 1.5 2003/11/23 14:55:26 per_nyfelt Exp $
2

3 import java.io.*;
4 import java.util.Random JavaDoc;
5 import org.ozoneDB.*;
6 import org.ozoneDB.util.OzoneDebugLevel;
7 import org.ozoneDB.tools.*;
8
9
10 /**
11  * CattellImpl is the central class of the Cattell/OO1 benchmark. It holds all
12  * PartImpl objects in an array and provides several methods to perform various
13  * benchmarks on them. Because CattellImpl itself is a database object these
14  * benchmarks methods run inside the server as transactions.
15  *
16  *
17  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
18  * @version $Revision: 1.5 $Date: 2003/11/23 14:55:26 $
19  */

20 public class CattellImpl extends OzoneObject implements Cattell, OO1Benchmark, Externalizable {
21
22     public final static long serialVersionUID = 2L;
23
24     public final static byte perms = Database.Public;
25
26     public transient BenchmarkProgressLog log = new BenchmarkProgressLog();
27
28     private Part[] parts = new Part[0];
29
30     private int size = 0;
31
32
33     public static void main( String JavaDoc[] args ) {
34         OO1BenchmarkApp.main( args );
35     }
36
37
38     public CattellImpl() {
39     }
40
41
42     public void setProgressLog( BenchmarkProgressLog blog ) {
43         log = blog;
44     }
45
46
47     public ExternalDatabase openDatabase( String JavaDoc serverHost ) throws Exception JavaDoc {
48         ExternalDatabase db;
49         if (serverHost.equals( "local" )) {
50             db = new LocalDatabase();
51             String JavaDoc dbDir=System.getProperty("java.io.tmpdir") + "/tmpdb";
52             if (!Install.dbExists(dbDir)) {
53                 log.logMessage( "DB not found, creating in " + dbDir + "..." );
54                 ((LocalDatabase)db).create( dbDir );
55             }
56             ((LocalDatabase)db).open( dbDir, OzoneDebugLevel.INFO_STR );
57             log.logMessage( "local DB databaseDir: " + dbDir );
58         } else {
59             RemoteDatabase rdb = new RemoteDatabase();
60             log.logMessage( "server host: " + serverHost );
61             rdb.open( serverHost, 3333 );
62
63             db = rdb;
64
65         // db = new ClientCacheDatabase (rdb, true);
66
}
67         log.logMessage( "connected..." );
68
69         db.reloadClasses();
70
71         return db;
72     }
73
74
75     public void create( int countParts, String JavaDoc objectName, String JavaDoc serverHost ) throws Exception JavaDoc {
76         ExternalDatabase db = openDatabase( serverHost );
77         //cleanup
78
Cattell cattell = (Cattell)db.objectForName( objectName );
79         if (cattell != null) {
80             cattell.done();
81             db.deleteObject( cattell );
82         }
83
84         long start = System.currentTimeMillis();
85         cattell = (Cattell)db.createObject( CattellImpl.class.getName(), perms, objectName );
86         cattell.addParts( countParts );
87         log.logTime( System.currentTimeMillis() - start );
88         db.close();
89     }
90
91
92     public void lookup( int count, String JavaDoc objectName, String JavaDoc serverHost ) throws Exception JavaDoc {
93         ExternalDatabase db = openDatabase( serverHost );
94         Cattell cattell = (Cattell)db.objectForName( objectName );
95         for (int i = 0; i < 10; i++) {
96             long start = System.currentTimeMillis();
97             cattell.lookup( count );
98             log.logTime( System.currentTimeMillis() - start );
99         }
100         db.close();
101     }
102
103
104     public void traversal( int depth, String JavaDoc objectName, String JavaDoc serverHost ) throws Exception JavaDoc {
105         ExternalDatabase db = openDatabase( serverHost );
106         Cattell cattell = (Cattell)db.objectForName( objectName );
107         for (int i = 0; i < 10; i++) {
108             long start = System.currentTimeMillis();
109             cattell.traversal( depth );
110             log.logTime( System.currentTimeMillis() - start );
111         }
112         db.close();
113     }
114
115
116     public void insertAndDelete( int countParts, String JavaDoc objectName, String JavaDoc serverHost ) throws Exception JavaDoc {
117         ExternalDatabase db = openDatabase( serverHost );
118         Cattell cattell = (Cattell)db.objectForName( objectName );
119         for (int i = 0; i < 10; i++) {
120 // ExternalTransaction tx = db.newTransaction();
121
// tx.begin();
122

123             long start = System.currentTimeMillis();
124             cattell.insert( countParts );
125             cattell.delete( countParts );
126             log.logTime( System.currentTimeMillis() - start );
127
128 // tx.commit();
129
}
130         db.close();
131     }
132
133
134     public void insert( int countParts, String JavaDoc objectName, String JavaDoc serverHost ) throws Exception JavaDoc {
135         ExternalDatabase db = openDatabase( serverHost );
136         Cattell cattell = (Cattell)db.objectForName( objectName );
137         long start = System.currentTimeMillis();
138         cattell.insert( countParts );
139         log.logTime( System.currentTimeMillis() - start );
140         db.close();
141     }
142
143
144     public void delete( int countParts, String JavaDoc objectName, String JavaDoc serverHost ) throws Exception JavaDoc {
145         ExternalDatabase db = openDatabase( serverHost );
146         Cattell cattell = (Cattell)db.objectForName( objectName );
147         long start = System.currentTimeMillis();
148         cattell.delete( countParts );
149         log.logTime( System.currentTimeMillis() - start );
150         db.close();
151     }
152
153
154     public void createObjects( int count, String JavaDoc objectName, String JavaDoc serverHost ) throws Exception JavaDoc {
155         ExternalDatabase db = openDatabase( serverHost );
156         Cattell cattell;
157         // clean up
158
for (int i = 0; i < count; i++) {
159             cattell = (Cattell)db.objectForName( objectName + String.valueOf( i ) );
160             if (cattell != null) {
161                 cattell.done();
162                 db.deleteObject( cattell );
163             }
164         }
165         for (int i = 0; i < count; i++) {
166             log.logMessage( "name: " + objectName + String.valueOf( i ) );
167             long start = System.currentTimeMillis();
168             cattell = (Cattell)db.createObject( CattellImpl.class.getName(),
169                     OzoneInterface.AllRead | OzoneInterface.AllLock, objectName + String.valueOf( i ) );
170             cattell.addParts( 1000 );
171             log.logTime( System.currentTimeMillis() - start );
172         }
173         db.close();
174     }
175
176
177     public void traversalObjects( int count, String JavaDoc objectName, String JavaDoc serverHost ) throws Exception JavaDoc {
178         ExternalDatabase db = openDatabase( serverHost );
179         Random JavaDoc rand = new Random JavaDoc();
180         //changed to only loop "count" number of times instead of infinite
181
for (int loop=0; loop < count; loop++) {
182             int i = (int)(rand.nextDouble() * count);
183             log.logMessage( "name: " + objectName + String.valueOf( i ) );
184             for (int j = 1; j < 5; j++) {
185                 try {
186                     Cattell cattell = (Cattell)db.objectForName( objectName + String.valueOf( i ) );
187                     if (cattell != null) {
188                         long start = System.currentTimeMillis();
189                         cattell.traversal( 8 );
190                         log.logTime( System.currentTimeMillis() - start );
191                     }
192                 } catch (Exception JavaDoc e) {
193                     e.printStackTrace();
194                 }
195             }
196         }
197         db.close();
198     }
199
200
201     public void insertObjects( int count, String JavaDoc objectName, String JavaDoc serverHost ) throws Exception JavaDoc {
202         ExternalDatabase db = openDatabase( serverHost );
203         Random JavaDoc rand = new Random JavaDoc();
204         //changed to only loop "count" number of times instead of infinite
205
for (int loop=0; loop < count; loop++) {
206             int i = (int)(rand.nextDouble() * count);
207             log.logMessage( "name: " + objectName + String.valueOf( i ) );
208             ExternalTransaction tx = db.newTransaction();
209             tx.begin();
210             try {
211                 Cattell cattell = (Cattell)db.objectForName( objectName + String.valueOf( i ) );
212                 if (cattell != null) {
213                     long start = System.currentTimeMillis();
214                     cattell.insert( 100 );
215                     cattell.delete( 100 );
216                     log.logTime( System.currentTimeMillis() - start );
217                 }
218                 tx.commit();
219             }
220             catch (Exception JavaDoc e) {
221                 tx.rollback();
222                 throw e;
223             }
224         }
225         db.close();
226     }
227
228
229     public void addParts( int n ) throws Exception JavaDoc {
230         int oldSize = size;
231         size += n;
232
233         System.out.println( "adding " + n + " to " + oldSize + " old parts..." );
234
235         System.out.println( "copying the array..." );
236         Part[] newArray = new Part[size];
237         System.arraycopy( parts, 0, newArray, 0, oldSize );
238         parts = newArray;
239
240         System.out.println( "creating parts..." );
241         OzoneInterface db = database();
242         for (int i = oldSize; i < size; i++) {
243             if (i % 200 == 0) {
244                 System.out.println( "" + i );
245             }
246             parts[i] = (Part)db.createObject( PartImpl.class.getName(), perms, null );
247         }
248         connectParts( n );
249     }
250
251
252     /**
253      * stellt die assoziationen zwischen den letzten num parts her
254      */

255     public void connectParts( int n ) throws Exception JavaDoc {
256         System.out.println( "connecting the last " + n + " of " + size + " parts..." );
257
258         Random JavaDoc rand = new Random JavaDoc();
259         for (int i = size - n; i < size; i++) {
260             if (i % 200 == 0) {
261                 System.out.println( "" + i );
262             }
263             for (int j = 0; j < 3; j++) {
264                 int cpart;
265                 if (rand.nextDouble() > 0.1) {
266                     cpart = i + (int) (2 * rand.nextDouble() - 1) * size / 200;
267                     if (cpart < size / 200F) {
268                         cpart += (int) size / 200F;
269                     }
270                     if (cpart > (size - size / 200F)) {
271                         cpart -= (int) size / 200F;
272                     }
273                 } else {
274                     cpart = (int) (rand.nextDouble() * size);
275                 }
276                 // System.out.println ("connecting " + i + ":" + cpart);
277
parts[i].connect( j, parts[cpart] );
278             }
279         }
280     }
281
282
283     public void lookup( int n ) throws Exception JavaDoc {
284         System.out.println( "" + n + " lookups on " + size + " objects..." );
285         Random JavaDoc rand = new Random JavaDoc();
286         int start = (int)(rand.nextDouble() * (size - n));
287         System.out.println( "start: " + start );
288         for (int i = 0; i < n; i++) {
289             // parts[start+i].set (new DxString("part"), 1, 1);
290
parts[start + i].get();
291         }
292         System.out.println( "lookup: ready" );
293     }
294
295
296     public void traversal( int n ) throws Exception JavaDoc {
297         System.out.println( "traversal (" + n + " hops)..." );
298         Random JavaDoc rand = new Random JavaDoc();
299         int j = (int)(rand.nextDouble() * size);
300         parts[j].traversal( 2, 2, n );
301     }
302
303
304     public void insert( int n ) throws Exception JavaDoc {
305         int oldSize = size;
306         System.out.println( "inserting " + n + " parts..." );
307         addParts( n );
308         System.out.println( "invoke..." );
309         for (int i = oldSize; i < size; i++) {
310             parts[i].set( "part", 1, 1 );
311         }
312     }
313
314
315     public void delete( int n ) throws Exception JavaDoc {
316         OzoneInterface db = database();
317         n = n != -1 ? n : size;
318
319         System.out.println( "delete: waiting..." );
320         System.out.println( "delete: go on..." );
321
322         System.out.println( "deconnecting " + n + " of " + size + " parts..." );
323         for (int i = size - n; i < size; i++) {
324             parts[i].deconnect();
325         }
326
327         System.out.println( "deleting " + n + " of " + size + " parts..." );
328         for (int i = size - n; i < size; i++) {
329             db.deleteObject( parts[i] );
330         }
331
332         size -= n;
333         Part[] newArray = new Part[size];
334         System.arraycopy( parts, 0, newArray, 0, size );
335         parts = newArray;
336         System.out.println( "delete: ready" );
337     }
338
339
340     public void writeExternal( ObjectOutput out ) throws IOException {
341         // System.out.println ("CattelImpl.writeExternal " + size + " ...");
342
out.writeInt( size );
343         for (int i = 0; i < size; i++) {
344             out.writeObject( parts[i] );
345         }
346     }
347
348
349     public synchronized void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException JavaDoc {
350         size = in.readInt();
351         // System.out.println ("Cattell read " + size + " ...");
352
parts = new Part[size];
353         for (int i = 0; i < size; i++) {
354             parts[i] = (Part)in.readObject();
355         }
356     }
357
358
359     public void done() throws Exception JavaDoc {
360         delete( -1 );
361     }
362
363 }
364
Popular Tags