@@ -463,6 +463,76 @@ ResultType Catalog::CreateIndex(
463463 return ResultType::SUCCESS;
464464}
465465
466+ /*
467+ *
468+ // Create a new layout
469+ ResultType CreateLayout(oid_t database_oid, oid_t table_oid,
470+ const column_map_type &column_map,
471+ concurrency::TransactionContext *txn);
472+ // Create a new layout and set it as the default for the table
473+ ResultType CreateDefaultLayout(oid_t database_oid, oid_t table_oid,
474+ const column_map_type &column_map,
475+ concurrency::TransactionContext *txn);
476+ */
477+
478+ /*
479+ * @brief create a new layout for a table
480+ * @param database_oid database to which the table belongs to
481+ * @param table_oid table to which the layout has to be added
482+ * @param column_map column_map of the new layout to be created
483+ * @param txn TransactionContext
484+ * @return shared_ptr shared_ptr to the newly created layout in case of
485+ * success. nullptr in case of failure.
486+ */
487+ std::shared_ptr<const storage::Layout>
488+ Catalog::CreateLayout (oid_t database_oid, oid_t table_oid,
489+ const column_map_type &column_map,
490+ concurrency::TransactionContext *txn) {
491+
492+ auto storage_manager = storage::StorageManager::GetInstance ();
493+ auto database = storage_manager->GetDatabaseWithOid (database_oid);
494+ auto table = database->GetTableWithOid (table_oid);
495+
496+ oid_t layout_oid = table->GetNextLayoutOid ();
497+ // Ensure that the new layout
498+ PELOTON_ASSERT (layout_oid < INVALID_OID);
499+ auto new_layout = std::shared_ptr<const storage::Layout>(
500+ new const storage::Layout (column_map, layout_oid));
501+ auto layout_catalog = catalog::LayoutCatalog::GetInstance ();
502+ bool result = layout_catalog->InsertLayout (table_oid, new_layout,
503+ pool_.get (), txn);
504+ if (!result) {
505+ LOG_DEBUG (" Failed to create a new layout for table %u" , table_oid);
506+ return nullptr ;
507+ }
508+ return new_layout;
509+ }
510+
511+ /*
512+ * @brief create a new layout for a table and make it the deafult if
513+ * if the creating is successsful.
514+ * @param database_oid database to which the table belongs to
515+ * @param table_oid table to which the layout has to be added
516+ * @param column_map column_map of the new layout to be created
517+ * @param txn TransactionContext
518+ * @return shared_ptr shared_ptr to the newly created layout in case of
519+ * success. nullptr in case of failure.
520+ */
521+ std::shared_ptr<const storage::Layout>
522+ Catalog::CreateDefaultLayout (oid_t database_oid, oid_t table_oid,
523+ const column_map_type &column_map,
524+ concurrency::TransactionContext *txn) {
525+ auto new_layout = CreateLayout (database_oid, table_oid, column_map, txn);
526+ // If the layout creation was successful, set it as the default
527+ if (new_layout != nullptr ) {
528+ auto storage_manager = storage::StorageManager::GetInstance ();
529+ auto database = storage_manager->GetDatabaseWithOid (database_oid);
530+ auto table = database->GetTableWithOid (table_oid);
531+ table->SetDefaultLayout (new_layout);
532+ }
533+ return new_layout;
534+ }
535+
466536// ===----------------------------------------------------------------------===//
467537// DROP FUNCTIONS
468538// ===----------------------------------------------------------------------===//
@@ -634,6 +704,39 @@ ResultType Catalog::DropIndex(const std::string &index_name,
634704 return result;
635705}
636706
707+ /* @brief Drop layout
708+ * tile_groups
709+ * @param database_oid the database to which the table belongs
710+ * @param table_oid the table to which the layout belongs
711+ * @param layout_oid the layout to be dropped
712+ * @param txn TransactionContext
713+ * @return TransactionContext ResultType(SUCCESS or FAILURE)
714+ */
715+ ResultType Catalog::DropLayout (oid_t database_oid, oid_t table_oid,
716+ oid_t layout_oid,
717+ concurrency::TransactionContext *txn) {
718+ // Check if the default_layout of the table is the same.
719+ // If true reset it to a row store.
720+ auto storage_manager = storage::StorageManager::GetInstance ();
721+ auto database = storage_manager->GetDatabaseWithOid (database_oid);
722+ auto table = database->GetTableWithOid (table_oid);
723+ auto default_layout = table->GetDefaultLayout ();
724+
725+ if (default_layout.GetOid () == layout_oid) {
726+ table->ResetDefaultLayout ();
727+ }
728+
729+ auto layout_catalog = LayoutCatalog::GetInstance ();
730+ if (!layout_catalog->DeleteLayout (table_oid, layout_oid, txn)) {
731+ auto layout = table->GetDefaultLayout ();
732+ LOG_DEBUG (" Layout delete failed. Default layout id: %u" ,
733+ layout.GetOid ());
734+ return ResultType::FAILURE;
735+ }
736+
737+ return ResultType::SUCCESS;
738+ }
739+
637740// ===--------------------------------------------------------------------===//
638741// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION
639742// ===--------------------------------------------------------------------===//
0 commit comments