package simpledb.test;

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

public class INLJoinTest extends Test {
    public boolean runTest(String args[]) {
	TransactionId tid = new TransactionId();
	SeqScan ss1 = new SeqScan(tid, 
				  file[Integer.parseInt(args[1])-1][0].id());
	IndexPredicate ipred = null;
	HashScan hs2 = new HashScan(tid, 
				    hashfile[Integer.parseInt(args[2])-1][1].id(),
				    ipred);
	INLJoin j = new INLJoin(ss1, Integer.parseInt(args[3]), hs2);

	Query q = new Query(j, tid);

	// print out the joined table
	try {
	    q.start();

	    try {
		while (true) {
		    Tuple tup = q.getNext();
		    tup.print();
		}
	    } catch (NoSuchElementException e) {
		j.close();
	    }
	} catch(TransactionAbortedException te) {
	    te.printStackTrace();
	    return false;
	} catch (DbException e) {
	    e.printStackTrace();
	    return false;
	} catch (IOException e) {
	    e.printStackTrace();
	    return false;
	}

	// XXX hack for testing purposes;
	try {
	    Database.getBufferPool().flushAllPages();
	} catch(Exception e) {
	    e.printStackTrace();
	}
	return true;
    }
}
