/**
 *
 * 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.activeio.journal.active;

import org.activeio.Packet;

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 $
 */
final public class BatchedWrite {

    private final Packet packet;
    public Throwable error;
    private Location mark;
    private boolean appendDisabled = false;
    private boolean appendInProgress = false;
    private Latch writeDoneLatch;

    /**
     * @param packet
     */
    public BatchedWrite(Packet packet) {
        this.packet = packet;
    }

    /**
     * @throws InterruptedException
     * 
     */
    synchronized private void disableAppend() throws InterruptedException {
        appendDisabled = true;
        while (appendInProgress) {
            wait();
        }
    }

    /**
     * @param packet2
     * @param mark2
     * @return
     */
    public boolean append(Record record, Location recordMark, boolean force) {

        synchronized (this) {
            if (appendDisabled)
                return false;
            appendInProgress = true;
        }
        
        
        if( force && writeDoneLatch==null)
            writeDoneLatch = new Latch();
        
        record.read(packet);

        // if we fit the record in this batch
        if ( !record.hasRemaining() ) {
            if (recordMark != null)
                mark = recordMark;
        }

        synchronized (this) {
            appendInProgress = false;
            this.notify();

            if (appendDisabled)
                return false;
            else
                return packet.remaining() > 0;
        }
    }

    public void waitForForce() throws Throwable {
        if( writeDoneLatch!=null ) {
            writeDoneLatch.acquire();
            synchronized (this) {
                if (error != null)
                    throw error;
            }
        }
    }

    public void forced() {
        if( writeDoneLatch!=null ) {
            writeDoneLatch.release();
        }
    }

    public void writeFailed(Throwable error) {
        if( writeDoneLatch!=null ) {
            synchronized (this) {
                this.error = error;
            }
            writeDoneLatch.release();
        }
    }

    public Packet getPacket() {
        return packet;
    }

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

    /**
     * @throws InterruptedException
     * 
     */
    public void flip() throws InterruptedException {
        disableAppend();
        packet.flip();
    }

    public boolean getForce() {
        return writeDoneLatch!=null;
    }

}
