CreateTableCommand.java 15.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
package edu.caltech.nanodb.commands;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import edu.caltech.nanodb.indexes.IndexManager;
import edu.caltech.nanodb.relations.ColumnInfo;
import edu.caltech.nanodb.relations.ForeignKeyColumnRefs;
import edu.caltech.nanodb.relations.IndexColumnRefs;
import edu.caltech.nanodb.relations.KeyColumnRefs;
import edu.caltech.nanodb.relations.Schema;
import edu.caltech.nanodb.relations.TableConstraintType;
import edu.caltech.nanodb.relations.TableInfo;
import edu.caltech.nanodb.server.NanoDBServer;
import edu.caltech.nanodb.server.properties.PropertyRegistry;
import edu.caltech.nanodb.storage.TableManager;
import edu.caltech.nanodb.storage.StorageManager;


/**
 * This command handles the <tt>CREATE TABLE</tt> DDL operation.
 */
public class CreateTableCommand extends Command {

    /** A logging object for reporting anything interesting that happens. */
    private static Logger logger = LogManager.getLogger(CreateTableCommand.class);


    /** Name of the table to be created. */
    private String tableName;


    /** If this flag is {@code true} then the table is a temporary table. */
    private boolean temporary;


    /**
     * If this flag is {@code true} then the create-table operation should
     * only be performed if the specified table doesn't already exist.
     */
    private boolean ifNotExists;


    /** List of column-declarations for the new table. */
    private List<ColumnInfo> columnInfos = new ArrayList<>();


    /** List of constraints for the new table. */
    private List<ConstraintDecl> constraints = new ArrayList<>();


    /** Any additional properties specified in the command. */
    private CommandProperties properties;


    /**
     * Create a new object representing a <tt>CREATE TABLE</tt> statement.
     *
     * @param tableName the name of the table to be created
     */
    public CreateTableCommand(String tableName) {
        super(Command.Type.DDL);

        if (tableName == null)
            throw new IllegalArgumentException("tableName cannot be null");

        this.tableName = tableName;
    }


    /**
     * Returns {@code true} if the table is a temporary table, {@code false}
     * otherwise.
     *
     * @return {@code true} if the table is a temporary table, {@code false}
     *         otherwise.
     */
    public boolean isTemporary() {
        return temporary;
    }


    /**
     * Specifies whether the table is a temporary table or not.
     *
     * @param b {@code true} if the table is a temporary table, {@code false}
     *        otherwise.
     */
    public void setTemporary(boolean b) {
        temporary = b;
    }


    /**
     * Returns {@code true} if table creation should only be attempted if it
     * doesn't already exist, {@code false} if table creation should always
     * be attempted.
     *
     * @return {@code true} if table creation should only be attempted if it
     *         doesn't already exist, {@code false} if table creation should
     *         always be attempted.
     */
    public boolean getIfNotExists() {
        return ifNotExists;
    }


    /**
     * Sets the flag indicating whether table creation should only be
     * attempted if it doesn't already exist.
     *
     * @param b the flag indicating whether table creation should only be
     *        attempted if it doesn't already exist.
     */
    public void setIfNotExists(boolean b) {
        ifNotExists = b;
    }


    /**
     * Sets any additional properties associated with the command.  The
     * value may be {@code null} to indicate no properties.
     *
     * @param properties any additional properties to associate with the
     *        command.
     */
    public void setProperties(CommandProperties properties) {
        this.properties = properties;
    }


    /**
     * Returns any additional properties specified for the command, or
     * {@code null} if no additional properties were specified.
     *
     * @return any additional properties specified for the command, or
     *         {@code null} if no additional properties were specified.
     */
    public CommandProperties getProperties() {
        return properties;
    }


    /**
     * Adds a column description to this create-table command.  This method is
     * primarily used by the SQL parser.
     *
     * @param colDecl the details of the column to add
     *
     * @throws NullPointerException if colDecl is null
     */
    public void addColumn(TableColumnDecl colDecl) {
        if (colDecl == null)
            throw new IllegalArgumentException("colDecl cannot be null");

        // Pull out the column-info value first, and add it to the table
        // specification.  If the table-name doesn't match, update this.

        ColumnInfo colInfo = colDecl.getColumnInfo();
        if (!tableName.equals(colInfo.getTableName())) {
            colInfo = new ColumnInfo(colInfo.getName(), tableName,
                colInfo.getType());
        }
        columnInfos.add(colInfo);

        // Next, if any column-level constraints are specified,
        // add them to the collection of constraints.
        for (ConstraintDecl constraint : colDecl.getConstraints())
            addConstraint(constraint);
    }


    /**
     * Returns an immutable list of the column descriptions that are part of
     * this <tt>CREATE TABLE</tt> command.
     *
     * @return an immutable list of the column descriptions that are part of
     *         this <tt>CREATE TABLE</tt> command.
     */
    public List<ColumnInfo> getColumns() {
        return Collections.unmodifiableList(columnInfos);
    }


    /**
     * Adds a constraint to this create-table command.  This method is primarily
     * used by the SQL parser.
     *
     * @param con the details of the table constraint to add
     *
     * @throws NullPointerException if con is null
     */
    public void addConstraint(ConstraintDecl con) {
        if (con == null)
            throw new IllegalArgumentException("con cannot be null");

        constraints.add(con);
    }


    /**
     * Returns an immutable list of the constraint declarations that are part
     * of this <tt>CREATE TABLE</tt> command.
     *
     * @return an immutable list of the constraint declarations that are part
     *         of this <tt>CREATE TABLE</tt> command.
     */
    public List<ConstraintDecl> getConstraints() {
        return Collections.unmodifiableList(constraints);
    }


    @Override
    public void execute(NanoDBServer server) throws ExecutionException {

        StorageManager storageManager = server.getStorageManager();
        TableManager tableManager = storageManager.getTableManager();

        PropertyRegistry propReg = server.getPropertyRegistry();
        boolean createIndexesOnKeys = propReg.getBooleanProperty(
            NanoDBServer.PROP_CREATE_INDEXES_ON_KEYS);

        // See if the table already exists.
        if (ifNotExists && tableManager.tableExists(tableName)) {
            out.printf("Table %s already exists; skipping create-table.%n",
                tableName);
            return;
        }

        // Set up the table's schema based on the command details.

        logger.debug("Creating a TableSchema object for the new table " +
            tableName + ".");

        Schema schema = new Schema();
        for (ColumnInfo colInfo : columnInfos) {
            try {
                schema.addColumnInfo(colInfo);
            }
            catch (IllegalArgumentException iae) {
                throw new ExecutionException("Duplicate or invalid column \"" +
                    colInfo.getName() + "\".", iae);
            }
        }

        // Do some basic verification of the table constraints:
        //  * Verify that all named constraints are uniquely named.
        //  * Open all tables referenced by foreign-key constraints, to ensure
        //    they exist.  (More verification will occur later.)
        HashSet<String> constraintNames = new HashSet<>();
        HashMap<String, TableInfo> referencedTables = new HashMap<>();
        for (ConstraintDecl cd: constraints) {
            String name = cd.getName();
            if (name != null && !constraintNames.add(name)) {
                throw new ExecutionException("Constraint name " + name +
                    " appears multiple times.");
            }

            if (cd.getType() == TableConstraintType.FOREIGN_KEY) {
                String refTableName = cd.getRefTable();
                TableInfo refTblInfo = tableManager.openTable(refTableName);
                if (refTblInfo == null) {
                    throw new ExecutionException("Referenced table " +
                        refTableName + " doesn't exist.");
                }

                referencedTables.put(refTableName, refTblInfo);
            }
        }

        // Initialize all the constraints on the table.
        initTableConstraints(storageManager, schema, referencedTables);

        // Create the table.
        logger.debug("Creating the new table " + tableName + " on disk.");
        TableInfo tableInfo = tableManager.createTable(tableName, schema, properties);
        logger.debug("New table " + tableName + " was created.");

        if (createIndexesOnKeys) {
            logger.debug("Creating indexes on the new table, and any " +
                "referenced tables");
            initIndexes(storageManager, tableInfo);
        }

        out.println("Created table:  " + tableName);
    }


    private void initTableConstraints(StorageManager storageManager,
        Schema schema, HashMap<String, TableInfo> referencedTables) {

        if (constraints.isEmpty()) {
            logger.debug("No table constraints specified, our work is done.");
            return;
        }

        TableManager tableManager = storageManager.getTableManager();

        logger.debug("Adding " + constraints.size() +
            " constraints to the table.");

        HashSet<String> constraintNames = new HashSet<>();

        for (ConstraintDecl cd : constraints) {
            // Make sure that if constraint names are specified, every
            // constraint is actually uniquely named.
            if (cd.getName() != null) {
                if (!constraintNames.add(cd.getName())) {
                    throw new ExecutionException("Constraint name " +
                        cd.getName() + " appears multiple times.");
                }
            }

            TableConstraintType type = cd.getType();
            if (type == TableConstraintType.PRIMARY_KEY ||
                type == TableConstraintType.UNIQUE) {
                // Make a candidate-key constraint and put it on the schema.

                int[] cols = schema.getColumnIndexes(cd.getColumnNames());
                KeyColumnRefs ck = new KeyColumnRefs(cols, cd.getName(), type);

                if (type == TableConstraintType.PRIMARY_KEY) {
                    // Add NOT NULL constraints for all primary-key columns.
                    for (int iCol : cols)
                        schema.addNotNull(iCol);
                }

                schema.addCandidateKey(ck);
            }
            else if (type == TableConstraintType.FOREIGN_KEY) {
                // Make a foreign key constraint and put it on the schema.
                // This involves these steps:
                // 1)  Create the foreign key on this table's schema.
                // 2)  Update the referenced table's schema to record that
                //     this table references that table.

                // This should never be null since we already resolved all
                // foreign-key table references earlier.
                TableInfo refTableInfo = referencedTables.get(cd.getRefTable());
                Schema refSchema = refTableInfo.getSchema();

                // The makeForeignKey() method ensures that the referenced
                // columns are also a candidate key (or primary key) on the
                // referenced table.
                ForeignKeyColumnRefs fk = DDLUtils.makeForeignKey(schema,
                    refTableInfo, cd);

                schema.addForeignKey(fk);

                // Update the referenced table's schema to record that this
                // table has a foreign-key reference to the table.
                if (refSchema.addReferencingTable(tableName))
                    tableManager.saveTableInfo(refTableInfo);
            }
            else if (type == TableConstraintType.NOT_NULL) {
                int idx = schema.getColumnIndex(cd.getColumnNames().get(0));
                schema.addNotNull(idx);
            }
            else {
                throw new ExecutionException("Unexpected constraint type " +
                    cd.getType());
            }
        }
    }


    private void initIndexes(StorageManager storageManager,
                             TableInfo tableInfo) {
        IndexManager indexManager = storageManager.getIndexManager();
        Schema schema = tableInfo.getSchema();

        for (ConstraintDecl cd : constraints) {
            TableConstraintType type = cd.getType();
            if (type == TableConstraintType.PRIMARY_KEY ||
                type == TableConstraintType.UNIQUE) {

                int[] cols = schema.getColumnIndexes(cd.getColumnNames());
                IndexColumnRefs ck = new IndexColumnRefs(cd.getName(), cols);

                // Make the index.  This also updates the table schema with
                // the fact that there is another candidate key on the table.
                indexManager.addIndexToTable(tableInfo, ck);
            }
            else if (type == TableConstraintType.FOREIGN_KEY) {

                // Check if there is already an index on the foreign-key
                // columns.  If there is not, we will create a non-unique
                // index on those columns.

                int[] fkCols = schema.getColumnIndexes(cd.getColumnNames());
                IndexColumnRefs fkColRefs = null; // schema.getIndexOnColumns(new ColumnRefs(fkCols));
                if (fkColRefs == null) {
                    // Need to make a new index for this foreign-key reference
                    fkColRefs = new IndexColumnRefs(cd.getName(), fkCols);
                    fkColRefs.setConstraintType(TableConstraintType.FOREIGN_KEY);

                    // Make the index.
                    indexManager.addIndexToTable(tableInfo, fkColRefs);

                    logger.debug(String.format(
                        "Created index %s on table %s to enforce foreign key.",
                        fkColRefs.getIndexName(), tableInfo.getTableName()));
                }
            }
        }
    }


    @Override
    public String toString() {
        return "CreateTable[" + tableName + "]";
    }


    /**
     * Returns a verbose, multi-line string containing all of the details of
     * this table.
     *
     * @return a detailed description of the table described by this command
     */
    public String toVerboseString() {
        StringBuilder strBuf = new StringBuilder();

        strBuf.append(toString());
        strBuf.append('\n');

        for (ColumnInfo colInfo : columnInfos) {
            strBuf.append('\t');
            strBuf.append(colInfo.toString());
            strBuf.append('\n');
        }

        for (ConstraintDecl con : constraints) {
            strBuf.append('\t');
            strBuf.append(con.toString());
            strBuf.append('\n');
        }

        return strBuf.toString();
    }
}