blob: ad6719ac7defb987388af66feffb95ae9ea1a3d7 (
plain)
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
|
#include "map/booking_filter_cache.hpp"
using namespace std::chrono;
namespace booking
{
namespace filter
{
namespace availability
{
Cache::Cache(size_t maxCount, size_t expiryPeriodSeconds)
: m_maxCount(maxCount), m_expiryPeriodSeconds(expiryPeriodSeconds)
{
}
Cache::HotelStatus Cache::Get(std::string const & hotelId)
{
HotelStatus result = Get(m_notReadyHotels, hotelId);
if (result == HotelStatus::Absent)
result = Get(m_hotelToStatus, hotelId);
return result;
}
void Cache::Reserve(std::string const & hotelId)
{
ASSERT(m_hotelToStatus.find(hotelId) == m_hotelToStatus.end(), ());
m_notReadyHotels.emplace(hotelId, Item());
}
void Cache::Insert(std::string const & hotelId, HotelStatus const s)
{
ASSERT_NOT_EQUAL(s, HotelStatus::NotReady,
("Please, use Cache::Reserve method for HotelStatus::NotReady"));
RemoveExtra();
Item item(s);
m_hotelToStatus[hotelId] = std::move(item);
m_notReadyHotels.erase(hotelId);
}
void Cache::RemoveOutdated()
{
if (m_expiryPeriodSeconds == 0)
return;
RemoveOutdated(m_hotelToStatus);
RemoveOutdated(m_notReadyHotels);
}
void Cache::Clear()
{
m_hotelToStatus.clear();
m_notReadyHotels.clear();
}
void Cache::RemoveExtra()
{
if (m_maxCount == 0 || m_hotelToStatus.size() < m_maxCount)
return;
m_hotelToStatus.clear();
}
bool Cache::IsExpired(Clock::time_point const & timestamp) const
{
return Clock::now() > timestamp + seconds(m_expiryPeriodSeconds);
}
Cache::HotelStatus Cache::Get(HotelsMap & src, std::string const & hotelId)
{
auto const it = src.find(hotelId);
if (it == src.cend())
return HotelStatus::Absent;
if (m_expiryPeriodSeconds != 0 && IsExpired(it->second.m_timestamp))
{
src.erase(it);
return HotelStatus::Absent;
}
return it->second.m_status;
}
void Cache::RemoveOutdated(HotelsMap & src)
{
for (auto it = src.begin(); it != src.end();)
{
if (IsExpired(it->second.m_timestamp))
it = src.erase(it);
else
++it;
}
}
std::string DebugPrint(Cache::HotelStatus status)
{
switch (status)
{
case Cache::HotelStatus::Absent: return "Absent";
case Cache::HotelStatus::NotReady: return "NotReady";
case Cache::HotelStatus::Unavailable: return "Unavailable";
case Cache::HotelStatus::Available: return "Available";
}
UNREACHABLE();
}
} // namespace availability
} // namespace filter
} // namespace booking
|