Browse Source

[trac643] Add clear() function to lru list, which is used to clear all the elements of list.

zhanglikun 14 years ago
parent
commit
050c451f53

+ 5 - 0
src/lib/cache/message_cache.cc

@@ -37,6 +37,11 @@ MessageCache::MessageCache(boost::shared_ptr<RRsetCache> rrset_cache,
 {
 {
 }
 }
 
 
+MessageCache::~MessageCache() {
+    // Destroy all the message entries in the cache.
+    message_lru_.clear();
+}
+
 bool
 bool
 MessageCache::lookup(const isc::dns::Name& qname,
 MessageCache::lookup(const isc::dns::Name& qname,
                      const isc::dns::RRType& qtype,
                      const isc::dns::RRType& qtype,

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

@@ -42,7 +42,7 @@ public:
                  uint32_t cache_size, uint16_t message_class);
                  uint32_t cache_size, uint16_t message_class);
 
 
     /// \brief Destructor function
     /// \brief Destructor function
-    virtual ~MessageCache() {}
+    virtual ~MessageCache();
 
 
     /// \brief Look up message in cache.
     /// \brief Look up message in cache.
     /// \param message generated response message if the message entry
     /// \param message generated response message if the message entry

+ 4 - 2
src/lib/cache/rrset_cache.h

@@ -40,12 +40,14 @@ private:
     RRsetCache(const RRsetCache&);
     RRsetCache(const RRsetCache&);
     RRsetCache& operator=(const RRsetCache&);
     RRsetCache& operator=(const RRsetCache&);
 public:
 public:
-    /// \brief Constructor
+    /// \brief Constructor and Destructor
     ///
     ///
     /// \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);
-    virtual ~RRsetCache() {}
+    virtual ~RRsetCache() {
+        rrset_lru_.clear(); // Clear the rrset entries in the list.
+    }
     //@}
     //@}
 
 
     /// \brief Look up rrset in cache.
     /// \brief Look up rrset in cache.

+ 26 - 0
src/lib/nsas/lru_list.h

@@ -109,6 +109,13 @@ public:
     /// \param element Reference to the element to touch.
     /// \param element Reference to the element to touch.
     virtual void touch(boost::shared_ptr<T>& element);
     virtual void touch(boost::shared_ptr<T>& element);
 
 
+    /// \brief Drop All the Elements in the List .
+    ///
+    /// All the elements will be dropped from the list container, and their
+    /// drop handler(if there is one) will be called, left the size of list
+    /// to 0.
+    virtual void clear();
+
     /// \brief Return Size of the List
     /// \brief Return Size of the List
     ///
     ///
     /// An independent count is kept of the list size, as list.size() may take
     /// An independent count is kept of the list size, as list.size() may take
@@ -228,6 +235,25 @@ void LruList<T>::touch(boost::shared_ptr<T>& element) {
     }
     }
 }
 }
 
 
+// Clear the list-  left the size of list to 0
+template <typename T>
+void LruList<T>::clear() {
+    // Protect list against concurrent access
+    isc::locks::scoped_lock<isc::locks::mutex> lock(mutex_);
+
+    // ... and update the count while we have the mutex.
+    count_ = 0;
+    typename std::list<boost::shared_ptr<T> >::iterator iter;
+    if (dropped_) {
+        for (iter = lru_.begin(); iter != lru_.end(); ++iter) {
+            // Call the drop handler.
+            (*dropped_)(iter->get());
+        }
+    }
+
+    lru_.clear();
+}
+
 }   // namespace nsas
 }   // namespace nsas
 }   // namespace isc
 }   // namespace isc
 
 

+ 29 - 0
src/lib/nsas/tests/lru_list_unittest.cc

@@ -251,6 +251,35 @@ TEST_F(LruListTest, Dropped) {
     EXPECT_EQ(0, (entry3_->getClass().getCode() & 0x8000));
     EXPECT_EQ(0, (entry3_->getClass().getCode() & 0x8000));
 }
 }
 
 
+// Clear functor tests: tests whether all the elements in
+// the list are dropped properly and the size of list is
+// set to 0.
+TEST_F(LruListTest, Clear) {
+    // Create an object with an expiration handler.
+    LruList<TestEntry> lru(3, new Dropped());
+
+    // Fill the list
+    lru.add(entry1_);
+    lru.add(entry2_);
+    lru.add(entry3_);
+
+    EXPECT_EQ(RRClass::IN(), entry1_->getClass());
+    EXPECT_EQ(RRClass::CH(), entry2_->getClass());
+    EXPECT_EQ(RRClass::HS(), entry3_->getClass());
+
+    EXPECT_EQ(0, (entry1_->getClass().getCode() & 0x8000));
+    EXPECT_EQ(0, (entry2_->getClass().getCode() & 0x8000));
+    EXPECT_EQ(0, (entry3_->getClass().getCode() & 0x8000));
+
+    // Clear the lru list, and check the drop handler run
+    lru.clear();
+    EXPECT_NE(0, (entry1_->getClass().getCode() & 0x8000));
+    EXPECT_NE(0, (entry2_->getClass().getCode() & 0x8000));
+    EXPECT_NE(0, (entry3_->getClass().getCode() & 0x8000));
+ 
+    EXPECT_EQ(0, lru.size());
+}
+
 // Miscellaneous tests - pathological conditions
 // Miscellaneous tests - pathological conditions
 TEST_F(LruListTest, Miscellaneous) {
 TEST_F(LruListTest, Miscellaneous) {