[svn] commit: r2245 - /branches/trac192/src/lib/datasrc/cache.cc
BIND 10 source code commits
bind10-changes at lists.isc.org
Wed Jun 23 18:36:56 UTC 2010
Author: each
Date: Wed Jun 23 18:36:56 2010
New Revision: 2245
Log:
some improved documentation in cache.cc. also reorganized the file
for clarity
Modified:
branches/trac192/src/lib/datasrc/cache.cc
Modified: branches/trac192/src/lib/datasrc/cache.cc
==============================================================================
--- branches/trac192/src/lib/datasrc/cache.cc (original)
+++ branches/trac192/src/lib/datasrc/cache.cc Wed Jun 23 18:36:56 2010
@@ -22,20 +22,20 @@
namespace isc {
namespace datasrc {
-/// \brief A \c CacheEntry object contains a pointer to an RRset,
-/// and the query-response flags that were returned when the RRset
+/// \brief A \c CacheEntry object contains the data stored with
+/// each \c CacheNode: a pointer to the cached RRset (empty in
+/// the case of a negative cache entry), and a copy of the
+/// query-response flags that were returned when the RRset
/// was originally looked up in the low-level data source.
class CacheEntry {
private:
/// The copy constructor and the assignment operator are intentionally
/// defined as private.
- //@{
CacheEntry(const CacheEntry& source);
CacheEntry& operator=(const CacheEntry& source);
public:
CacheEntry(RRsetPtr r, uint32_t f) : rrset(r), flags(f) {};
- //@}
RRsetPtr rrset;
uint32_t flags;
@@ -43,10 +43,13 @@
typedef boost::shared_ptr<CacheEntry> CacheEntryPtr;
-/// \brief A \c CacheNode is a node in the \c HotCache.
+/// \brief A \c CacheNode is a node in the \c HotCache LRU queue. It
+/// contains a pointer to a \c CacheEntry, a reference to the \c Question
+/// that we are answering, a lifespan during which this entry remains
+/// valid, and pointers to the next and previous entries in the list.
class CacheNode {
private:
- /// \name Constructors, Assignment Operator and Destructor.
+ /// \name Constructors and Assignment Operator
///
/// Note: The copy constructor and the assignment operator are intentionally
/// defined as private.
@@ -77,9 +80,6 @@
const RRType& rrtype,
uint32_t flags,
time_t lifespan);
-
- // \brief Destructor
- ~CacheNode();
//@}
/// \name Getter and Setter Methods
@@ -106,11 +106,8 @@
CacheNodePtr prev;
CacheNodePtr next;
- // The standard query tuple: qname/qclass/qtype.
- // Used as the search key for cache nodes.
- const Name qname;
- const RRClass qclass;
- const RRType qtype;
+ /// The \c Question (name/rrclass/rrtype) answered by this cache node
+ const isc::dns::Question q;
private:
// The cached RRset data
@@ -120,7 +117,43 @@
time_t expiry;
};
-/// This class abstracts the implementation details for the HotCache.
+// CacheNode constructor for a positive cache entry
+CacheNode::CacheNode(const RRsetPtr rrset, const uint32_t flags,
+ const time_t lifespan) :
+ q(Question(rrset->getName(), rrset->getClass(), rrset->getType()))
+{
+ const time_t now = time(NULL);
+ expiry = now + lifespan;
+
+ entry = CacheEntryPtr(new CacheEntry(rrset, flags));
+ prev = CacheNodePtr();
+ next = CacheNodePtr();
+}
+
+// CacheNode constructor for a negative cache entry
+CacheNode::CacheNode(const Name& name,
+ const RRClass& rrclass,
+ const RRType& rrtype,
+ const uint32_t flags,
+ const time_t lifespan) :
+ q(Question(name, rrclass, rrtype))
+{
+ const time_t now = time(NULL);
+ expiry = now + lifespan;
+
+ entry = CacheEntryPtr(new CacheEntry(RRsetPtr(), flags));
+ prev = CacheNodePtr();
+ next = CacheNodePtr();
+}
+
+// Returns true if the node has not yet expired.
+bool
+CacheNode::isValid() const {
+ const time_t now = time(NULL);
+ return (now < expiry);
+}
+
+/// This class abstracts the implementation details for \c HotCache.
///
/// Each node inserted into the cache is placed at the head of a
/// doubly-linked list. Whenever that node is retrieved from the cache,
@@ -162,43 +195,6 @@
void insert(CacheNodePtr node);
};
-// CacheNode constructors
-CacheNode::CacheNode(const RRsetPtr rrset, const uint32_t flags,
- const time_t lifespan) :
- qname(rrset->getName()), qclass(rrset->getClass()), qtype(rrset->getType())
-{
- const time_t now = time(NULL);
- expiry = now + lifespan;
-
- entry = CacheEntryPtr(new CacheEntry(rrset, flags));
- prev = CacheNodePtr();
- next = CacheNodePtr();
-}
-
-CacheNode::CacheNode(const Name& name,
- const RRClass& rrclass,
- const RRType& rrtype,
- const uint32_t flags,
- const time_t lifespan) :
- qname(name), qclass(rrclass), qtype(rrtype)
-{
- const time_t now = time(NULL);
- expiry = now + lifespan;
-
- entry = CacheEntryPtr(new CacheEntry(RRsetPtr(), flags));
- prev = CacheNodePtr();
- next = CacheNodePtr();
-}
-
-// CacheNode destructor
-CacheNode::~CacheNode() {}
-
-bool
-CacheNode::isValid() const {
- const time_t now = time(NULL);
- return (now < expiry);
-}
-
// HotCacheImpl constructor
HotCacheImpl::HotCacheImpl(const int slots) : slots_(slots), count_(0) {}
@@ -215,10 +211,11 @@
lru_head_ = lru_tail_ = CacheNodePtr();
}
+// Insert a cache node into the cache
inline void
HotCacheImpl::insert(const CacheNodePtr node) {
std::map<Question, CacheNodePtr>::const_iterator iter;
- iter = map_.find(Question(node->qname, node->qclass, node->qtype));
+ iter = map_.find(node->q);
if (iter != map_.end()) {
CacheNodePtr old = iter->second;
if (old && old->isValid()) {
@@ -236,7 +233,7 @@
lru_tail_ = node;
}
- map_[Question(node->qname, node->qclass, node->qtype)] = node;
+ map_[node->q] = node;
++count_;
if (slots_ != 0 && count_ > slots_ && lru_tail_) {
@@ -245,6 +242,7 @@
}
}
+// Promote a node to the head of the LRU queue
void
HotCacheImpl::promote(CacheNodePtr node) {
if (!node || node == lru_head_) {
@@ -270,6 +268,7 @@
lru_head_ = node;
}
+// Remove a node from the LRU queue and the map
void
HotCacheImpl::remove(ConstCacheNodePtr node) {
if (!node) {
@@ -292,7 +291,7 @@
node->prev->next = node->next;
}
- map_.erase(Question(node->qname, node->qclass, node->qtype));
+ map_.erase(node->q);
--count_;
}
@@ -306,6 +305,7 @@
delete impl_;
}
+// Add a positive entry to the cache
void
HotCache::addPositive(RRsetPtr rrset, const uint32_t flags,
const time_t lifespan)
@@ -313,6 +313,7 @@
impl_->insert(CacheNodePtr(new CacheNode(rrset, flags, lifespan)));
}
+// Add a negative entry to the cache
void
HotCache::addNegative(const Name& name, const RRClass &rrclass,
const RRType& rrtype, const uint32_t flags,
@@ -326,6 +327,8 @@
flags, lifespan)));
}
+// Try to retrieve an entry from the cache, returning true if
+// it was found and valid.
bool
HotCache::retrieve(const Name& n, const RRClass& c, const RRType& t,
RRsetPtr& rrset, uint32_t& flags)
@@ -349,6 +352,7 @@
return (false);
}
+// Set the number of slots in the cache.
void
HotCache::setSlots(const int slots) {
impl_->slots_ = slots;
@@ -360,11 +364,13 @@
}
}
+// Return the number of slots in the cache
int
HotCache::getSlots() const {
return (impl_->slots_);
}
+// Return the number of entries in the cache
int
HotCache::getCount() const {
return (impl_->count_);
More information about the bind10-changes
mailing list