Skip to content

Commit c81a338

Browse files
committed
odb: Refactored. Added a new helper rebuildModuleHash() in dbBlock.cpp
Signed-off-by: Jaehyun Kim <[email protected]>
1 parent c2f344c commit c81a338

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

src/odb/src/db/dbBlock.cpp

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,44 @@ dbOStream& operator<<(dbOStream& stream, const _dbBlock& block)
859859
return stream;
860860
}
861861

862+
863+
/**
864+
* @brief Rebuilds the name-to-ID hash maps for hierarchical objects within
865+
* their respective modules.
866+
*
867+
* This function is used during database loading (operator>>) to restore the
868+
* fast-lookup hashes that were stored in the ODB.
869+
*
870+
* @tparam T The public ODB object type (e.g., dbInst, dbModNet).
871+
* @tparam T_impl The internal implementation type (e.g., _dbInst, _dbModNet).
872+
* @param block The database block containing the objects.
873+
* @param table The internal database table storing the object implementations.
874+
* @param module_field Member pointer to the field storing the parent module ID
875+
* (e.g., &_dbInst::module_ or &_dbModInst::parent_).
876+
* @param hash_field Member pointer to the hash map member in _dbModule where
877+
* the object ID should be stored.
878+
*/
879+
template <typename T, typename T_impl>
880+
static void rebuildModuleHash(_dbBlock& block,
881+
dbTable<T_impl>* table,
882+
dbId<_dbModule> T_impl::*module_field,
883+
std::unordered_map<std::string, dbId<T_impl>> _dbModule::*
884+
hash_field)
885+
{
886+
dbSet<T> items((dbBlock*) &block, table);
887+
for (T* obj : items) {
888+
T_impl* _obj = (T_impl*) obj;
889+
dbId<_dbModule> mid = _obj->*module_field;
890+
if (mid == 0) {
891+
mid = block.top_module_;
892+
}
893+
_dbModule* module = block.module_tbl_->getPtr(mid);
894+
if (module && _obj->name_) {
895+
(module->*hash_field)[_obj->name_] = _obj->getId();
896+
}
897+
}
898+
}
899+
862900
dbIStream& operator>>(dbIStream& stream, _dbBlock& block)
863901
{
864902
_dbDatabase* db = block.getImpl()->getDatabase();
@@ -952,47 +990,28 @@ dbIStream& operator>>(dbIStream& stream, _dbBlock& block)
952990
}
953991
if (db->isSchema(kSchemaDbRemoveHash)) {
954992
// Construct dbinst_hash_
955-
dbSet<dbInst> insts((dbBlock*) &block, block.inst_tbl_);
956-
for (dbInst* obj : insts) {
957-
_dbInst* _obj = (_dbInst*) obj;
958-
_dbModule* module = nullptr;
959-
if (_obj->module_ == 0) {
960-
module = block.module_tbl_->getPtr(block.top_module_);
961-
} else {
962-
module = block.module_tbl_->getPtr(_obj->module_);
963-
}
964-
if (_obj->name_) {
965-
module->dbinst_hash_[_obj->name_] = _obj->getId();
966-
}
967-
}
993+
rebuildModuleHash<dbInst>(
994+
block, block.inst_tbl_, &_dbInst::module_, &_dbModule::dbinst_hash_);
968995
}
969996
if (db->isSchema(kSchemaBlockOwnsScanInsts)) {
970997
stream >> *block.scan_inst_tbl_;
971998
}
972999
stream >> *block.modinst_tbl_;
9731000
if (db->isSchema(kSchemaDbRemoveHash)) {
9741001
// Construct modinst_hash_
975-
dbSet<dbModInst> modinsts((dbBlock*) &block, block.modinst_tbl_);
976-
for (dbModInst* obj : modinsts) {
977-
_dbModInst* _obj = (_dbModInst*) obj;
978-
_dbModule* module = block.module_tbl_->getPtr(_obj->parent_);
979-
if (_obj->name_) {
980-
module->modinst_hash_[_obj->name_] = _obj->getId();
981-
}
982-
}
1002+
rebuildModuleHash<dbModInst>(block,
1003+
block.modinst_tbl_,
1004+
&_dbModInst::parent_,
1005+
&_dbModule::modinst_hash_);
9831006
}
9841007
if (db->isSchema(kSchemaUpdateHierarchy)) {
9851008
stream >> *block.modbterm_tbl_;
9861009
if (db->isSchema(kSchemaDbRemoveHash)) {
9871010
// Construct modbterm_hash_
988-
dbSet<dbModBTerm> modbterms((dbBlock*) &block, block.modbterm_tbl_);
989-
for (dbModBTerm* obj : modbterms) {
990-
_dbModBTerm* _obj = (_dbModBTerm*) obj;
991-
_dbModule* module = block.module_tbl_->getPtr(_obj->parent_);
992-
if (_obj->name_) {
993-
module->modbterm_hash_[_obj->name_] = _obj->getId();
994-
}
995-
}
1011+
rebuildModuleHash<dbModBTerm>(block,
1012+
block.modbterm_tbl_,
1013+
&_dbModBTerm::parent_,
1014+
&_dbModule::modbterm_hash_);
9961015
}
9971016
if (db->isSchema(kSchemaDbRemoveHash)) {
9981017
stream >> *block.busport_tbl_;
@@ -1001,14 +1020,10 @@ dbIStream& operator>>(dbIStream& stream, _dbBlock& block)
10011020
stream >> *block.modnet_tbl_;
10021021
if (db->isSchema(kSchemaDbRemoveHash)) {
10031022
// Construct modnet_hash_
1004-
dbSet<dbModNet> modnets((dbBlock*) &block, block.modnet_tbl_);
1005-
for (dbModNet* obj : modnets) {
1006-
_dbModNet* _obj = (_dbModNet*) obj;
1007-
_dbModule* module = block.module_tbl_->getPtr(_obj->parent_);
1008-
if (_obj->name_) {
1009-
module->modnet_hash_[_obj->name_] = _obj->getId();
1010-
}
1011-
}
1023+
rebuildModuleHash<dbModNet>(block,
1024+
block.modnet_tbl_,
1025+
&_dbModNet::parent_,
1026+
&_dbModule::modnet_hash_);
10121027
}
10131028
}
10141029
stream >> *block.powerdomain_tbl_;

src/odb/test/cpp/TestWriteReadDbHier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ TEST_F(TestWriteReadDbHier, WriteReadOdbMultiBlocks)
226226
EXPECT_STREQ(mod0_a_2->getName(), "A");
227227

228228
// Verify Inst hash is populated
229-
dbInst* child_inst_2 = child_block2->findInst("child_inst");
229+
dbInst* child_inst_2 = mod0_2->findDbInst("child_inst");
230230
ASSERT_NE(child_inst_2, nullptr);
231-
EXPECT_EQ(child_inst_2->getName(), "child_inst");
231+
EXPECT_EQ(child_inst_2->getName(), "child_inst");
232232
}
233233

234234
} // namespace

0 commit comments

Comments
 (0)