Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit ba59937

Browse files
dasdipanjan04apavlo
authored andcommitted
DropIndex By Index Name Implementation
Implemeted the requested changes (formatting chages and proper implemetation of drop_test.cpp) by @poojanilangekar. The same implemetation can also soleve the issue: #285
1 parent 2d3ff6e commit ba59937

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

src/catalog/catalog.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ ResultType Catalog::CreateIndex(const std::string &database_name,
374374
if (txn == nullptr)
375375
throw CatalogException("Do not have transaction to create database " +
376376
index_name);
377+
377378
LOG_TRACE("Trying to create index %s for table %s", index_name.c_str(),
378379
table_name.c_str());
379380
// check if database exists
@@ -609,21 +610,23 @@ ResultType Catalog::DropIndex(oid_t index_oid,
609610
}
610611

611612
ResultType Catalog::DropIndex(const std::string &index_name,
612-
concurrency::TransactionContext *txn){
613-
if(txn == nullptr){
614-
throw CatalogException("Do not have transaction to drop index " + index_name);
613+
concurrency::TransactionContext *txn) {
614+
if(txn == nullptr) {
615+
throw CatalogException("Do not have transaction to drop index " +
616+
index_name);
615617
}
616-
auto index_object = catalog::IndexCatalog::GetInstance()->GetIndexObject(index_name,txn);
617-
if(index_object == nullptr){
618+
auto index_object = catalog::IndexCatalog::GetInstance()->GetIndexObject(
619+
index_name, txn);
620+
if(index_object == nullptr) {
618621
throw CatalogException("Index name " + index_name + " cannot be found");
619622
}
620-
ResultType result = DropIndex(index_object->GetIndexOid(),txn);
623+
ResultType result = DropIndex(index_object->GetIndexOid(), txn);
621624

622625
return result;
623626
}
624627

625628
//===--------------------------------------------------------------------===//
626-
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION //
629+
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION
627630
//===--------------------------------------------------------------------===//
628631

629632
/* Check database from pg_database with database_name using txn,

src/include/catalog/catalog.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ class Catalog {
178178
concurrency::TransactionContext *txn);
179179

180180

181-
182181
//===--------------------------------------------------------------------===//
183182
// DEPRECATED FUNCTIONS
184183
//===--------------------------------------------------------------------===//

test/executor/drop_test.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cstdio>
1414

1515
#include "gtest/gtest.h"
16+
1617
#include "catalog/index_catalog.h"
1718
#include "catalog/catalog.h"
1819
#include "catalog/database_catalog.h"
@@ -224,46 +225,62 @@ TEST_F(DropTests, DroppingIndexByName) {
224225
catalog->Bootstrap();
225226

226227
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
227-
228228
auto txn = txn_manager.BeginTransaction();
229+
229230
catalog->CreateDatabase(TEST_DB_NAME, txn);
230231
auto db = catalog->GetDatabaseWithName(TEST_DB_NAME, txn);
231-
232232
// Insert a table first
233233
auto id_column = catalog::Column(
234234
type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
235235
"dept_id", true);
236236
auto name_column =
237237
catalog::Column(type::TypeId::VARCHAR, 32, "dept_name", false);
238238

239-
240239
std::unique_ptr<catalog::Schema> table_schema(
241240
new catalog::Schema({id_column, name_column}));
242241
std::unique_ptr<catalog::Schema> table_schema2(
243242
new catalog::Schema({id_column, name_column}));
243+
txn_manager.CommitTransaction(txn);
244244

245-
catalog->CreateTable(TEST_DB_NAME, "department_table_01", std::move(table_schema),
246-
txn);
245+
txn = txn_manager.BeginTransaction();
246+
catalog->CreateTable(TEST_DB_NAME, "department_table_01",
247+
std::move(table_schema), txn);
248+
txn_manager.CommitTransaction(txn);
247249

250+
txn = txn_manager.BeginTransaction();
248251
auto source_table = db->GetTableWithName("department_table_01");
249-
oid_t col_id = source_table->GetSchema()->GetColumnID(id_column.column_name);
252+
oid_t col_id = source_table->GetSchema()->GetColumnID(
253+
id_column.column_name);
250254
std::vector<oid_t> source_col_ids;
251255
source_col_ids.push_back(col_id);
252-
std::string index_name1 = "pg_table_pkey";
256+
std::string index_name1 = "Testing_Drop_Index_By_Name";
253257
catalog->CreateIndex(TEST_DB_NAME, "department_table_01",
254258
source_col_ids, index_name1, false,
255259
IndexType::BWTREE, txn);
260+
txn_manager.CommitTransaction(txn);
256261

257-
auto index_catalog = catalog::IndexCatalog::GetInstance()->GetIndexObject(index_name1, txn);
258-
auto index_oid = index_catalog->GetIndexOid();
259-
catalog->DropIndex(index_oid,txn);
262+
txn = txn_manager.BeginTransaction();
263+
auto index_catalog = catalog::IndexCatalog::GetInstance()->GetIndexObject(
264+
index_name1, txn);
265+
// Check the effect of drop
266+
// Most major check in this test case
267+
EXPECT_EQ(catalog::IndexCatalog::GetInstance()->GetIndexObject(
268+
index_name1, txn), index_catalog);
260269

261-
std::string index_name2 = "pg_database_pkey";
262-
catalog->CreateIndex(TEST_DB_NAME, "department_table_01",
263-
source_col_ids, index_name2, false,
264-
IndexType::BWTREE, txn);
265-
catalog->DropIndex(index_name2,txn);
270+
// Now dropping the index using the DropIndex functionality
271+
catalog->DropIndex(index_name1,txn);
272+
EXPECT_EQ(catalog::IndexCatalog::GetInstance()->GetIndexObject(
273+
index_name1, txn), nullptr);
274+
txn_manager.CommitTransaction(txn);
266275

276+
// Drop the table just created
277+
txn = txn_manager.BeginTransaction();
278+
catalog->DropTable(TEST_DB_NAME, "department_table_01", txn);
279+
txn_manager.CommitTransaction(txn);
280+
281+
// free the database just created
282+
txn = txn_manager.BeginTransaction();
283+
catalog->DropDatabaseWithName(TEST_DB_NAME, txn);
267284
txn_manager.CommitTransaction(txn);
268285
}
269286
} // namespace test

0 commit comments

Comments
 (0)