replace hash2prefix/prefix2hash with boost::bimap
refs: #5065 Change-Id: I32f690f0a85d5129c56ac2cfda382facbdaaaa49
This commit is contained in:
+14
-9
@@ -198,10 +198,14 @@ FullProducer::onSyncInterest(const ndn::Name& prefixName, const ndn::Interest& i
|
||||
|
||||
State state;
|
||||
for (const auto& hash : positive) {
|
||||
const ndn::Name& prefix = m_hash2prefix[hash];
|
||||
// Don't sync up sequence number zero
|
||||
if (m_prefixes[prefix] != 0 && !isFutureHash(prefix.toUri(), negative)) {
|
||||
state.addContent(ndn::Name(prefix).appendNumber(m_prefixes[prefix]));
|
||||
auto nameIt = m_biMap.left.find(hash);
|
||||
if (nameIt != m_biMap.left.end()) {
|
||||
ndn::Name nameWithoutSeq = nameIt->second.getPrefix(-1);
|
||||
// Don't sync up sequence number zero
|
||||
if (m_prefixes[nameWithoutSeq] != 0 &&
|
||||
!isFutureHash(nameWithoutSeq.toUri(), negative)) {
|
||||
state.addContent(nameIt->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,7 +280,7 @@ FullProducer::onSyncData(const ndn::Interest& interest, const ndn::ConstBufferPt
|
||||
NDN_LOG_DEBUG("Sync Data Received: " << state);
|
||||
|
||||
for (const auto& content : state.getContent()) {
|
||||
const ndn::Name& prefix = content.getPrefix(-1);
|
||||
ndn::Name prefix = content.getPrefix(-1);
|
||||
uint64_t seq = content.get(content.size() - 1).toNumber();
|
||||
|
||||
if (m_prefixes.find(prefix) == m_prefixes.end() || m_prefixes[prefix] < seq) {
|
||||
@@ -324,10 +328,11 @@ FullProducer::satisfyPendingInterests()
|
||||
|
||||
State state;
|
||||
for (const auto& hash : positive) {
|
||||
ndn::Name prefix = m_hash2prefix[hash];
|
||||
|
||||
if (m_prefixes[prefix] != 0) {
|
||||
state.addContent(ndn::Name(prefix).appendNumber(m_prefixes[prefix]));
|
||||
auto nameIt = m_biMap.left.find(hash);
|
||||
if (nameIt != m_biMap.left.end()) {
|
||||
if (m_prefixes[nameIt->second.getPrefix(-1)] != 0) {
|
||||
state.addContent(nameIt->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -173,11 +173,13 @@ PartialProducer::onSyncInterest(const ndn::Name& prefix, const ndn::Interest& in
|
||||
NDN_LOG_TRACE("Size of positive set " << positive.size());
|
||||
NDN_LOG_TRACE("Size of negative set " << negative.size());
|
||||
for (const auto& hash : positive) {
|
||||
ndn::Name prefix = m_hash2prefix[hash];
|
||||
if (bf.contains(prefix.toUri())) {
|
||||
// generate data
|
||||
state.addContent(ndn::Name(prefix).appendNumber(m_prefixes[prefix]));
|
||||
NDN_LOG_DEBUG("Content: " << prefix << " " << std::to_string(m_prefixes[prefix]));
|
||||
auto nameIt = m_biMap.left.find(hash);
|
||||
if (nameIt != m_biMap.left.end()) {
|
||||
if (bf.contains(nameIt->second.getPrefix(-1).toUri())) {
|
||||
// generate data
|
||||
state.addContent(nameIt->second);
|
||||
NDN_LOG_DEBUG("Content: " << nameIt->second << " " << nameIt->first);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
-16
@@ -74,12 +74,10 @@ ProducerBase::removeUserNode(const ndn::Name& prefix)
|
||||
m_prefixes.erase(it);
|
||||
|
||||
ndn::Name prefixWithSeq = ndn::Name(prefix).appendNumber(seqNo);
|
||||
auto hashIt = m_prefix2hash.find(prefixWithSeq);
|
||||
if (hashIt != m_prefix2hash.end()) {
|
||||
uint32_t hash = hashIt->second;
|
||||
m_prefix2hash.erase(hashIt);
|
||||
m_hash2prefix.erase(hash);
|
||||
m_iblt.erase(hash);
|
||||
auto hashIt = m_biMap.right.find(prefixWithSeq);
|
||||
if (hashIt != m_biMap.right.end()) {
|
||||
m_iblt.erase(hashIt->second);
|
||||
m_biMap.right.erase(hashIt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,21 +106,18 @@ ProducerBase::updateSeqNo(const ndn::Name& prefix, uint64_t seq)
|
||||
// Because we don't insert zeroth prefix in IBF so no need to delete that
|
||||
if (oldSeq != 0) {
|
||||
ndn::Name prefixWithSeq = ndn::Name(prefix).appendNumber(oldSeq);
|
||||
auto hashIt = m_prefix2hash.find(prefixWithSeq);
|
||||
if (hashIt != m_prefix2hash.end()) {
|
||||
uint32_t hash = hashIt->second;
|
||||
m_prefix2hash.erase(hashIt);
|
||||
m_hash2prefix.erase(hash);
|
||||
m_iblt.erase(hash);
|
||||
auto hashIt = m_biMap.right.find(prefixWithSeq);
|
||||
if (hashIt != m_biMap.right.end()) {
|
||||
m_iblt.erase(hashIt->second);
|
||||
m_biMap.right.erase(hashIt);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert the new seq no
|
||||
// Insert the new seq no in m_prefixes, m_biMap, and m_iblt
|
||||
it->second = seq;
|
||||
ndn::Name prefixWithSeq = ndn::Name(prefix).appendNumber(seq);
|
||||
uint32_t newHash = murmurHash3(N_HASHCHECK, prefixWithSeq.toUri());
|
||||
m_prefix2hash[prefixWithSeq] = newHash;
|
||||
m_hash2prefix[newHash] = prefix;
|
||||
m_biMap.insert({newHash, prefixWithSeq});
|
||||
m_iblt.insert(newHash);
|
||||
}
|
||||
|
||||
@@ -149,4 +144,4 @@ ProducerBase::onRegisterFailed(const ndn::Name& prefix, const std::string& msg)
|
||||
BOOST_THROW_EXCEPTION(Error(msg));
|
||||
}
|
||||
|
||||
} // namespace psync
|
||||
} // namespace psync
|
||||
@@ -33,11 +33,14 @@
|
||||
#include <ndn-cxx/security/key-chain.hpp>
|
||||
#include <ndn-cxx/security/validator-config.hpp>
|
||||
|
||||
#include <boost/bimap.hpp>
|
||||
#include <boost/bimap/unordered_set_of.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace psync {
|
||||
|
||||
namespace bm = boost::bimaps;
|
||||
using namespace ndn::time_literals;
|
||||
|
||||
const ndn::time::milliseconds SYNC_REPLY_FRESHNESS = 1_s;
|
||||
@@ -121,7 +124,7 @@ PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
|
||||
* Whoever calls this needs to make sure that prefix is in m_prefixes
|
||||
* We remove already existing prefix/seq from IBF
|
||||
* (unless seq is zero because we don't insert zero seq into IBF)
|
||||
* Then we update m_prefix, m_prefix2hash, m_hash2prefix, and IBF
|
||||
* Then we update m_prefixes, m_biMap, and IBF
|
||||
*
|
||||
* @param prefix prefix of the update
|
||||
* @param seq sequence number of the update
|
||||
@@ -155,6 +158,9 @@ PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
|
||||
void
|
||||
onRegisterFailed(const ndn::Name& prefix, const std::string& msg) const;
|
||||
|
||||
using HashNameBiMap = bm::bimap<bm::unordered_set_of<uint32_t>,
|
||||
bm::unordered_set_of<ndn::Name, std::hash<ndn::Name>>>;
|
||||
|
||||
PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
|
||||
IBLT m_iblt;
|
||||
uint32_t m_expectedNumEntries;
|
||||
@@ -164,11 +170,7 @@ PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
|
||||
|
||||
// prefix and sequence number
|
||||
std::map<ndn::Name, uint64_t> m_prefixes;
|
||||
// Just for looking up hash faster (instead of calculating it again)
|
||||
// Only used in updateSeqNo, prefix/seqNo is the key
|
||||
std::map<ndn::Name, uint32_t> m_prefix2hash;
|
||||
// Value is prefix (and not prefix/seqNo)
|
||||
std::map<uint32_t, ndn::Name> m_hash2prefix;
|
||||
HashNameBiMap m_biMap;
|
||||
|
||||
ndn::Face& m_face;
|
||||
ndn::KeyChain m_keyChain;
|
||||
|
||||
@@ -54,7 +54,7 @@ public:
|
||||
shared_ptr<FullProducer> nodes[4];
|
||||
};
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(FullSync, FullSyncFixture)
|
||||
BOOST_FIXTURE_TEST_SUITE(TestFullSync, FullSyncFixture)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TwoNodesSimple)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2014-2019, The University of Memphis
|
||||
* Copyright (c) 2014-2020, The University of Memphis
|
||||
*
|
||||
* This file is part of PSync.
|
||||
* See AUTHORS.md for complete list of PSync authors and contributors.
|
||||
@@ -147,7 +147,7 @@ public:
|
||||
int numSyncDataRcvd;
|
||||
};
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(PartialSync, PartialSyncFixture)
|
||||
BOOST_FIXTURE_TEST_SUITE(TestPartialSync, PartialSyncFixture)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Simple)
|
||||
{
|
||||
@@ -430,4 +430,4 @@ BOOST_AUTO_TEST_CASE(SegmentedSync)
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
} // namespace psync
|
||||
} // namespace psync
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2014-2019, The University of Memphis
|
||||
* Copyright (c) 2014-2020, The University of Memphis
|
||||
*
|
||||
* This file is part of PSync.
|
||||
* See AUTHORS.md for complete list of PSync authors and contributors.
|
||||
@@ -51,18 +51,19 @@ BOOST_AUTO_TEST_CASE(Basic)
|
||||
BOOST_CHECK(producerBase.getSeqNo(userNode.toUri()).value() == 1);
|
||||
|
||||
std::string prefixWithSeq = Name(userNode).appendNumber(1).toUri();
|
||||
uint32_t hash = producerBase.m_prefix2hash[prefixWithSeq];
|
||||
BOOST_CHECK_EQUAL(producerBase.m_hash2prefix[hash], userNode.toUri());
|
||||
uint32_t hash = producerBase.m_biMap.right.find(prefixWithSeq)->second;
|
||||
Name prefix(producerBase.m_biMap.left.find(hash)->second);
|
||||
BOOST_CHECK_EQUAL(prefix.getPrefix(-1), userNode);
|
||||
|
||||
producerBase.removeUserNode(userNode);
|
||||
BOOST_CHECK(producerBase.getSeqNo(userNode.toUri()) == ndn::nullopt);
|
||||
BOOST_CHECK(producerBase.m_prefix2hash.find(prefixWithSeq) == producerBase.m_prefix2hash.end());
|
||||
BOOST_CHECK(producerBase.m_hash2prefix.find(hash) == producerBase.m_hash2prefix.end());
|
||||
BOOST_CHECK(producerBase.m_biMap.right.find(prefixWithSeq) == producerBase.m_biMap.right.end());
|
||||
BOOST_CHECK(producerBase.m_biMap.left.find(hash) == producerBase.m_biMap.left.end());
|
||||
|
||||
Name nonExistentUserNode("/notAUser");
|
||||
producerBase.updateSeqNo(nonExistentUserNode, 1);
|
||||
BOOST_CHECK(producerBase.m_prefix2hash.find(Name(nonExistentUserNode).appendNumber(1).toUri()) ==
|
||||
producerBase.m_prefix2hash.end());
|
||||
BOOST_CHECK(producerBase.m_biMap.right.find(Name(nonExistentUserNode).appendNumber(1).toUri()) ==
|
||||
producerBase.m_biMap.right.end());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ApplicationNack)
|
||||
|
||||
Reference in New Issue
Block a user