/* Copyright (C) 2004 - 2005  db4objects Inc.  http://www.db4o.com

This file is part of the db4o open source object database.

db4o is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL 
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

db4o is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */
using System.IO;

namespace com.db4o.test.acid
{
	public class CrashSimulatingBatch
	{
		internal com.db4o.foundation.Collection4 writes = new com.db4o.foundation.Collection4
			();

		internal com.db4o.foundation.Collection4 currentWrite = new com.db4o.foundation.Collection4
			();

		public virtual void add(byte[] bytes, long offset, int length)
		{
			currentWrite.add(new com.db4o.test.acid.CrashSimulatingWrite(bytes, offset, length
				));
		}

		public virtual void sync()
		{
			writes.add(currentWrite);
			currentWrite = new com.db4o.foundation.Collection4();
		}

		public virtual int numSyncs()
		{
			return writes.size();
		}

		public virtual int writeVersions(string file)
		{
			int count = 0;
			int rcount = 0;
			string lastFileName = file + "0";
			string rightFileName = file + "R";
			copyFile(lastFileName,rightFileName);
			com.db4o.foundation.Iterator4 syncIter = writes.strictIterator();
			while (syncIter.hasNext())
			{
				com.db4o.foundation.Collection4 writesBetweenSync = (com.db4o.foundation.Collection4
					)syncIter.next();
				j4o.io.RandomAccessFile rightRaf = new j4o.io.RandomAccessFile(rightFileName, "rw"
					);
				com.db4o.foundation.Iterator4 singleForwardIter = writesBetweenSync.strictIterator
					();
				while (singleForwardIter.hasNext())
				{
					com.db4o.test.acid.CrashSimulatingWrite csw = (com.db4o.test.acid.CrashSimulatingWrite
						)singleForwardIter.next();
					csw.write(rightRaf);
				}
				rightRaf.close();
				com.db4o.foundation.Iterator4 singleBackwardIter = writesBetweenSync.iterator();
				while (singleBackwardIter.hasNext())
				{
					count++;
					com.db4o.test.acid.CrashSimulatingWrite csw = (com.db4o.test.acid.CrashSimulatingWrite
						)singleBackwardIter.next();
					string currentFileName = file + "W" + count;
					copyFile(lastFileName,currentFileName);
					j4o.io.RandomAccessFile raf = new j4o.io.RandomAccessFile(currentFileName, "rw");
					csw.write(raf);
					raf.close();
					lastFileName = currentFileName;
				}
				rcount++;
				copyFile(rightFileName,rightFileName + rcount);
				lastFileName = rightFileName;
			}
			return count;
		}
		
		private void copyFile(string from,string to) 
		{
			File.Delete(to);
			File.Copy(from,to);
		}
	}
}
