package simpledb;

import java.util.*;

/**
 * The Catalog keeps track of all available tables in the database and their
 * associated schemas.
 * For now, this is a stub catalog that must be populated with tables by a
 * user program before it can be used -- eventually, this should be converted
 * to a catalog that reads a catalog table from disk.
 */

public class Catalog {
    private Map<Integer, Table> tables;

    //  private final static Catalog _instance = new Catalog();

    /**
     * Constructor.
     * Creates a new, empty catalog.
     */
    public Catalog() {
        tables = new HashMap();
    }

    /**
     * Add a new table to the catalog.
     * This table has tuples formatted using the specified TupleDesc and its
     * contents are stored in the specified DbFile. 
     * @param file the contents of the table to add
     * @param t the format of tuples that are being added
     */
    public void addTable(DbFile file, TupleDesc t) {
        Table tab = new Table(file, t);
        tables.put(file.id(), tab);
    }
    
    /**
     * Returns the tuple descriptor (schema) of the specified table
     */
    public TupleDesc getTupleDesc(int tableid) throws NoSuchElementException{
        return tables.get(tableid).desc;
    }
    
    /**
     * Returns the DbFile that can be used to read the contents of the 
     * specified table.
     */
    public DbFile getDbFile(int tableid) throws NoSuchElementException {
        return tables.get(tableid).file;
    }


    public void clear() {
        tables.clear();
    }


    private class Table {
        public DbFile file;
        public TupleDesc desc;

        public Table(DbFile file, TupleDesc desc) {
            this.file = file;
            this.desc = desc;
        }
    }
}
