Browse Source

[trac493] Refactor the code by removing duplicated code, breaking the long line

zhanglikun 14 years ago
parent
commit
ca06de9c9e

+ 15 - 20
src/lib/cache/message_entry.cc

@@ -232,25 +232,24 @@ MessageEntry::parseNegativeResponseAuthoritySection(const isc::dns::Message& msg
         uint32_t& min_ttl,
         uint16_t& rrset_count)
 {
-
     // We found the SOA record, so we can cache the message and RRsets in the cache
     uint16_t count = 0;
     for (RRsetIterator iter = msg.beginSection(Message::SECTION_AUTHORITY);
             iter != msg.endSection(Message::SECTION_AUTHORITY);
             ++iter) {
         RRsetPtr rrset_ptr = *iter;
-        RRsetTrustLevel level = getRRsetTrustLevel(msg, rrset_ptr, Message::SECTION_AUTHORITY);
-        uint32_t rrset_ttl = 0;
-        if (rrset_ptr->getType() == RRType::SOA()){
-            RRsetEntryPtr rrset_entry = negative_soa_cache_->update(*rrset_ptr, level);
-            rrsets_.push_back(RRsetRef(rrset_ptr->getName(), rrset_ptr->getType(), negative_soa_cache_));
-            rrset_ttl = rrset_entry->getTTL();
-        } else {
-            RRsetEntryPtr rrset_entry = rrset_cache_->update(*rrset_ptr, level);
-            rrsets_.push_back(RRsetRef(rrset_ptr->getName(), rrset_ptr->getType(), rrset_cache_));
-            rrset_ttl = rrset_entry->getTTL();
-        }
-
+        RRsetTrustLevel level = getRRsetTrustLevel(msg, rrset_ptr,
+                                                   Message::SECTION_AUTHORITY);
+        boost::shared_ptr<RRsetCache> rrset_cache_ptr = rrset_cache_;
+        if (rrset_ptr->getType() == RRType::SOA()) {
+            rrset_cache_ptr.reset(negative_soa_cache_);
+        } 
+
+        RRsetEntryPtr rrset_entry = rrset_cache_ptr->update(*rrset_ptr, level);
+        rrsets_.push_back(RRsetRef(rrset_ptr->getName(),
+                                   rrset_ptr->getType(),
+                                   rrset_cache_ptr));
+        uint32_t rrset_ttl = rrset_entry->getTTL();
         if (min_ttl > rrset_ttl) {
             min_ttl = rrset_ttl;
         }
@@ -280,22 +279,18 @@ MessageEntry::initMessageEntry(const isc::dns::Message& msg) {
         parseSection(msg, Message::SECTION_AUTHORITY, min_ttl, authority_count_);
         parseSection(msg, Message::SECTION_ADDITIONAL, min_ttl, additional_count_);
     } else {
-        uint16_t rrset_count = 0;
-
-        // For negative response, if no soa RRset is found in authority section, dont cache it
+        // For negative response, if no soa RRset is found in authority 
+        // section, don't cache it
         if (!hasTheRecordInAuthoritySection(msg, RRType::SOA())) {
             return;
         }
 
-        parseNegativeResponseAuthoritySection(msg, min_ttl, rrset_count);
-
-        authority_count_ = rrset_count;
+        parseNegativeResponseAuthoritySection(msg, min_ttl, authority_count_);
         parseSection(msg, Message::SECTION_ANSWER, min_ttl, answer_count_);
         parseSection(msg, Message::SECTION_ADDITIONAL, min_ttl, additional_count_);
 
     }
     expire_time_ = time(NULL) + min_ttl;
-
 }
 
 bool

+ 8 - 4
src/lib/cache/message_entry.h

@@ -168,23 +168,27 @@ protected:
     //@}
 
 private:
-    /// \brief Check whetehr the message is a negative response(NXDOMAIN or NOERROR_NODATA)
+    /// \brief Check whetehr the message is a negative response
+    ///        (NXDOMAIN or NOERROR_NODATA)
     ///
     /// \param msg The response message
     bool isNegativeResponse(const isc::dns::Message& msg);
 
-    /// \brief Check whether there is some type of record in Authority section
+    /// \brief Check whether there is some type of record in
+    ///        Authority section
     ///
     /// \param msg The response message to be checked
     /// \param type The RR type that need to check
-    bool hasTheRecordInAuthoritySection(const isc::dns::Message& msg, const isc::dns::RRType& type);
+    bool hasTheRecordInAuthoritySection(const isc::dns::Message& msg,
+                                        const isc::dns::RRType& type);
 
     std::string entry_name_; // The name for this entry(name + type)
     HashKey* hash_key_ptr_;  // the key for messag entry in hash table.
 
     std::vector<RRsetRef> rrsets_;
     boost::shared_ptr<RRsetCache> rrset_cache_; //Normal rrset cache
-    boost::shared_ptr<RRsetCache> negative_soa_cache_; // SOA rrset from negative response
+    // SOA rrset from negative response
+    boost::shared_ptr<RRsetCache> negative_soa_cache_;
 
     std::string query_name_; // query name of the message.
     uint16_t query_class_; // query class of the message.

+ 4 - 2
src/lib/cache/resolver_cache.cc

@@ -35,7 +35,8 @@ ResolverClassCache::ResolverClassCache(const RRClass& cache_class) :
     rrsets_cache_ = RRsetCachePtr(new RRsetCache(RRSET_CACHE_DEFAULT_SIZE,
                                                  cache_class_.getCode()));
     // SOA rrset cache from negative response
-    negative_soa_cache_ = RRsetCachePtr(new RRsetCache(NEGATIVE_RRSET_CACHE_DEFAULT_SIZE, cache_class_.getCode()));
+    negative_soa_cache_ = RRsetCachePtr(new RRsetCache(NEGATIVE_RRSET_CACHE_DEFAULT_SIZE,
+                                                       cache_class_.getCode()));
 
     messages_cache_ = MessageCachePtr(new MessageCache(rrsets_cache_,
                                       MESSAGE_CACHE_DEFAULT_SIZE,
@@ -51,7 +52,8 @@ ResolverClassCache::ResolverClassCache(CacheSizeInfo cache_info) :
     rrsets_cache_ = RRsetCachePtr(new
                         RRsetCache(cache_info.rrset_cache_size, klass));
     // SOA rrset cache from negative response
-    negative_soa_cache_ = RRsetCachePtr(new RRsetCache(cache_info.rrset_cache_size, klass));
+    negative_soa_cache_ = RRsetCachePtr(new RRsetCache(cache_info.rrset_cache_size,
+                                                       klass));
 
     messages_cache_ = MessageCachePtr(new MessageCache(rrsets_cache_,
                                       cache_info.message_cache_size,

+ 1 - 1
src/lib/cache/resolver_cache.h

@@ -47,7 +47,7 @@ public:
     /// \param cls The RRClass code
     /// \param msg_cache_size The size for the message cache
     /// \param rst_cache_size The size for the RRset cache
-    CacheSizeInfo(const isc::dns::RRClass& cls, 
+    CacheSizeInfo(const isc::dns::RRClass& cls,
                   uint32_t msg_cache_size,
                   uint32_t rst_cache_size):
                     cclass(cls),

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

@@ -65,3 +65,10 @@ EXTRA_DIST += testdata/message_fromWire3
 EXTRA_DIST += testdata/message_fromWire4
 EXTRA_DIST += testdata/message_fromWire5
 EXTRA_DIST += testdata/message_fromWire6
+EXTRA_DIST += testdata/message_cname_referral.wire
+EXTRA_DIST += testdata/message_example_com_soa.wire
+EXTRA_DIST += testdata/message_nodata_with_soa.wire
+EXTRA_DIST += testdata/message_nxdomain_cname.wire
+EXTRA_DIST += testdata/message_nxdomain_no_soa.wire
+EXTRA_DIST += testdata/message_nxdomain_with_soa.wire
+EXTRA_DIST += testdata/message_referral.wire

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

@@ -53,8 +53,8 @@ public:
         uint16_t class_ = RRClass::IN().getCode();
         rrset_cache_.reset(new RRsetCache(RRSET_CACHE_DEFAULT_SIZE, class_));
         negative_soa_cache_.reset(new RRsetCache(NEGATIVE_RRSET_CACHE_DEFAULT_SIZE, class_));
-        message_cache_.reset(new DerivedMessageCache(rrset_cache_, 
-                                          MESSAGE_CACHE_DEFAULT_SIZE, class_, 
+        message_cache_.reset(new DerivedMessageCache(rrset_cache_,
+                                          MESSAGE_CACHE_DEFAULT_SIZE, class_,
                                           negative_soa_cache_));
     }