未验证 提交 a2cad460 编写于 作者: X XuPeng-SH 提交者: GitHub

modificate name to id data structure in Snapshots (#4483)

Signed-off-by: NXuPeng-SH <xupeng3112@163.com>
上级 9d76f3a6
......@@ -55,7 +55,12 @@ Snapshots::DoDropCollection(ScopedSnapshotT& ss, const LSN_TYPE& lsn) {
{
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
alive_cids_.erase(context.collection->GetID());
name_id_map_.erase(context.collection->GetName());
auto& ids = name_id_map_[context.collection->GetName()];
if (ids.size() == 1) {
name_id_map_.erase(context.collection->GetName());
} else {
ids.erase(context.collection->GetID());
}
/* holders_.erase(context.collection->GetID()); */
auto h = holders_.find(context.collection->GetID());
if (h != holders_.end()) {
......@@ -133,8 +138,8 @@ Snapshots::GetCollectionIds(IDS_TYPE& ids) const {
Status
Snapshots::GetCollectionNames(std::vector<std::string>& names) const {
std::shared_lock<std::shared_timed_mutex> lock(mutex_);
for (auto& kv : name_id_map_) {
names.push_back(kv.first);
for (auto& [name, _] : name_id_map_) {
names.push_back(name);
}
return Status::OK();
}
......@@ -180,8 +185,9 @@ Snapshots::GetHolder(const std::string& name, SnapshotHolderPtr& holder) const {
std::shared_lock<std::shared_timed_mutex> lock(mutex_);
auto kv = name_id_map_.find(name);
if (kv != name_id_map_.end()) {
auto id = *(kv->second.rbegin());
lock.unlock();
return GetHolder(kv->second, holder);
return GetHolder(id, holder);
}
std::stringstream emsg;
emsg << "Snapshots::GetHolderNoLock: Specified snapshot holder for collection ";
......@@ -215,7 +221,12 @@ Snapshots::LoadHolder(StorePtr store, const ID_TYPE& collection_id, SnapshotHold
holders_[collection_id] = holder;
ScopedSnapshotT ss;
STATUS_CHECK(holder->Load(store, ss));
name_id_map_[ss->GetName()] = collection_id;
auto it = name_id_map_.find(ss->GetName());
if (it == name_id_map_.end()) {
name_id_map_[ss->GetName()] = {collection_id};
} else {
name_id_map_[ss->GetName()].insert(collection_id);
}
alive_cids_.insert(collection_id);
return Status::OK();
}
......@@ -284,14 +295,14 @@ Snapshots::OnReaderTimer(const boost::system::error_code& ec) {
}
auto aids = op2->GetIDs();
std::set<ID_TYPE> diff;
std::set<ID_TYPE> stale_ids;
{
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
std::set_difference(alive_cids_.begin(), alive_cids_.end(), aids.begin(), aids.end(),
std::inserter(diff, diff.begin()));
std::inserter(stale_ids, stale_ids.begin()));
}
for (auto& cid : diff) {
for (auto& cid : stale_ids) {
ScopedSnapshotT ss;
status = GetSnapshot(ss, cid);
if (!status.ok()) {
......@@ -300,7 +311,12 @@ Snapshots::OnReaderTimer(const boost::system::error_code& ec) {
}
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
alive_cids_.erase(cid);
name_id_map_.erase(ss->GetName());
auto& ids = name_id_map_[ss->GetName()];
if (ids.size() == 1) {
name_id_map_.erase(ss->GetName());
} else {
ids.erase(ss->GetID());
}
holders_.erase(cid);
}
}
......
......@@ -100,7 +100,7 @@ class Snapshots {
mutable std::shared_timed_mutex mutex_;
std::map<ID_TYPE, SnapshotHolderPtr> holders_;
std::set<ID_TYPE> alive_cids_;
std::map<std::string, ID_TYPE> name_id_map_;
std::map<std::string, std::set<ID_TYPE>> name_id_map_;
mutable std::shared_timed_mutex inactive_mtx_;
std::map<ID_TYPE, SnapshotHolderPtr> inactive_holders_;
std::set<ID_TYPE> invalid_ssid_;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册