Skip to content

Commit 27f3cfc

Browse files
committed
fix: review comments
1 parent 29efd5a commit 27f3cfc

File tree

3 files changed

+28
-44
lines changed

3 files changed

+28
-44
lines changed

src/server/search/doc_index.cc

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,6 @@ size_t ShardDocIndex::DocKeyIndex::Size() const {
242242
return ids_.size();
243243
}
244244

245-
std::vector<std::string> ShardDocIndex::DocKeyIndex::GetAllKeys() const {
246-
std::vector<std::string> keys;
247-
keys.reserve(ids_.size());
248-
249-
for (const auto& [key, id] : ids_) {
250-
if (!key.empty()) {
251-
keys.push_back(key);
252-
}
253-
}
254-
255-
return keys;
256-
}
257-
258245
uint8_t DocIndex::GetObjCode() const {
259246
return type == JSON ? OBJ_JSON : OBJ_HASH;
260247
}
@@ -620,10 +607,6 @@ DocIndexInfo ShardDocIndex::GetInfo() const {
620607
return {*base_, key_index_.Size()};
621608
}
622609

623-
std::vector<std::string> ShardDocIndex::GetAllKeys() const {
624-
return key_index_.GetAllKeys();
625-
}
626-
627610
io::Result<StringVec, ErrorReply> ShardDocIndex::GetTagVals(string_view field) const {
628611
search::BaseIndex* base_index = indices_->GetIndex(field);
629612
if (base_index == nullptr) {
@@ -662,15 +645,16 @@ void ShardDocIndices::InitIndex(const OpArgs& op_args, std::string_view name,
662645
});
663646
}
664647

665-
bool ShardDocIndices::DropIndex(string_view name) {
648+
unique_ptr<ShardDocIndex> ShardDocIndices::DropIndex(string_view name) {
666649
auto it = indices_.find(name);
667650
if (it == indices_.end())
668-
return false;
651+
return nullptr;
669652

670653
DropIndexCache(*it->second);
654+
auto index = std::move(it->second);
671655
indices_.erase(it);
672656

673-
return true;
657+
return index;
674658
}
675659

676660
void ShardDocIndices::DropAllIndices() {

src/server/search/doc_index.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,10 @@ class ShardDocIndex {
224224
std::string_view Get(DocId id) const;
225225
size_t Size() const;
226226

227-
// Get all keys in the index
228-
std::vector<std::string> GetAllKeys() const;
227+
// Get const reference to the internal ids map
228+
const absl::flat_hash_map<std::string, DocId>& GetDocKeysMap() const {
229+
return ids_;
230+
}
229231

230232
private:
231233
absl::flat_hash_map<std::string, DocId> ids_;
@@ -265,9 +267,6 @@ class ShardDocIndex {
265267

266268
DocIndexInfo GetInfo() const;
267269

268-
// Get all document keys in this index
269-
std::vector<std::string> GetAllKeys() const;
270-
271270
io::Result<StringVec, facade::ErrorReply> GetTagVals(std::string_view field) const;
272271

273272
// Get synonym manager for this shard
@@ -283,6 +282,11 @@ class ShardDocIndex {
283282
void RebuildForGroup(const OpArgs& op_args, const std::string_view& group_id,
284283
const std::vector<std::string_view>& terms);
285284

285+
// Public access to key index for direct operations (e.g., when dropping index with DD)
286+
const DocKeyIndex& key_index() const {
287+
return key_index_;
288+
}
289+
286290
private:
287291
// Clears internal data. Traverses all matching documents and assigns ids.
288292
void Rebuild(const OpArgs& op_args, PMR_NS::memory_resource* mr);
@@ -315,8 +319,8 @@ class ShardDocIndices {
315319
void InitIndex(const OpArgs& op_args, std::string_view name,
316320
std::shared_ptr<const DocIndex> index);
317321

318-
// Drop index, return true if it existed and was dropped
319-
bool DropIndex(std::string_view name);
322+
// Drop index, return the dropped index if it existed or nullptr otherwise
323+
std::unique_ptr<ShardDocIndex> DropIndex(std::string_view name);
320324

321325
// Drop all indices
322326
void DropAllIndices();

src/server/search/search_family.cc

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,8 @@ void SearchFamily::FtAlter(CmdArgList args, const CommandContext& cmd_cntx) {
11201120
// Rebuild index
11211121
// TODO: Introduce partial rebuild
11221122
auto upd_cb = [idx_name, index_info](Transaction* tx, EngineShard* es) {
1123-
es->search_indices()->DropIndex(idx_name);
1123+
// Drop the old index (we don't need the documents)
1124+
(void)es->search_indices()->DropIndex(idx_name);
11241125
es->search_indices()->InitIndex(tx->GetOpArgs(es), idx_name, index_info);
11251126
return OpStatus::OK;
11261127
};
@@ -1142,31 +1143,26 @@ void SearchFamily::FtDropIndex(CmdArgList args, const CommandContext& cmd_cntx)
11421143
}
11431144
}
11441145

1145-
// Collect document keys (if DD is set), drop index, and delete documents in single transaction
1146-
vector<vector<string>> shard_doc_keys(delete_docs ? shard_set->size() : 0);
11471146
atomic_uint num_deleted{0};
11481147

11491148
auto cb = [&](Transaction* t, EngineShard* es) {
1150-
// Step 1: If DD is set, collect all document keys before dropping the index
1151-
if (delete_docs) {
1152-
auto* index = es->search_indices()->GetIndex(idx_name);
1153-
if (index) {
1154-
shard_doc_keys[es->shard_id()] = index->GetAllKeys();
1155-
}
1156-
}
1149+
// Drop the index and get its pointer
1150+
auto index = es->search_indices()->DropIndex(idx_name);
1151+
if (!index)
1152+
return OpStatus::OK;
11571153

1158-
// Step 2: Drop the index
1159-
if (es->search_indices()->DropIndex(idx_name))
1160-
num_deleted.fetch_add(1);
1154+
num_deleted.fetch_add(1);
11611155

1162-
// Step 3: If DD is set, delete all documents that were in the index
1156+
// If DD is set, delete all documents that were in the index
11631157
if (delete_docs) {
1164-
const auto& keys = shard_doc_keys[es->shard_id()];
1165-
if (!keys.empty()) {
1158+
// Get const reference to document keys map (index will be destroyed after this scope)
1159+
const auto& doc_keys = index->key_index().GetDocKeysMap();
1160+
1161+
if (!doc_keys.empty()) {
11661162
auto op_args = t->GetOpArgs(es);
11671163
auto& db_slice = op_args.GetDbSlice();
11681164

1169-
for (const auto& key : keys) {
1165+
for (const auto& [key, doc_id] : doc_keys) {
11701166
auto it = db_slice.FindMutable(op_args.db_cntx, key).it;
11711167
if (IsValid(it)) {
11721168
db_slice.Del(op_args.db_cntx, it);

0 commit comments

Comments
 (0)