001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test.performance;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.io.*;
027:
028: /**
029: * IO adapter for benchmark.
030: * @exclude
031: */
032: public class RecordingIoAdapter extends VanillaIoAdapter {
033: // NOTE/FIXME: Ugly hack to prevent YapRandomAccessFile timer file handle from
034: // writing asonchronously to our log file. Very fragile, basically YapRandomAccessFile
035: // timer handles - or IoAdapter design ;P - needs to be fixed.
036: private int _runningId;
037:
038: private String _logPath;
039:
040: private RandomAccessFile _writer;
041:
042: private long _pos;
043:
044: private int _runs;
045:
046: public RecordingIoAdapter(IoAdapter adapter, String logPath) {
047: super (adapter);
048: _logPath = logPath;
049: _runningId = 0;
050: }
051:
052: protected RecordingIoAdapter(IoAdapter adapter, String logPath,
053: String file, boolean append, long initialLength,
054: boolean readOnly) throws Db4oIOException {
055: super (adapter, file, append, initialLength, readOnly);
056: try {
057: _writer = new RandomAccessFile(logPath, "rw");
058: } catch (IOException e) {
059: throw new Db4oIOException(e);
060: }
061: _runs = 0;
062: }
063:
064: public void close() throws Db4oIOException {
065: super .close();
066: writeLogChar('q');
067: //System.err.println(_runs);
068: }
069:
070: public IoAdapter open(String path, boolean lockFile,
071: long initialLength, boolean readOnly)
072: throws Db4oIOException {
073: _runningId++;
074: return new RecordingIoAdapter(_delegate, _logPath + "."
075: + _runningId, path, lockFile, initialLength, readOnly);
076: }
077:
078: public int read(byte[] buffer, int length) throws Db4oIOException {
079: writeLog('r', _pos, length);
080: return super .read(buffer, length);
081:
082: }
083:
084: public void seek(long pos) throws Db4oIOException {
085: _pos = pos;
086: super .seek(pos);
087: }
088:
089: public void write(byte[] buffer, int length) throws Db4oIOException {
090: writeLog('w', _pos, length);
091: super .write(buffer, length);
092: }
093:
094: public void sync() throws Db4oIOException {
095: writeLogChar('f');
096: super .sync();
097: }
098:
099: private void writeLog(char type, long pos, int length)
100: throws Db4oIOException {
101: try {
102: _writer.writeChar(type);
103: _writer.writeLong(pos);
104: _writer.writeInt(length);
105: } catch (IOException e) {
106: throw new Db4oIOException(e);
107: }
108: _runs++;
109: }
110:
111: private void writeLogChar(char c) throws Db4oIOException {
112: try {
113: _writer.writeChar(c);
114: } catch (IOException e) {
115: throw new Db4oIOException(e);
116: }
117: _runs++;
118: }
119: }
|