package simpledb;
import java.util.*;
import java.io.*;

/** 
 * The delete operator.  Delete reads tuples from its child operator and
 * removes them from the table they belong to.
 */
public class Delete implements DbIterator {
    private TransactionId tid;
    private DbIterator child;
    private TupleDesc td;
    private DbFile dbFile;
    private int deleteCount;
    private boolean opened;
    private boolean resultRead;

    /**
     * Constructor specifying the transaction that this delete belongs to as
     * well as the child to read from.
     * @param t The transaction this delete runs in
     * @param child The child operator to read tuples for deletion from
     */
    public Delete(TransactionId t, DbIterator child) {
        this.tid = t;
        this.child = child;

        Type[] types = new Type[1];
        types[0] = Type.INT_TYPE;
        td = new TupleDesc(types);

        opened = false;
        resultRead = false;
    }

    public TupleDesc getTupleDesc() {
        return td;
    }

    public void open() throws DbException, TransactionAbortedException {
        child.open();
        deleteCount = 0;

        while (true) {
            try {
                Tuple t = child.getNext();
                Database.getBufferPool().deleteTuple(tid, t);
                deleteCount++;
            } catch (NoSuchElementException e) {
                break;
            }
        }

        child.close();
        opened = true;
        resultRead = false;
    }

    public void close() {
        opened = false;
        resultRead = false;
    }
    
    public void rewind() throws DbException, TransactionAbortedException {
        resultRead = false;
    }

    /**
     * DbIterator getNext method.
     * Deletes tuples as they are read from the child operator. Deletes are
     * processed via the buffer pool (which can be access via the
     * Database.getBufferPool() method.
     * @return A 1-field tuple containing the number of deleted records.
     * @see Database#getBufferPool
     * @see BufferPool#deleteTuple
     */
    public Tuple getNext() 
	throws NoSuchElementException, TransactionAbortedException, DbException {
        if (!opened) {
            throw new DbException("Iterator is not open");
        }

        if (resultRead) {
            throw new NoSuchElementException();
        }

        Tuple t = new Tuple(td);
        t.setField(0, new IntField(deleteCount));
        resultRead = true;
        return t;
    }
}
