forked from organicmaps/organicmaps
[search] Dummy thread search with 2 mutex and RunAsync.
This commit is contained in:
parent
63fbe9f8a8
commit
491ecba905
4 changed files with 50 additions and 7 deletions
|
@ -407,6 +407,7 @@ static void OnSearchResultCallback(search::Results const & res, int queryId)
|
|||
- (void)addResult:(id)result
|
||||
{
|
||||
/// @todo Temporary, for test.
|
||||
LOG(LINFO, ("Clear results"));
|
||||
m_results.clear();
|
||||
|
||||
search::Results const * r = [result get];
|
||||
|
|
|
@ -65,6 +65,8 @@ public:
|
|||
typedef vector<Result>::const_iterator IterT;
|
||||
IterT Begin() const { return m_vec.begin(); }
|
||||
IterT End() const { return m_vec.end(); }
|
||||
|
||||
size_t Count() const { return m_vec.size(); }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include "../indexer/search_string_utils.hpp"
|
||||
#include "../indexer/mercator.hpp"
|
||||
|
||||
#include "../platform/platform.hpp"
|
||||
|
||||
#include "../geometry/distance_on_sphere.hpp"
|
||||
|
||||
#include "../base/logging.hpp"
|
||||
|
@ -21,6 +23,7 @@
|
|||
#include "../std/string.hpp"
|
||||
#include "../std/utility.hpp"
|
||||
#include "../std/vector.hpp"
|
||||
#include "../std/bind.hpp"
|
||||
|
||||
|
||||
namespace search
|
||||
|
@ -145,16 +148,49 @@ void Engine::SetPreferredLanguage(string const & lang)
|
|||
m_pQuery->SetPreferredLanguage(lang);
|
||||
}
|
||||
|
||||
void Engine::Search(string const & queryText, SearchCallbackT const & f)
|
||||
void Engine::Search(string const & query, SearchCallbackT const & callback)
|
||||
{
|
||||
LOG(LDEBUG, (queryText));
|
||||
{
|
||||
// Update new search params.
|
||||
threads::MutexGuard guard(m_updateMutex);
|
||||
m_callback = callback;
|
||||
m_queryText = query;
|
||||
}
|
||||
|
||||
m_callback = f;
|
||||
m_queryText = queryText;
|
||||
GetPlatform().RunAsync(bind(&Engine::SearchAsync, this));
|
||||
}
|
||||
|
||||
void Engine::SearchAsync()
|
||||
{
|
||||
Results res;
|
||||
m_pQuery->Search(queryText, res);
|
||||
SearchCallbackT f;
|
||||
|
||||
{
|
||||
// Enter to run new search.
|
||||
threads::MutexGuard searchGuard(m_searchMutex);
|
||||
|
||||
{
|
||||
// First - get new search params.
|
||||
threads::MutexGuard updateGuard(m_updateMutex);
|
||||
|
||||
if (m_queryInProgress != m_queryText)
|
||||
{
|
||||
m_queryInProgress = m_queryText;
|
||||
f = m_callback;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If search params don't changed - skip this query.
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
LOG(LINFO, ("Call search for query: ", m_queryInProgress));
|
||||
m_pQuery->Search(m_queryInProgress, res);
|
||||
}
|
||||
|
||||
// emit results without search mutex lock
|
||||
LOG(LINFO, ("Emit search results: ", res.Count()));
|
||||
f(res);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include "../base/base.hpp"
|
||||
#include "../base/string_utils.hpp"
|
||||
#include "../base/mutex.hpp"
|
||||
|
||||
#include "../std/scoped_ptr.hpp"
|
||||
#include "../std/string.hpp"
|
||||
|
@ -45,17 +46,20 @@ public:
|
|||
|
||||
void EnablePositionTrack(bool enable);
|
||||
|
||||
void Search(string const & query, SearchCallbackT const & f);
|
||||
void Search(string const & query, SearchCallbackT const & callback);
|
||||
|
||||
string GetCountryFile(m2::PointD const & pt) const;
|
||||
|
||||
private:
|
||||
void InitializeCategoriesAndSuggestStrings(CategoriesHolder const & categories);
|
||||
|
||||
void SearchAsync();
|
||||
void RepeatSearch();
|
||||
|
||||
threads::Mutex m_searchMutex, m_updateMutex;
|
||||
|
||||
SearchCallbackT m_callback;
|
||||
string m_queryText;
|
||||
string m_queryText, m_queryInProgress;
|
||||
m2::RectD m_savedViewport;
|
||||
|
||||
bool m_trackEnable;
|
||||
|
|
Loading…
Add table
Reference in a new issue