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

Commit 93e4527

Browse files
dasdipanjan04apavlo
authored andcommitted
Implement DropIndex
DropIndex by Index Name method implemented in Catalog class. Test Added for DropIndex
1 parent 93f5e0e commit 93e4527

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

src/catalog/catalog.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,8 @@ 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-
378377
LOG_TRACE("Trying to create index %s for table %s", index_name.c_str(),
379378
table_name.c_str());
380-
381379
// check if database exists
382380
auto database_object =
383381
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
@@ -393,11 +391,9 @@ ResultType Catalog::CreateIndex(const std::string &database_name,
393391

394392
IndexConstraintType index_constraint =
395393
unique_keys ? IndexConstraintType::UNIQUE : IndexConstraintType::DEFAULT;
396-
397394
ResultType success = CreateIndex(
398395
database_object->GetDatabaseOid(), table_object->GetTableOid(), key_attrs,
399396
index_name, index_type, index_constraint, unique_keys, txn);
400-
401397
return success;
402398
}
403399

@@ -612,8 +608,22 @@ ResultType Catalog::DropIndex(oid_t index_oid,
612608
return ResultType::SUCCESS;
613609
}
614610

611+
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);
615+
}
616+
auto index_object = catalog::IndexCatalog::GetInstance()->GetIndexObject(index_name,txn);
617+
if(index_object == nullptr){
618+
throw CatalogException("Index name " + index_name + " cannot be found");
619+
}
620+
ResultType result = DropIndex(index_object->GetIndexOid(),txn);
621+
622+
return result;
623+
}
624+
615625
//===--------------------------------------------------------------------===//
616-
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION
626+
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION //
617627
//===--------------------------------------------------------------------===//
618628

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

src/include/catalog/catalog.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace catalog {
2323
class Schema;
2424
class DatabaseCatalogObject;
2525
class TableCatalogObject;
26+
class IndexCatalogObject;
2627
} // namespace catalog
2728

2829
namespace concurrency {
@@ -134,6 +135,9 @@ class Catalog {
134135
// Drop an index, using its index_oid
135136
ResultType DropIndex(oid_t index_oid, concurrency::TransactionContext *txn);
136137

138+
// Drop an index, using its index name
139+
ResultType DropIndex(const std::string &index_name,
140+
concurrency::TransactionContext *txn);
137141
//===--------------------------------------------------------------------===//
138142
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION
139143
//===--------------------------------------------------------------------===//
@@ -173,6 +177,11 @@ class Catalog {
173177
oid_t database_oid, oid_t table_oid,
174178
concurrency::TransactionContext *txn);
175179

180+
181+
182+
std::shared_ptr<IndexCatalogObject> GetIndexObject(
183+
const std::string &database_name, const std::string &index_name, const std::string &table_name,
184+
concurrency::TransactionContext *txn);
176185
//===--------------------------------------------------------------------===//
177186
// DEPRECATED FUNCTIONS
178187
//===--------------------------------------------------------------------===//

test/executor/drop_test.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <cstdio>
1414

1515
#include "gtest/gtest.h"
16-
16+
#include "catalog/index_catalog.h"
1717
#include "catalog/catalog.h"
1818
#include "catalog/database_catalog.h"
1919
#include "common/harness.h"
@@ -219,5 +219,52 @@ TEST_F(DropTests, DroppingTrigger) {
219219
txn_manager.CommitTransaction(txn);
220220
}
221221

222+
TEST_F(DropTests, DroppingIndexByName) {
223+
auto catalog = catalog::Catalog::GetInstance();
224+
catalog->Bootstrap();
225+
226+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
227+
228+
auto txn = txn_manager.BeginTransaction();
229+
catalog->CreateDatabase(TEST_DB_NAME, txn);
230+
auto db = catalog->GetDatabaseWithName(TEST_DB_NAME, txn);
231+
232+
// Insert a table first
233+
auto id_column = catalog::Column(
234+
type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
235+
"dept_id", true);
236+
auto name_column =
237+
catalog::Column(type::TypeId::VARCHAR, 32, "dept_name", false);
238+
239+
240+
std::unique_ptr<catalog::Schema> table_schema(
241+
new catalog::Schema({id_column, name_column}));
242+
std::unique_ptr<catalog::Schema> table_schema2(
243+
new catalog::Schema({id_column, name_column}));
244+
245+
catalog->CreateTable(TEST_DB_NAME, "department_table_01", std::move(table_schema),
246+
txn);
247+
248+
auto source_table = db->GetTableWithName("department_table_01");
249+
oid_t col_id = source_table->GetSchema()->GetColumnID(id_column.column_name);
250+
std::vector<oid_t> source_col_ids;
251+
source_col_ids.push_back(col_id);
252+
std::string index_name1 = "pg_table_pkey";
253+
catalog->CreateIndex(TEST_DB_NAME, "department_table_01",
254+
source_col_ids, index_name1, false,
255+
IndexType::BWTREE, txn);
256+
257+
auto index_catalog = catalog::IndexCatalog::GetInstance()->GetIndexObject(index_name1, txn);
258+
auto index_oid = index_catalog->GetIndexOid();
259+
catalog->DropIndex(index_oid,txn);
260+
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);
266+
267+
txn_manager.CommitTransaction(txn);
268+
}
222269
} // namespace test
223270
} // namespace peloton

0 commit comments

Comments
 (0)