Move pubkey_set into auth.h header

This allows it to be brought in without the full lokimq.h header.
This commit is contained in:
Jason Rhinelander 2020-04-13 13:03:19 -03:00
parent 3b86eb1341
commit 84bd5544cc
3 changed files with 24 additions and 22 deletions

View File

@ -1,5 +1,8 @@
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <string>
#include <cstring>
#include <unordered_set>
namespace lokimq { namespace lokimq {
@ -29,4 +32,24 @@ struct Access {
: auth{auth}, remote_sn{remote_sn}, local_sn{local_sn} {} : auth{auth}, remote_sn{remote_sn}, local_sn{local_sn} {}
}; };
/// Simple hash implementation for a string that is *already* a hash-like value (such as a pubkey).
/// Falls back to std::hash<std::string> if given a string smaller than a size_t.
struct already_hashed {
size_t operator()(const std::string& s) const {
if (s.size() < sizeof(size_t))
return std::hash<std::string>{}(s);
size_t hash;
std::memcpy(&hash, &s[0], sizeof(hash));
return hash;
}
};
/// std::unordered_set specialization for specifying pubkeys (used, in particular, by
/// LokiMQ::set_active_sns and LokiMQ::update_active_sns); this is a std::string unordered_set that
/// also uses a specialized trivial hash function that uses part of the value itself (i.e. the
/// pubkey) directly as a hash value. (This is nice and fast for uniformly distributed values like
/// pubkeys and a terrible hash choice for anything else).
using pubkey_set = std::unordered_set<std::string, already_hashed>;
} }

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "auth.h"
#include "string_view.h" #include "string_view.h"
#include <cstring>
namespace lokimq { namespace lokimq {
@ -72,19 +72,6 @@ private:
friend std::ostream& operator<<(std::ostream& o, const ConnectionID& conn); friend std::ostream& operator<<(std::ostream& o, const ConnectionID& conn);
}; };
/// Simple hash implementation for a string that is *already* a hash-like value (such as a pubkey).
/// Falls back to std::hash<std::string> if given a string smaller than a size_t.
struct already_hashed {
size_t operator()(const std::string& s) const {
if (s.size() < sizeof(size_t))
return std::hash<std::string>{}(s);
size_t hash;
std::memcpy(&hash, &s[0], sizeof(hash));
return hash;
}
};
} // namespace lokimq } // namespace lokimq
namespace std { namespace std {
template <> struct hash<lokimq::ConnectionID> { template <> struct hash<lokimq::ConnectionID> {

View File

@ -86,14 +86,6 @@ static constexpr size_t MAX_COMMAND_LENGTH = 200;
class CatHelper; class CatHelper;
/// std::unordered_set specialization for specifying pubkeys (used, in particular, by
/// LokiMQ::set_active_sns and LokiMQ::update_active_sns); this is a std::string unordered_set that
/// also uses a specialized trivial hash function that uses part of the value itself (i.e. the
/// pubkey) directly as a hash value. (This is nice and fast for uniformly distributed values like
/// pubkeys and a terrible hash choice for anything else).
using pubkey_set = std::unordered_set<std::string, already_hashed>;
/** /**
* Class that handles LokiMQ listeners, connections, proxying, and workers. An application * Class that handles LokiMQ listeners, connections, proxying, and workers. An application
* typically has just one instance of this class. * typically has just one instance of this class.