/**
 *
 * Copyright 2004 Hiram Chirino
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **/
package org.codehaus.activemq.journal.impl;

import java.nio.ByteBuffer;

import EDU.oswego.cs.dl.util.concurrent.Latch;

/**
 * This contains all the data needed to write and force a list of records to a LogFile.
 * The more records that can be cramed into a single BatchedWrite, the higher throughput
 * that can be achived by a write and force operation.
 * 
 * @version $Revision: 1.1 $
 */
public class BatchedWrite {
	
	private final Latch writeDoneLatch = new Latch();	
	private Record data=null;
    public Throwable error;
	private final ByteBuffer byteBuffer;
	private long firstSequenceId=-1;
	private long lastSequenceId;
	private Mark mark;
	
    private boolean appendDisabled=false;
    private boolean appendInProgress=false;
	
	/**
	 * @param byteBuffer
	 */
	public BatchedWrite(ByteBuffer byteBuffer) {
		this.byteBuffer = byteBuffer;
	}

    /**
     * @throws InterruptedException
     * 
     */
    synchronized public void disableAppend() throws InterruptedException {
        appendDisabled=true;
        while( appendInProgress ) {
            wait();
        }
    }
	
	/**
	 * @param record
	 */
	public boolean append(Record record) {
	    
	    synchronized(this) {
	        if( appendDisabled )
	            return false;
	        appendInProgress=true;
	    }
	    
		record.fill( byteBuffer );
		// if we fit the record in this batch
		if( record.remaining()==0 ) {
			if( firstSequenceId== -1 )
				firstSequenceId = record.getHeader().sequenceId;
			lastSequenceId = record.getHeader().sequenceId;;
			if( record.getMark()!=null )
				mark = record.getMark();
		}

		synchronized(this) {		    
	        appendInProgress=false;
	        this.notify();
	        
	        if( appendDisabled )
	            return false;
	        else
	            return byteBuffer.remaining()>0;
	    }			
	}
	
	public void waitForForce() throws Throwable {
		writeDoneLatch.acquire();
		synchronized(this) {
		    if( error!=null )
		        throw error; 
		}
	}

	public void forced() {
		writeDoneLatch.release();
	}

	public void writeFailed(Throwable error) {
		synchronized(this) {
		    this.error=error; 
		}
		writeDoneLatch.release();
	}
	
	/**
	 * @return Returns the data.
	 */
	public Record getData() {
		return data;
	}
	
	public ByteBuffer getByteBuffer() {
		return byteBuffer;
	}

	/**
	 * @return
	 */
	public Mark getMark() {
		return mark;
	}

	/**
	 * @return
	 */
	public long getLastSequenceId() {
		return lastSequenceId;
	}

	/**
	 * @return
	 */
	public long getFirstSequenceId() {
		return firstSequenceId;
	}


}
