package simpledb.test;

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

public class JoinTest extends Test {
  public boolean runTest(String args[]) {
    TransactionId tid = new TransactionId();
    SeqScan ss1 = new SeqScan(tid, file[Integer.parseInt(args[1])-1][0].id());
    SeqScan ss2 = new SeqScan(tid, file[Integer.parseInt(args[2])-1][1].id());
    JoinPredicate p = new JoinPredicate(Integer.parseInt(args[3]),
                                        Predicate.Op.getOp(args[4]),
                                        Integer.parseInt(args[5]));
    Join j = new Join(p, ss1, ss2);

    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;
  }
}
