diff options
author | gorshenin <y.gorshenin@corp.mail.ru> | 2015-07-01 16:48:58 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:53:27 +0300 |
commit | a7bcdb5ae8b8e75c1a3806b00b19f3b80c72c5b3 (patch) | |
tree | e24caedef110b579fcedc15d29c62cc43e3f6d9c /indexer/index.cpp | |
parent | e0470160313b3ed0bc396ffec72b16fded6b8520 (diff) |
Revert "[storage, framework, index] Storage redesign. Plain strings are replaced to CountryFile and LocalCountryFile."
Diffstat (limited to 'indexer/index.cpp')
-rw-r--r-- | indexer/index.cpp | 142 |
1 files changed, 124 insertions, 18 deletions
diff --git a/indexer/index.cpp b/indexer/index.cpp index 12d0a02fe7..e27814dc74 100644 --- a/indexer/index.cpp +++ b/indexer/index.cpp @@ -6,27 +6,32 @@ #include "coding/file_name_utils.hpp" #include "coding/internal/file_data.hpp" -using platform::CountryFile; -using platform::LocalCountryFile; ////////////////////////////////////////////////////////////////////////////////// // MwmValue implementation ////////////////////////////////////////////////////////////////////////////////// -MwmValue::MwmValue(LocalCountryFile const & localFile) - : m_cont(GetPlatform().GetCountryReader(localFile, TMapOptions::EMap)), - m_countryFile(localFile.GetCountryFile()) +MwmValue::MwmValue(string const & name) + : m_cont(GetPlatform().GetReader(name)) { m_factory.Load(m_cont); } +string MwmValue::GetFileName() const +{ + string s = m_cont.GetFileName(); + my::GetNameFromFullPath(s); + my::GetNameWithoutExt(s); + return s; +} + ////////////////////////////////////////////////////////////////////////////////// // Index implementation ////////////////////////////////////////////////////////////////////////////////// -bool Index::GetVersion(LocalCountryFile const & localFile, MwmInfo & info) const +bool Index::GetVersion(string const & name, MwmInfo & info) const { - MwmValue value(localFile); + MwmValue value(name); feature::DataHeader const & h = value.GetHeader(); if (!h.IsMWMSuitable()) @@ -42,9 +47,9 @@ bool Index::GetVersion(LocalCountryFile const & localFile, MwmInfo & info) const return true; } -MwmSet::TMwmValueBasePtr Index::CreateValue(LocalCountryFile const & localFile) const +MwmSet::TMwmValueBasePtr Index::CreateValue(string const & name) const { - TMwmValueBasePtr p(new MwmValue(localFile)); + TMwmValueBasePtr p(new MwmValue(name)); ASSERT(static_cast<MwmValue &>(*p.get()).GetHeader().IsMWMSuitable(), ()); return p; } @@ -58,23 +63,124 @@ Index::~Index() Cleanup(); } -pair<MwmSet::MwmLock, bool> Index::RegisterMap(LocalCountryFile const & localFile) +namespace { - pair<MwmSet::MwmLock, bool> result = Register(localFile); - if (result.first.IsLocked() && result.second) - m_observers.ForEach(&Observer::OnMapRegistered, localFile); + // Deletes map file denoted by @path and all temporary files related + // to it. + void DeleteMapFiles(string const & path, bool deleteReady) + { + (void)my::DeleteFileX(path); + (void)my::DeleteFileX(path + RESUME_FILE_EXTENSION); + (void)my::DeleteFileX(path + DOWNLOADING_FILE_EXTENSION); + + if (deleteReady) + (void)my::DeleteFileX(path + READY_FILE_EXTENSION); + } + + string GetFullPath(string const & fileName) + { + return GetPlatform().WritablePathForFile(fileName); + } + + // Deletes all files related to @fileName and renames + // @fileName.READY_FILE_EXTENSION to @fileName. + void ReplaceFileWithReady(string const & fileName) + { + string const path = GetFullPath(fileName); + DeleteMapFiles(path, false /* deleteReady */); + CHECK(my::RenameFileX(path + READY_FILE_EXTENSION, path), (path)); + } +} + +pair<MwmSet::MwmLock, bool> Index::RegisterMap(string const & fileName) +{ + if (GetPlatform().IsFileExistsByFullPath(GetFullPath(fileName + READY_FILE_EXTENSION))) + { + pair<MwmSet::MwmLock, UpdateStatus> updateResult = UpdateMap(fileName); + switch (updateResult.second) + { + case UPDATE_STATUS_OK: + return make_pair(move(updateResult.first), true); + case UPDATE_STATUS_BAD_FILE: + return make_pair(move(updateResult.first), false); + case UPDATE_STATUS_UPDATE_DELAYED: + // Not dangerous, but it's strange when adding existing maps. + ASSERT(false, ()); + return make_pair(move(updateResult.first), true); + } + } + + pair<MwmSet::MwmLock, bool> result = Register(fileName); + if (result.second) + m_observers.ForEach(&Observer::OnMapRegistered, fileName); return result; } -bool Index::DeregisterMap(CountryFile const & countryFile) { return Deregister(countryFile); } +bool Index::DeleteMap(string const & fileName) +{ + { + lock_guard<mutex> lock(m_lock); + + if (!DeregisterImpl(fileName)) + return false; + + DeleteMapFiles(GetFullPath(fileName), true /* deleteReady */); + } + m_observers.ForEach(&Observer::OnMapDeleted, fileName); + return true; +} bool Index::AddObserver(Observer & observer) { return m_observers.Add(observer); } bool Index::RemoveObserver(Observer const & observer) { return m_observers.Remove(observer); } -void Index::OnMwmDeregistered(LocalCountryFile const & localFile) +pair<MwmSet::MwmLock, Index::UpdateStatus> Index::UpdateMap(string const & fileName) +{ + pair<MwmSet::MwmLock, UpdateStatus> result; + result.second = UPDATE_STATUS_BAD_FILE; + + { + lock_guard<mutex> lock(m_lock); + + MwmId const id = GetMwmIdByFileNameImpl(fileName); + shared_ptr<MwmInfo> info = id.GetInfo(); + if (id.IsAlive() && info->m_lockCount > 0) + { + info->SetStatus(MwmInfo::STATUS_PENDING_UPDATE); + result.first = GetLock(id); + result.second = UPDATE_STATUS_UPDATE_DELAYED; + } + else + { + ReplaceFileWithReady(fileName); + pair<MwmSet::MwmLock, bool> registerResult = RegisterImpl(fileName); + if (registerResult.second) + { + result.first = move(registerResult.first); + result.second = UPDATE_STATUS_OK; + } + } + } + if (result.second != UPDATE_STATUS_BAD_FILE) + m_observers.ForEach(&Observer::OnMapUpdateIsReady, fileName); + if (result.second == UPDATE_STATUS_OK) + m_observers.ForEach(&Observer::OnMapUpdated, fileName); + return result; +} + +void Index::OnMwmDeleted(shared_ptr<MwmInfo> const & info) +{ + string const & fileName = info->m_fileName; + DeleteMapFiles(fileName, true /* deleteReady */); + m_observers.ForEach(&Observer::OnMapDeleted, fileName); +} + +void Index::OnMwmReadyForUpdate(shared_ptr<MwmInfo> const & info) { - m_observers.ForEach(&Observer::OnMapDeregistered, localFile); + ClearCache(MwmId(info)); + ReplaceFileWithReady(info->m_fileName); + info->SetStatus(MwmInfo::STATUS_UP_TO_DATE); + m_observers.ForEach(&Observer::OnMapUpdated, info->m_fileName); } ////////////////////////////////////////////////////////////////////////////////// @@ -88,11 +194,11 @@ Index::FeaturesLoaderGuard::FeaturesLoaderGuard(Index const & parent, MwmId id) { } -string Index::FeaturesLoaderGuard::GetCountryFileName() const +string Index::FeaturesLoaderGuard::GetFileName() const { if (!m_lock.IsLocked()) return string(); - return m_lock.GetValue<MwmValue>()->GetCountryFile().GetNameWithoutExt(); + return m_lock.GetValue<MwmValue>()->GetFileName(); } bool Index::FeaturesLoaderGuard::IsWorld() const |