-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86e899a
commit 0f1cb0e
Showing
6 changed files
with
125 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <Storages/KVStore/Types.h> | ||
#include <TiDB/Schema/TiDB.h> | ||
|
||
#include <shared_mutex> | ||
#include <unordered_map> | ||
|
||
namespace DB | ||
{ | ||
|
||
class DatabaseInfoCache | ||
{ | ||
public: | ||
TiDB::DBInfoPtr getDBInfoByName(const String & database_name) const | ||
{ | ||
std::shared_lock lock(mtx_databases); | ||
|
||
auto it = std::find_if(databases.begin(), databases.end(), [&](const auto & pair) { | ||
return pair.second->name == database_name; | ||
}); | ||
if (it == databases.end()) | ||
return nullptr; | ||
return it->second; | ||
} | ||
|
||
template <typename NameMapper> | ||
TiDB::DBInfoPtr getDBInfoByMappedName(const String & mapped_database_name) const | ||
{ | ||
std::shared_lock lock(mtx_databases); | ||
|
||
auto it = std::find_if(databases.begin(), databases.end(), [&](const auto & pair) { | ||
return NameMapper().mapDatabaseName(*pair.second) == mapped_database_name; | ||
}); | ||
if (it == databases.end()) | ||
return nullptr; | ||
return it->second; | ||
} | ||
|
||
void addDatabaseInfo(const TiDB::DBInfoPtr & db_info) | ||
{ | ||
std::unique_lock lock(mtx_databases); | ||
databases.emplace(db_info->id, db_info); | ||
} | ||
|
||
TiDB::DBInfoPtr getDBInfo(DatabaseID database_id) const | ||
{ | ||
std::shared_lock shared_lock(mtx_databases); | ||
if (auto it = databases.find(database_id); likely(it == databases.end())) | ||
{ | ||
return it->second; | ||
} | ||
return nullptr; | ||
} | ||
|
||
bool exists(DatabaseID database_id) const | ||
{ | ||
std::shared_lock shared_lock(mtx_databases); | ||
return databases.contains(database_id); | ||
} | ||
|
||
void eraseDBInfo(DatabaseID database_id) | ||
{ | ||
std::unique_lock shared_lock(mtx_databases); | ||
databases.erase(database_id); | ||
} | ||
|
||
void clear() | ||
{ | ||
std::unique_lock lock(mtx_databases); | ||
databases.clear(); | ||
} | ||
|
||
private: | ||
mutable std::shared_mutex mtx_databases; | ||
std::unordered_map<DB::DatabaseID, TiDB::DBInfoPtr> databases; | ||
}; | ||
} // namespace DB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters