Skip to content

Commit

Permalink
Network links mappings use VecMap
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierHecart committed Mar 3, 2021
1 parent 0e859bc commit 11477fa
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zenoh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ shared_memory = "0.11.4"
socket2 = "0.3.19"
uhlc = "0.2.1"
uuid = { version = "0.8.2", features = ["v4"] }
vec_map = "0.8.2"

[dev-dependencies]
clap = "2.33"
Expand Down
16 changes: 8 additions & 8 deletions zenoh/src/net/routing/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Face {
.unwrap()
.get_link(&self.state.pid)
{
Some(link) => match link.mappings.get(&routing_context) {
Some(link) => match link.get_pid(&routing_context) {
Some(router) => router.clone(),
None => {
log::error!(
Expand Down Expand Up @@ -153,7 +153,7 @@ impl Face {
| (whatami::PEER, whatami::PEER) => match routing_context {
Some(routing_context) => {
let peer = match tables.peers_net.as_ref().unwrap().get_link(&self.state.pid) {
Some(link) => match link.mappings.get(&routing_context) {
Some(link) => match link.get_pid(&routing_context) {
Some(peer) => peer.clone(),
None => {
log::error!(
Expand Down Expand Up @@ -214,7 +214,7 @@ impl Face {
.unwrap()
.get_link(&self.state.pid)
{
Some(link) => match link.mappings.get(&routing_context) {
Some(link) => match link.get_pid(&routing_context) {
Some(router) => router.clone(),
None => {
log::error!(
Expand Down Expand Up @@ -250,7 +250,7 @@ impl Face {
| (whatami::PEER, whatami::PEER) => match routing_context {
Some(routing_context) => {
let peer = match tables.peers_net.as_ref().unwrap().get_link(&self.state.pid) {
Some(link) => match link.mappings.get(&routing_context) {
Some(link) => match link.get_pid(&routing_context) {
Some(peer) => peer.clone(),
None => {
log::error!(
Expand Down Expand Up @@ -310,7 +310,7 @@ impl Face {
.unwrap()
.get_link(&self.state.pid)
{
Some(link) => match link.mappings.get(&routing_context) {
Some(link) => match link.get_pid(&routing_context) {
Some(router) => router.clone(),
None => {
log::error!(
Expand Down Expand Up @@ -346,7 +346,7 @@ impl Face {
| (whatami::PEER, whatami::PEER) => match routing_context {
Some(routing_context) => {
let peer = match tables.peers_net.as_ref().unwrap().get_link(&self.state.pid) {
Some(link) => match link.mappings.get(&routing_context) {
Some(link) => match link.get_pid(&routing_context) {
Some(peer) => peer.clone(),
None => {
log::error!(
Expand Down Expand Up @@ -396,7 +396,7 @@ impl Face {
.unwrap()
.get_link(&self.state.pid)
{
Some(link) => match link.mappings.get(&routing_context) {
Some(link) => match link.get_pid(&routing_context) {
Some(router) => router.clone(),
None => {
log::error!(
Expand Down Expand Up @@ -432,7 +432,7 @@ impl Face {
| (whatami::PEER, whatami::PEER) => match routing_context {
Some(routing_context) => {
let peer = match tables.peers_net.as_ref().unwrap().get_link(&self.state.pid) {
Some(link) => match link.mappings.get(&routing_context) {
Some(link) => match link.get_pid(&routing_context) {
Some(peer) => peer.clone(),
None => {
log::error!(
Expand Down
22 changes: 16 additions & 6 deletions zenoh/src/net/routing/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
//
use petgraph::graph::NodeIndex;
use petgraph::visit::{VisitMap, Visitable};
use std::collections::HashMap;
use std::convert::TryInto;
use vec_map::VecMap;

use super::protocol::core::{whatami, PeerId, ZInt};
use super::protocol::link::Locator;
Expand All @@ -39,16 +39,26 @@ impl std::fmt::Debug for Node {

pub(crate) struct Link {
pub(crate) session: Session,
pub(crate) mappings: HashMap<ZInt, PeerId>,
pub(crate) mappings: VecMap<PeerId>,
}

impl Link {
fn new(session: Session) -> Self {
Link {
session,
mappings: HashMap::new(),
mappings: VecMap::new(),
}
}

#[inline]
pub(crate) fn set_pid_mapping(&mut self, psid: ZInt, pid: PeerId) {
self.mappings.insert(psid.try_into().unwrap(), pid);
}

#[inline]
pub(crate) fn get_pid(&self, psid: &ZInt) -> Option<&PeerId> {
self.mappings.get((*psid).try_into().unwrap())
}
}

#[derive(Clone)]
Expand Down Expand Up @@ -238,7 +248,7 @@ impl Network {
.into_iter()
.filter_map(|link_state| {
if let Some(pid) = link_state.pid {
src_link.mappings.insert(link_state.psid, pid.clone());
src_link.set_pid_mapping(link_state.psid, pid.clone());
Some((
pid,
link_state.whatami.or(Some(whatami::ROUTER)).unwrap(),
Expand All @@ -247,7 +257,7 @@ impl Network {
link_state.links,
))
} else {
match src_link.mappings.get(&link_state.psid) {
match src_link.get_pid(&link_state.psid) {
Some(pid) => Some((
pid.clone(),
link_state.whatami.or(Some(whatami::ROUTER)).unwrap(),
Expand Down Expand Up @@ -276,7 +286,7 @@ impl Network {
let links: Vec<PeerId> = links
.iter()
.filter_map(|l| {
if let Some(pid) = src_link.mappings.get(&l) {
if let Some(pid) = src_link.get_pid(&l) {
Some(pid.clone())
} else {
log::error!(
Expand Down
9 changes: 3 additions & 6 deletions zenoh/src/net/routing/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,7 @@ pub async fn route_data(
&routers_net
.get_link(&face.pid)
.unwrap()
.mappings
.get(&routing_context.unwrap())
.get_pid(&routing_context.unwrap())
.unwrap(),
)
.unwrap()
Expand All @@ -983,8 +982,7 @@ pub async fn route_data(
&peers_net
.get_link(&face.pid)
.unwrap()
.mappings
.get(&routing_context.unwrap())
.get_pid(&routing_context.unwrap())
.unwrap(),
)
.unwrap()
Expand Down Expand Up @@ -1013,8 +1011,7 @@ pub async fn route_data(
&peers_net
.get_link(&face.pid)
.unwrap()
.mappings
.get(&routing_context.unwrap())
.get_pid(&routing_context.unwrap())
.unwrap(),
)
.unwrap()
Expand Down
9 changes: 3 additions & 6 deletions zenoh/src/net/routing/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,7 @@ pub async fn route_query(
&routers_net
.get_link(&face.pid)
.unwrap()
.mappings
.get(&routing_context.unwrap())
.get_pid(&routing_context.unwrap())
.unwrap(),
)
.unwrap()
Expand All @@ -887,8 +886,7 @@ pub async fn route_query(
&peers_net
.get_link(&face.pid)
.unwrap()
.mappings
.get(&routing_context.unwrap())
.get_pid(&routing_context.unwrap())
.unwrap(),
)
.unwrap()
Expand Down Expand Up @@ -917,8 +915,7 @@ pub async fn route_query(
&peers_net
.get_link(&face.pid)
.unwrap()
.mappings
.get(&routing_context.unwrap())
.get_pid(&routing_context.unwrap())
.unwrap(),
)
.unwrap()
Expand Down

0 comments on commit 11477fa

Please sign in to comment.