interest: Cache Link object after the first call to getLink
Change-Id: I2625cdb665365cb3c1a69ec7d917d09ad621988d
Refs: #3158
diff --git a/src/interest.cpp b/src/interest.cpp
index d4ce90b..35be5ce 100644
--- a/src/interest.cpp
+++ b/src/interest.cpp
@@ -313,10 +313,9 @@
// Selectors
Block::element_const_iterator val = m_wire.find(tlv::Selectors);
- if (val != m_wire.elements_end())
- {
- m_selectors.wireDecode(*val);
- }
+ if (val != m_wire.elements_end()) {
+ m_selectors.wireDecode(*val);
+ }
else
m_selectors = Selectors();
@@ -325,27 +324,28 @@
// InterestLifetime
val = m_wire.find(tlv::InterestLifetime);
- if (val != m_wire.elements_end())
- {
- m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
- }
- else
- {
- m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
- }
+ if (val != m_wire.elements_end()) {
+ m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
+ }
+ else {
+ m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
+ }
// Link object
+ m_linkCached.reset();
val = m_wire.find(tlv::Data);
- if (val != m_wire.elements_end())
- {
- m_link = (*val);
- }
+ if (val != m_wire.elements_end()) {
+ m_link = (*val);
+ }
+ else {
+ m_link = Block();
+ }
// SelectedDelegation
val = m_wire.find(tlv::SelectedDelegation);
if (val != m_wire.elements_end()) {
if (!this->hasLink()) {
- BOOST_THROW_EXCEPTION(Error("Interest contains selectedDelegation, but no LINK object"));
+ BOOST_THROW_EXCEPTION(Error("Interest contains SelectedDelegation, but no LINK object"));
}
uint64_t selectedDelegation = readNonNegativeInteger(*val);
if (selectedDelegation < uint64_t(Link::countDelegationsFromWire(m_link))) {
@@ -355,23 +355,26 @@
BOOST_THROW_EXCEPTION(Error("Invalid selected delegation index when decoding Interest"));
}
}
+ else {
+ m_selectedDelegationIndex = INVALID_SELECTED_DELEGATION_INDEX;
+ }
}
bool
Interest::hasLink() const
{
- if (m_link.hasWire())
- return true;
- return false;
+ return m_link.hasWire();
}
-Link
+const Link&
Interest::getLink() const
{
- if (hasLink())
- {
- return Link(m_link);
+ if (hasLink()) {
+ if (!m_linkCached) {
+ m_linkCached = make_shared<Link>(m_link);
}
+ return *m_linkCached;
+ }
BOOST_THROW_EXCEPTION(Error("There is no encapsulated link object"));
}
@@ -383,6 +386,7 @@
BOOST_THROW_EXCEPTION(Error("The given link does not have a wire format"));
}
m_wire.reset();
+ m_linkCached.reset();
this->unsetSelectedDelegation();
}
@@ -391,17 +395,14 @@
{
m_link.reset();
m_wire.reset();
+ m_linkCached.reset();
this->unsetSelectedDelegation();
}
bool
Interest::hasSelectedDelegation() const
{
- if (m_selectedDelegationIndex != INVALID_SELECTED_DELEGATION_INDEX)
- {
- return true;
- }
- return false;
+ return m_selectedDelegationIndex != INVALID_SELECTED_DELEGATION_INDEX;
}
Name
diff --git a/src/interest.hpp b/src/interest.hpp
index 80a1b18..686f0a0 100644
--- a/src/interest.hpp
+++ b/src/interest.hpp
@@ -134,8 +134,9 @@
* @brief Get the link object for this interest
* @return The link object if there is one contained in this interest
* @throws Interest::Error if there is no link object contained in the interest
+ * @throws tlv::Error if the incorporated link object is malformed
*/
- Link
+ const Link&
getLink() const;
/**
@@ -147,7 +148,8 @@
setLink(const Block& link);
/**
- *@brief Reset the wire format of the given interest and the contained link
+ * @brief Delete the link object for this interest
+ * @post !hasLink()
*/
void
unsetLink();
@@ -442,6 +444,7 @@
time::milliseconds m_interestLifetime;
mutable Block m_link;
+ mutable shared_ptr<Link> m_linkCached;
size_t m_selectedDelegationIndex;
mutable Block m_wire;
diff --git a/src/link.cpp b/src/link.cpp
index d4de993..587f480 100644
--- a/src/link.cpp
+++ b/src/link.cpp
@@ -161,11 +161,11 @@
preference = static_cast<uint32_t>(readNonNegativeInteger(*val));
}
catch (tlv::Error&) {
- BOOST_THROW_EXCEPTION(Error("Missing preference field in Link Encoding"));
+ BOOST_THROW_EXCEPTION(Error("Missing Preference field in Link Encoding"));
}
++val;
if (val == delegation.elements_end()) {
- BOOST_THROW_EXCEPTION(Error("Missing name field in Link Encoding"));
+ BOOST_THROW_EXCEPTION(Error("Missing Name field in Link Encoding"));
}
Name name(*val);
m_delegations.insert({preference, name});