Browse Source

Merge branch 'trac449' of ssh://bind10.isc.org/var/bind10/git/bind10 into trac449

zhanglikun 14 years ago
parent
commit
ac8359174e

+ 1 - 1
doc/Doxyfile

@@ -568,7 +568,7 @@ WARN_LOGFILE           =
 # directories like "/usr/src/myproject". Separate the files or directories
 # directories like "/usr/src/myproject". Separate the files or directories
 # with spaces.
 # with spaces.
 
 
-INPUT                  = ../src/lib/cc ../src/lib/config ../src/lib/dns ../src/lib/exceptions ../src/lib/datasrc ../src/bin/auth ../src/lib/bench ../src/lib/log ../src/lib/asiolink/ ../src/lib/nsas
+INPUT                  = ../src/lib/cc ../src/lib/config ../src/lib/dns ../src/lib/exceptions ../src/lib/datasrc ../src/bin/auth ../src/lib/bench ../src/lib/log ../src/lib/asiolink/ ../src/lib/nsas ../src/lib/cache
 
 
 # This tag can be used to specify the character encoding of the source files
 # This tag can be used to specify the character encoding of the source files
 # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is
 # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is

+ 20 - 8
src/lib/cache/rrset_cache.h

@@ -32,26 +32,37 @@ class RRsetEntry;
 /// The object of RRsetCache represented the cache for class-specific
 /// The object of RRsetCache represented the cache for class-specific
 /// RRsets.
 /// RRsets.
 class RRsetCache{
 class RRsetCache{
+    ///
+    /// \name Constructors and Destructor
+    ///
+    /// Note: The copy constructor and the assignment operator are intentionally
+    /// defined as private to make it uncopyable
+    //@{
+private:
+    RRsetCache(const RRsetCache&);
+    RRsetCache& operator=(const RRsetCache&);
 public:
 public:
     /// \param cache_size the size of rrset cache.
     /// \param cache_size the size of rrset cache.
     /// \param rrset_class the class of rrset cache.
     /// \param rrset_class the class of rrset cache.
     RRsetCache(uint32_t cache_size, uint16_t rrset_class);
     RRsetCache(uint32_t cache_size, uint16_t rrset_class);
-    
+    ~RRsetCache() {}
+    //@}
+
     /// \brief Look up rrset in cache.
     /// \brief Look up rrset in cache.
-    /// \return return the shared_ptr of rrset entry if it can 
+    /// \return return the shared_ptr of rrset entry if it can
     /// found in the cache, or else, return NULL.
     /// found in the cache, or else, return NULL.
     RRsetEntryPtr lookup(const isc::dns::Name& qname,
     RRsetEntryPtr lookup(const isc::dns::Name& qname,
                          const isc::dns::RRType& qtype);
                          const isc::dns::RRType& qtype);
 
 
     /// \brief Update RRset Cache
     /// \brief Update RRset Cache
     /// Update the rrset entry in the cache with the new one.
     /// Update the rrset entry in the cache with the new one.
-    /// If the rrset has expired or doesn't exist in the cache, 
-    /// it will be added directly. It may be ingored if the new 
+    /// If the rrset has expired or doesn't exist in the cache,
+    /// it will be added directly. It may be ingored if the new
     /// rrset is not more authoritative than the old rrset in cache.
     /// rrset is not more authoritative than the old rrset in cache.
-    /// 
+    ///
     /// \param rrset The new rrset used to update cache.
     /// \param rrset The new rrset used to update cache.
     /// \param level trustworthiness of the rrset.
     /// \param level trustworthiness of the rrset.
-    /// \return return the rrset entry in the cache, it may be the 
+    /// \return return the rrset entry in the cache, it may be the
     /// new added rrset entry or existed one if it is not replaced.
     /// new added rrset entry or existed one if it is not replaced.
     RRsetEntryPtr update(const isc::dns::RRset& rrset,
     RRsetEntryPtr update(const isc::dns::RRset& rrset,
                          const RRsetTrustLevel& level);
                          const RRsetTrustLevel& level);
@@ -64,7 +75,7 @@ public:
     /// \todo It should can be loaded from one configured database.
     /// \todo It should can be loaded from one configured database.
     void load(const std::string& file_name);
     void load(const std::string& file_name);
 
 
-    /// \brief Resize the size of rrset cache in runtime. 
+    /// \brief Resize the size of rrset cache in runtime.
     bool resize(uint32_t size);
     bool resize(uint32_t size);
 
 
 private:
 private:
@@ -74,7 +85,8 @@ private:
 };
 };
 
 
 typedef boost::shared_ptr<RRsetCache> RRsetCachePtr;
 typedef boost::shared_ptr<RRsetCache> RRsetCachePtr;
-    
+typedef boost::shared_ptr<const RRsetCache> ConstRRsetCachePtr;
+
 } // namespace cache
 } // namespace cache
 } // namespace isc
 } // namespace isc
 
 

+ 4 - 8
src/lib/cache/rrset_entry.cc

@@ -24,11 +24,12 @@ using namespace isc::dns;
 namespace isc {
 namespace isc {
 namespace cache {
 namespace cache {
 
 
-RRsetEntry::RRsetEntry(const isc::dns::RRset& rrset, const RRsetTrustLevel& level): 
-    entry_name_(genCacheEntryName(rrset.getName(), rrset.getType())), 
+RRsetEntry::RRsetEntry(const isc::dns::RRset& rrset, const RRsetTrustLevel& level):
+    entry_name_(genCacheEntryName(rrset.getName(), rrset.getType())),
     expire_time_(time(NULL) + rrset.getTTL().getValue()),
     expire_time_(time(NULL) + rrset.getTTL().getValue()),
     trust_level_(level),
     trust_level_(level),
-    rrset_(new RRset(rrset.getName(), rrset.getClass(), rrset.getType(), rrset.getTTL()))
+    rrset_(new RRset(rrset.getName(), rrset.getClass(), rrset.getType(), rrset.getTTL())),
+    hash_key_(HashKey(entry_name_, rrset_->getClass()))
 {
 {
     RdataIteratorPtr rdata_itor = rrset.getRdataIterator();
     RdataIteratorPtr rdata_itor = rrset.getRdataIterator();
     rdata_itor->first();
     rdata_itor->first();
@@ -54,11 +55,6 @@ RRsetEntry::getExpireTime() const {
     return expire_time_;
     return expire_time_;
 }
 }
 
 
-HashKey
-RRsetEntry::hashKey() const {
-    return HashKey(entry_name_, rrset_->getClass());
-}
-
 void
 void
 RRsetEntry::updateTTL(){
 RRsetEntry::updateTTL(){
     uint32_t oldTTL = rrset_->getTTL().getValue();
     uint32_t oldTTL = rrset_->getTTL().getValue();

+ 28 - 11
src/lib/cache/rrset_entry.h

@@ -34,7 +34,7 @@ namespace cache {
 /// RFC2181 section5.4.1.
 /// RFC2181 section5.4.1.
 /// Bigger value is more trustworthy.
 /// Bigger value is more trustworthy.
 enum RRsetTrustLevel {
 enum RRsetTrustLevel {
-    // Default trust for RRset. 
+    // Default trust for RRset.
     RRSET_TRUST_DEFAULT = 0,
     RRSET_TRUST_DEFAULT = 0,
     // Additional information from non-authoritative answer.
     // Additional information from non-authoritative answer.
     RRSET_TRUST_ADDITIONAL_NONAA,
     RRSET_TRUST_ADDITIONAL_NONAA,
@@ -50,8 +50,8 @@ enum RRsetTrustLevel {
     // Glue from a primary zone, or glue from a zone transfer.
     // Glue from a primary zone, or glue from a zone transfer.
     RRSET_TRUST_PRIM_GLUE,
     RRSET_TRUST_PRIM_GLUE,
     // Data from the authority section of an authoritative answer.
     // Data from the authority section of an authoritative answer.
-    RRSET_TRUST_AUTHORITY_AA, 
-    // Authoritative data included in the answer section of 
+    RRSET_TRUST_AUTHORITY_AA,
+    // Authoritative data included in the answer section of
     // an authoritative reply.
     // an authoritative reply.
     RRSET_TRUST_ANSWER_AA,
     RRSET_TRUST_ANSWER_AA,
     // Data from a primary zone file, other than glue data.
     // Data from a primary zone file, other than glue data.
@@ -60,20 +60,34 @@ enum RRsetTrustLevel {
 
 
 /// \brief RRset Entry
 /// \brief RRset Entry
 /// The object of RRsetEntry represents one cached RRset.
 /// The object of RRsetEntry represents one cached RRset.
-/// Each RRset entry may be refered using shared_ptr by several message 
+/// Each RRset entry may be refered using shared_ptr by several message
 /// entries.
 /// entries.
 class RRsetEntry : public NsasEntry<RRsetEntry>
 class RRsetEntry : public NsasEntry<RRsetEntry>
 {
 {
+    ///
+    /// \name Constructors and Destructor
+    ///
+    /// Note: The copy constructor and the assignment operator are intentionally
+    /// defined as private to make it uncopyable
+    //@{
+private:
+    RRsetEntry(const RRsetEntry&);
+    RRsetEntry& operator=(const RRsetEntry&);
 public:
 public:
     /// \brief Constructor
     /// \brief Constructor
     /// \param rrset The RRset used to initialize the RRset entry.
     /// \param rrset The RRset used to initialize the RRset entry.
     /// \param level trustworthiness of the RRset.
     /// \param level trustworthiness of the RRset.
     RRsetEntry(const isc::dns::RRset& rrset, const RRsetTrustLevel& level);
     RRsetEntry(const isc::dns::RRset& rrset, const RRsetTrustLevel& level);
 
 
+    /// The destructor.
+    ~RRsetEntry() {}
+    //@}
+
     /// \brief Return a pointer to a generated RRset
     /// \brief Return a pointer to a generated RRset
     isc::dns::RRsetPtr getRRset();
     isc::dns::RRsetPtr getRRset();
-    
+
     /// \brief Get the expiration time of the RRset.
     /// \brief Get the expiration time of the RRset.
+    /// \todo RRsig expiration processing
     time_t getExpireTime() const;
     time_t getExpireTime() const;
 
 
     /// \brief Get the ttl of the RRset.
     /// \brief Get the ttl of the RRset.
@@ -83,7 +97,9 @@ public:
     }
     }
 
 
     /// \return return hash key
     /// \return return hash key
-    virtual HashKey hashKey() const;
+    HashKey hashKey() const{
+        return hash_key_;
+    }
 
 
     /// \brief get RRset trustworthiness
     /// \brief get RRset trustworthiness
     RRsetTrustLevel getTrustLevel() const {
     RRsetTrustLevel getTrustLevel() const {
@@ -94,13 +110,14 @@ private:
     void updateTTL();
     void updateTTL();
 
 
 private:
 private:
-    std::string entry_name_; // the entry name for this rrset entry.
-    time_t expire_time_;    // Expiration time of rrset.
-    RRsetTrustLevel trust_level_; // rrset trustworthiness.
+    std::string entry_name_; // The entry name for this rrset entry.
+    time_t expire_time_;     // Expiration time of rrset.
+    RRsetTrustLevel trust_level_; // RRset trustworthiness.
     boost::shared_ptr<isc::dns::RRset> rrset_;
     boost::shared_ptr<isc::dns::RRset> rrset_;
+    HashKey hash_key_;       // RRsetEntry hash key
 };
 };
-    
-typedef boost::shared_ptr<RRsetEntry> RRsetEntryPtr;    
+
+typedef boost::shared_ptr<RRsetEntry> RRsetEntryPtr;
 
 
 } // namespace cache
 } // namespace cache
 } // namespace isc
 } // namespace isc

+ 1 - 0
src/lib/cache/tests/Makefile.am

@@ -33,6 +33,7 @@ TESTS += run_unittests
 run_unittests_SOURCES  = run_unittests.cc
 run_unittests_SOURCES  = run_unittests.cc
 run_unittests_SOURCES  += $(top_srcdir)/src/lib/dns/tests/unittest_util.cc
 run_unittests_SOURCES  += $(top_srcdir)/src/lib/dns/tests/unittest_util.cc
 run_unittests_SOURCES  += rrset_entry_unittest.cc
 run_unittests_SOURCES  += rrset_entry_unittest.cc
+run_unittests_SOURCES  += rrset_cache_unittest.cc
 run_unittests_SOURCES  += message_cache_unittest.cc
 run_unittests_SOURCES  += message_cache_unittest.cc
 run_unittests_SOURCES  += message_entry_unittest.cc
 run_unittests_SOURCES  += message_entry_unittest.cc
 
 

+ 84 - 0
src/lib/cache/tests/rrset_cache_unittest.cc

@@ -0,0 +1,84 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+#include <config.h>
+#include <string>
+#include <gtest/gtest.h>
+#include <cache/recursor_cache.h>
+#include <cache/cache_entry_key.h>
+#include <cache/rrset_entry.h>
+#include <cache/rrset_cache.h>
+#include <dns/name.h>
+#include <dns/rrclass.h>
+#include <dns/rrtype.h>
+#include <dns/rrttl.h>
+#include <dns/rrset.h>
+
+using namespace isc::cache;
+using namespace isc::dns;
+using namespace std;
+
+namespace {
+
+class RRsetCacheTest : public testing::Test {
+protected:
+    RRsetCacheTest():
+        cache(RRSET_CACHE_DEFAULT_SIZE, RRClass::IN().getCode()),
+        name("example.com"),
+        rrset1(name, RRClass::IN(), RRType::A(), RRTTL(20)),
+        rrset2(name, RRClass::IN(), RRType::A(), RRTTL(10)),
+        rrset_entry1(rrset1, RRSET_TRUST_ADDITIONAL_AA),
+        rrset_entry2(rrset2, RRSET_TRUST_PRIM_ZONE_NONGLUE)
+    {
+    }
+
+    RRsetCache cache;
+    Name name;
+    RRset rrset1;
+    RRset rrset2;
+    RRsetEntry rrset_entry1;
+    RRsetEntry rrset_entry2;
+};
+
+TEST_F(RRsetCacheTest, lookup) {
+    const RRType& type = RRType::A();
+    EXPECT_TRUE(cache.lookup(name, type) == NULL);
+
+    cache.update(rrset1, rrset_entry1.getTrustLevel());
+    RRsetEntryPtr rrset_entry_ptr = cache.lookup(name, type);
+    EXPECT_EQ(rrset_entry_ptr->getTrustLevel(), rrset_entry1.getTrustLevel());
+    EXPECT_EQ(rrset_entry_ptr->getRRset()->getName(), rrset_entry1.getRRset()->getName());
+    EXPECT_EQ(rrset_entry_ptr->getRRset()->getType(), rrset_entry1.getRRset()->getType());
+    EXPECT_EQ(rrset_entry_ptr->getRRset()->getClass(), rrset_entry1.getRRset()->getClass());
+}
+
+TEST_F(RRsetCacheTest, update) {
+    const RRType& type = RRType::A();
+
+    cache.update(rrset1, rrset_entry1.getTrustLevel());
+    RRsetEntryPtr rrset_entry_ptr = cache.lookup(name, type);
+    EXPECT_EQ(rrset_entry_ptr->getTrustLevel(), rrset_entry1.getTrustLevel());
+
+    cache.update(rrset2, rrset_entry2.getTrustLevel());
+    rrset_entry_ptr = cache.lookup(name, type);
+    // The trust level should be updated
+    EXPECT_EQ(rrset_entry_ptr->getTrustLevel(), rrset_entry2.getTrustLevel());
+
+    cache.update(rrset1, rrset_entry1.getTrustLevel());
+    // The trust level should not be updated
+    EXPECT_EQ(rrset_entry_ptr->getTrustLevel(), rrset_entry2.getTrustLevel());
+}
+
+}

+ 2 - 2
src/lib/cache/tests/rrset_entry_unittest.cc

@@ -50,8 +50,8 @@ TEST_F(GenCacheKeyTest, genCacheEntryKey2) {
 #define TEST_TTL 100
 #define TEST_TTL 100
 class RRsetEntryTest : public ::testing::Test {
 class RRsetEntryTest : public ::testing::Test {
 protected:
 protected:
-    RRsetEntryTest(): 
-        name("test.example.com"), 
+    RRsetEntryTest():
+        name("test.example.com"),
         rrset(name, RRClass::IN(), RRType::A(), RRTTL(TEST_TTL)),
         rrset(name, RRClass::IN(), RRType::A(), RRTTL(TEST_TTL)),
         trust_level(RRSET_TRUST_ADDITIONAL_AA),
         trust_level(RRSET_TRUST_ADDITIONAL_AA),
         rrset_entry(rrset, trust_level)
         rrset_entry(rrset, trust_level)