MXS-1849 Combine table and database mapping

Previously schemarouter only mapped databases to the servers
they were resided on. Now all the tables are also mapped to allow the
router to route queries to the right server based on the tables used in
that query.
This commit is contained in:
Marko
2018-06-24 22:17:22 +03:00
parent b38cac4939
commit f308dd281a
4 changed files with 101 additions and 266 deletions

View File

@ -32,50 +32,49 @@ bool Shard::add_location(std::string db, SERVER* target)
return m_map.insert(std::make_pair(db, target)).second;
}
bool Shard::add_table_location(std::string table, SERVER* target)
{
std::transform(table.begin(), table.end(), table.begin(), ::tolower);
return m_table_map.insert(std::make_pair(table, target)).second;
}
void Shard::replace_location(std::string db, SERVER* target)
{
std::transform(db.begin(), db.end(), db.begin(), ::tolower);
m_map[db] = target;
}
SERVER* Shard::get_location(std::string db)
{
SERVER* rval = NULL;
std::transform(db.begin(), db.end(), db.begin(), ::tolower);
ServerMap::iterator iter = m_map.find(db);
if (iter != m_map.end())
{
rval = iter->second;
}
return rval;
}
SERVER* Shard::get_table_location(std::string table)
SERVER* Shard::get_location(std::string table)
{
SERVER* rval = NULL;
std::transform(table.begin(), table.end(), table.begin(), ::tolower);
std::size_t pos = table.find(".");
if(pos != std::string::npos)
if (table.find(".") == std::string::npos)
{
table = table.substr(pos + 1);
for (ServerMap::iterator it = m_map.begin(); it != m_map.end(); it++)
{
std::string db = it->first.substr(0, it->first.find("."));
if (db.compare(table) == 0)
{
if ((rval && rval != it->second))
{
MXS_DEBUG("There are 2 databases with same name on a different servers: '%s' and '%s'. Connecting to '%s'"
, rval->name,it->second->name, rval->name);
break;
}
else
{
rval = it->second;
}
}
}
}
ServerMap::iterator iter = m_table_map.find(table);
if (iter != m_table_map.end())
else
{
rval = iter->second;
ServerMap::iterator iter = m_map.find(table);
if (iter != m_map.end())
{
rval = iter->second;
}
}
return rval;
}
bool Shard::stale(double max_interval) const
{
time_t now = time(NULL);
@ -88,11 +87,6 @@ bool Shard::empty() const
return m_map.size() == 0;
}
bool Shard::tables_empty() const
{
return m_table_map.size() == 0;
}
void Shard::get_content(ServerMap& dest)
{
for (ServerMap::iterator it = m_map.begin(); it != m_map.end(); it++)