Commit 050c451f authored by zhanglikun's avatar zhanglikun

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

parent acbd9518
......@@ -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
MessageCache::lookup(const isc::dns::Name& qname,
const isc::dns::RRType& qtype,
......
......@@ -42,7 +42,7 @@ public:
uint32_t cache_size, uint16_t message_class);
/// \brief Destructor function
virtual ~MessageCache() {}
virtual ~MessageCache();
/// \brief Look up message in cache.
/// \param message generated response message if the message entry
......
......@@ -40,12 +40,14 @@ private:
RRsetCache(const RRsetCache&);
RRsetCache& operator=(const RRsetCache&);
public:
/// \brief Constructor
/// \brief Constructor and Destructor
///
/// \param cache_size the size of rrset cache.
/// \param rrset_class the class of rrset cache.
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.
......
......@@ -109,6 +109,13 @@ public:
/// \param element Reference to the element to touch.
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
///
/// 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 isc
......
......@@ -251,6 +251,35 @@ TEST_F(LruListTest, Dropped) {
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
TEST_F(LruListTest, Miscellaneous) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment