11
11
#include " base/logging.h"
12
12
#include " server/engine_shard_set.h"
13
13
#include " server/search/doc_accessors.h"
14
+ #include " server/server_state.h"
14
15
15
16
extern " C" {
16
17
#include " redis/object.h"
@@ -150,10 +151,13 @@ bool DocIndex::Matches(string_view key, unsigned obj_code) const {
150
151
}
151
152
152
153
ShardDocIndex::ShardDocIndex (shared_ptr<DocIndex> index)
153
- : base_{index}, indices_{index-> schema }, key_index_{} {
154
+ : base_{index}, indices_{{} }, key_index_{} {
154
155
}
155
156
156
- void ShardDocIndex::Init (const OpArgs& op_args) {
157
+ void ShardDocIndex::Rebuild (const OpArgs& op_args) {
158
+ key_index_ = DocKeyIndex{};
159
+ indices_ = search::FieldIndices{base_->schema };
160
+
157
161
auto cb = [this ](string_view key, BaseAccessor* doc) { indices_.Add (key_index_.Add (key), doc); };
158
162
TraverseAllMatching (*base_, op_args, cb);
159
163
}
@@ -213,7 +217,11 @@ void ShardDocIndices::InitIndex(const OpArgs& op_args, std::string_view name,
213
217
shared_ptr<DocIndex> index_ptr) {
214
218
auto shard_index = make_unique<ShardDocIndex>(index_ptr);
215
219
auto [it, _] = indices_.emplace (name, move (shard_index));
216
- it->second ->Init (op_args);
220
+
221
+ // Don't build while loading, shutting down, etc.
222
+ // After loading, indices are rebuilt separately
223
+ if (ServerState::tlocal ()->gstate () == GlobalState::ACTIVE)
224
+ it->second ->Rebuild (op_args);
217
225
218
226
op_args.shard ->db_slice ().SetDocDeletionCallback (
219
227
[this ](string_view key, const DbContext& cntx, const PrimeValue& pv) {
@@ -235,6 +243,11 @@ bool ShardDocIndices::DropIndex(string_view name) {
235
243
return true ;
236
244
}
237
245
246
+ void ShardDocIndices::RebuildAllIndices (const OpArgs& op_args) {
247
+ for (auto & [_, ptr] : indices_)
248
+ ptr->Rebuild (op_args);
249
+ }
250
+
238
251
vector<string> ShardDocIndices::GetIndexNames () const {
239
252
vector<string> names{};
240
253
names.reserve (indices_.size ());
0 commit comments