1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
#include "map/booking_availability_filter.hpp"
#include "search/result.hpp"
#include "partners_api/booking_api.hpp"
#include "editor/editable_data_source.hpp"
#include "indexer/data_source.hpp"
#include "indexer/feature_decl.hpp"
#include "indexer/ftypes_sponsored.hpp"
#include "platform/platform.hpp"
#include "base/macros.hpp"
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
using namespace booking::filter;
namespace
{
auto constexpr kMaxCountInRequest = booking::RawApi::GetMaxHotelsInAvailabilityRequest();
template <typename T>
class HotelInfo
{
public:
explicit HotelInfo(T const & requestedVia) : m_mappedValue(requestedVia) {}
HotelInfo(std::string const & hotelId, availability::Cache::HotelStatus const & cacheStatus,
T const & mappedValue)
: m_hotelId(hotelId), m_cacheStatus(cacheStatus), m_mappedValue(mappedValue)
{
}
void SetHotelId(std::string const & hotelId) { m_hotelId = hotelId; }
void SetStatus(availability::Cache::HotelStatus cacheStatus) { m_cacheStatus = cacheStatus; }
std::string const & GetHotelId() const { return m_hotelId; }
availability::Cache::HotelStatus GetStatus() const { return m_cacheStatus; }
T const & GetMappedValue() const { return m_mappedValue; }
T & GetMappedValue() { return m_mappedValue; }
private:
std::string m_hotelId;
availability::Cache::HotelStatus m_cacheStatus = availability::Cache::HotelStatus::Absent;
T m_mappedValue;
};
template <typename T>
using HotelsMapping = std::vector<HotelInfo<T>>;
using HotelToResult = HotelInfo<search::Result>;
using HotelToFeatureId = HotelInfo<FeatureID>;
using HotelToResults = HotelsMapping<search::Result>;
using HotelToFeatureIds = HotelsMapping<FeatureID>;
template <typename T>
void UpdateCache(HotelsMapping<T> const & hotelsMapping, std::vector<std::string> const & hotelIds,
availability::Cache & cache)
{
using availability::Cache;
ASSERT(std::is_sorted(hotelIds.begin(), hotelIds.end()), ());
for (auto & hotel : hotelsMapping)
{
if (hotel.GetStatus() != Cache::HotelStatus::Absent)
continue;
if (std::binary_search(hotelIds.cbegin(), hotelIds.cend(), hotel.GetHotelId()))
cache.Insert(hotel.GetHotelId(), Cache::HotelStatus::Available);
else
cache.Insert(hotel.GetHotelId(), Cache::HotelStatus::Unavailable);
}
}
template <typename T, typename Inserter>
void FillResults(HotelsMapping<T> && hotelsMapping, std::vector<std::string> const & hotelIds,
availability::Cache & cache, Inserter const & inserter)
{
using availability::Cache;
ASSERT(std::is_sorted(hotelIds.begin(), hotelIds.end()), ());
for (auto & hotel : hotelsMapping)
{
switch (hotel.GetStatus())
{
case Cache::HotelStatus::Unavailable: continue;
case Cache::HotelStatus::Available:
{
inserter(std::move(hotel.GetMappedValue()));
continue;
}
case Cache::HotelStatus::NotReady:
{
auto hotelStatus = cache.Get(hotel.GetHotelId());
if (hotelStatus == Cache::HotelStatus::Available)
inserter(std::move(hotel.GetMappedValue()));
continue;
}
case Cache::HotelStatus::Absent:
{
if (std::binary_search(hotelIds.cbegin(), hotelIds.cend(), hotel.GetHotelId()))
inserter(std::move(hotel.GetMappedValue()));
continue;
}
}
}
}
void FillResults(HotelToResults && hotelToResults, std::vector<std::string> const & hotelIds,
availability::Cache & cache, search::Results & results)
{
auto const inserter = [&results](search::Result && result)
{
results.AddResult(std::move(result));
};
FillResults(std::move(hotelToResults), hotelIds, cache, inserter);
}
void FillResults(HotelToFeatureIds && hotelToFeatureIds, std::vector<std::string> const & hotelIds,
availability::Cache & cache, std::vector<FeatureID> & results)
{
auto const inserter = [&results](FeatureID && result)
{
results.emplace_back(std::move(result));
};
FillResults(std::move(hotelToFeatureIds), hotelIds, cache, inserter);
}
void PrepareData(DataSource const & dataSource, search::Results const & results,
HotelToResults & hotelToResults, availability::Cache & cache,
booking::AvailabilityParams & p)
{
std::vector<FeatureID> features;
for (auto const & r : results)
{
if (!r.m_metadata.m_isSponsoredHotel || r.GetResultType() != search::Result::Type::Feature)
continue;
features.push_back(r.GetFeatureID());
hotelToResults.emplace_back(r);
}
std::sort(features.begin(), features.end());
MwmSet::MwmId mwmId;
std::unique_ptr<FeaturesLoaderGuard> guard;
for (auto const & featureId : features)
{
if (mwmId != featureId.m_mwmId)
{
guard = std::make_unique<FeaturesLoaderGuard>(dataSource, featureId.m_mwmId);
mwmId = featureId.m_mwmId;
}
auto it = std::find_if(hotelToResults.begin(), hotelToResults.end(),
[&featureId](HotelToResult const & item)
{
return item.GetMappedValue().GetFeatureID() == featureId;
});
ASSERT(it != hotelToResults.cend(), ());
auto ft = guard->GetFeatureByIndex(featureId.m_index);
if (!ft)
{
hotelToResults.erase(it);
LOG(LERROR, ("Feature can't be loaded:", featureId));
continue;
}
if (!ftypes::IsBookingChecker::Instance()(*ft))
continue;
auto const hotelId = ft->GetMetadata().Get(feature::Metadata::FMD_SPONSORED_ID);
auto const status = cache.Get(hotelId);
it->SetHotelId(hotelId);
it->SetStatus(status);
if (status != availability::Cache::HotelStatus::Absent)
continue;
cache.Reserve(hotelId);
p.m_hotelIds.push_back(std::move(hotelId));
}
}
void PrepareData(DataSource const & dataSource, std::vector<FeatureID> const & featureIds,
HotelToFeatureIds & hotelToFeatures, availability::Cache & cache,
booking::AvailabilityParams & p)
{
ASSERT(std::is_sorted(featureIds.begin(), featureIds.end()), ());
MwmSet::MwmId mwmId;
std::unique_ptr<FeaturesLoaderGuard> guard;
for (auto const featureId : featureIds)
{
if (mwmId != featureId.m_mwmId)
{
guard = std::make_unique<FeaturesLoaderGuard>(dataSource, featureId.m_mwmId);
mwmId = featureId.m_mwmId;
}
auto ft = guard->GetFeatureByIndex(featureId.m_index);
if (!ft)
{
LOG(LERROR, ("Feature can't be loaded:", featureId));
continue;
}
if (!ftypes::IsBookingChecker::Instance()(*ft))
continue;
auto const hotelId = ft->GetMetadata().Get(feature::Metadata::FMD_SPONSORED_ID);
auto const status = cache.Get(hotelId);
hotelToFeatures.emplace_back(hotelId, status, featureId);
if (status != availability::Cache::HotelStatus::Absent)
continue;
cache.Reserve(hotelId);
p.m_hotelIds.push_back(std::move(hotelId));
}
}
} // namespace
namespace booking
{
namespace filter
{
AvailabilityFilter::AvailabilityFilter(Delegate const & d) : FilterBase(d) {}
void AvailabilityFilter::ApplyFilter(search::Results const & results,
ParamsInternal const & filterParams)
{
ApplyFilterInternal<search::Result>(results, filterParams);
}
void AvailabilityFilter::ApplyFilter(std::vector<FeatureID> const & featureIds,
ParamsRawInternal const & filterParams)
{
ApplyFilterInternal<FeatureID>(featureIds, filterParams);
}
void AvailabilityFilter::UpdateParams(ParamsBase const & apiParams)
{
if (m_apiParams.Equals(apiParams))
return;
m_apiParams.Set(apiParams);
m_cache = std::make_shared<availability::Cache>();
}
template <typename SourceValue, typename Source, typename Parameters>
void AvailabilityFilter::ApplyFilterInternal(Source const & source, Parameters const & filterParams)
{
ASSERT(filterParams.m_apiParams, ());
auto const & cb = filterParams.m_callback;
UpdateParams(*filterParams.m_apiParams);
m_apiParams.m_hotelIds.clear();
HotelsMapping<SourceValue> hotelsToSourceValue;
PrepareData(GetDelegate().GetDataSource(), source, hotelsToSourceValue, *m_cache, m_apiParams);
UNUSED_VALUE(kMaxCountInRequest);
ASSERT_LESS_OR_EQUAL(m_apiParams.m_hotelIds.size(), kMaxCountInRequest, ());
if (m_apiParams.m_hotelIds.empty())
{
Source result;
FillResults(std::move(hotelsToSourceValue), {} /* hotelIds */, *m_cache, result);
cb(result);
return;
}
auto const apiCallback =
[cb, cache = m_cache, hotelToValue = std::move(hotelsToSourceValue)]
(std::vector<std::string> hotelIds) mutable
{
GetPlatform().RunTask(
Platform::Thread::File,
[cb, cache, hotelToValue = std::move(hotelToValue), hotelIds = std::move(hotelIds)]() mutable
{
Source updatedResults;
std::sort(hotelIds.begin(), hotelIds.end());
UpdateCache(hotelToValue, hotelIds, *cache);
FillResults(std::move(hotelToValue), hotelIds, *cache, updatedResults);
cb(updatedResults);
});
};
GetDelegate().GetApi().GetHotelAvailability(m_apiParams, apiCallback);
m_cache->RemoveOutdated();
}
void AvailabilityFilter::GetFeaturesFromCache(search::Results const & results,
std::vector<FeatureID> & sortedResults)
{
sortedResults.clear();
std::vector<FeatureID> features;
for (auto const & r : results)
{
if (!r.m_metadata.m_isSponsoredHotel || r.GetResultType() != search::Result::Type::Feature)
continue;
features.push_back(r.GetFeatureID());
}
std::sort(features.begin(), features.end());
MwmSet::MwmId mwmId;
std::unique_ptr<FeaturesLoaderGuard> guard;
for (auto const & featureId : features)
{
if (mwmId != featureId.m_mwmId)
{
guard = std::make_unique<FeaturesLoaderGuard>(GetDelegate().GetDataSource(), featureId.m_mwmId);
mwmId = featureId.m_mwmId;
}
auto ft = guard->GetFeatureByIndex(featureId.m_index);
if (!ft)
{
LOG(LERROR, ("Feature can't be loaded:", featureId));
continue;
}
auto const & hotelId = ft->GetMetadata().Get(feature::Metadata::FMD_SPONSORED_ID);
if (m_cache->Get(hotelId) == availability::Cache::HotelStatus::Available)
sortedResults.push_back(featureId);
}
}
} // namespace booking
} // namespace filter
|