7 #include <boost/asio.hpp>
8 #include <boost/format.hpp>
9 #include <boost/iostreams/device/back_inserter.hpp>
10 #include <boost/iostreams/stream.hpp>
11 #include <boost/iostreams/stream_buffer.hpp>
12 #include <boost/thread.hpp>
15 #include <shared_mutex>
18 #include <unordered_set>
29 std::weak_ptr<T>
as_weak(std::shared_ptr<T> sp) {
30 return std::weak_ptr<T>(sp);
33 template <
typename Int1,
typename Int2>
45 std::vector<T>
asVector(
const Json::Value &json) {
47 v.reserve(json.size());
48 std::transform(json.begin(), json.end(), std::back_inserter(v),
49 [](
const Json::Value &item) { return T(item.asString()); });
55 using stream = std::basic_streambuf<uint8_t>;
56 using bufferstream = boost::iostreams::stream_buffer<boost::iostreams::basic_array_source<uint8_t>>;
57 using vectorstream = boost::iostreams::stream_buffer<boost::iostreams::back_insert_device<std::vector<uint8_t>>>;
63 static_assert(std::is_standard_layout<T>::value,
"Cannot stream read non-standard layout types");
64 auto bytes(stm.sgetn(
reinterpret_cast<uint8_t *
>(&value),
sizeof(value)));
65 return bytes ==
sizeof(value);
70 static_assert(std::is_standard_layout<T>::value,
"Cannot stream write non-standard layout types");
71 auto bytes(stm.sputn(
reinterpret_cast<uint8_t
const *
>(&value),
sizeof(value)));
72 assert(
bytes ==
sizeof(value));
73 return bytes ==
sizeof(value);
89 template <
typename K,
typename V>
94 std::pair<V, bool>
get(K
const &hash) {
98 auto kv = *(iter->second);
101 lru_.erase(iter->second);
122 lru_.push_front({hash, status});
131 auto iter =
status_.find(hash);
133 lru_.erase(iter->second);
134 lru_.push_front({hash, status});
140 bool update(K
const &hash, V status, V expected_status) {
143 auto iter =
status_.find(hash);
144 if (iter !=
status_.end() && iter->second == expected_status) {
145 lru_.erase(iter->second);
146 lru_.push_front({hash, status});
162 auto iter =
status_.find(hash);
164 lru_.erase(iter->second);
179 for (
int i = 0; i < sz; ++i) {
180 auto kv =
lru_.rbegin();
184 assert(!
lru_.empty());
190 std::unordered_map<K, typename std::list<std::pair<K, V>>::iterator>
status_;
191 std::list<std::pair<K, V>>
lru_;
194 template <
typename... TS>
195 std::string
fmt(
const std::string &pattern,
const TS &...args) {
196 return (boost::format(pattern) % ... % args).str();
220 std::unique_lock lock(
mtx_);
221 if (!
cache_.insert(key).second) {
237 std::shared_lock lck(
mtx_);
238 return cache_.contains(key);
242 std::unique_lock lck(
mtx_);
246 std::size_t
count(Key
const &key)
const {
247 std::shared_lock lck(
mtx_);
252 std::shared_lock lck(
mtx_);
257 std::unique_lock lck(
mtx_);
266 mutable std::shared_mutex
mtx_;
287 bool insert(Key
const &key, uint64_t block_number) {
292 std::unique_lock lock(this->
mtx_);
293 if (!this->
cache_.insert(key).second) {
302 this->
cache_.erase(exp.first);
310 if (this->
cache_.size() > this->kMaxSize) {
321 std::shared_lock lck(this->
mtx_);
332 template <
typename T>
333 auto slice(std::vector<T>
const &v, std::size_t from = -1, std::size_t to = -1) {
335 return std::vector<T>(from == (std::size_t)-1 ? b : b + from, to == (std::size_t)-1 ? v.end() : b + to);
338 template <
typename T>
345 template <
class Key,
class Value>
359 bool insert(Key
const &key, Value
const &value) {
361 std::shared_lock lock(
mtx_);
367 std::unique_lock lock(
mtx_);
370 if (!
cache_.emplace(key, value).second) {
381 std::size_t
count(Key
const &key)
const {
382 std::shared_lock lck(
mtx_);
387 std::shared_lock lck(
mtx_);
391 std::pair<Value, bool>
get(Key
const &key)
const {
392 std::shared_lock lck(
mtx_);
393 auto it =
cache_.find(key);
394 if (it ==
cache_.end())
return std::make_pair(Value(),
false);
395 return std::make_pair(it->second,
true);
399 std::unique_lock lck(
mtx_);
404 void update(Key
const &key, Value value) {
405 std::unique_lock lck(
mtx_);
423 std::unique_lock lck(
mtx_);
429 std::unique_lock lck(
mtx_);
430 std::pair<Value, bool> ret;
431 auto it =
cache_.find(key);
433 ret = std::make_pair(Value(),
false);
435 ret = std::make_pair(it->second,
true);
449 std::shared_lock lck(
mtx_);
453 bool update(Key
const &key, Value value, Value expected_value) {
454 std::unique_lock lck(
mtx_);
455 auto it =
cache_.find(key);
456 if (it !=
cache_.end() && it->second == expected_value) {
482 mutable std::shared_mutex
mtx_;
485 template <
class Key,
class Value>
488 bool emplace(Key
const &key, Value
const &value) {
489 std::unique_lock lock(
mtx_);
490 return map_.emplace(key, value).second;
493 std::size_t
count(Key
const &key)
const {
494 std::shared_lock lck(
mtx_);
495 return map_.count(key);
499 std::shared_lock lck(
mtx_);
503 std::pair<Value, bool>
get(Key
const &key)
const {
504 std::shared_lock lck(
mtx_);
505 auto it =
map_.find(key);
506 if (it ==
map_.end())
return std::make_pair(Value(),
false);
507 return std::make_pair(it->second,
true);
511 std::vector<Value> values;
512 uint32_t counter = 0;
513 std::shared_lock lck(
mtx_);
514 values.reserve(
map_.size());
515 for (
auto const &t :
map_) {
516 values.emplace_back(t.second);
519 if (counter ==
count) {
527 void erase(std::function<
bool(Value)> condition) {
528 std::unique_lock lck(
mtx_);
529 for (
auto it =
map_.cbegin(), next_it = it; it !=
map_.cend(); it = next_it) {
531 if (condition(it->second)) {
538 std::unique_lock lck(
mtx_);
543 std::unique_lock lck(
mtx_);
544 return map_.erase(key);
548 std::unordered_map<Key, Value>
map_;
549 mutable std::shared_mutex
mtx_;
556 std::unique_lock lock(
mtx_);
557 return set_.insert(key).second;
560 std::size_t
count(Key
const &key)
const {
561 std::shared_lock lck(
mtx_);
562 return set_.count(key);
566 std::unique_lock lck(
mtx_);
571 std::unique_lock lck(
mtx_);
572 return set_.erase(key);
576 std::shared_lock lck(
mtx_);
582 mutable std::shared_mutex
mtx_;
ExpirationBlockNumberCache(uint32_t max_size, uint32_t delete_step, uint32_t blocks_to_keep)
Definition: util.hpp:275
void clear()
Definition: util.hpp:320
std::deque< std::pair< Key, uint64_t > > block_expiration_
Definition: util.hpp:327
const uint32_t kBlocksToKeep
Definition: util.hpp:328
bool insert(Key const &key, uint64_t block_number)
Inserts key into the cache map. In case provided key is already in cache, only shared lock is acquire...
Definition: util.hpp:287
uint64_t last_block_number_
Definition: util.hpp:329
const uint32_t kDeleteStep
Definition: util.hpp:265
bool insert(Key const &key)
Inserts key into the cache map. In case provided key is already in cache, only shared lock is acquire...
Definition: util.hpp:214
std::size_t count(Key const &key) const
Definition: util.hpp:246
const uint32_t kMaxSize
Definition: util.hpp:264
std::shared_mutex mtx_
Definition: util.hpp:266
ExpirationCache(uint32_t max_size, uint32_t delete_step)
Definition: util.hpp:204
void clear()
Definition: util.hpp:256
bool contains(Key const &key) const
Definition: util.hpp:236
void erase(Key const &key)
Definition: util.hpp:241
std::unordered_set< Key > cache_
Definition: util.hpp:263
std::size_t size() const
Definition: util.hpp:251
std::deque< Key > expiration_
Definition: util.hpp:269
virtual void eraseOldest()
Definition: util.hpp:471
bool insert(Key const &key, Value const &value)
Inserts <key,value> pair into the cache map. In case provided key is already in cache,...
Definition: util.hpp:359
std::size_t size() const
Definition: util.hpp:386
std::size_t count(Key const &key) const
Definition: util.hpp:381
std::unordered_map< Key, Value > getRawMap()
Definition: util.hpp:448
std::deque< Key > expiration_
Definition: util.hpp:479
std::unordered_map< Key, Value > cache_
Definition: util.hpp:478
ExpirationCacheMap(uint32_t max_size, uint32_t delete_step)
Definition: util.hpp:348
void erase(const Key &key)
Definition: util.hpp:422
const uint32_t kDeleteStep
Definition: util.hpp:481
void update(Key const &key, Value value)
Definition: util.hpp:404
std::shared_mutex mtx_
Definition: util.hpp:482
std::pair< Value, bool > get(Key const &key) const
Definition: util.hpp:391
std::pair< Value, bool > updateWithGet(Key const &key, Value value)
Definition: util.hpp:428
const uint32_t kMaxSize
Definition: util.hpp:480
bool update(Key const &key, Value value, Value expected_value)
Definition: util.hpp:453
void clear()
Definition: util.hpp:398
std::shared_mutex mtx_
Definition: util.hpp:549
std::unordered_map< Key, Value > map_
Definition: util.hpp:548
std::vector< Value > getValues(uint32_t count=0) const
Definition: util.hpp:510
bool erase(const Key &key)
Definition: util.hpp:542
bool emplace(Key const &key, Value const &value)
Definition: util.hpp:488
std::size_t count(Key const &key) const
Definition: util.hpp:493
std::size_t size() const
Definition: util.hpp:498
std::pair< Value, bool > get(Key const &key) const
Definition: util.hpp:503
void clear()
Definition: util.hpp:537
void erase(std::function< bool(Value)> condition)
Definition: util.hpp:527
void clear()
Definition: util.hpp:565
bool emplace(Key const &key)
Definition: util.hpp:555
bool erase(const Key &key)
Definition: util.hpp:570
std::unordered_set< Key > set_
Definition: util.hpp:581
std::size_t size() const
Definition: util.hpp:575
std::size_t count(Key const &key) const
Definition: util.hpp:560
std::shared_mutex mtx_
Definition: util.hpp:582
Class for writing to an RLP bytestream.
Definition: RLP.h:484
bytes && invalidate()
Invalidate the object and steal the output byte stream.
Definition: RLP.h:620
void clearOldData()
Definition: util.hpp:177
std::pair< V, bool > get(K const &hash)
Definition: util.hpp:94
size_t capacity_
Definition: util.hpp:188
bool update(K const &hash, V status, V expected_status)
Definition: util.hpp:140
StatusTable(size_t capacity=10000)
Definition: util.hpp:93
bool erase(K const &hash)
Definition: util.hpp:159
std::list< std::pair< K, V > > lru_
Definition: util.hpp:191
void update(K const &hash, V status)
Definition: util.hpp:129
std::shared_mutex shared_mutex_
Definition: util.hpp:189
bool insert(K const &hash, V status)
Definition: util.hpp:113
std::unordered_map< K, typename std::list< std::pair< K, V > >::iterator > status_
Definition: util.hpp:190
void clear()
Definition: util.hpp:154
std::unordered_map< K, V > UnsafeStatusTable
Definition: util.hpp:92
unsigned long size() const
Definition: util.hpp:109
std::list< std::pair< K, V > > Element
Definition: util.hpp:187
UnsafeStatusTable getThreadUnsafeCopy() const
Definition: util.hpp:171
std::vector<::byte > bytes
Definition: Common.h:46
std::basic_streambuf< uint8_t > stream
Definition: util.hpp:55
void thisThreadSleepForMicroSeconds(unsigned microsec)
Definition: util.cpp:17
bool read(stream &stm, T &value)
Definition: util.hpp:62
std::weak_ptr< T > as_weak(std::shared_ptr< T > sp)
Definition: util.hpp:29
std::vector< T > asVector(const Json::Value &json)
Definition: util.hpp:45
void thisThreadSleepForMilliSeconds(unsigned millisec)
Definition: util.cpp:13
boost::iostreams::stream_buffer< boost::iostreams::back_insert_device< std::vector< uint8_t > >> vectorstream
Definition: util.hpp:57
std::vector< byte > bytes
Definition: types.hpp:53
std::string getFormattedVersion(std::initializer_list< uint32_t > list)
Definition: util.cpp:26
void thisThreadSleepForSeconds(unsigned sec)
Definition: util.cpp:11
std::string fmt(const std::string &pattern, const TS &...args)
Definition: util.hpp:195
bool write(stream &stm, T const &value)
Definition: util.hpp:69
std::vector< uint64_t > asUInt64Vector(const Json::Value &json)
Definition: util.cpp:34
std::string jsonToUnstyledString(const Json::Value &value)
Definition: util.cpp:5
boost::iostreams::stream_buffer< boost::iostreams::basic_array_source< uint8_t > > bufferstream
Definition: util.hpp:56
auto int_pow(Int1 x, Int2 y)
Definition: util.hpp:34
unsigned long getCurrentTimeMilliSeconds()
Definition: util.cpp:21
auto slice(std::vector< T > const &v, std::size_t from=-1, std::size_t to=-1)
Definition: util.hpp:333
auto getRlpBytes(T const &t)
Definition: util.hpp:339