Beispiel #1
0
TEST_F(CatalogTests, DroppingTable) {
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  auto catalog = catalog::Catalog::GetInstance();
  EXPECT_EQ(
      3,
      (int)catalog->GetDatabaseObject("EMP_DB", txn)->GetTableObjects().size());
  auto database_object =
      catalog::Catalog::GetInstance()->GetDatabaseObject("EMP_DB", txn);
  EXPECT_NE(nullptr, database_object);
  catalog::Catalog::GetInstance()->DropTable("EMP_DB", "department_table", txn);

  database_object =
      catalog::Catalog::GetInstance()->GetDatabaseObject("EMP_DB", txn);
  EXPECT_NE(nullptr, database_object);
  auto department_table_object =
      database_object->GetTableObject("department_table");
  //  catalog::Catalog::GetInstance()->PrintCatalogs();
  EXPECT_EQ(
      2,
      (int)catalog->GetDatabaseObject("EMP_DB", txn)->GetTableObjects().size());
  txn_manager.CommitTransaction(txn);

  EXPECT_EQ(nullptr, department_table_object);

  // Try to drop again
  txn = txn_manager.BeginTransaction();
  EXPECT_THROW(catalog::Catalog::GetInstance()->DropTable(
                   "EMP_DB", "department_table", txn),
               CatalogException);

  EXPECT_EQ(
      2,
      (int)catalog->GetDatabaseObject("EMP_DB", txn)->GetTableObjects().size());
  txn_manager.CommitTransaction(txn);

  // Drop a table that does not exist
  txn = txn_manager.BeginTransaction();
  EXPECT_THROW(
      catalog::Catalog::GetInstance()->DropTable("EMP_DB", "void_table", txn),
      CatalogException);
  EXPECT_EQ(
      2,
      (int)catalog->GetDatabaseObject("EMP_DB", txn)->GetTableObjects().size());
  txn_manager.CommitTransaction(txn);

  // Drop the other table
  txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->DropTable("EMP_DB", "emp_table", txn);
  EXPECT_EQ(
      1,
      (int)catalog->GetDatabaseObject("EMP_DB", txn)->GetTableObjects().size());
  txn_manager.CommitTransaction(txn);
}
Beispiel #2
0
/* @brief   Get table catalog object from cache or all the way from storage
 * @param   table_oid     table oid of the requested table catalog object
 * @param   cached_only   if cached only, return nullptr on a cache miss
 * @return  Shared pointer to the requested table catalog object
 */
std::shared_ptr<TableCatalogObject> DatabaseCatalogObject::GetTableObject(
    oid_t table_oid, bool cached_only) {
  auto it = table_objects_cache.find(table_oid);
  if (it != table_objects_cache.end()) return it->second;

  if (cached_only) {
    // cache miss return empty object
    return nullptr;
  } else {
    // cache miss get from pg_table
    auto pg_table = Catalog::GetInstance()
                        ->GetSystemCatalogs(database_oid)
                        ->GetTableCatalog();
    return pg_table->GetTableObject(table_oid, txn);
  }
}
Beispiel #3
0
/* @brief   Get table catalog object from cache (cached_only == true),
            or all the way from storage (cached_only == false)
 * @param   table_name     table name of the requested table catalog object
 * @param   schema_name    schema name of the requested table catalog object
 * @param   cached_only   if cached only, return nullptr on a cache miss
 * @return  Shared pointer to the requested table catalog object
 */
std::shared_ptr<TableCatalogObject> DatabaseCatalogObject::GetTableObject(
    const std::string &table_name, const std::string &schema_name,
    bool cached_only) {
  std::string key = schema_name + "." + table_name;
  auto it = table_name_cache.find(key);
  if (it != table_name_cache.end()) {
    return it->second;
  }

  if (cached_only) {
    // cache miss return empty object
    return nullptr;
  } else {
    // cache miss get from pg_table
    auto pg_table = Catalog::GetInstance()
                        ->GetSystemCatalogs(database_oid)
                        ->GetTableCatalog();
    return pg_table->GetTableObject(table_name, schema_name, txn);
  }
}
Beispiel #4
0
TEST_F(CatalogTests, LayoutCatalogTest) {
  // This test creates a table, changes its layout.
  // Create another additional layout.
  // Ensure that default is not changed.
  // Drops layout and verifies that the default_layout is reset.
  // It also queries pg_layout to ensure that the entry is removed.

  auto db_name = "temp_db";
  auto table_name = "temp_table";
  auto catalog = catalog::Catalog::GetInstance();
  // Create database.
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  EXPECT_EQ(ResultType::SUCCESS, catalog->CreateDatabase(db_name, txn));

  // Create table.
  auto val0 = catalog::Column(type::TypeId::INTEGER,
                              type::Type::GetTypeSize(type::TypeId::INTEGER),
                              "val0", true);
  auto val1 = catalog::Column(type::TypeId::INTEGER,
                              type::Type::GetTypeSize(type::TypeId::INTEGER),
                              "val1", true);
  auto val2 = catalog::Column(type::TypeId::INTEGER,
                              type::Type::GetTypeSize(type::TypeId::INTEGER),
                              "val2", true);
  auto val3 = catalog::Column(type::TypeId::INTEGER,
                              type::Type::GetTypeSize(type::TypeId::INTEGER),
                              "val3", true);
  std::unique_ptr<catalog::Schema> table_schema(
      new catalog::Schema({val0, val1, val2, val3}));
  EXPECT_EQ(ResultType::SUCCESS,
            catalog->CreateTable(db_name, DEFAULT_SCHEMA_NAME, table_name,
                                 std::move(table_schema), txn));
  txn_manager.CommitTransaction(txn);

  txn = txn_manager.BeginTransaction();
  auto database_oid =
      catalog->GetDatabaseObject(db_name, txn)->GetDatabaseOid();
  auto table_object =
      catalog->GetTableObject(db_name, DEFAULT_SCHEMA_NAME, table_name, txn);
  auto table_oid = table_object->GetTableOid();
  auto table =
      catalog->GetTableWithName(db_name, DEFAULT_SCHEMA_NAME, table_name, txn);
  txn_manager.CommitTransaction(txn);

  // Change default layout.

  std::map<oid_t, std::pair<oid_t, oid_t>> default_map;
  default_map[0] = std::make_pair(0, 0);
  default_map[1] = std::make_pair(0, 1);
  default_map[2] = std::make_pair(1, 0);
  default_map[3] = std::make_pair(1, 1);

  txn = txn_manager.BeginTransaction();
  auto default_layout =
      catalog->CreateDefaultLayout(database_oid, table_oid, default_map, txn);
  txn_manager.CommitTransaction(txn);
  EXPECT_NE(nullptr, default_layout);

  // Create additional layout.
  std::map<oid_t, std::pair<oid_t, oid_t>> non_default_map;
  non_default_map[0] = std::make_pair(0, 0);
  non_default_map[1] = std::make_pair(0, 1);
  non_default_map[2] = std::make_pair(1, 0);
  non_default_map[3] = std::make_pair(1, 1);

  txn = txn_manager.BeginTransaction();
  auto other_layout =
      catalog->CreateLayout(database_oid, table_oid, non_default_map, txn);
  txn_manager.CommitTransaction(txn);

  // Check that the default layout is still the same.
  EXPECT_NE(*other_layout.get(), table->GetDefaultLayout());

  // Drop the default layout.
  auto default_layout_oid = default_layout->GetOid();
  txn = txn_manager.BeginTransaction();
  EXPECT_EQ(ResultType::SUCCESS, catalog->DropLayout(database_oid, table_oid,
                                                     default_layout_oid, txn));
  txn_manager.CommitTransaction(txn);

  // Check that default layout is reset and set to row_store.
  EXPECT_NE(*default_layout.get(), table->GetDefaultLayout());
  EXPECT_TRUE(table->GetDefaultLayout().IsRowStore());

  // Query pg_layout to ensure that the entry is dropped
  txn = txn_manager.BeginTransaction();
  auto pg_layout = catalog->GetSystemCatalogs(database_oid)->GetLayoutCatalog();
  EXPECT_EQ(nullptr,
            pg_layout->GetLayoutWithOid(table_oid, default_layout_oid, txn));

  // The additional layout must be present in pg_layout
  auto other_layout_oid = other_layout->GetOid();
  EXPECT_EQ(
      *(other_layout.get()),
      *(pg_layout->GetLayoutWithOid(table_oid, other_layout_oid, txn).get()));
  txn_manager.CommitTransaction(txn);

  // Drop database
  txn = txn_manager.BeginTransaction();
  catalog->DropDatabaseWithName(db_name, txn);
  txn_manager.CommitTransaction(txn);
}
Beispiel #5
0
TEST_F(CatalogTests, DroppingTable) {
  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  auto txn = txn_manager.BeginTransaction();
  auto catalog = catalog::Catalog::GetInstance();
  // NOTE: everytime we create a database, there will be 9 catalog tables
  // inside. Additionally, we create 3 tables for the test.
  oid_t expected_table_count = CATALOG_TABLES_COUNT + 3;
  EXPECT_EQ(
      expected_table_count,
      (int)catalog->GetDatabaseObject("emp_db", txn)->GetTableObjects().size());
  auto database_object =
      catalog::Catalog::GetInstance()->GetDatabaseObject("emp_db", txn);
  EXPECT_NE(nullptr, database_object);
  catalog::Catalog::GetInstance()->DropTable("emp_db", DEFAULT_SCHEMA_NAME,
                                             "department_table", txn);

  database_object =
      catalog::Catalog::GetInstance()->GetDatabaseObject("emp_db", txn);
  EXPECT_NE(nullptr, database_object);
  auto department_table_object =
      database_object->GetTableObject("department_table", DEFAULT_SCHEMA_NAME);
  // Decrement expected_table_count to account for the dropped table.
  expected_table_count--;
  EXPECT_EQ(
      expected_table_count,
      (int)catalog->GetDatabaseObject("emp_db", txn)->GetTableObjects().size());
  txn_manager.CommitTransaction(txn);

  EXPECT_EQ(nullptr, department_table_object);

  // Try to drop again
  txn = txn_manager.BeginTransaction();
  EXPECT_THROW(catalog::Catalog::GetInstance()->DropTable(
                   "emp_db", DEFAULT_SCHEMA_NAME, "department_table", txn),
               CatalogException);
  EXPECT_EQ(
      expected_table_count,
      (int)catalog->GetDatabaseObject("emp_db", txn)->GetTableObjects().size());
  txn_manager.CommitTransaction(txn);

  // Drop a table that does not exist
  txn = txn_manager.BeginTransaction();
  EXPECT_THROW(catalog::Catalog::GetInstance()->DropTable(
                   "emp_db", DEFAULT_SCHEMA_NAME, "void_table", txn),
               CatalogException);
  EXPECT_EQ(
      expected_table_count,
      (int)catalog->GetDatabaseObject("emp_db", txn)->GetTableObjects().size());
  txn_manager.CommitTransaction(txn);

  // Drop the other table
  txn = txn_manager.BeginTransaction();
  catalog::Catalog::GetInstance()->DropTable("emp_db", DEFAULT_SCHEMA_NAME,
                                             "emp_table", txn);
  // Account for the dropped table.
  expected_table_count--;
  EXPECT_EQ(
      expected_table_count,
      (int)catalog->GetDatabaseObject("emp_db", txn)->GetTableObjects().size());
  txn_manager.CommitTransaction(txn);
}