KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > performance > RecordingIoAdapter


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.test.performance;
22
23 import java.io.*;
24
25 import com.db4o.io.*;
26
27 /**
28  * IO adapter for benchmark.
29  * @exclude
30  */

31 public class RecordingIoAdapter extends VanillaIoAdapter {
32     // NOTE/FIXME: Ugly hack to prevent YapRandomAccessFile timer file handle from
33
// writing asonchronously to our log file. Very fragile, basically YapRandomAccessFile
34
// timer handles - or IoAdapter design ;P - needs to be fixed.
35
private int _runningId;
36     
37     private String JavaDoc _logPath;
38
39     private RandomAccessFile _writer;
40
41     private long _pos;
42     
43     private int _runs;
44
45     public RecordingIoAdapter(IoAdapter adapter, String JavaDoc logPath) {
46         super(adapter);
47         _logPath = logPath;
48         _runningId=0;
49     }
50
51     protected RecordingIoAdapter(IoAdapter adapter, String JavaDoc logPath,
52             String JavaDoc file, boolean append, long initialLength) throws IOException {
53         super(adapter, file, append, initialLength);
54         _writer = new RandomAccessFile(logPath, "rw");
55         _runs=0;
56     }
57
58     public void close() throws IOException {
59         super.close();
60         writeLogChar('q');
61         //System.err.println(_runs);
62
}
63
64     public IoAdapter open(String JavaDoc path, boolean lockFile, long initialLength)
65             throws IOException {
66         _runningId++;
67         return new RecordingIoAdapter(_delegate, _logPath+"."+_runningId, path, lockFile,
68                 initialLength);
69     }
70
71     public int read(byte[] buffer, int length) throws IOException {
72         writeLog('r',_pos,length);
73         return super.read(buffer,length);
74
75     }
76
77     public void seek(long pos) throws IOException {
78         _pos = pos;
79         super.seek(pos);
80     }
81
82     public void write(byte[] buffer, int length) throws IOException {
83         writeLog('w',_pos,length);
84         super.write(buffer,length);
85     }
86     
87     public void sync() throws IOException {
88         writeLogChar('f');
89         super.sync();
90     }
91     
92     private void writeLog(char type,long pos,int length) throws IOException {
93         _writer.writeChar(type);
94         _writer.writeLong(pos);
95         _writer.writeInt(length);
96         _runs++;
97     }
98     
99     private void writeLogChar(char c) throws IOException {
100         _writer.writeChar(c);
101         _runs++;
102     }
103 }
Popular Tags