@@ -17,15 +17,14 @@ namespace brain {
17
17
18
18
CompressedIndexConfigContainer::CompressedIndexConfigContainer (
19
19
const std::string &database_name, const std::set<oid_t > &ignore_table_oids,
20
- size_t max_index_size, RunMode run_mode, catalog::Catalog *catalog,
20
+ size_t max_index_size, catalog::Catalog *catalog,
21
21
concurrency::TransactionManager *txn_manager)
22
22
: database_name_{database_name},
23
- run_mode_{run_mode},
24
23
catalog_{catalog},
25
24
txn_manager_{txn_manager},
26
25
next_table_offset_{0 },
27
26
cur_index_config_{nullptr } {
28
- if (nullptr == catalog_ ) {
27
+ if (catalog_ == nullptr ) {
29
28
catalog_ = catalog::Catalog::GetInstance ();
30
29
catalog_->Bootstrap ();
31
30
}
@@ -37,7 +36,7 @@ CompressedIndexConfigContainer::CompressedIndexConfigContainer(
37
36
auto txn = txn_manager_->BeginTransaction ();
38
37
39
38
const auto db_obj = catalog_->GetDatabaseObject (database_name_, txn);
40
- const auto db_oid = db_obj->GetDatabaseOid ();
39
+ database_oid_ = db_obj->GetDatabaseOid ();
41
40
const auto table_objs = db_obj->GetTableObjects ();
42
41
43
42
// Uniq identifier per index config
@@ -86,14 +85,12 @@ CompressedIndexConfigContainer::CompressedIndexConfigContainer(
86
85
} else {
87
86
for (const auto &index_obj : index_objs) {
88
87
const auto &indexed_cols = index_obj.second ->GetKeyAttrs ();
89
- const auto index_oid = index_obj.first ;
90
88
91
89
std::vector<oid_t > col_oids (indexed_cols);
92
90
auto idx_obj = std::make_shared<brain::HypotheticalIndexObject>(
93
- db_oid , table_oid, col_oids);
91
+ database_oid_ , table_oid, col_oids);
94
92
95
93
const auto global_index_offset = GetGlobalOffset (idx_obj);
96
- offset_to_indexoid_[global_index_offset] = index_oid;
97
94
98
95
SetBit (global_index_offset);
99
96
}
@@ -124,39 +121,26 @@ void CompressedIndexConfigContainer::EnumerateConfigurations(
124
121
}
125
122
}
126
123
124
+ // TODO: Add HypotheticalIndexObject set to Add/Drop index RPC call here
127
125
void CompressedIndexConfigContainer::AdjustIndexes (
128
- const boost::dynamic_bitset<> &new_bitset) {
126
+ const boost::dynamic_bitset<> &new_bitset,
127
+ std::set<std::shared_ptr<brain::HypotheticalIndexObject>>& add_set,
128
+ std::set<std::shared_ptr<brain::HypotheticalIndexObject>>& drop_set) {
129
+
129
130
boost::dynamic_bitset<> &ori_bitset = *cur_index_config_;
130
131
131
132
const auto drop_bitset = ori_bitset - new_bitset;
132
133
133
- auto txn = txn_manager_->BeginTransaction ();
134
- const auto database_oid =
135
- catalog_->GetDatabaseObject (database_name_, txn)->GetDatabaseOid ();
136
134
for (size_t current_bit = drop_bitset.find_first ();
137
135
current_bit != boost::dynamic_bitset<>::npos;
138
136
current_bit = drop_bitset.find_next (current_bit)) {
139
137
// 1. unset current bit
140
138
UnsetBit (current_bit);
141
139
142
- // Current bit is not an empty index (empty set)
143
- if (run_mode_ == RunMode::ActualRun &&
144
- table_offset_reverse_map_.find (current_bit) ==
145
- table_offset_reverse_map_.end ()) {
146
- // 2. drop its corresponding index in catalog
147
- oid_t index_oid = offset_to_indexoid_.at (current_bit);
148
- // TODO (weichenl): This will call into the storage manager and delete the
149
- // index in the real table storage, which we don't have on the brain side.
150
- // We need a way to only delete the entry in the catalog table, and then
151
- // issue a RPC call to let Peloton server really drop the index (using
152
- // this DropIndex method).
153
- catalog_->DropIndex (database_oid, index_oid, txn);
154
-
155
- // 3. erase its entry in the maps
156
- offset_to_indexoid_.erase (current_bit);
157
- }
140
+ // 2. add to the drop_set
141
+ drop_set.insert (GetIndex (current_bit));
142
+
158
143
}
159
- txn_manager_->CommitTransaction (txn);
160
144
161
145
const auto add_bitset = new_bitset - ori_bitset;
162
146
@@ -166,45 +150,8 @@ void CompressedIndexConfigContainer::AdjustIndexes(
166
150
// 1. set current bit
167
151
SetBit (current_bit);
168
152
169
- // Current bit is not an empty index (empty set)
170
- if (run_mode_ == RunMode::ActualRun &&
171
- table_offset_reverse_map_.find (current_bit) ==
172
- table_offset_reverse_map_.end ()) {
173
- txn = txn_manager_->BeginTransaction ();
174
-
175
- // 2. add its corresponding index in catalog
176
- const auto new_index = GetIndex (current_bit);
177
- const auto table_name = catalog_->GetDatabaseObject (database_name_, txn)
178
- ->GetTableObject (new_index->table_oid )
179
- ->GetTableName ();
180
-
181
- std::set<oid_t > temp_oids (new_index->column_oids .begin (),
182
- new_index->column_oids .end ());
183
-
184
- std::vector<oid_t > index_vector (temp_oids.begin (), temp_oids.end ());
185
-
186
- std::ostringstream stringStream;
187
- stringStream << " automated_index_" << current_bit;
188
- const std::string temp_index_name = stringStream.str ();
189
-
190
- catalog_->CreateIndex (database_name_, DEFAULT_SCHEMA_NAME, table_name,
191
- index_vector, temp_index_name, false ,
192
- IndexType::BWTREE, txn);
193
-
194
- txn_manager_->CommitTransaction (txn);
195
-
196
- txn = txn_manager_->BeginTransaction ();
197
-
198
- // 3. insert its entry in the maps
199
- const auto index_object = catalog_->GetDatabaseObject (database_name_, txn)
200
- ->GetTableObject (new_index->table_oid )
201
- ->GetIndexObject (temp_index_name);
202
- const auto index_oid = index_object->GetIndexOid ();
203
-
204
- txn_manager_->CommitTransaction (txn);
205
-
206
- offset_to_indexoid_[current_bit] = index_oid;
207
- }
153
+ // 2. add to add_set
154
+ add_set.insert (GetIndex (current_bit));
208
155
}
209
156
}
210
157
@@ -234,7 +181,12 @@ void CompressedIndexConfigContainer::UnsetBit(size_t offset) {
234
181
size_t CompressedIndexConfigContainer::GetGlobalOffset (
235
182
const std::shared_ptr<brain::HypotheticalIndexObject> &index_obj) const {
236
183
oid_t table_oid = index_obj->table_oid ;
237
- return table_indexid_map_.at (table_oid).at (index_obj->column_oids );
184
+ if (index_obj->column_oids .empty ()) {
185
+ return table_offset_map_.at (table_oid);
186
+ } else {
187
+ return table_indexid_map_.at (table_oid).at (index_obj->column_oids );
188
+ }
189
+
238
190
}
239
191
240
192
bool CompressedIndexConfigContainer::IsSet (
@@ -249,53 +201,14 @@ bool CompressedIndexConfigContainer::IsSet(const size_t offset) const {
249
201
250
202
std::shared_ptr<brain::HypotheticalIndexObject>
251
203
CompressedIndexConfigContainer::GetIndex (size_t global_offset) const {
252
- size_t table_offset;
253
- if (table_offset_reverse_map_.find (global_offset) ==
254
- table_offset_reverse_map_.end ()) {
255
- auto it = table_offset_reverse_map_.lower_bound (global_offset);
256
- if (it == table_offset_reverse_map_.end ()) {
257
- table_offset = table_offset_reverse_map_.rbegin ()->first ;
258
- } else {
259
- --it;
260
- table_offset = it->first ;
261
- }
262
- } else {
263
- table_offset = global_offset;
264
- }
265
-
266
- const oid_t table_oid = table_offset_reverse_map_.at (table_offset);
204
+ const oid_t table_oid = GetCurrentTableOID (global_offset);
267
205
std::vector<oid_t > col_oids =
268
206
indexid_table_map_.at (table_oid).at (global_offset);
269
207
270
- auto txn = txn_manager_->BeginTransaction ();
271
- const auto db_oid =
272
- catalog_->GetDatabaseObject (database_name_, txn)->GetDatabaseOid ();
273
- txn_manager_->CommitTransaction (txn);
274
-
275
- return std::make_shared<brain::HypotheticalIndexObject>(db_oid, table_oid,
208
+ return std::make_shared<brain::HypotheticalIndexObject>(database_oid_, table_oid,
276
209
col_oids);
277
210
}
278
211
279
- std::vector<oid_t > CompressedIndexConfigContainer::GetIndexColumns (
280
- size_t global_offset) const {
281
- size_t table_offset;
282
- if (table_offset_reverse_map_.find (global_offset) ==
283
- table_offset_reverse_map_.end ()) {
284
- auto it = table_offset_reverse_map_.lower_bound (global_offset);
285
- if (it == table_offset_reverse_map_.end ()) {
286
- table_offset = table_offset_reverse_map_.rbegin ()->first ;
287
- } else {
288
- --it;
289
- table_offset = it->first ;
290
- }
291
- } else {
292
- table_offset = global_offset;
293
- }
294
-
295
- const oid_t table_oid = table_offset_reverse_map_.at (table_offset);
296
- return indexid_table_map_.at (table_oid).at (global_offset);
297
- }
298
-
299
212
size_t CompressedIndexConfigContainer::GetConfigurationCount () const {
300
213
return next_table_offset_;
301
214
}
@@ -329,6 +242,18 @@ size_t CompressedIndexConfigContainer::GetTableOffsetEnd(
329
242
return GetNextTableIdx (start_idx);
330
243
}
331
244
245
+ oid_t CompressedIndexConfigContainer::GetCurrentTableOID (size_t idx) const {
246
+ auto gteq_iter = table_offset_reverse_map_.lower_bound (idx);
247
+ if (gteq_iter->first == idx) {
248
+ // Idx = Offset corresponding to table OID
249
+ return gteq_iter->second ;
250
+ } else {
251
+ // Idx = Offset corresponding to table OID one after the one we want
252
+ gteq_iter--;
253
+ return gteq_iter->second ;
254
+ }
255
+ }
256
+
332
257
size_t CompressedIndexConfigContainer::GetNextTableIdx (size_t start_idx) const {
333
258
auto next_tbl_offset_iter = table_offset_reverse_map_.upper_bound (start_idx);
334
259
if (next_tbl_offset_iter == table_offset_reverse_map_.end ()) {
0 commit comments