#ifndef _SCAN_ITER_H
#define _SCAN_ITER_H

#include "schema.h"
#include "tuple.h"
#include "iterator.h"
#include "predicate.h"

/*
 * Creates a new scan iterator from a relation schema information,
 * a projection list and a logical expression used as selection 
 * condition.
 * Input: rel_info - relation schema information.
 * Input: proj_list - projection list (actually an array).
 * Input: expr - tree with logical expression for selection
 * Input: cons - optional tuple containing constants that appear in the query.
 * Output: scan iterator instance (a pointer to it)
 */
scan_iterator *make_scan_iterator(relation_info *rel_info, 
				  proj_attr_list  *proj_list,
				  bool_expr *expr,
				  tuple *cons);

/*
 * This function takes care of opening the data file from which
 * tuples will be read.
 * Input: s_iter - pointer to the scan iterator structure.
 * Output: status code of OK indicating if the operation was successful,
 * or FILE_NOT_FOUND if the data file could not be opened.
 */
int open_scan_iterator(scan_iterator *s_iter);


/*
 * This function is used to obtain the next tuple that results
 * from evaluating a selection condition, and a series of projections
 * of a tuple read from the data file (database).
 * Input: s_iter - pointer to the scan iterator object.
 * Input: status - flag to be set with the diagnostic value that 
 * indicates what was the result of the call to next. Possible values are:
 *     OK : a tuple was returned
 *     ITERATOR_EMPTY : no more tuples are available
 *     NOT_ENOUGH_DATA: no enough data is avaliable to read a full tuple.
 *
 * Output: the next tuple that passes the selection and projections, or 
 * NULL if either there are no more tuples, or an error condition 
 * occurred. The value of status must be set accordingly.
 */
tuple *next_scan_iterator(scan_iterator *s_iter, int& status);


/*
 * This function is used to close the file from which tuples where being
 * read. The function should also set the empty iterator flag in the 
 * scan iterator structure to the value true.
 * Input: s_iter - pointer to the scan iterator structure
 * Output: status code of OK if the file gets closed, or UNKNOWN_FILE_ERROR
 * if the file cannot be closed.  
 */
int close_scan_iterator(scan_iterator *s_iter);

/*
 * This function is used to evaluate  a seleciton condition.
 * If the expr is equal to NULL, then the function must return
 * true immediately.
 * Input: expr - pointer to the selection condition 
 * Input: src - pointer to the tuple read from the database.
 * Input: cons - pointer to the tuple with constants.
 * Output: true if the src tuple passes the selection condition, or 
 * false otherwise.
 */
bool eval_selection(bool_expr *expr, tuple *src, tuple *cons);
#endif
