KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > acid > CrashSimulatingBatch


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.db4ounit.common.acid;
22
23 import java.io.*;
24
25 import com.db4o.foundation.*;
26 import com.db4o.foundation.io.*;
27
28
29 public class CrashSimulatingBatch {
30     
31     Collection4 writes = new Collection4();
32     Collection4 currentWrite = new Collection4();
33     
34     public void add(byte[] bytes, long offset, int length){
35         currentWrite.add(new CrashSimulatingWrite(bytes, offset, length));
36     }
37
38     public void sync() {
39         writes.add(currentWrite);
40         currentWrite = new Collection4();
41     }
42
43     public int numSyncs() {
44         return writes.size();
45     }
46     
47     public int writeVersions(String JavaDoc file) throws IOException {
48         
49         int count = 0;
50         int rcount = 0;
51         
52         String JavaDoc lastFileName = file + "0";
53         
54         String JavaDoc rightFileName = file + "R" ;
55         
56         File4.copy(lastFileName, rightFileName);
57                 
58         Iterator4 syncIter = writes.iterator();
59         while(syncIter.moveNext()){
60             
61             rcount++;
62             
63             Collection4 writesBetweenSync = (Collection4)syncIter.current();
64             
65             if(CrashSimulatingTestCase.LOG){
66                 System.out.println("Writing file " + rightFileName + rcount );
67             }
68             
69             RandomAccessFile rightRaf = new RandomAccessFile(rightFileName, "rw");
70             Iterator4 singleForwardIter = writesBetweenSync.iterator();
71             while(singleForwardIter.moveNext()){
72                 CrashSimulatingWrite csw = (CrashSimulatingWrite)singleForwardIter.current();
73                 csw.write(rightRaf);
74                 
75                 if(CrashSimulatingTestCase.LOG){
76                     System.out.println(csw);
77                 }
78                 
79             }
80             rightRaf.close();
81                         
82             Iterator4 singleBackwardIter = writesBetweenSync.iterator();
83             while(singleBackwardIter.moveNext()){
84                 count ++;
85                 CrashSimulatingWrite csw = (CrashSimulatingWrite)singleBackwardIter.current();
86                 String JavaDoc currentFileName = file + "W" + count;
87                 File4.copy(lastFileName, currentFileName);
88                 
89                 RandomAccessFile raf = new RandomAccessFile(currentFileName, "rw");
90                 csw.write(raf);
91                 raf.close();
92                 lastFileName = currentFileName;
93             }
94             File4.copy(rightFileName, rightFileName+rcount);
95             lastFileName = rightFileName;
96         }
97         return count;
98     }
99
100 }
101
Popular Tags