GetDefaultCategories() function to access categories from everywhere.

This commit is contained in:
Alex Zolotarev 2016-02-15 21:28:58 +03:00 committed by Sergey Yershov
parent 64d7f76943
commit 68fec8f1f4
8 changed files with 26 additions and 18 deletions

View file

@ -44,10 +44,7 @@ private:
Name2CatContT m_name2type;
public:
CategoriesHolder() {}
/// Takes ownership of reader.
explicit CategoriesHolder(Reader * reader);
void LoadFromStream(istream & s);
template <class ToDo>
@ -104,3 +101,6 @@ inline void swap(CategoriesHolder & a, CategoriesHolder & b)
{
return a.Swap(b);
}
// Defined in categories_holder_loader.cpp.
CategoriesHolder const & GetDefaultCategories();

View file

@ -0,0 +1,11 @@
#include "categories_holder.hpp"
#include "platform/platform.hpp"
#include "defines.hpp"
CategoriesHolder const & GetDefaultCategories()
{
static CategoriesHolder const instance(GetPlatform().GetReader(SEARCH_CATEGORIES_FILE_NAME));
return instance;
}

View file

@ -11,6 +11,7 @@ include($$ROOT_DIR/common.pri)
SOURCES += \
categories_holder.cpp \
categories_holder_loader.cpp \
classificator.cpp \
classificator_loader.cpp \
coding_params.cpp \

View file

@ -5,6 +5,7 @@
#include "indexer/classificator_loader.hpp"
#include "coding/multilang_utf8_string.hpp"
#include "coding/reader.hpp"
#include "std/sstream.hpp"
@ -86,10 +87,7 @@ UNIT_TEST(LoadCategories)
{
classificator::Load();
CategoriesHolder h;
istringstream buffer(TEST_STRING);
h.LoadFromStream(buffer);
CategoriesHolder h(new MemReader(TEST_STRING, strlen(TEST_STRING)));
size_t count = 0;
Checker f(count);
h.ForEachCategory(f);

View file

@ -1116,12 +1116,10 @@ void Framework::InitSearchEngine()
{
ASSERT(!m_searchEngine.get(), ("InitSearchEngine() must be called only once."));
ASSERT(m_infoGetter.get(), ());
Platform const & platform = GetPlatform();
try
{
m_searchEngine.reset(new search::Engine(
const_cast<Index &>(m_model.GetIndex()), platform.GetReader(SEARCH_CATEGORIES_FILE_NAME),
const_cast<Index &>(m_model.GetIndex()), GetDefaultCategories(),
*m_infoGetter, languages::GetCurrentOrig(), make_unique<search::SearchQueryFactory>()));
}
catch (RootException const & e)

View file

@ -112,9 +112,9 @@ void QueryHandle::Detach()
m_query = nullptr;
}
Engine::Engine(Index & index, Reader * categoriesR, storage::CountryInfoGetter const & infoGetter,
Engine::Engine(Index & index, CategoriesHolder const & categories, storage::CountryInfoGetter const & infoGetter,
string const & locale, unique_ptr<SearchQueryFactory> && factory)
: m_categories(categoriesR), m_factory(move(factory)), m_shutdown(false)
: m_categories(categories), m_factory(move(factory)), m_shutdown(false)
{
InitSuggestions doInit;
m_categories.ForEachName(bind<void>(ref(doInit), _1));

View file

@ -78,7 +78,7 @@ class Engine
{
public:
// Doesn't take ownership of index. Takes ownership of categoriesR.
Engine(Index & index, Reader * categoriesR, storage::CountryInfoGetter const & infoGetter,
Engine(Index & index, CategoriesHolder const & categories, storage::CountryInfoGetter const & infoGetter,
string const & locale, unique_ptr<SearchQueryFactory> && factory);
~Engine();
@ -112,7 +112,7 @@ private:
void DoClearCaches();
CategoriesHolder m_categories;
CategoriesHolder const & m_categories;
vector<Suggest> m_suggests;
unique_ptr<Query> m_query;

View file

@ -54,7 +54,7 @@ TestSearchEngine::TestSearchEngine(string const & locale)
: m_platform(GetPlatform())
, m_infoGetter(new storage::CountryInfoReader(m_platform.GetReader(PACKED_POLYGONS_FILE),
m_platform.GetReader(COUNTRIES_FILE)))
, m_engine(*this, m_platform.GetReader(SEARCH_CATEGORIES_FILE_NAME), *m_infoGetter, locale,
, m_engine(*this, GetDefaultCategories(), *m_infoGetter, locale,
make_unique<TestSearchQueryFactory>())
{
}
@ -63,7 +63,7 @@ TestSearchEngine::TestSearchEngine(string const & locale,
unique_ptr<storage::CountryInfoGetter> infoGetter)
: m_platform(GetPlatform())
, m_infoGetter(move(infoGetter))
, m_engine(*this, m_platform.GetReader(SEARCH_CATEGORIES_FILE_NAME), *m_infoGetter, locale,
, m_engine(*this, GetDefaultCategories(), *m_infoGetter, locale,
make_unique<TestSearchQueryFactory>())
{
}
@ -73,7 +73,7 @@ TestSearchEngine::TestSearchEngine(string const & locale,
unique_ptr<::search::SearchQueryFactory> factory)
: m_platform(GetPlatform())
, m_infoGetter(move(infoGetter))
, m_engine(*this, m_platform.GetReader(SEARCH_CATEGORIES_FILE_NAME), *m_infoGetter, locale,
, m_engine(*this, GetDefaultCategories(), *m_infoGetter, locale,
move(factory))
{
}
@ -83,7 +83,7 @@ TestSearchEngine::TestSearchEngine(string const & locale,
: m_platform(GetPlatform())
, m_infoGetter(new storage::CountryInfoReader(m_platform.GetReader(PACKED_POLYGONS_FILE),
m_platform.GetReader(COUNTRIES_FILE)))
, m_engine(*this, m_platform.GetReader(SEARCH_CATEGORIES_FILE_NAME), *m_infoGetter, locale,
, m_engine(*this, GetDefaultCategories(), *m_infoGetter, locale,
move(factory))
{
}