]> granicus.if.org Git - pdns/commitdiff
rec NegCache: Add count() for QNAME|QTYPE
authorPieter Lexis <pieter.lexis@powerdns.com>
Mon, 10 Apr 2017 15:58:55 +0000 (17:58 +0200)
committerPieter Lexis <pieter.lexis@powerdns.com>
Mon, 10 Apr 2017 15:58:55 +0000 (17:58 +0200)
pdns/recursordist/negcache.cc
pdns/recursordist/negcache.hh
pdns/recursordist/test-negcache_cc.cc

index 736d24560c1225ec27f0f7f877cafd85a6be001b..c719eae85a9b3c379a7258eb8178e209302a58a9 100644 (file)
@@ -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.
index 6f0b4cf9fe36ce1afc690d306599fbfd192c04ca..d721fe70b7d26731aa395cf5a2c7362237da942d 100644 (file)
@@ -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);
index 97cdaf2901f96cebb72f9e998144bceb0eaecfff..111483423f40413f1af8cdec5c7fbcbfd9550cc5 100644 (file)
@@ -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()