Skip to content

Commit

Permalink
Refactor index optimize
Browse files Browse the repository at this point in the history
Signed-off-by: Jin Hai <[email protected]>
  • Loading branch information
JinHai-CN committed Jan 26, 2025
1 parent c2812e9 commit c132286
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/executor/operator/physical_optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import infinity_context;

namespace infinity {

void PhysicalOptimize::Init(QueryContext* query_context) {}
void PhysicalOptimize::Init(QueryContext *query_context) {}

bool PhysicalOptimize::Execute(QueryContext *query_context, OperatorState *operator_state) {
StorageMode storage_mode = InfinityContext::instance().storage()->GetStorageMode();
Expand Down Expand Up @@ -65,27 +65,26 @@ void PhysicalOptimize::OptimizeIndex(QueryContext *query_context, OperatorState
// Get tables from catalog
LOG_INFO(fmt::format("OptimizeIndex {}.{} begin", db_name_, table_name_));
auto txn = query_context->GetTxn();
auto [table_entry, table_status] = txn->GetTableByName(db_name_, table_name_);

if (!table_status.ok()) {
operator_state->status_ = table_status;
RecoverableError(table_status);
Status status = txn->OptimizeIndexes(db_name_, table_name_);
if (!status.ok()) {
operator_state->status_ = status;
RecoverableError(status);
return;
}

table_entry->OptimizeIndex(txn);
LOG_INFO(fmt::format("OptimizeIndex {}.{} end", db_name_, table_name_));
}

void PhysicalOptimize::OptIndex(QueryContext *query_context, OperatorState *operator_state) {
LOG_INFO(fmt::format("OptimizeIndex {}.{}::{} begin", db_name_, table_name_, index_name_));
auto txn = query_context->GetTxn();
auto [table_index_entry, status] = txn->GetIndexByName(db_name_, table_name_, index_name_);
Status status = txn->OptimizeIndexByName(db_name_, table_name_, index_name_, std::move(opt_params_));
if (!status.ok()) {
operator_state->status_ = status;
RecoverableError(status);
return;
}
txn->OptIndex(table_index_entry, std::move(opt_params_));
LOG_INFO(fmt::format("OptimizeIndex {}.{}::{} end", db_name_, table_name_, index_name_));
}

} // namespace infinity
19 changes: 19 additions & 0 deletions src/storage/txn/txn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ Txn::Compact(TableEntry *table_entry, Vector<Pair<SharedPtr<SegmentEntry>, Vecto
return compact_status;
}

Status Txn::OptimizeIndexes(const String &db_name, const String &table_name) {
auto [table_entry, table_status] = this->GetTableByName(db_name, table_name);
if (!table_status.ok()) {
return status;
}
table_entry->OptimizeIndex(this);
return Status::OK();
}

Status
Txn::OptimizeIndexByName(const String &db_name, const String &table_name, const String &index_name, Vector<UniquePtr<InitParameter>> init_params) {
auto [table_index_entry, status] = this->GetIndexByName(db_name, table_name, index_name);
if (!status.ok()) {
return status;
}
this->OptIndex(table_index_entry, std::move(init_params));
}


Status Txn::OptIndex(TableIndexEntry *table_index_entry, Vector<UniquePtr<InitParameter>> init_params) {
TableEntry *table_entry = table_index_entry->table_index_meta()->table_entry();
TxnTableStore *txn_table_store = this->GetTxnTableStore(table_entry);
Expand Down
4 changes: 4 additions & 0 deletions src/storage/txn/txn.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ public:

Status Compact(TableEntry *table_entry, Vector<Pair<SharedPtr<SegmentEntry>, Vector<SegmentEntry *>>> &&segment_data, CompactStatementType type);

// Internal operation
Status OptimizeIndexes(const String &db_name, const String &table_name);
Status
OptimizeIndexByName(const String &db_name, const String &table_name, const String &index_name, Vector<UniquePtr<InitParameter>> init_params);
Status OptIndex(TableIndexEntry *table_index_entry, Vector<UniquePtr<InitParameter>> init_params);

// Getter
Expand Down

0 comments on commit c132286

Please sign in to comment.