Implemented user duplicate input message check.

This commit is contained in:
Ravin Perera
2019-11-05 11:47:42 +05:30
parent 83189556de
commit 8a153d5bb5
7 changed files with 103 additions and 66 deletions

View File

@@ -1,8 +1,45 @@
#include "pchheader.hpp"
#include "util.hpp"
namespace util
{
// rollover_hashset class methods
rollover_hashset::rollover_hashset(uint32_t maxsize)
{
this->maxsize = maxsize == 0 ? 1 : maxsize;
}
/**
* Inserts the given hash to the list.
* @return True on succesful insertion. False if hash already exists.
*/
bool rollover_hashset::try_emplace(std::string hash)
{
auto itr = recent_hashes.find(hash);
if (itr == recent_hashes.end()) // Not found
{
// Add the new message hash to the set.
auto [newitr, success] = recent_hashes.emplace(hash);
// Insert a pointer to the stored hash value to the back of the ordered list of hashes.
recent_hashes_list.push_back(&(*newitr));
// Remove oldest hash if exceeding max size.
if (recent_hashes_list.size() > maxsize)
{
const std::string &oldest_hash = *recent_hashes_list.front();
recent_hashes.erase(oldest_hash);
recent_hashes_list.pop_front();
}
return true; // Hash was inserted successfuly.
}
return false; // Hash already exists.
}
/**
* Encodes provided bytes to hex string.
*