size_t hash_value(DNSName const& d)
{
- boost::hash<string> hasher;
- return hasher(toLower(d.toString())); // FIXME400 HACK - we rely on this lowercasing though in packetcache.hh
+ return d.hash();
}
string DNSName::escapeLabel(const std::string& label)
#include <strings.h>
#include <stdexcept>
+uint32_t burtleCI(const unsigned char* k, uint32_t lengh, uint32_t init);
+
// #include "dns.hh"
// #include "logger.hh"
bool isRoot() const { return d_storage.size()==1 && d_storage[0]==0; }
void clear() { d_storage.clear(); }
void trimToLabels(unsigned int);
+ size_t hash() const
+ {
+ return burtleCI((const unsigned char*)d_storage.c_str(), d_storage.size(), 0);
+ }
DNSName& operator+=(const DNSName& rhs)
{
if(d_storage.size() + rhs.d_storage.size() > 256) // reserve one byte for the root label
void setFilenumLimit(unsigned int lim);
bool readFileIfThere(const char* fname, std::string* line);
uint32_t burtle(const unsigned char* k, uint32_t lengh, uint32_t init);
-uint32_t burtleCI(const unsigned char* k, uint32_t lengh, uint32_t init);
bool setSocketTimestamps(int fd);
//! Sets the socket into blocking mode.
vector<MapCombo> d_maps;
MapCombo& getMap(const DNSName& qname)
{
- return d_maps[hash_value(qname) % d_maps.size()];
+ return d_maps[qname.hash() % d_maps.size()];
}
AtomicCounter d_ops;
}
+BOOST_AUTO_TEST_CASE(test_hash) {
+ DNSName a("wwW.Ds9A.Nl"), b("www.ds9a.nl");
+ BOOST_CHECK_EQUAL(a.hash(), b.hash());
+
+ vector<uint32_t> counts(1500);
+
+ for(unsigned int n=0; n < 100000; ++n) {
+ DNSName dn(std::to_string(n)+"."+std::to_string(n*2)+"ds9a.nl");
+ DNSName dn2(std::to_string(n)+"."+std::to_string(n*2)+"Ds9a.nL");
+ BOOST_CHECK_EQUAL(dn.hash(), dn2.hash());
+ counts[dn.hash() % counts.size()]++;
+ }
+
+ double sum = std::accumulate(std::begin(counts), std::end(counts), 0.0);
+ double m = sum / counts.size();
+
+ double accum = 0.0;
+ std::for_each (std::begin(counts), std::end(counts), [&](const double d) {
+ accum += (d - m) * (d - m);
+ });
+
+ double stdev = sqrt(accum / (counts.size()-1));
+ BOOST_CHECK(stdev < 10);
+}
+
BOOST_AUTO_TEST_CASE(test_QuestionHash) {
vector<unsigned char> packet;
reportBasicTypes();