-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtimestore.hpp
318 lines (266 loc) · 9.88 KB
/
timestore.hpp
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
// solid/frame/timestore.hpp
//
// Copyright (c) 2015 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#pragma once
#include <array>
#include <deque>
#include <vector>
#include "solid/system/cassert.hpp"
#include "solid/system/log.hpp"
#include "solid/system/nanotime.hpp"
#include "solid/utility/common.hpp"
#include "solid/utility/innerlist.hpp"
namespace solid {
namespace frame {
namespace time_store_impl {
enum struct LinkE : size_t {
Free = 0,
// add above
Count,
};
enum struct IntervalE : size_t {
Second,
Minute,
Hour,
Other,
// add above
Count
};
inline IntervalE dispatch(const NanoTime& _now, const NanoTime& _expiry)
{
if (_expiry.seconds() < _now.seconds() || _now.seconds() == _expiry.seconds() || (_now.seconds() + 1) == _expiry.seconds()) {
return IntervalE::Second;
} else if (_expiry.seconds() <= (_now.seconds() + 60)) {
return IntervalE::Minute;
} else if (_expiry.seconds() <= (_now.seconds() + 3600)) {
return IntervalE::Hour;
}
return IntervalE::Other;
}
} // namespace time_store_impl
class TimeStore {
struct ProxyNode : inner::Node<to_underlying(time_store_impl::LinkE::Count)> {
size_t value_ = InvalidIndex{};
size_t internal_index_ = InvalidIndex{};
time_store_impl::IntervalE interval_ = time_store_impl::IntervalE::Count;
void clear()
{
value_ = InvalidIndex{};
internal_index_ = InvalidIndex{};
interval_ = time_store_impl::IntervalE::Count;
}
bool empty() const
{
return interval_ == time_store_impl::IntervalE::Count;
}
};
struct Value {
NanoTime expiry_ = NanoTime::max();
size_t proxy_index_ = InvalidIndex{};
Value() = default;
Value(const NanoTime& _expiry, const size_t _proxy_index)
: expiry_(_expiry)
, proxy_index_(_proxy_index)
{
}
};
using ValueVectorT = std::vector<Value>;
struct Interval {
NanoTime min_expiry_ = NanoTime::max();
ValueVectorT values_;
};
using ProxyNodesT = std::deque<ProxyNode>;
using ProxyFreeListT = inner::List<ProxyNodesT, to_underlying(time_store_impl::LinkE::Free)>;
using IntervalsT = std::array<Interval, to_underlying(time_store_impl::IntervalE::Count)>;
NanoTime min_expiry_ = NanoTime::max();
ProxyNodesT proxy_nodes_;
ProxyFreeListT proxy_free_list_;
IntervalsT intervals_;
public:
TimeStore(const size_t _capacity = 0);
size_t size() const;
bool empty() const;
size_t push(const NanoTime& _now, const NanoTime& _expiry, const size_t _value);
void update(const size_t _proxy_index, const NanoTime& _now, const NanoTime& _expiry);
void pop(const size_t _index);
template <class Fnc>
size_t pop(const NanoTime& _now, Fnc&& _fnc);
const NanoTime& expiry() const
{
return min_expiry_;
}
private:
Interval& interval(const time_store_impl::IntervalE _iv)
{
return intervals_[to_underlying(_iv)];
}
template <class Fnc>
size_t doPop(const time_store_impl::IntervalE _iv, const NanoTime& _now, Fnc&& _fnc);
};
inline TimeStore::TimeStore(const size_t _capacity)
: proxy_free_list_(proxy_nodes_)
{
for (auto& iv : intervals_) {
iv.values_.reserve(_capacity);
}
}
inline size_t TimeStore::size() const
{
return proxy_nodes_.size() - proxy_free_list_.size();
}
inline bool TimeStore::empty() const
{
return size() == 0;
}
inline size_t TimeStore::push(const NanoTime& _now, const NanoTime& _expiry, const size_t _value)
{
size_t proxy_index = InvalidIndex{};
if (!proxy_free_list_.empty()) {
proxy_index = proxy_free_list_.popBack();
} else {
proxy_index = proxy_nodes_.size();
proxy_nodes_.emplace_back();
}
auto& rnode = proxy_nodes_[proxy_index];
rnode.value_ = _value;
rnode.interval_ = time_store_impl::dispatch(_now, _expiry);
auto& rinterval = interval(rnode.interval_);
rnode.internal_index_ = rinterval.values_.size();
rinterval.values_.emplace_back(_expiry, proxy_index);
solid_dbg(generic_logger, Verbose, " interval = " << to_underlying(rnode.interval_) << " internal index =" << rnode.internal_index_ << " " << _expiry);
if (_expiry < rinterval.min_expiry_) {
rinterval.min_expiry_ = _expiry;
}
if (_expiry < min_expiry_) {
min_expiry_ = _expiry;
}
return proxy_index;
}
inline void TimeStore::update(const size_t _proxy_index, const NanoTime& _now, const NanoTime& _expiry)
{
solid_assert(_proxy_index < proxy_nodes_.size() && !proxy_nodes_[_proxy_index].empty());
auto& rnode = proxy_nodes_[_proxy_index];
const auto new_interval = time_store_impl::dispatch(_now, _expiry);
if (new_interval == rnode.interval_) {
solid_dbg(generic_logger, Verbose, " interval = " << to_underlying(rnode.interval_) << " internal index =" << rnode.internal_index_);
auto& rinterval = interval(rnode.interval_);
solid_assert(rinterval.values_.size() > rnode.internal_index_);
rinterval.values_[rnode.internal_index_].expiry_ = _expiry;
if (_expiry < rinterval.min_expiry_) {
rinterval.min_expiry_ = _expiry;
}
if (_expiry < min_expiry_) {
min_expiry_ = _expiry;
}
} else {
solid_dbg(generic_logger, Verbose, "from iv = " << to_underlying(rnode.interval_) << " to iv = " << to_underlying(new_interval) << " internal index =" << rnode.internal_index_);
{
auto& rinterval = interval(rnode.interval_);
rinterval.values_[rnode.internal_index_] = rinterval.values_.back();
proxy_nodes_[rinterval.values_[rnode.internal_index_].proxy_index_].internal_index_ = rnode.internal_index_;
rinterval.values_.pop_back();
if (rinterval.values_.empty()) {
rinterval.min_expiry_ = NanoTime::max();
}
}
{
rnode.interval_ = new_interval;
auto& rinterval = interval(rnode.interval_);
rnode.internal_index_ = rinterval.values_.size();
rinterval.values_.emplace_back(_expiry, _proxy_index);
if (_expiry < rinterval.min_expiry_) {
rinterval.min_expiry_ = _expiry;
}
if (_expiry < min_expiry_) {
min_expiry_ = _expiry;
}
}
}
}
inline void TimeStore::pop(const size_t _proxy_index)
{
solid_assert(_proxy_index < proxy_nodes_.size() && !proxy_nodes_[_proxy_index].empty());
auto& rnode = proxy_nodes_[_proxy_index];
auto& rinterval = interval(rnode.interval_);
rinterval.values_[rnode.internal_index_] = rinterval.values_.back();
proxy_nodes_[rinterval.values_[rnode.internal_index_].proxy_index_].internal_index_ = rnode.internal_index_;
rinterval.values_.pop_back();
if (rinterval.values_.empty()) {
rinterval.min_expiry_ = NanoTime::max();
}
rnode.clear();
proxy_free_list_.pushBack(_proxy_index);
}
template <class Fnc>
size_t TimeStore::doPop(const time_store_impl::IntervalE _iv, const NanoTime& _now, Fnc&& _fnc)
{
using namespace time_store_impl;
auto& rinterval = interval(_iv);
size_t count = 0;
auto min_expiry = NanoTime::max();
rinterval.min_expiry_ = min_expiry;
for (size_t i = 0; i < rinterval.values_.size();) {
auto& rval = rinterval.values_[i];
if (_now < rval.expiry_) {
++i;
if (rval.expiry_ < min_expiry) {
min_expiry = rval.expiry_;
}
continue;
} else {
const auto proxy_index = rval.proxy_index_;
auto& rnode = proxy_nodes_[proxy_index];
const auto value = rnode.value_;
const auto expiry = rval.expiry_;
solid_assert(rnode.internal_index_ == i);
rval = rinterval.values_.back();
proxy_nodes_[rval.proxy_index_].internal_index_ = i;
rnode.clear();
proxy_free_list_.pushBack(proxy_index);
rinterval.values_.pop_back();
_fnc(value, expiry, proxy_index);
++count;
}
}
if (min_expiry < rinterval.min_expiry_) {
rinterval.min_expiry_ = min_expiry;
}
return count;
}
template <class Fnc>
inline size_t TimeStore::pop(const NanoTime& _now, Fnc&& _fnc)
{
using namespace time_store_impl;
size_t count = 0;
if (_now < expiry()) {
return count;
}
if (!(_now < interval(IntervalE::Second).min_expiry_)) {
count += doPop(IntervalE::Second, _now, _fnc);
}
if (!(_now < interval(IntervalE::Minute).min_expiry_)) {
count += doPop(IntervalE::Minute, _now, _fnc);
}
if (!(_now < interval(IntervalE::Hour).min_expiry_)) {
count += doPop(IntervalE::Hour, _now, _fnc);
}
if (!(_now < interval(IntervalE::Other).min_expiry_)) {
count += doPop(IntervalE::Other, _now, _fnc);
}
min_expiry_ = std::min(
interval(IntervalE::Second).min_expiry_,
std::min(
interval(IntervalE::Minute).min_expiry_,
std::min(
interval(IntervalE::Hour).min_expiry_,
interval(IntervalE::Other).min_expiry_)));
return count;
}
} // namespace frame
} // namespace solid