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.db4ounit.common.acid;
022:
023: import java.io.*;
024:
025: import com.db4o.foundation.*;
026: import com.db4o.foundation.io.*;
027:
028: public class CrashSimulatingBatch {
029:
030: Collection4 writes = new Collection4();
031: Collection4 currentWrite = new Collection4();
032:
033: public void add(byte[] bytes, long offset, int length) {
034: currentWrite
035: .add(new CrashSimulatingWrite(bytes, offset, length));
036: }
037:
038: public void sync() {
039: writes.add(currentWrite);
040: currentWrite = new Collection4();
041: }
042:
043: public int numSyncs() {
044: return writes.size();
045: }
046:
047: public int writeVersions(String file) throws IOException {
048:
049: int count = 0;
050: int rcount = 0;
051:
052: String lastFileName = file + "0";
053:
054: String rightFileName = file + "R";
055:
056: File4.copy(lastFileName, rightFileName);
057:
058: Iterator4 syncIter = writes.iterator();
059: while (syncIter.moveNext()) {
060:
061: rcount++;
062:
063: Collection4 writesBetweenSync = (Collection4) syncIter
064: .current();
065:
066: if (CrashSimulatingTestCase.LOG) {
067: System.out.println("Writing file " + rightFileName
068: + rcount);
069: }
070:
071: RandomAccessFile rightRaf = new RandomAccessFile(
072: rightFileName, "rw");
073: Iterator4 singleForwardIter = writesBetweenSync.iterator();
074: while (singleForwardIter.moveNext()) {
075: CrashSimulatingWrite csw = (CrashSimulatingWrite) singleForwardIter
076: .current();
077: csw.write(rightRaf);
078:
079: if (CrashSimulatingTestCase.LOG) {
080: System.out.println(csw);
081: }
082:
083: }
084: rightRaf.close();
085:
086: Iterator4 singleBackwardIter = writesBetweenSync.iterator();
087: while (singleBackwardIter.moveNext()) {
088: count++;
089: CrashSimulatingWrite csw = (CrashSimulatingWrite) singleBackwardIter
090: .current();
091: String currentFileName = file + "W" + count;
092: File4.copy(lastFileName, currentFileName);
093:
094: RandomAccessFile raf = new RandomAccessFile(
095: currentFileName, "rw");
096: csw.write(raf);
097: raf.close();
098: lastFileName = currentFileName;
099: }
100: File4.copy(rightFileName, rightFileName + rcount);
101: lastFileName = rightFileName;
102: }
103: return count;
104: }
105:
106: }
|