[search] Bookmarks are exposed to search::Processor.

This commit is contained in:
Yuri Gorshenin 2017-12-25 16:04:52 +03:00 committed by Tatiana Yan
parent 75740e844a
commit bc01869e14
14 changed files with 158 additions and 21 deletions

View file

@ -433,17 +433,17 @@ Framework::Framework(FrameworkParams const & params)
m_bmManager = make_unique<BookmarkManager>(BookmarkManager::Callbacks(
[this]() -> StringsBundle const & { return m_stringsBundle; },
[](std::vector<std::pair<df::MarkID, BookmarkData>> const & marks)
[this](std::vector<std::pair<df::MarkID, BookmarkData>> const & marks)
{
// TODO: Add processing of the created marks.
GetSearchAPI().OnBookmarksCreated(marks);
},
[](std::vector<std::pair<df::MarkID, BookmarkData>> const & marks)
[this](std::vector<std::pair<df::MarkID, BookmarkData>> const & marks)
{
// TODO: Add processing of the updated marks.
GetSearchAPI().OnBookmarksUpdated(marks);
},
[](std::vector<df::MarkID> const & marks)
[this](std::vector<df::MarkID> const & marks)
{
// TODO: Add processing of the deleted marks.
GetSearchAPI().OnBookmarksDeleted(marks);
}));
m_ParsedMapApi.SetBookmarkManager(m_bmManager.get());

View file

@ -5,6 +5,7 @@
#include "search/geometry_utils.hpp"
#include "search/hotels_filter.hpp"
#include "search/bookmarks/processor.hpp"
#include "storage/downloader_search_params.hpp"
@ -14,14 +15,18 @@
#include "base/string_utils.hpp"
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <type_traits>
using namespace search;
using namespace std;
namespace
{
using BookmarkIdDoc = pair<bookmarks::Id, bookmarks::Doc>;
double const kDistEqualQueryMeters = 100.0;
// Cancels search query by |handle|.
@ -38,6 +43,39 @@ bool IsCianMode(string query)
strings::AsciiToLower(query);
return query == "cian";
}
bookmarks::Id MarkIDToBookmarkId(df::MarkID id)
{
static_assert(is_integral<df::MarkID>::value, "");
static_assert(is_integral<bookmarks::Id>::value, "");
static_assert(is_unsigned<df::MarkID>::value, "");
static_assert(is_unsigned<bookmarks::Id>::value, "");
static_assert(sizeof(bookmarks::Id) >= sizeof(df::MarkID), "");
return static_cast<bookmarks::Id>(id);
}
void AppendBookmarkIdDocs(vector<pair<df::MarkID, BookmarkData>> const & marks,
vector<BookmarkIdDoc> & result)
{
result.reserve(result.size() + marks.size());
for (auto const & mark : marks)
{
auto const & id = mark.first;
auto const & data = mark.second;
result.emplace_back(MarkIDToBookmarkId(id),
bookmarks::Doc(data.GetName(), data.GetDescription(), data.GetType()));
}
}
void AppendBookmarkIds(vector<df::MarkID> const & marks, vector<bookmarks::Id> & result)
{
result.reserve(result.size() + marks.size());
transform(marks.begin(), marks.end(), back_inserter(result), MarkIDToBookmarkId);
}
} // namespace
SearchAPI::SearchAPI(Index & index, storage::Storage const & storage,
@ -204,7 +242,7 @@ void SearchAPI::CancelAllSearches()
CancelSearch(static_cast<Mode>(i));
}
void SearchAPI::RunUITask(std::function<void()> fn) { return m_delegate.RunUITask(fn); }
void SearchAPI::RunUITask(function<void()> fn) { return m_delegate.RunUITask(fn); }
void SearchAPI::SetHotelDisplacementMode()
{
@ -227,6 +265,27 @@ bool SearchAPI::IsLocalAdsCustomer(Result const & result) const
return m_delegate.IsLocalAdsCustomer(result);
}
void SearchAPI::OnBookmarksCreated(vector<pair<df::MarkID, BookmarkData>> const & marks)
{
vector<BookmarkIdDoc> data;
AppendBookmarkIdDocs(marks, data);
m_engine.OnBookmarksCreated(data);
}
void SearchAPI::OnBookmarksUpdated(vector<std::pair<df::MarkID, BookmarkData>> const & marks)
{
vector<BookmarkIdDoc> data;
AppendBookmarkIdDocs(marks, data);
m_engine.OnBookmarksUpdated(data);
}
void SearchAPI::OnBookmarksDeleted(vector<df::MarkID> const & marks)
{
vector<bookmarks::Id> data;
AppendBookmarkIds(marks, data);
m_engine.OnBookmarksDeleted(data);
}
bool SearchAPI::Search(SearchParams const & params, bool forceSearch)
{
if (m_delegate.ParseSearchQueryCommand(params))
@ -300,7 +359,7 @@ bool SearchAPI::QueryMayBeSkipped(SearchParams const & prevParams,
return true;
}
void SearchAPI::UpdateSponsoredMode(std::string const & query,
void SearchAPI::UpdateSponsoredMode(string const & query,
booking::filter::availability::Params const & params)
{
m_sponsoredMode = SponsoredMode::None;

View file

@ -1,5 +1,7 @@
#pragma once
#include "map/bookmark.hpp"
#include "search/downloader_search_callback.hpp"
#include "search/engine.hpp"
#include "search/everywhere_search_callback.hpp"
@ -8,6 +10,8 @@
#include "search/search_params.hpp"
#include "search/viewport_search_callback.hpp"
#include "drape_frontend/user_marks_provider.hpp"
#include "geometry/point2d.hpp"
#include "geometry/rect2d.hpp"
@ -15,6 +19,8 @@
#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <boost/optional.hpp>
@ -132,6 +138,10 @@ public:
search::Results::ConstIter end) override;
bool IsLocalAdsCustomer(search::Result const & result) const override;
void OnBookmarksCreated(std::vector<std::pair<df::MarkID, BookmarkData>> const & marks);
void OnBookmarksUpdated(std::vector<std::pair<df::MarkID, BookmarkData>> const & marks);
void OnBookmarksDeleted(std::vector<df::MarkID> const & marks);
private:
struct SearchIntent
{

View file

@ -7,6 +7,7 @@ set(
approximate_string_match.hpp
base/inverted_list.hpp
base/mem_search_index.hpp
bookmarks/data.cpp
bookmarks/data.hpp
bookmarks/processor.cpp
bookmarks/processor.hpp

21
search/bookmarks/data.cpp Normal file
View file

@ -0,0 +1,21 @@
#include "search/bookmarks/data.hpp"
#include <sstream>
using namespace std;
namespace search
{
namespace bookmarks
{
string DebugPrint(Data const & data)
{
ostringstream os;
os << "Data [";
os << "name: " << data.m_name << ", ";
os << "description: " << data.m_description << ", ";
os << "type: " << data.m_type << "]";
return os.str();
}
} // namespace bookmarks
} // namespace search

View file

@ -38,5 +38,7 @@ struct Data
std::string m_description;
std::string m_type;
};
std::string DebugPrint(Data const & data);
} // namespace bookmarks
} // namespace search

View file

@ -37,7 +37,7 @@ struct RankingInfo
struct IdInfoPair
{
IdInfoPair(Processor::Id const & id, RankingInfo const & info) : m_id(id), m_info(info) {}
IdInfoPair(Id const & id, RankingInfo const & info) : m_id(id), m_info(info) {}
bool operator<(IdInfoPair const & rhs) const
{
@ -46,7 +46,7 @@ struct IdInfoPair
return m_id < rhs.m_id;
}
Processor::Id m_id;
Id m_id;
RankingInfo m_info;
};
@ -76,7 +76,7 @@ void Processor::Erase(Id const & id, Doc const & doc)
m_docs.erase(id);
}
vector<Processor::Id> Processor::Search(QueryParams const & params) const
vector<Id> Processor::Search(QueryParams const & params) const
{
set<Id> ids;
auto insertId = MakeInsertFunctor(ids);

View file

@ -17,11 +17,12 @@ namespace search
{
namespace bookmarks
{
using Id = uint64_t;
using Doc = Data;
class Processor : public IdfMap::Delegate
{
public:
using Id = uint64_t;
using Doc = Data;
using Index = base::MemSearchIndex<Id, Doc>;
~Processor() override = default;

View file

@ -157,6 +157,24 @@ void Engine::LoadCitiesBoundaries()
[this](Processor & processor) { processor.LoadCitiesBoundaries(); });
}
void Engine::OnBookmarksCreated(vector<pair<bookmarks::Id, bookmarks::Doc>> const & marks)
{
PostMessage(Message::TYPE_BROADCAST,
[this, marks](Processor & processor) { processor.OnBookmarksCreated(marks); });
}
void Engine::OnBookmarksUpdated(vector<pair<bookmarks::Id, bookmarks::Doc>> const & marks)
{
PostMessage(Message::TYPE_BROADCAST,
[this, marks](Processor & processor) { processor.OnBookmarksUpdated(marks); });
}
void Engine::OnBookmarksDeleted(vector<bookmarks::Id> const & marks)
{
PostMessage(Message::TYPE_BROADCAST,
[this, marks](Processor & processor) { processor.OnBookmarksDeleted(marks); });
}
void Engine::MainLoop(Context & context)
{
while (true)

View file

@ -1,5 +1,6 @@
#pragma once
#include "search/bookmarks/processor.hpp"
#include "search/result.hpp"
#include "search/search_params.hpp"
#include "search/suggest.hpp"
@ -19,6 +20,7 @@
#include "std/shared_ptr.hpp"
#include "std/string.hpp"
#include "std/unique_ptr.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"
#include "std/weak_ptr.hpp"
@ -105,10 +107,14 @@ public:
// Posts request to reload cities boundaries tables.
void LoadCitiesBoundaries();
void OnBookmarksCreated(vector<pair<bookmarks::Id, bookmarks::Doc>> const & marks);
void OnBookmarksUpdated(vector<pair<bookmarks::Id, bookmarks::Doc>> const & marks);
void OnBookmarksDeleted(vector<bookmarks::Id> const & marks);
private:
struct Message
{
using TFn = function<void(Processor & processor)>;
using Fn = function<void(Processor & processor)>;
enum Type
{
@ -116,12 +122,13 @@ private:
TYPE_BROADCAST
};
Message(Type type, TFn && fn) : m_type(type), m_fn(move(fn)) {}
template <typename Gn>
Message(Type type, Gn && gn) : m_type(type), m_fn(forward<Gn>(gn)) {}
void operator()(Processor & processor) { m_fn(processor); }
Type m_type;
TFn m_fn;
Fn m_fn;
};
// alignas() is used here to prevent false-sharing between different

View file

@ -317,6 +317,21 @@ void Processor::LoadCitiesBoundaries()
LOG(LWARNING, ("Can't load cities boundaries"));
}
void Processor::OnBookmarksCreated(vector<pair<bookmarks::Id, bookmarks::Doc>> const & /* marks */)
{
// TODO(@y): do something useful with marks.
}
void Processor::OnBookmarksUpdated(vector<pair<bookmarks::Id, bookmarks::Doc>> const & /* marks */)
{
// TODO(@y): do something useful with marks.
}
void Processor::OnBookmarksDeleted(vector<bookmarks::Id> const & /* marks */)
{
// TODO(@y): do something useful with marks.
}
Locales Processor::GetCategoryLocales() const
{
static int8_t const enLocaleCode = CategoriesHolder::MapLocaleToInteger("en");

View file

@ -1,5 +1,6 @@
#pragma once
#include "search/bookmarks/processor.hpp"
#include "search/categories_cache.hpp"
#include "search/categories_set.hpp"
#include "search/cities_boundaries_table.hpp"
@ -27,6 +28,7 @@
#include "std/cstdint.hpp"
#include "std/string.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"
class FeatureType;
@ -90,6 +92,10 @@ public:
void ClearCaches();
void LoadCitiesBoundaries();
void OnBookmarksCreated(vector<pair<bookmarks::Id, bookmarks::Doc>> const & marks);
void OnBookmarksUpdated(vector<pair<bookmarks::Id, bookmarks::Doc>> const & marks);
void OnBookmarksDeleted(vector<bookmarks::Id> const & marks);
protected:
Locales GetCategoryLocales() const;

View file

@ -585,7 +585,7 @@ UNIT_CLASS_TEST(ProcessorTest, TestPostcodes)
Index::FeaturesLoaderGuard loader(m_index, countryId);
FeatureType ft;
TEST(loader.GetFeatureByIndex(base::checked_cast<uint32_t>(index), ft), ());
TEST(loader.GetFeatureByIndex(::base::checked_cast<uint32_t>(index), ft), ());
auto rule = ExactMatch(countryId, building31);
TEST(rule->Matches(ft), ());

View file

@ -18,9 +18,6 @@ namespace
class BookmarksProcessorTest
{
public:
using Id = Processor::Id;
using Doc = Processor::Doc;
void Add(Id const & id, Doc const & doc) { m_processor.Add(id, doc); }
void Erase(Id const & id, Doc const & doc) { m_processor.Erase(id, doc); }