void SchemaExtractor::addToMap(SchemaMap &map, const char *key, const long value) { SchemaMap::iterator itr = map.find(key); if (itr == map.end()) { vector<long> newVector; newVector.push_back(value); map.insert(make_pair(supportSubclasses.addNew(key, Utils::decode_short(key) + 2), newVector)); } else { if (!isPresent(value, itr->second)) { (itr->second).push_back(value); } } }
bool SchemaManager::reload() { ServiceI& service = ServiceI::instance(); Ice::PropertiesPtr props = service.getCommunicator()->getProperties(); int load_hidden = props->getPropertyAsIntWithDefault("Service." + service.getName() + ".LoadHidden", 0); // TODO : template表很大的时候,只加载最新更新的template SchemaMap schemas; set<int> merge_types; map<int, int> type2bigtype; map<int, vector<int> > bigtype2types; // 加载 data_schemas 表信息 { Statement sql; sql << "SELECT id, name, display, home_visible, key_list, type,merge,bigtype FROM data_schemas"; MCE_DEBUG("schema load hidden : " << load_hidden); if (!load_hidden) sql << " WHERE display > 0"; try { mysqlpp::StoreQueryResult res = QueryRunner("notify", CDbRServer).store(sql); if (!res) { MCE_WARN("select table data_schemas error."); return false; } MCE_DEBUG("load schema size : " << res.num_rows()); for (size_t i = 0; i < res.num_rows(); ++i) { mysqlpp::Row row = res.at(i); int id = (int) row["id"]; schemas[id].id = id; schemas[id].name = row["name"].c_str(); schemas[id].display = (int)row["display"]; schemas[id].is_reply = (int)row["home_visible"]; schemas[id].bigtype= (int)row["bigtype"]; ParseSchemaKeys(row["key_list"].c_str(), schemas[id].keys); int type = (int) row["type"]; bool merge = (int)row["merge"]; MCE_DEBUG("schema id ------>" << id << " bigtype " << schemas[id].bigtype << " type:" << type); type2bigtype[type] = schemas[id].bigtype; map<int,vector<int> >::iterator bit = bigtype2types.find(schemas[id].bigtype); if(bit == bigtype2types.end()){ vector<int> v; bigtype2types.insert(make_pair<int, vector<int> >(schemas[id].bigtype, v)); } bigtype2types[schemas[id].bigtype].push_back(type); if(merge){ merge_types.insert(type); } } } catch (std::exception& e) { MCE_WARN("load table data_schemas err : " << e.what()); return false; } catch (...) { MCE_WARN("load table data_schemas unknown exception"); return false; } } // 加载 template 表信息 { Statement sql; sql << "select id, schema_id, view, template, update_time from template"; try { mysqlpp::StoreQueryResult res = QueryRunner("notify", CDbRServer).store(sql); if (!res) { MCE_WARN("select table 'template' error "); return false; } time_t update_time; MCE_DEBUG("load template size : " << res.num_rows()); for (size_t i = 0; i < res.num_rows(); ++i) { mysqlpp::Row row = res.at(i); int schema_id = (int) row["schema_id"]; if (schemas.find(schema_id) == schemas.end()) { MCE_DEBUG("inconsistent template item : schema id " << schema_id << " doesn't exist."); continue; } int view = (int) row["view"]; schemas[schema_id].templates[view] = ctemplate::Template::StringToTemplate( row["template"].c_str(), DO_NOT_STRIP); if(!ctemplate::Template::StringToTemplate(row["template"].c_str(), DO_NOT_STRIP)){ MCE_INFO("NULL return. schema_id:"<< schema_id << " view:" << view << " << content:" << row["template"]); } MCE_DEBUG("load template (" << schema_id << ',' << view << ") = " << row["template"].c_str()); update_time = (time_t) mysqlpp::DateTime(row["update_time"]); if (_load_time < update_time) { _load_time = update_time; } } } catch (std::exception& e) { MCE_WARN("load table template err : " << e.what()); return false; } catch (...) { MCE_WARN("load table template unknown exception"); return false; } } for(map<int, int>::iterator logit = type2bigtype.begin(); logit != type2bigtype.end(); ++logit) { MCE_DEBUG("Reload schema--> type2bigtype info:type=" << logit->first << " bigtype=" << logit->second); } { IceUtil::RWRecMutex::WLock lock(_mutex); _schema_map.swap(schemas); _type2bigtype.swap(type2bigtype); _bigtype2types.swap(bigtype2types); merge_types_.swap(merge_types); } MCE_INFO("Reload schema. merge_types size:" << merge_types_.size() << "," << PrintHelper::Print(merge_types_,10000)); for (SchemaMap::iterator i1 = schemas.begin(); i1 != schemas.end(); ++i1) { map<int, ctemplate::Template*>::iterator i2 = i1->second.templates.begin(), end = i1->second.templates.end(); for (; i2 != end; ++i2) { delete i2->second; } } return true; }