diff options
author | Yuri Gorshenin <y@maps.me> | 2016-02-16 13:12:12 +0300 |
---|---|---|
committer | Sergey Yershov <yershov@corp.mail.ru> | 2016-03-23 16:21:14 +0300 |
commit | 2510d87a99189503d61d8abde2870ef9abe38d1d (patch) | |
tree | e323702654c86c26ae59e6e4a8a0e1c4f407940d /indexer/mwm_set.cpp | |
parent | 700c45f2f26a4dc4cf98d20a0d6b2e0050846bc1 (diff) |
[index] Fixed notifications logic, added UPDATE event.
Diffstat (limited to 'indexer/mwm_set.cpp')
-rw-r--r-- | indexer/mwm_set.cpp | 200 |
1 files changed, 147 insertions, 53 deletions
diff --git a/indexer/mwm_set.cpp b/indexer/mwm_set.cpp index 97592cabdd..83ca9e107b 100644 --- a/indexer/mwm_set.cpp +++ b/indexer/mwm_set.cpp @@ -85,38 +85,57 @@ MwmSet::MwmId MwmSet::GetMwmIdByCountryFileImpl(CountryFile const & countryFile) pair<MwmSet::MwmId, MwmSet::RegResult> MwmSet::Register(LocalCountryFile const & localFile) { - lock_guard<mutex> lock(m_lock); - - CountryFile const & countryFile = localFile.GetCountryFile(); - MwmId const id = GetMwmIdByCountryFileImpl(countryFile); - if (!id.IsAlive()) - return RegisterImpl(localFile); - - shared_ptr<MwmInfo> info = id.GetInfo(); - - // Deregister old mwm for the country. - if (info->GetVersion() < localFile.GetVersion()) - { - DeregisterImpl(id); - return RegisterImpl(localFile); - } - - string const name = countryFile.GetNameWithoutExt(); - // Update the status of the mwm with the same version. - if (info->GetVersion() == localFile.GetVersion()) - { - LOG(LINFO, ("Updating already registered mwm:", name)); - info->SetStatus(MwmInfo::STATUS_REGISTERED); - info->m_file = localFile; - return make_pair(id, RegResult::VersionAlreadyExists); - } - - LOG(LWARNING, ("Trying to add too old (", localFile.GetVersion(), ") mwm (", name, - "), current version:", info->GetVersion())); - return make_pair(MwmId(), RegResult::VersionTooOld); + pair<MwmSet::MwmId, MwmSet::RegResult> result; + WithEventLog([&](EventList & events) + { + CountryFile const & countryFile = localFile.GetCountryFile(); + MwmId const id = GetMwmIdByCountryFileImpl(countryFile); + if (!id.IsAlive()) + { + result = RegisterImpl(localFile, events); + return; + } + + shared_ptr<MwmInfo> info = id.GetInfo(); + + // Deregister old mwm for the country. + if (info->GetVersion() < localFile.GetVersion()) + { + EventList subEvents; + DeregisterImpl(id, subEvents); + result = RegisterImpl(localFile, subEvents); + + // In the case of success all sub-events are + // replaced with a single UPDATE event. Otherwise, + // sub-events are reported as is. + if (result.second == MwmSet::RegResult::Success) + events.Add(Event(Event::TYPE_UPDATED, localFile, info->GetLocalFile())); + else + events.Append(subEvents); + return; + } + + string const name = countryFile.GetNameWithoutExt(); + // Update the status of the mwm with the same version. + if (info->GetVersion() == localFile.GetVersion()) + { + LOG(LINFO, ("Updating already registered mwm:", name)); + SetStatus(*info, MwmInfo::STATUS_REGISTERED, events); + info->m_file = localFile; + result = make_pair(id, RegResult::VersionAlreadyExists); + return; + } + + LOG(LWARNING, ("Trying to add too old (", localFile.GetVersion(), ") mwm (", + name, "), current version:", info->GetVersion())); + result = make_pair(MwmId(), RegResult::VersionTooOld); + return; + }); + return result; } -pair<MwmSet::MwmId, MwmSet::RegResult> MwmSet::RegisterImpl(LocalCountryFile const & localFile) +pair<MwmSet::MwmId, MwmSet::RegResult> MwmSet::RegisterImpl(LocalCountryFile const & localFile, + EventList & events) { // This function can throw an exception for a bad mwm file. shared_ptr<MwmInfo> info(CreateInfo(localFile)); @@ -124,13 +143,13 @@ pair<MwmSet::MwmId, MwmSet::RegResult> MwmSet::RegisterImpl(LocalCountryFile con return make_pair(MwmId(), RegResult::UnsupportedFileFormat); info->m_file = localFile; - info->SetStatus(MwmInfo::STATUS_REGISTERED); + SetStatus(*info, MwmInfo::STATUS_REGISTERED, events); m_info[localFile.GetCountryName()].push_back(info); return make_pair(MwmId(info), RegResult::Success); } -bool MwmSet::DeregisterImpl(MwmId const & id) +bool MwmSet::DeregisterImpl(MwmId const & id, EventList & events) { if (!id.IsAlive()) return false; @@ -138,29 +157,32 @@ bool MwmSet::DeregisterImpl(MwmId const & id) shared_ptr<MwmInfo> const & info = id.GetInfo(); if (info->m_numRefs == 0) { - info->SetStatus(MwmInfo::STATUS_DEREGISTERED); + SetStatus(*info, MwmInfo::STATUS_DEREGISTERED, events); vector<shared_ptr<MwmInfo>> & infos = m_info[info->GetCountryName()]; infos.erase(remove(infos.begin(), infos.end(), info), infos.end()); - OnMwmDeregistered(info->GetLocalFile()); return true; } - info->SetStatus(MwmInfo::STATUS_MARKED_TO_DEREGISTER); + SetStatus(*info, MwmInfo::STATUS_MARKED_TO_DEREGISTER, events); return false; } bool MwmSet::Deregister(CountryFile const & countryFile) { - lock_guard<mutex> lock(m_lock); - return DeregisterImpl(countryFile); + bool deregistered = false; + WithEventLog([&](EventList & events) + { + deregistered = DeregisterImpl(countryFile, events); + }); + return deregistered; } -bool MwmSet::DeregisterImpl(CountryFile const & countryFile) +bool MwmSet::DeregisterImpl(CountryFile const & countryFile, EventList & events) { MwmId const id = GetMwmIdByCountryFileImpl(countryFile); if (!id.IsAlive()) return false; - bool const deregistered = DeregisterImpl(id); + bool const deregistered = DeregisterImpl(id, events); ClearCache(id); return deregistered; } @@ -185,13 +207,54 @@ void MwmSet::GetMwmsInfo(vector<shared_ptr<MwmInfo>> & info) const } } +void MwmSet::SetStatus(MwmInfo & info, MwmInfo::Status status, EventList & events) +{ + MwmInfo::Status oldStatus = info.SetStatus(status); + if (oldStatus == status) + return; + + switch (status) + { + case MwmInfo::STATUS_REGISTERED: + events.Add(Event(Event::TYPE_REGISTERED, info.GetLocalFile())); + break; + case MwmInfo::STATUS_MARKED_TO_DEREGISTER: break; + case MwmInfo::STATUS_DEREGISTERED: + events.Add(Event(Event::TYPE_DEREGISTERED, info.GetLocalFile())); + break; + } +} + +void MwmSet::ProcessEventList(EventList & events) +{ + for (auto const & event : events.Get()) + { + switch (event.m_type) + { + case Event::TYPE_REGISTERED: + m_observers.ForEach(&Observer::OnMapRegistered, event.m_file); + break; + case Event::TYPE_UPDATED: + m_observers.ForEach(&Observer::OnMapUpdated, event.m_file, event.m_oldFile); + break; + case Event::TYPE_DEREGISTERED: + m_observers.ForEach(&Observer::OnMapDeregistered, event.m_file); + break; + } + } +} + unique_ptr<MwmSet::MwmValueBase> MwmSet::LockValue(MwmId const & id) { - lock_guard<mutex> lock(m_lock); - return LockValueImpl(id); + unique_ptr<MwmSet::MwmValueBase> result; + WithEventLog([&](EventList & events) + { + result = LockValueImpl(id, events); + }); + return result; } -unique_ptr<MwmSet::MwmValueBase> MwmSet::LockValueImpl(MwmId const & id) +unique_ptr<MwmSet::MwmValueBase> MwmSet::LockValueImpl(MwmId const & id, EventList & events) { if (!id.IsAlive()) return nullptr; @@ -224,18 +287,20 @@ unique_ptr<MwmSet::MwmValueBase> MwmSet::LockValueImpl(MwmId const & id) LOG(LERROR, ("Can't create MWMValue for", info->GetCountryName(), "Reason", ex.what())); --info->m_numRefs; - DeregisterImpl(id); + DeregisterImpl(id, events); return nullptr; } } void MwmSet::UnlockValue(MwmId const & id, unique_ptr<MwmValueBase> && p) { - lock_guard<mutex> lock(m_lock); - UnlockValueImpl(id, move(p)); + WithEventLog([&](EventList & events) + { + UnlockValueImpl(id, move(p), events); + }); } -void MwmSet::UnlockValueImpl(MwmId const & id, unique_ptr<MwmValueBase> && p) +void MwmSet::UnlockValueImpl(MwmId const & id, unique_ptr<MwmValueBase> && p, EventList & events) { ASSERT(id.IsAlive() && p, (id)); if (!id.IsAlive() || !p) @@ -245,7 +310,7 @@ void MwmSet::UnlockValueImpl(MwmId const & id, unique_ptr<MwmValueBase> && p) ASSERT_GREATER(info->m_numRefs, 0, ()); --info->m_numRefs; if (info->m_numRefs == 0 && info->GetStatus() == MwmInfo::STATUS_MARKED_TO_DEREGISTER) - VERIFY(DeregisterImpl(id), ()); + VERIFY(DeregisterImpl(id, events), ()); if (info->IsUpToDate()) { @@ -282,21 +347,29 @@ MwmSet::MwmId MwmSet::GetMwmIdByCountryFile(CountryFile const & countryFile) con MwmSet::MwmHandle MwmSet::GetMwmHandleByCountryFile(CountryFile const & countryFile) { - lock_guard<mutex> lock(m_lock); - return GetMwmHandleByIdImpl(GetMwmIdByCountryFileImpl(countryFile)); + MwmSet::MwmHandle handle; + WithEventLog([&](EventList & events) + { + handle = GetMwmHandleByIdImpl(GetMwmIdByCountryFileImpl(countryFile), events); + }); + return handle; } MwmSet::MwmHandle MwmSet::GetMwmHandleById(MwmId const & id) { - lock_guard<mutex> lock(m_lock); - return GetMwmHandleByIdImpl(id); + MwmSet::MwmHandle handle; + WithEventLog([&](EventList & events) + { + handle = GetMwmHandleByIdImpl(id, events); + }); + return handle; } -MwmSet::MwmHandle MwmSet::GetMwmHandleByIdImpl(MwmId const & id) +MwmSet::MwmHandle MwmSet::GetMwmHandleByIdImpl(MwmId const & id, EventList & events) { unique_ptr<MwmValueBase> value; if (id.IsAlive()) - value = LockValueImpl(id); + value = LockValueImpl(id, events); return MwmHandle(*this, id, move(value)); } @@ -330,3 +403,24 @@ string DebugPrint(MwmSet::RegResult result) return "UnsupportedFileFormat"; } } + +string DebugPrint(MwmSet::Event::Type type) +{ + switch (type) + { + case MwmSet::Event::TYPE_REGISTERED: return "Registered"; + case MwmSet::Event::TYPE_UPDATED: return "Updated"; + case MwmSet::Event::TYPE_DEREGISTERED: return "Deregistered"; + } + return "Undefined"; +} + +string DebugPrint(MwmSet::Event const & event) +{ + ostringstream os; + os << "MwmSet::Event [" << DebugPrint(event.m_type) << ", " << DebugPrint(event.m_file); + if (event.m_type == MwmSet::Event::TYPE_UPDATED) + os << ", " << DebugPrint(event.m_oldFile); + os << "]"; + return os.str(); +} |