/*
 * Copyright (C) 2004-2007  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.util;

import com.exedio.cope.Model;

public final class TransactionSlicer
{
	private final Model model;
	private final int bitesPerSlice;
	private final String transactionName;

	private int bitsLeft;
	private int sliceCount = 0;
	
	public TransactionSlicer(final Model model, final int bitesPerSlice)
	{
		this.model = model;
		this.bitesPerSlice = bitesPerSlice;
		this.transactionName = model.getCurrentTransaction().getName();
		this.bitsLeft = bitesPerSlice;
		
		if(bitesPerSlice<=0)
			throw new IllegalArgumentException("bitesPerSlice must be positive, but was " + bitesPerSlice);
	}
	
	public boolean biteOff()
	{
		if((--bitsLeft)>0)
			return false;

		model.commit();
		bitsLeft = bitesPerSlice;
		sliceCount++;
		model.startTransaction(transactionName!=null ? (transactionName+"-slice"+sliceCount) : ("slice"+sliceCount));
		return true;
	}
	
	public int getSliceCount()
	{
		return sliceCount;
	}
}

@@ -1571,16 +1571,21 @@
 saction(
+%0A%09%09%09%09
 transact
@@ -1597,17 +1597,21 @@
 me!=null
- 
+%0A%09%09%09%09
 ? (trans
@@ -1641,17 +1641,21 @@
 ceCount)
- 
+%0A%09%09%09%09
 : (%22slic

format source