Browse Source

commit the forgotten files

zhanglikun 14 years ago
parent
commit
4e8c15eae9

+ 7 - 7
src/lib/cache/cache_entry_key.cc

@@ -21,26 +21,26 @@ using namespace std;
 
 namespace isc {
 namespace cache {
-CacheEntryKey
-genCacheEntryKey(const isc::dns::Name& name,
+const std::string
+genCacheEntryName(const isc::dns::Name& name,
                  const isc::dns::RRType& type) 
 {
     std::string keystr = name.toText();
     ostringstream stream;
     stream << type.getCode();
     keystr += stream.str();
-    return CacheEntryKey(keystr.c_str(), keystr.length());
+    return keystr;
 }
-    
-CacheEntryKey
-genCacheEntryKey(const std::string& namestr,
+
+const std::string
+genCacheEntryName(const std::string& namestr,
                  const uint16_t type) 
 {
     std::string keystr = namestr;
     ostringstream stream;
     stream << type;
     keystr += stream.str();
-    return CacheEntryKey(keystr.c_str(), keystr.length());
+    return keystr;
 }
     
 } // namespace cache

+ 11 - 10
src/lib/cache/cache_entry_key.h

@@ -24,22 +24,23 @@
 namespace isc {
 namespace cache {
 
-typedef std::pair<const char*, const uint32_t> CacheEntryKey;
-
-/// \brief Key Generation Functions
-/// Generate the hash key for message/rrset entries.
-/// The key is name(name of rrset) + type(16bits). 
+/// \brief Entry Name Generation Functions
+/// Generate the name for message/rrset entries.
+/// The name is name(name of rrset, for message entry, 
+/// the name is query name) + str(type)
 /// Note. the name is text string, not wire format.
-/// \return a pair(key_name, key_lenght) is returned
-CacheEntryKey
-genCacheEntryKey(const isc::dns::Name& name, 
+/// eg. if name is 'example.com.', type is 'A', the return
+/// value is 'example.com.1'
+/// \return return the entry name.
+const std::string
+genCacheEntryName(const isc::dns::Name& name, 
                  const isc::dns::RRType& type);
    
 /// 
 /// \overload
 /// 
-CacheEntryKey
-genCacheEntryKey(const std::string& namestr, const uint16_t type);
+const std::string
+genCacheEntryName(const std::string& namestr, const uint16_t type);
 
 } // namespace cache
 } // namespace isc

+ 9 - 12
src/lib/cache/message_cache.cc

@@ -41,7 +41,9 @@ MessageCache::lookup(const isc::dns::Name& qname,
                      const isc::dns::RRType& qtype,
                      isc::dns::Message& response)
 {
-    HashKey entry_key = getEntryHashKey(qname, qtype);
+
+    std::string entry_name = genCacheEntryName(qname, qtype);
+    HashKey entry_key = HashKey(entry_name, RRClass(message_class_));
     MessageEntryPtr msg_entry = message_table_.get(entry_key);
     if(msg_entry) {
         message_lru_.touch(msg_entry);
@@ -54,12 +56,14 @@ MessageCache::lookup(const isc::dns::Name& qname,
 bool
 MessageCache::update(const Message& msg) {
     QuestionIterator iter = msg.beginQuestion();
-    HashKey entry_key = getEntryHashKey((*iter)->getName(),
-                                           (*iter)->getType());
+    std::string entry_name = genCacheEntryName((*iter)->getName(), (*iter)->getType());
+    HashKey entry_key = HashKey(entry_name, RRClass(message_class_));
     
     // The simplest way to update is removing the old message entry directly.
     // We have find the existed message entry, since we need to delete it
     // from lru list too.
+    // TODO, but there should be a better way, since we here have to remove and
+    // add the message entry, maybe there is one way to touch it once.
     MessageEntryPtr old_msg_entry = message_table_.get(entry_key);
     if (old_msg_entry) {
         message_table_.remove(entry_key);
@@ -67,15 +71,8 @@ MessageCache::update(const Message& msg) {
     }
 
     MessageEntryPtr msg_entry(new MessageEntry(msg, rrset_cache_));
-    message_lru_.touch(msg_entry); // Touch the new message entry
-    return message_table_.add(msg_entry, entry_key, true);
-}
-
-HashKey
-MessageCache::getEntryHashKey(const Name& name, const RRType& type) const 
-{
-    CacheEntryKey keydata = genCacheEntryKey(name, type);
-    return HashKey(keydata.first, keydata.second, RRClass(message_class_));
+    message_lru_.touch(msg_entry); 
+    return message_table_.add(msg_entry, entry_key, true); 
 }
 
 void

+ 2 - 0
src/lib/cache/message_cache.h

@@ -83,6 +83,8 @@ protected:
     isc::nsas::LruList<MessageEntry> message_lru_;
 };
     
+typedef boost::shared_ptr<MessageCache> MessageCachePtr;
+
 } // namespace cache
 } // namespace isc
 

+ 2 - 2
src/lib/cache/message_entry.cc

@@ -37,6 +37,7 @@ MessageEntry::MessageEntry(const isc::dns::Message& msg,
     headerflag_ad_(false)
 {
     initMessageEntry(msg);
+    entry_name_ = genCacheEntryName(query_name_, query_type_);
 }
     
 bool
@@ -168,8 +169,7 @@ MessageEntry::initMessageEntry(const isc::dns::Message& msg) {
 
 HashKey
 MessageEntry::hashKey() const {
-    CacheEntryKey keydata = genCacheEntryKey(query_name_, query_type_);
-    return HashKey(keydata.first, keydata.second, RRClass(query_class_));
+    return HashKey(entry_name_, RRClass(query_class_));
 }
 
 } // namespace cache

+ 1 - 0
src/lib/cache/message_entry.h

@@ -90,6 +90,7 @@ protected:
     //@}
 private:
     time_t expire_time_;  // Expiration time of the message.
+    std::string entry_name_; // The name for this entry(name + type)
 
     std::string query_name_; // query name of the message.
     uint16_t query_class_; // query class of the message.

+ 1 - 2
src/lib/cache/recursor_cache.h

@@ -22,13 +22,12 @@
 #include <boost/shared_ptr.hpp>
 #include <dns/message.h>
 #include "message_cache.h"
+#include "rrset_cache.h"
 
 namespace isc {
 namespace cache {
 class RRsetCache;
 
-typedef boost::shared_ptr<MessageCache> MessageCachePtr;
-typedef boost::shared_ptr<RRsetCache> RRsetCachePtr;
 typedef std::map<uint16_t, MessageCachePtr> MessageCacheMap;
 typedef std::map<uint16_t, RRsetCachePtr> RRsetCacheMap;
 

+ 3 - 12
src/lib/cache/rrset_cache.cc

@@ -22,6 +22,7 @@
 
 using namespace isc::nsas;
 using namespace isc::dns;
+using namespace std;
 
 namespace isc {
 namespace cache {
@@ -39,11 +40,8 @@ RRsetEntryPtr
 RRsetCache::lookup(const isc::dns::Name& qname,
                    const isc::dns::RRType& qtype)
 {
-    CacheEntryKey keydata = genCacheEntryKey(qname, qtype);
-    //TODO, HashKey need to be refactored, since we don't need query class
-    // as the parameters.
-    RRsetEntryPtr entry_ptr = rrset_table_.get(HashKey(
-           keydata.first, keydata.second, RRClass(class_)));
+    const string entry_name = genCacheEntryName(qname, qtype);
+    RRsetEntryPtr entry_ptr = rrset_table_.get(HashKey(entry_name, RRClass(class_)));
 
     //If the rrset entry has expired, return NULL.
     if(entry_ptr && (time(NULL) > entry_ptr->getExpireTime())) {
@@ -84,13 +82,6 @@ RRsetCache::update(const isc::dns::RRset& rrset, const RRsetTrustLevel& level) {
     }
 }
 
-HashKey
-RRsetCache::getEntryHashKey(const Name& name, const RRType& type) const 
-{
-    CacheEntryKey keydata = genCacheEntryKey(name, type);
-    return HashKey(keydata.first, keydata.second, RRClass(class_));
-}
-
 void
 RRsetCache::dump(const std::string&) {
     //TODO

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

@@ -67,14 +67,6 @@ public:
     /// \brief Resize the size of rrset cache in runtime. 
     bool resize(uint32_t size);
 
-protected:
-    /// \brief Get the hash key for the rrset entry in the cache.
-    /// \param name name of the rrset.
-    /// \param type type of the rrset.
-    /// \return return the hash key.
-    HashKey getEntryHashKey(const isc::dns::Name& name, 
-                            const isc::dns::RRType& type) const;
-
 private:
     uint16_t class_; // The class of the rrset cache.
     isc::nsas::HashTable<RRsetEntry> rrset_table_;

+ 2 - 2
src/lib/cache/rrset_entry.cc

@@ -25,6 +25,7 @@ namespace isc {
 namespace cache {
 
 RRsetEntry::RRsetEntry(const isc::dns::RRset& rrset, const RRsetTrustLevel& level): 
+    entry_name_(genCacheEntryName(rrset.getName(), rrset.getType())), 
     expire_time_(time(NULL) + rrset.getTTL().getValue()),
     trust_level_(level),
     rrset_(new RRset(rrset.getName(), rrset.getClass(), rrset.getType(), rrset.getTTL()))
@@ -50,8 +51,7 @@ RRsetEntry::getExpireTime() const {
 
 HashKey
 RRsetEntry::hashKey() const {
-    CacheEntryKey keydata = genCacheEntryKey(rrset_->getName().toText(), rrset_->getType().getCode());
-    return HashKey(keydata.first, keydata.second, RRClass(rrset_->getClass().getCode()));
+    return HashKey(entry_name_, rrset_->getClass());
 }
 
 void

+ 1 - 0
src/lib/cache/rrset_entry.h

@@ -94,6 +94,7 @@ private:
     void updateTTL();
 
 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.
     boost::shared_ptr<isc::dns::RRset> rrset_;

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

@@ -4,8 +4,11 @@ AM_CPPFLAGS  = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
 AM_CPPFLAGS += $(BOOST_INCLUDES) $(MULTITHREADING_FLAG)
 AM_CPPFLAGS += -I$(top_srcdir)/src/lib/dns -I$(top_builddir)/src/lib/dns
 AM_CPPFLAGS += -I$(top_srcdir)/src/lib/cache -I$(top_builddir)/src/lib/cache
+AM_CPPFLAGS += -DTEST_DATA_SRCDIR=\"$(srcdir)/testdata\"
+AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/lib/cache/tests/testdata\"
 AM_CXXFLAGS = $(B10_CXXFLAGS)
 
+
 AM_LDFLAGS = $(PTHREAD_LDFLAGS)
 if USE_STATIC_LINK
 AM_LDFLAGS += -static
@@ -28,7 +31,9 @@ TESTS =
 if HAVE_GTEST
 TESTS += run_unittests
 run_unittests_SOURCES  = run_unittests.cc
+run_unittests_SOURCES  += $(top_srcdir)/src/lib/dns/tests/unittest_util.cc
 run_unittests_SOURCES  += rrset_entry_unittest.cc
+run_unittests_SOURCES  += message_cache_unittest.cc
 
 run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
 run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)

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

@@ -31,18 +31,14 @@ TEST_F(GenCacheKeyTest, genCacheEntryKey1) {
     uint16_t type = 12;
     string name_type = "example.com.12";
 
-    pair<const char*, const int32_t> key =  genCacheEntryKey(name, type);
-    EXPECT_EQ(name_type, string(key.first));
-    EXPECT_EQ(name_type.length(), key.second);
+    EXPECT_EQ(name_type, genCacheEntryName(name, type));
 }
 
 TEST_F(GenCacheKeyTest, genCacheEntryKey2) {
     Name name("example.com");
     RRType type(1234);
     string keystr = "example.com.1234";
-    pair<const char*, const int32_t> key = genCacheEntryKey(name, type);
-    EXPECT_EQ(keystr, string(key.first));
-    EXPECT_EQ(keystr.length(), key.second);
+    EXPECT_EQ(keystr, genCacheEntryName(name, type));
 }
 
 }   // namespace

+ 2 - 0
src/lib/cache/tests/run_unittests.cc

@@ -22,6 +22,8 @@
 int
 main(int argc, char* argv[]) {
     ::testing::InitGoogleTest(&argc, argv);
+    isc::UnitTestUtil::addDataPath(TEST_DATA_SRCDIR);
+    isc::UnitTestUtil::addDataPath(TEST_DATA_BUILDDIR);
 
     return (RUN_ALL_TESTS());
 }