@@ -17,15 +17,14 @@ namespace brain {
1717
1818CompressedIndexConfigContainer::CompressedIndexConfigContainer (
1919 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,
2121 concurrency::TransactionManager *txn_manager)
2222 : database_name_{database_name},
23- run_mode_{run_mode},
2423 catalog_{catalog},
2524 txn_manager_{txn_manager},
2625 next_table_offset_{0 },
2726 cur_index_config_{nullptr } {
28- if (nullptr == catalog_ ) {
27+ if (catalog_ == nullptr ) {
2928 catalog_ = catalog::Catalog::GetInstance ();
3029 catalog_->Bootstrap ();
3130 }
@@ -37,7 +36,7 @@ CompressedIndexConfigContainer::CompressedIndexConfigContainer(
3736 auto txn = txn_manager_->BeginTransaction ();
3837
3938 const auto db_obj = catalog_->GetDatabaseObject (database_name_, txn);
40- const auto db_oid = db_obj->GetDatabaseOid ();
39+ database_oid_ = db_obj->GetDatabaseOid ();
4140 const auto table_objs = db_obj->GetTableObjects ();
4241
4342 // Uniq identifier per index config
@@ -86,14 +85,12 @@ CompressedIndexConfigContainer::CompressedIndexConfigContainer(
8685 } else {
8786 for (const auto &index_obj : index_objs) {
8887 const auto &indexed_cols = index_obj.second ->GetKeyAttrs ();
89- const auto index_oid = index_obj.first ;
9088
9189 std::vector<oid_t > col_oids (indexed_cols);
9290 auto idx_obj = std::make_shared<brain::HypotheticalIndexObject>(
93- db_oid , table_oid, col_oids);
91+ database_oid_ , table_oid, col_oids);
9492
9593 const auto global_index_offset = GetGlobalOffset (idx_obj);
96- offset_to_indexoid_[global_index_offset] = index_oid;
9794
9895 SetBit (global_index_offset);
9996 }
@@ -124,39 +121,26 @@ void CompressedIndexConfigContainer::EnumerateConfigurations(
124121 }
125122}
126123
124+ // TODO: Add HypotheticalIndexObject set to Add/Drop index RPC call here
127125void 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+
129130 boost::dynamic_bitset<> &ori_bitset = *cur_index_config_;
130131
131132 const auto drop_bitset = ori_bitset - new_bitset;
132133
133- auto txn = txn_manager_->BeginTransaction ();
134- const auto database_oid =
135- catalog_->GetDatabaseObject (database_name_, txn)->GetDatabaseOid ();
136134 for (size_t current_bit = drop_bitset.find_first ();
137135 current_bit != boost::dynamic_bitset<>::npos;
138136 current_bit = drop_bitset.find_next (current_bit)) {
139137 // 1. unset current bit
140138 UnsetBit (current_bit);
141139
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+
158143 }
159- txn_manager_->CommitTransaction (txn);
160144
161145 const auto add_bitset = new_bitset - ori_bitset;
162146
@@ -166,45 +150,8 @@ void CompressedIndexConfigContainer::AdjustIndexes(
166150 // 1. set current bit
167151 SetBit (current_bit);
168152
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));
208155 }
209156}
210157
@@ -234,7 +181,12 @@ void CompressedIndexConfigContainer::UnsetBit(size_t offset) {
234181size_t CompressedIndexConfigContainer::GetGlobalOffset (
235182 const std::shared_ptr<brain::HypotheticalIndexObject> &index_obj) const {
236183 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+
238190}
239191
240192bool CompressedIndexConfigContainer::IsSet (
@@ -249,53 +201,14 @@ bool CompressedIndexConfigContainer::IsSet(const size_t offset) const {
249201
250202std::shared_ptr<brain::HypotheticalIndexObject>
251203CompressedIndexConfigContainer::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);
267205 std::vector<oid_t > col_oids =
268206 indexid_table_map_.at (table_oid).at (global_offset);
269207
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,
276209 col_oids);
277210}
278211
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-
299212size_t CompressedIndexConfigContainer::GetConfigurationCount () const {
300213 return next_table_offset_;
301214}
@@ -329,6 +242,18 @@ size_t CompressedIndexConfigContainer::GetTableOffsetEnd(
329242 return GetNextTableIdx (start_idx);
330243}
331244
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+
332257size_t CompressedIndexConfigContainer::GetNextTableIdx (size_t start_idx) const {
333258 auto next_tbl_offset_iter = table_offset_reverse_map_.upper_bound (start_idx);
334259 if (next_tbl_offset_iter == table_offset_reverse_map_.end ()) {
0 commit comments