diff options
author | Yuri Gorshenin <y@maps.me> | 2015-03-20 18:59:02 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:41:33 +0300 |
commit | 725aab207201f4b55bb0b8529b8731d0adfcd5cc (patch) | |
tree | 92273e0d2160883871e3d8dff2158153dd8dfb6c /indexer/mwm_set.cpp | |
parent | efaeab8e7b3b8cc863f601f29f98d4ff860955ce (diff) |
Added Observers interface to Index.
Diffstat (limited to 'indexer/mwm_set.cpp')
-rw-r--r-- | indexer/mwm_set.cpp | 84 |
1 files changed, 35 insertions, 49 deletions
diff --git a/indexer/mwm_set.cpp b/indexer/mwm_set.cpp index f38056f311..46d9bea017 100644 --- a/indexer/mwm_set.cpp +++ b/indexer/mwm_set.cpp @@ -9,11 +9,10 @@ #include "../std/algorithm.hpp" - -MwmInfo::MwmInfo() : m_lockCount(0), m_status(STATUS_REMOVED) +MwmInfo::MwmInfo() : m_lockCount(0), m_status(STATUS_DEREGISTERED) { - // Important: STATUS_REMOVED - is the default value. - // Apply STATUS_ACTIVE before adding to maps container. + // Important: STATUS_DEREGISTERED - is the default value. + // Apply STATUS_UP_TO_DATE before adding to maps container. } MwmInfo::MwmTypeT MwmInfo::GetType() const @@ -24,7 +23,6 @@ MwmInfo::MwmTypeT MwmInfo::GetType() const return COASTS; } - MwmSet::MwmLock::MwmLock(MwmSet & mwmSet, MwmId mwmId) : m_mwmSet(mwmSet), m_id(mwmId), m_pValue(mwmSet.LockValue(mwmId)) { @@ -50,15 +48,14 @@ MwmSet::~MwmSet() void MwmSet::Cleanup() { - threads::MutexGuard mutexGuard(m_lock); - UNUSED_VALUE(mutexGuard); + lock_guard<mutex> lock(m_lock); ClearCacheImpl(m_cache.begin(), m_cache.end()); #ifdef DEBUG for (MwmId i = 0; i < m_info.size(); ++i) { - if (m_info[i].IsActive()) + if (m_info[i].IsUpToDate()) { ASSERT_EQUAL(m_info[i].m_lockCount, 0, (i, m_name[i])); ASSERT_NOT_EQUAL(m_name[i], string(), (i)); @@ -69,8 +66,8 @@ void MwmSet::Cleanup() void MwmSet::UpdateMwmInfo(MwmId id) { - if (m_info[id].m_status == MwmInfo::STATUS_TO_REMOVE) - (void)RemoveImpl(id); + if (m_info[id].GetStatus() == MwmInfo::STATUS_MARKED_TO_DEREGISTER) + (void)DeregisterImpl(id); } MwmSet::MwmId MwmSet::GetFreeId() @@ -78,7 +75,7 @@ MwmSet::MwmId MwmSet::GetFreeId() MwmId const size = m_info.size(); for (MwmId i = 0; i < size; ++i) { - if (m_info[i].m_status == MwmInfo::STATUS_REMOVED) + if (m_info[i].GetStatus() == MwmInfo::STATUS_DEREGISTERED) return i; } @@ -97,7 +94,7 @@ MwmSet::MwmId MwmSet::GetIdByName(string const & name) if (m_name[i] == name) { - ASSERT_NOT_EQUAL ( m_info[i].m_status, MwmInfo::STATUS_REMOVED, () ); + ASSERT_NOT_EQUAL(m_info[i].GetStatus(), MwmInfo::STATUS_DEREGISTERED, ()); return i; } } @@ -105,26 +102,25 @@ MwmSet::MwmId MwmSet::GetIdByName(string const & name) return INVALID_MWM_ID; } -int MwmSet::Add(string const & fileName, m2::RectD & rect) +int MwmSet::Register(string const & fileName, m2::RectD & rect) { - threads::MutexGuard mutexGuard(m_lock); - UNUSED_VALUE(mutexGuard); + lock_guard<mutex> lock(m_lock); MwmId const id = GetIdByName(fileName); if (id != INVALID_MWM_ID) { - if (m_info[id].IsExist()) + if (m_info[id].IsRegistered()) LOG(LWARNING, ("Trying to add already added map", fileName)); else - m_info[id].m_status = MwmInfo::STATUS_ACTIVE; + m_info[id].SetStatus(MwmInfo::STATUS_UP_TO_DATE); return -1; } - return AddImpl(fileName, rect); + return RegisterImpl(fileName, rect); } -int MwmSet::AddImpl(string const & fileName, m2::RectD & rect) +int MwmSet::RegisterImpl(string const & fileName, m2::RectD & rect) { // this function can throw an exception for bad mwm file MwmInfo info; @@ -132,7 +128,7 @@ int MwmSet::AddImpl(string const & fileName, m2::RectD & rect) if (version == -1) return -1; - info.m_status = MwmInfo::STATUS_ACTIVE; + info.SetStatus(MwmInfo::STATUS_UP_TO_DATE); MwmId const id = GetFreeId(); m_name[id] = fileName; @@ -143,37 +139,35 @@ int MwmSet::AddImpl(string const & fileName, m2::RectD & rect) return version; } -bool MwmSet::RemoveImpl(MwmId id) +bool MwmSet::DeregisterImpl(MwmId id) { if (m_info[id].m_lockCount == 0) { m_name[id].clear(); - m_info[id].m_status = MwmInfo::STATUS_REMOVED; + m_info[id].SetStatus(MwmInfo::STATUS_DEREGISTERED); return true; } else { - m_info[id].m_status = MwmInfo::STATUS_TO_REMOVE; + m_info[id].SetStatus(MwmInfo::STATUS_MARKED_TO_DEREGISTER); return false; } } -void MwmSet::Remove(string const & fileName) +void MwmSet::Deregister(string const & fileName) { - threads::MutexGuard mutexGuard(m_lock); - UNUSED_VALUE(mutexGuard); - - (void)RemoveImpl(fileName); + lock_guard<mutex> lock(m_lock); + (void)DeregisterImpl(fileName); } -bool MwmSet::RemoveImpl(string const & fileName) +bool MwmSet::DeregisterImpl(string const & fileName) { bool ret = true; MwmId const id = GetIdByName(fileName); if (id != INVALID_MWM_ID) { - ret = RemoveImpl(id); + ret = DeregisterImpl(id); ClearCache(id); } @@ -181,13 +175,12 @@ bool MwmSet::RemoveImpl(string const & fileName) return ret; } -void MwmSet::RemoveAll() +void MwmSet::DeregisterAll() { - threads::MutexGuard mutexGuard(m_lock); - UNUSED_VALUE(mutexGuard); + lock_guard<mutex> lock(m_lock); for (MwmId i = 0; i < m_info.size(); ++i) - (void)RemoveImpl(i); + (void)DeregisterImpl(i); // do not call ClearCache - it's under mutex lock ClearCacheImpl(m_cache.begin(), m_cache.end()); @@ -196,20 +189,16 @@ void MwmSet::RemoveAll() bool MwmSet::IsLoaded(string const & file) const { MwmSet * p = const_cast<MwmSet *>(this); - - threads::MutexGuard mutexGuard(p->m_lock); - UNUSED_VALUE(mutexGuard); + lock_guard<mutex> lock(p->m_lock); MwmId const id = p->GetIdByName(file + DATA_FILE_EXTENSION); - return (id != INVALID_MWM_ID && m_info[id].IsExist()); + return (id != INVALID_MWM_ID && m_info[id].IsRegistered()); } void MwmSet::GetMwmInfo(vector<MwmInfo> & info) const { MwmSet * p = const_cast<MwmSet *>(this); - - threads::MutexGuard mutexGuard(p->m_lock); - UNUSED_VALUE(mutexGuard); + lock_guard<mutex> lock(p->m_lock); for (MwmId i = 0; i < m_info.size(); ++i) p->UpdateMwmInfo(i); @@ -219,15 +208,14 @@ void MwmSet::GetMwmInfo(vector<MwmInfo> & info) const MwmSet::MwmValueBase * MwmSet::LockValue(MwmId id) { - threads::MutexGuard mutexGuard(m_lock); - UNUSED_VALUE(mutexGuard); + lock_guard<mutex> lock(m_lock); ASSERT_LESS(id, m_info.size(), ()); if (id >= m_info.size()) return NULL; UpdateMwmInfo(id); - if (!m_info[id].IsActive()) + if (!m_info[id].IsUpToDate()) return NULL; ++m_info[id].m_lockCount; @@ -247,8 +235,7 @@ MwmSet::MwmValueBase * MwmSet::LockValue(MwmId id) void MwmSet::UnlockValue(MwmId id, MwmValueBase * p) { - threads::MutexGuard mutexGuard(m_lock); - UNUSED_VALUE(mutexGuard); + lock_guard<mutex> lock(m_lock); ASSERT(p, (id)); ASSERT_LESS(id, m_info.size(), ()); @@ -260,7 +247,7 @@ void MwmSet::UnlockValue(MwmId id, MwmValueBase * p) --m_info[id].m_lockCount; UpdateMwmInfo(id); - if (m_info[id].IsActive()) + if (m_info[id].IsUpToDate()) { m_cache.push_back(make_pair(id, p)); if (m_cache.size() > m_cacheSize) @@ -276,8 +263,7 @@ void MwmSet::UnlockValue(MwmId id, MwmValueBase * p) void MwmSet::ClearCache() { - threads::MutexGuard mutexGuard(m_lock); - UNUSED_VALUE(mutexGuard); + lock_guard<mutex> lock(m_lock); ClearCacheImpl(m_cache.begin(), m_cache.end()); } |