From: Pieter Lexis Date: Mon, 10 Apr 2017 15:58:55 +0000 (+0200) Subject: rec NegCache: Add count() for QNAME|QTYPE X-Git-Tag: rec-4.1.0-alpha1~166^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=17f7162dcce70a11a581039f768561e143feeec5;p=pdns rec NegCache: Add count() for QNAME|QTYPE --- diff --git a/pdns/recursordist/negcache.cc b/pdns/recursordist/negcache.cc index 736d24560..c719eae85 100644 --- a/pdns/recursordist/negcache.cc +++ b/pdns/recursordist/negcache.cc @@ -100,11 +100,23 @@ void NegCache::add(const NegCacheEntry& ne) { /*! * Returns the amount of entries in the cache + * + * \param qname The name of the entries to be counted */ uint64_t NegCache::count(const DNSName& qname) const { return d_negcache.count(tie(qname)); } +/*! + * Returns the amount of entries in the cache for qname+qtype + * + * \param qname The name of the entries to be counted + * \param qtype The type of the entries to be counted + */ +uint64_t NegCache::count(const DNSName& qname, const QType qtype) const { + return d_negcache.count(tie(qname, qtype)); +} + /*! * Remove all entries for name from the cache. If subtree is true, wipe all names * underneath it. diff --git a/pdns/recursordist/negcache.hh b/pdns/recursordist/negcache.hh index 6f0b4cf9f..d721fe70b 100644 --- a/pdns/recursordist/negcache.hh +++ b/pdns/recursordist/negcache.hh @@ -59,6 +59,7 @@ class NegCache : public boost::noncopyable { bool get(const DNSName& qname, const QType& qtype, const struct timeval& now, NegCacheEntry& ne); bool getRootNXTrust(const DNSName& qname, const struct timeval& now, NegCacheEntry& ne); uint64_t count(const DNSName& qname) const; + uint64_t count(const DNSName& qname, const QType qtype) const; void prune(unsigned int maxEntries); void clear(); uint64_t dumpToFile(FILE* fd); diff --git a/pdns/recursordist/test-negcache_cc.cc b/pdns/recursordist/test-negcache_cc.cc index 97cdaf290..111483423 100644 --- a/pdns/recursordist/test-negcache_cc.cc +++ b/pdns/recursordist/test-negcache_cc.cc @@ -341,4 +341,31 @@ BOOST_AUTO_TEST_CASE(test_dumpToFile) { fclose(fp); } +BOOST_AUTO_TEST_CASE(test_count) { + string qname(".powerdns.com"); + string qname2("powerdns.org"); + DNSName auth("powerdns.com"); + + struct timeval now; + Utility::gettimeofday(&now, 0); + + NegCache cache; + NegCache::NegCacheEntry ne; + ne = genNegCacheEntry(auth, auth, now); + cache.add(ne); + + for(int i = 0; i < 400; i++) { + ne = genNegCacheEntry(DNSName(std::to_string(i) + qname), auth, now); + cache.add(ne); + ne = genNegCacheEntry(DNSName(std::to_string(i) + qname2), auth, now); + cache.add(ne); + } + + uint64_t count; + count = cache.count(auth); + BOOST_CHECK_EQUAL(count, 1); + count = cache.count(auth, QType(1)); + BOOST_CHECK_EQUAL(count, 0); +} + BOOST_AUTO_TEST_SUITE_END()