Factor out CoveringGetter. Use in index and search.

This commit is contained in:
vng 2011-09-27 17:29:19 +03:00 committed by Alex Zolotarev
parent f21e3ff4ac
commit 1545e4a6d4
5 changed files with 73 additions and 77 deletions

View file

@ -197,4 +197,30 @@ int GetCodingDepth(pair<int, int> const & scalesR)
return (RectId::DEPTH_LEVELS - delta);
}
IntervalsT const & CoveringGetter::Get(pair<int, int> const & scaleR)
{
int const cellDepth = GetCodingDepth(scaleR);
int const ind = (cellDepth == RectId::DEPTH_LEVELS ? 0 : 1);
if (m_res[ind].empty())
{
switch (m_mode)
{
case 0:
CoverViewportAndAppendLowerLevels(m_rect, cellDepth, m_res[ind]);
break;
case 1:
AppendLowerLevels(GetRectIdAsIs(m_rect), cellDepth, m_res[ind]);
break;
case 2:
m_res[ind].push_back(IntervalsT::value_type(0, static_cast<int64_t>((1ULL << 63) - 1)));
break;
}
}
return m_res[ind];
}
}

View file

@ -32,4 +32,21 @@ namespace covering
// Calculate cell coding depth according to max visual scale for mwm.
int GetCodingDepth(pair<int, int> const & scalesR);
class CoveringGetter
{
IntervalsT m_res[2];
m2::RectD const & m_rect;
int m_mode;
public:
/// @param[in] mode\n
/// - 0 - cover viewport with low lovels;\n
/// - 1 - cover append low levels only;\n
/// - 2 - make full cover\n
CoveringGetter(m2::RectD const & r, int mode) : m_rect(r), m_mode(mode) {}
IntervalsT const & Get(pair<int, int> const & scaleR);
};
}

View file

@ -35,31 +35,3 @@ Index::~Index()
{
Cleanup();
}
using namespace covering;
IntervalsT const & Index::CoveringGetter::Get(feature::DataHeader const & header)
{
int const cellDepth = GetCodingDepth(header.GetScaleRange());
int const ind = (cellDepth == RectId::DEPTH_LEVELS ? 0 : 1);
if (m_res[ind].empty())
{
switch (m_mode)
{
case 0:
CoverViewportAndAppendLowerLevels(m_rect, cellDepth, m_res[ind]);
break;
case 1:
AppendLowerLevels(GetRectIdAsIs(m_rect), cellDepth, m_res[ind]);
break;
case 2:
m_res[ind].push_back(IntervalsT::value_type(0, static_cast<int64_t>((1ULL << 63) - 1)));
break;
}
}
return m_res[ind];
}

View file

@ -89,31 +89,13 @@ private:
}
};
class CoveringGetter
{
typedef covering::IntervalsT ResT;
ResT m_res[2];
m2::RectD const & m_rect;
int m_mode;
public:
/// @param[in] mode\n
/// - 0 - cover viewport with low lovels;\n
/// - 1 - cover append low levels only;\n
/// - 2 - make full cover\n
CoveringGetter(m2::RectD const & r, int mode) : m_rect(r), m_mode(mode) {}
ResT const & Get(feature::DataHeader const & header);
};
template <typename F>
void ForEachInIntervals(F & f, int mode, m2::RectD const & rect, uint32_t scale) const
{
vector<MwmInfo> mwm;
GetMwmInfo(mwm);
CoveringGetter cov(rect, mode);
covering::CoveringGetter cov(rect, mode);
for (MwmId id = 0; id < mwm.size(); ++id)
{
@ -127,7 +109,7 @@ private:
feature::DataHeader const & header = pValue->GetHeader();
// prepare needed covering
covering::IntervalsT const & interval = cov.Get(header);
covering::IntervalsT const & interval = cov.Get(header.GetScaleRange());
// prepare features reading
FeaturesVector fv(pValue->m_cont, header);

View file

@ -47,9 +47,8 @@ void Query::UpdateViewportOffsets()
m_pIndex->GetMwmInfo(mwmInfo);
m_offsetsInViewport.resize(mwmInfo.size());
int const scale = min(max(scales::GetScaleLevel(m_viewport) + 7, 10), scales::GetUpperScale());
covering::IntervalsT intervals[2];
bool intervalCovered[2] = {false, false};
int const viewScale = scales::GetScaleLevel(m_viewport);
covering::CoveringGetter cov(m_viewport, 0);
for (MwmSet::MwmId mwmId = 0; mwmId < mwmInfo.size(); ++mwmId)
{
@ -57,28 +56,24 @@ void Query::UpdateViewportOffsets()
if (m_viewportExtended.IsIntersect(mwmInfo[mwmId].m_limitRect))
{
Index::MwmLock mwmLock(*m_pIndex, mwmId);
if (MwmValue * pMwmValue = mwmLock.GetValue())
if (MwmValue * pMwm = mwmLock.GetValue())
{
feature::DataHeader const & header = pMwmValue->GetHeader();
feature::DataHeader const & header = pMwm->GetHeader();
if (header.GetType() == feature::DataHeader::worldcoasts)
continue;
// TODO: Refactor me!
// prepare needed covering
int const cellDepth = covering::GetCodingDepth(header.GetScaleRange());
int const ind = (cellDepth == RectId::DEPTH_LEVELS ? 0 : 1);
pair<int, int> const scaleR = header.GetScaleRange();
int const scale = min(max(viewScale + 7, scaleR.first), scaleR.second);
if (!intervalCovered[ind])
{
covering::CoverViewportAndAppendLowerLevels(m_viewport, cellDepth, intervals[ind]);
intervalCovered[ind] = true;
}
covering::IntervalsT const & interval = cov.Get(scaleR);
ScaleIndex<ModelReaderPtr> index(pMwmValue->m_cont.GetReader(INDEX_FILE_TAG),
pMwmValue->m_factory);
ScaleIndex<ModelReaderPtr> index(pMwm->m_cont.GetReader(INDEX_FILE_TAG),
pMwm->m_factory);
for (size_t i = 0; i < intervals[ind].size(); ++i)
for (size_t i = 0; i < interval.size(); ++i)
{
index.ForEachInIntervalAndScale(MakeInsertFunctor(m_offsetsInViewport[mwmId]),
intervals[ind][i].first, intervals[ind][i].second,
interval[i].first, interval[i].second,
scale);
}
}
@ -182,19 +177,23 @@ void Query::SearchFeatures()
if (m_viewportExtended.IsIntersect(mwmInfo[mwmId].m_limitRect))
{
Index::MwmLock mwmLock(*m_pIndex, mwmId);
if (MwmValue * pMwmValue = mwmLock.GetValue())
if (MwmValue * pMwm = mwmLock.GetValue())
{
scoped_ptr<TrieIterator> pTrieRoot(::trie::reader::ReadTrie(
pMwmValue->m_cont.GetReader(SEARCH_INDEX_FILE_TAG),
::search::trie::ValueReader(),
::search::trie::EdgeValueReader()));
if (pTrieRoot)
if (pMwm->m_cont.IsReaderExist(SEARCH_INDEX_FILE_TAG))
{
FeaturesVector featuresVector(pMwmValue->m_cont, pMwmValue->GetHeader());
impl::FeatureLoader f(featuresVector, *this);
MatchFeaturesInTrie(m_tokens.data(), m_tokens.size(), m_prefix, *pTrieRoot,
&m_offsetsInViewport[mwmId], f, m_results.max_size() * 10);
LOG(LINFO, ("Matched: ", f.m_count));
scoped_ptr<TrieIterator> pTrieRoot(::trie::reader::ReadTrie(
pMwm->m_cont.GetReader(SEARCH_INDEX_FILE_TAG),
::search::trie::ValueReader(),
::search::trie::EdgeValueReader()));
if (pTrieRoot)
{
FeaturesVector featuresVector(pMwm->m_cont, pMwm->GetHeader());
impl::FeatureLoader f(featuresVector, *this);
MatchFeaturesInTrie(m_tokens.data(), m_tokens.size(), m_prefix, *pTrieRoot,
&m_offsetsInViewport[mwmId], f, m_results.max_size() * 10);
LOG(LINFO, ("Matched: ", f.m_count));
}
}
}
}