diff options
author | Yuri Gorshenin <y@maps.me> | 2015-06-16 18:15:09 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:53:25 +0300 |
commit | 598e71858b63ef4e587efd932db41474f8f516a0 (patch) | |
tree | 19e39c1dd331579432c9b58e5e1e82c750842ac3 /indexer/mwm_set.cpp | |
parent | 9148053b3aa67aac443ade98e2ed2ae899419095 (diff) |
Storage redesign.
Diffstat (limited to 'indexer/mwm_set.cpp')
-rw-r--r-- | indexer/mwm_set.cpp | 130 |
1 files changed, 69 insertions, 61 deletions
diff --git a/indexer/mwm_set.cpp b/indexer/mwm_set.cpp index c9005078ce..a360a4181b 100644 --- a/indexer/mwm_set.cpp +++ b/indexer/mwm_set.cpp @@ -9,6 +9,9 @@ #include "std/algorithm.hpp" +using platform::CountryFile; +using platform::LocalCountryFile; + MwmInfo::MwmInfo() : m_minScale(0), m_maxScale(0), m_status(STATUS_DEREGISTERED), m_lockCount(0) {} MwmInfo::MwmTypeT MwmInfo::GetType() const @@ -74,57 +77,65 @@ MwmSet::~MwmSet() void MwmSet::Cleanup() { lock_guard<mutex> lock(m_lock); - ClearCacheImpl(m_cache.begin(), m_cache.end()); - -#ifdef DEBUG - for (auto const & p : m_info) - { - MwmInfo const & info = *p.second; - if (info.IsUpToDate()) - { - ASSERT_EQUAL(info.m_lockCount, 0, (info.m_fileName)); - ASSERT(!info.m_fileName.empty(), ()); - } - } -#endif } -MwmSet::MwmId MwmSet::GetMwmIdByFileNameImpl(TMwmFileName const & name) const +MwmSet::MwmId MwmSet::GetMwmIdByCountryFileImpl(CountryFile const & countryFile) const { + string const name = countryFile.GetNameWithoutExt(); ASSERT(!name.empty(), ()); auto const it = m_info.find(name); - if (it == m_info.cend()) + if (it == m_info.cend() || it->second.empty()) return MwmId(); - return MwmId(it->second); + return MwmId(it->second.back()); } -pair<MwmSet::MwmLock, bool> MwmSet::Register(TMwmFileName const & fileName) +pair<MwmSet::MwmLock, bool> MwmSet::Register(LocalCountryFile const & localFile) { lock_guard<mutex> lock(m_lock); - MwmId const id = GetMwmIdByFileNameImpl(fileName); + CountryFile const & countryFile = localFile.GetCountryFile(); + string const name = countryFile.GetNameWithoutExt(); + + MwmId const id = GetMwmIdByCountryFileImpl(countryFile); if (!id.IsAlive()) - return RegisterImpl(fileName); + return RegisterImpl(localFile); + shared_ptr<MwmInfo> info = id.GetInfo(); - if (info->IsRegistered()) - LOG(LWARNING, ("Trying to add already registered mwm:", fileName)); - else - info->SetStatus(MwmInfo::STATUS_UP_TO_DATE); - return make_pair(GetLock(id), false); + + // Deregister old mwm for the country. + if (info->GetVersion() < localFile.GetVersion()) + { + DeregisterImpl(id); + return RegisterImpl(localFile); + } + + // Update the status of the mwm with the same version. + if (info->GetVersion() == localFile.GetVersion()) + { + LOG(LWARNING, ("Trying to add already registered mwm:", name)); + info->SetStatus(MwmInfo::STATUS_REGISTERED); + return make_pair(GetLock(id), false); + } + + LOG(LWARNING, ("Trying to add too old (", localFile.GetVersion(), ") mwm (", name, + "), current version:", info->GetVersion())); + return make_pair(MwmLock(), false); } -pair<MwmSet::MwmLock, bool> MwmSet::RegisterImpl(TMwmFileName const & fileName) +pair<MwmSet::MwmLock, bool> MwmSet::RegisterImpl(LocalCountryFile const & localFile) { shared_ptr<MwmInfo> info(new MwmInfo()); // This function can throw an exception for a bad mwm file. - if (!GetVersion(fileName, *info)) + if (!GetVersion(localFile, *info)) return make_pair(MwmLock(), false); - info->SetStatus(MwmInfo::STATUS_UP_TO_DATE); - info->m_fileName = fileName; - m_info[fileName] = info; + info->SetStatus(MwmInfo::STATUS_REGISTERED); + info->m_file = localFile; + string const name = localFile.GetCountryFile().GetNameWithoutExt(); + vector<shared_ptr<MwmInfo>> & infos = m_info[name]; + infos.push_back(info); return make_pair(GetLock(MwmId(info)), true); } @@ -133,26 +144,29 @@ bool MwmSet::DeregisterImpl(MwmId const & id) if (!id.IsAlive()) return false; shared_ptr<MwmInfo> const & info = id.GetInfo(); + string const name = info->GetCountryName(); + if (info->m_lockCount == 0) { info->SetStatus(MwmInfo::STATUS_DEREGISTERED); - m_info.erase(info->m_fileName); - OnMwmDeleted(info); + vector<shared_ptr<MwmInfo>> & infos = m_info[name]; + infos.erase(remove(infos.begin(), infos.end(), info), infos.end()); + OnMwmDeregistered(info->GetLocalFile()); return true; } info->SetStatus(MwmInfo::STATUS_MARKED_TO_DEREGISTER); return false; } -bool MwmSet::Deregister(TMwmFileName const & fileName) +bool MwmSet::Deregister(CountryFile const & countryFile) { lock_guard<mutex> lock(m_lock); - return DeregisterImpl(fileName); + return DeregisterImpl(countryFile); } -bool MwmSet::DeregisterImpl(TMwmFileName const & fileName) +bool MwmSet::DeregisterImpl(CountryFile const & countryFile) { - MwmId const id = GetMwmIdByFileNameImpl(fileName); + MwmId const id = GetMwmIdByCountryFileImpl(countryFile); if (!id.IsAlive()) return false; bool const deregistered = DeregisterImpl(id); @@ -164,21 +178,24 @@ void MwmSet::DeregisterAll() { lock_guard<mutex> lock(m_lock); - for (auto it = m_info.begin(); it != m_info.end();) + for (auto const & p : m_info) { - auto cur = it++; - DeregisterImpl(MwmId(cur->second)); + // Vector of shared pointers is copied here because an original + // vector will be modified by the body of the cycle. + vector<shared_ptr<MwmInfo>> infos = p.second; + for (shared_ptr<MwmInfo> info : infos) + DeregisterImpl(MwmId(info)); } // Do not call ClearCache - it's under mutex lock. ClearCacheImpl(m_cache.begin(), m_cache.end()); } -bool MwmSet::IsLoaded(TMwmFileName const & fileName) const +bool MwmSet::IsLoaded(CountryFile const & countryFile) const { lock_guard<mutex> lock(m_lock); - MwmId const id = GetMwmIdByFileNameImpl(fileName + DATA_FILE_EXTENSION); + MwmId const id = GetMwmIdByCountryFileImpl(countryFile); return id.IsAlive() && id.GetInfo()->IsRegistered(); } @@ -188,7 +205,10 @@ void MwmSet::GetMwmsInfo(vector<shared_ptr<MwmInfo>> & info) const info.clear(); info.reserve(m_info.size()); for (auto const & p : m_info) - info.push_back(p.second); + { + if (!p.second.empty()) + info.push_back(p.second.back()); + } } MwmSet::TMwmValueBasePtr MwmSet::LockValue(MwmId const & id) @@ -216,7 +236,7 @@ MwmSet::TMwmValueBasePtr MwmSet::LockValueImpl(MwmId const & id) return result; } } - return CreateValue(info->m_fileName); + return CreateValue(info->GetLocalFile()); } void MwmSet::UnlockValue(MwmId const & id, TMwmValueBasePtr p) @@ -235,20 +255,8 @@ void MwmSet::UnlockValueImpl(MwmId const & id, TMwmValueBasePtr p) shared_ptr<MwmInfo> const & info = id.GetInfo(); CHECK_GREATER(info->m_lockCount, 0, ()); --info->m_lockCount; - if (info->m_lockCount == 0) - { - switch (info->GetStatus()) - { - case MwmInfo::STATUS_MARKED_TO_DEREGISTER: - CHECK(DeregisterImpl(id), ()); - break; - case MwmInfo::STATUS_PENDING_UPDATE: - OnMwmReadyForUpdate(info); - break; - default: - break; - } - } + if (info->m_lockCount == 0 && info->GetStatus() == MwmInfo::STATUS_MARKED_TO_DEREGISTER) + CHECK(DeregisterImpl(id), ()); if (info->IsUpToDate()) { @@ -267,20 +275,20 @@ void MwmSet::ClearCache() ClearCacheImpl(m_cache.begin(), m_cache.end()); } -MwmSet::MwmId MwmSet::GetMwmIdByFileName(TMwmFileName const & fileName) const +MwmSet::MwmId MwmSet::GetMwmIdByCountryFile(CountryFile const & countryFile) const { lock_guard<mutex> lock(m_lock); - MwmId const id = GetMwmIdByFileNameImpl(fileName); - ASSERT(id.IsAlive(), ("Can't get an mwm's (", fileName, ") identifier.")); + MwmId const id = GetMwmIdByCountryFileImpl(countryFile); + ASSERT(id.IsAlive(), ("Can't get an mwm's (", countryFile.GetNameWithoutExt(), ") identifier.")); return id; } -MwmSet::MwmLock MwmSet::GetMwmLockByFileName(TMwmFileName const & fileName) +MwmSet::MwmLock MwmSet::GetMwmLockByCountryFile(CountryFile const & countryFile) { lock_guard<mutex> lock(m_lock); - MwmId const id = GetMwmIdByFileNameImpl(fileName); + MwmId const id = GetMwmIdByCountryFileImpl(countryFile); TMwmValueBasePtr value(nullptr); if (id.IsAlive()) value = LockValueImpl(id); |