Skip to content

Commit

Permalink
route: simplify Map with boost::bimap
Browse files Browse the repository at this point in the history
refs #5308

Change-Id: I920cc1fc484b3fbee9a8f1f73c81db0cbce40b45
  • Loading branch information
yoursunny committed Feb 10, 2024
1 parent 543f2b6 commit d6922b5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 93 deletions.
46 changes: 20 additions & 26 deletions src/route/map.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
Expand All @@ -23,51 +23,45 @@
#include "adjacent.hpp"
#include "lsa/lsa.hpp"
#include "lsdb.hpp"
#include "logger.hpp"

namespace nlsr {

INIT_LOGGER(route.Map);

void
Map::addEntry(const ndn::Name& rtrName)
{
MapEntry me {rtrName, m_mappingIndex};
if (addEntry(me)) {
m_mappingIndex++;
}
}

bool
Map::addEntry(MapEntry& mpe)
{
return m_entries.insert(mpe).second;
int32_t mappingNo = static_cast<int32_t>(m_bimap.size());
m_bimap.by<ndn::Name>().insert({rtrName, mappingNo});
}

std::optional<ndn::Name>
Map::getRouterNameByMappingNo(int32_t mn) const
{
auto&& mappingNumberView = m_entries.get<detail::byMappingNumber>();
auto it = mappingNumberView.find(mn);
return it == mappingNumberView.end() ? std::nullopt : std::optional(it->router);
auto it = m_bimap.by<MappingNo>().find(mn);
if (it == m_bimap.by<MappingNo>().end()) {
return std::nullopt;
}
return it->get<ndn::Name>();
}

std::optional<int32_t>
Map::getMappingNoByRouterName(const ndn::Name& rName)
{
auto&& routerNameView = m_entries.get<detail::byRouterName>();
auto it = routerNameView.find(rName);
return it == routerNameView.end() ? std::nullopt : std::optional(it->mappingNumber);
auto it = m_bimap.by<ndn::Name>().find(rName);
if (it == m_bimap.by<ndn::Name>().end()) {
return std::nullopt;
}
return it->get<Map::MappingNo>();
}

void
Map::writeLog()
std::ostream&
operator<<(std::ostream& os, const Map& map)
{
NLSR_LOG_DEBUG("---------------Map----------------------");
for (const auto& entry : m_entries.get<detail::byRouterName>()) {
NLSR_LOG_DEBUG("MapEntry: ( Router: " << entry.router << " Mapping No: " <<
entry.mappingNumber << " )");
os << "---------------Map----------------------";
for (const auto& entry : map.m_bimap) {
os << "\nMapEntry: ( Router: " << entry.get<ndn::Name>()
<< " Mapping No: " << entry.get<Map::MappingNo>() << " )";
}
return os;
}

} // namespace nlsr
66 changes: 21 additions & 45 deletions src/route/map.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
Expand All @@ -24,48 +24,16 @@
#include "common.hpp"
#include "lsa/adj-lsa.hpp"

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/tag.hpp>
#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>

#include <optional>

namespace nlsr {

struct MapEntry
{
ndn::Name router;
int32_t mappingNumber = -1;
};

namespace detail {

using namespace boost::multi_index;
// Define tags so that we can search by different indices.
struct byRouterName {};
struct byMappingNumber{};
using entryContainer = multi_index_container<
MapEntry,
indexed_by<
hashed_unique<tag<byRouterName>,
member<MapEntry, ndn::Name, &MapEntry::router>,
std::hash<ndn::Name>>,
hashed_unique<tag<byMappingNumber>,
member<MapEntry, int32_t, &MapEntry::mappingNumber>>
>
>;

} // namespace detail

class Map
{
public:
Map()
: m_mappingIndex(0)
{
}

/*! \brief Add a map entry to this map.
\param rtrName The name of the router.
Expand Down Expand Up @@ -114,22 +82,30 @@ class Map
getMappingNoByRouterName(const ndn::Name& rName);

size_t
getMapSize() const
size() const
{
return m_entries.size();
return m_bimap.size();
}

void
writeLog();

private:
bool
addEntry(MapEntry& mpe);

int32_t m_mappingIndex;
detail::entryContainer m_entries;
struct MappingNo;
boost::bimap<
boost::bimaps::unordered_set_of<
boost::bimaps::tagged<ndn::Name, ndn::Name>,
std::hash<ndn::Name>
>,
boost::bimaps::unordered_set_of<
boost::bimaps::tagged<int32_t, MappingNo>
>
> m_bimap;

friend std::ostream&
operator<<(std::ostream& os, const Map& map);
};

std::ostream&
operator<<(std::ostream& os, const Map& map);

} // namespace nlsr

#endif // NLSR_MAP_HPP
10 changes: 5 additions & 5 deletions src/route/routing-table.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
Expand Down Expand Up @@ -130,9 +130,9 @@ RoutingTable::calculateLsRoutingTable()
Map map;
auto lsaRange = m_lsdb.getLsdbIterator<AdjLsa>();
map.createFromAdjLsdb(lsaRange.first, lsaRange.second);
map.writeLog();
NLSR_LOG_DEBUG(map);

size_t nRouters = map.getMapSize();
size_t nRouters = map.size();

LinkStateRoutingTableCalculator calculator(nRouters);

Expand All @@ -156,9 +156,9 @@ RoutingTable::calculateHypRoutingTable(bool isDryRun)
Map map;
auto lsaRange = m_lsdb.getLsdbIterator<CoordinateLsa>();
map.createFromCoordinateLsdb(lsaRange.first, lsaRange.second);
map.writeLog();
NLSR_LOG_DEBUG(map);

size_t nRouters = map.getMapSize();
size_t nRouters = map.size();

HyperbolicRoutingCalculator calculator(nRouters, isDryRun, m_confParam.getRouterPrefix());

Expand Down
2 changes: 1 addition & 1 deletion tests/route/test-hyperbolic-calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class HyperbolicCalculatorFixture : public IoKeyChainFixture

void runTest(const double& expectedCost)
{
HyperbolicRoutingCalculator calculator(map.getMapSize(), false, ROUTER_A_NAME);
HyperbolicRoutingCalculator calculator(map.size(), false, ROUTER_A_NAME);
calculator.calculatePath(map, routingTable, lsdb, adjacencies);

RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
Expand Down
8 changes: 4 additions & 4 deletions tests/route/test-link-state-calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ BOOST_FIXTURE_TEST_SUITE(TestLinkStateRoutingCalculator, LinkStateCalculatorFixt

BOOST_AUTO_TEST_CASE(Basic)
{
LinkStateRoutingTableCalculator calculator(map.getMapSize());
LinkStateRoutingTableCalculator calculator(map.size());
calculator.calculatePath(map, routingTable, conf, lsdb);

RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
Expand Down Expand Up @@ -166,7 +166,7 @@ BOOST_AUTO_TEST_CASE(Asymmetric)
c->setLinkCost(higherLinkCost);

// Calculation should consider the link between B and C as having cost = higherLinkCost
LinkStateRoutingTableCalculator calculator(map.getMapSize());
LinkStateRoutingTableCalculator calculator(map.size());
calculator.calculatePath(map, routingTable, conf, lsdb);

RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
Expand Down Expand Up @@ -214,7 +214,7 @@ BOOST_AUTO_TEST_CASE(NonAdjacentCost)
c->setLinkCost(Adjacent::NON_ADJACENT_COST);

// Calculation should consider the link between B and C as down
LinkStateRoutingTableCalculator calculator(map.getMapSize());
LinkStateRoutingTableCalculator calculator(map.size());
calculator.calculatePath(map, routingTable, conf, lsdb);

// Router A should be able to get to B through B but not through C
Expand Down Expand Up @@ -268,7 +268,7 @@ BOOST_AUTO_TEST_CASE(AsymmetricZeroCostLink)
b->setLinkCost(0);

// Calculation should consider 0 link-cost between B and C
LinkStateRoutingTableCalculator calculator(map.getMapSize());
LinkStateRoutingTableCalculator calculator(map.size());
calculator.calculatePath(map, routingTable, conf, lsdb);

// Router A should be able to get to B through B and C
Expand Down
37 changes: 25 additions & 12 deletions tests/route/test-map.cpp
Original file line number Diff line number Diff line change
@@ -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-2024, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
Expand All @@ -23,25 +23,38 @@
#include "route/map.hpp"
#include "tests/boost-test.hpp"

namespace nlsr {
namespace test {
namespace nlsr::test {

BOOST_AUTO_TEST_SUITE(TestMap)

BOOST_AUTO_TEST_CASE(MapAddElementAndSize)
BOOST_AUTO_TEST_CASE(Basic)
{
Map map1;

std::string router1 = "r1";
std::string router2 = "r2";
ndn::Name name1("/r1");
ndn::Name name2("/r2");
ndn::Name name3("/r3");

map1.addEntry(router1);
map1.addEntry(router2);
map1.addEntry(name1);
map1.addEntry(name2);
BOOST_CHECK_EQUAL(map1.size(), 2);

BOOST_CHECK_EQUAL(map1.getMapSize(), 2);
std::optional<int32_t> mn1 = map1.getMappingNoByRouterName(name1);
std::optional<int32_t> mn2 = map1.getMappingNoByRouterName(name2);
BOOST_REQUIRE(mn1.has_value());
BOOST_REQUIRE(mn2.has_value());
BOOST_CHECK_NE(*mn1, *mn2);
BOOST_CHECK_EQUAL(map1.getMappingNoByRouterName(name3).has_value(), false);

BOOST_CHECK_EQUAL(map1.getRouterNameByMappingNo(*mn1).value_or(ndn::Name()), name1);
BOOST_CHECK_EQUAL(map1.getRouterNameByMappingNo(*mn2).value_or(ndn::Name()), name2);

int32_t mn3 = 3333;
BOOST_CHECK_NE(mn3, *mn1);
BOOST_CHECK_NE(mn3, *mn2);
BOOST_CHECK_EQUAL(map1.getRouterNameByMappingNo(mn3).has_value(), false);
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace test
} // namespace nlsr
} // namespace nlsr::test

0 comments on commit d6922b5

Please sign in to comment.