package relop;

import global.RID;
import heap.HeapFile;
import heap.HeapScan;

/**
 * Wrapper for heap file scan, the most basic access method. This "iterator"
 * version takes schema into consideration and generates real tuples.
 */
public class FileScan extends Iterator {

	// TODO: Done
	private HeapFile file;
	private HeapScan heapscan; // Wrapper for heap file scan

	/**
	 * Constructs a file scan, given the schema and heap file.
	 */
	public FileScan(Schema schema, HeapFile file) {
		// TODO: Done
		// Schema from Iterator class, for resulting tuples; must be set in all
		// subclass constructors
		this.setSchema(schema);
		this.file = file;

		this.heapscan = file.openScan();

		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Gives a one-line explaination of the iterator, repeats the call on any
	 * child iterators, and increases the indent depth along the way.
	 */
	public void explain(int depth) {
		throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Restarts the iterator, i.e. as if it were just constructed.
	 */
	public void restart() {
		// TODO: Done
		if (isOpen()) {
			heapscan.close();
		}
		this.heapscan = file.openScan();
		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Returns true if the iterator is open; false otherwise.
	 */
	public boolean isOpen() {
		// TODO: Done
		if (heapscan != null) {
			return true;
		}
		return false;
		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Closes the iterator, releasing any resources (i.e. pinned pages).
	 */
	public void close() {
		// TODO: close
		if (isOpen()) {
			heapscan.close();
		}
		heapscan = null;

		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Returns true if there are more tuples, false otherwise.
	 */
	public boolean hasNext() {
		// TODO: Done
		if (heapscan.hasNext()) {
			return true;
		}
		return false;

		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Gets the next tuple in the iteration.
	 * 
	 * @throws IllegalStateException
	 *             if no more tuples
	 */
	public Tuple getNext() {
		// TODO: get next tuple
		Tuple t = new Tuple(this.getSchema());
		return t;

		// throw new UnsupportedOperationException("Not implemented");
	}

	/**
	 * Gets the RID of the last tuple returned.
	 */
	public RID getLastRID() {
		//TODO: get the RID ?
		
		throw new UnsupportedOperationException("Not implemented");
	}

} // public class FileScan extends Iterator

@@ -381,16 +381,44 @@
 le scan%0D
+%0A%09private RID lastRecordID;%0D
 %0A%0D%0A%09/**%0D
@@ -740,16 +740,45 @@
 Scan();%0D
+%0A%09%09lastRecordID = new RID();%0D
 %0A%0D%0A%09%09// 
@@ -1033,24 +1033,105 @@
 depth) %7B%0D%0A%09%09
+this.indent(depth++);%0D%0A%09%09System.out.println(%22FileScan depth =%3E %22 + depth);%0D%0A%09%09// 
 throw new Un
@@ -1323,33 +1323,24 @@
 en()) %7B%0D%0A%09%09%09
-heapscan.
 close();%0D%0A%09%09
@@ -2099,24 +2099,19 @@
 if (
-heapscan.hasNext
+this.isOpen
 ()) 
@@ -2115,36 +2115,50 @@
 )) %7B%0D%0A%09%09%09return 
-true
+heapscan.hasNext()
 ;%0D%0A%09%09%7D%0D%0A%09%09return
@@ -2425,16 +2425,42 @@
  tuple%0D%0A
+%09%09if (this.hasNext()) %7B%0D%0A%09
 %09%09Tuple 
@@ -2491,24 +2491,25 @@
 hema());%0D%0A%09%09
+%09
 return t;%0D%0A%0D
@@ -2506,16 +2506,101 @@
 turn t;%0D
+%0A%09%09%7D else %7B%0D%0A%09%09%09throw new IllegalStateException(%22FilsScan - tuples underflow%22);%0D%0A%09%09%7D%0D
 %0A%0D%0A%09%09// 
@@ -2756,16 +2756,17 @@
  %7B%0D%0A%09%09//
+ 
 TODO: ge
@@ -2780,20 +2780,45 @@
 ID ?%0D%0A%09%09
+return lastRecordID;%0D%0A
 %0D%0A%09%09
+// 
 throw ne

update FileScan