Skip to content

Commit

Permalink
make keyexpr include/intersect checking functions generic
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Nov 19, 2024
1 parent 07795fa commit f219640
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
40 changes: 24 additions & 16 deletions zenoh/src/api/key_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,6 @@ impl KeyExpr<'static> {
s,
)))
}

pub(crate) fn string_intersects(left: &str, right: &str) -> bool {
if let (Ok(l), Ok(r)) = (KeyExpr::try_from(left), KeyExpr::try_from(right)) {
l.intersects(&r)
} else {
false
}
}

pub(crate) fn string_includes(left: &str, right: &str) -> bool {
if let (Ok(l), Ok(r)) = (KeyExpr::try_from(left), KeyExpr::try_from(right)) {
l.includes(&r)
} else {
false
}
}
}
impl<'a> KeyExpr<'a> {
/// Equivalent to `<KeyExpr as TryFrom>::try_from(t)`.
Expand Down Expand Up @@ -288,6 +272,30 @@ impl<'a> KeyExpr<'a> {
Ok(r.into())
}
}

pub(crate) fn keyexpr_intersect<'b, L, R>(left: L, right: R) -> bool
where
L: TryInto<KeyExpr<'a>>,
R: TryInto<KeyExpr<'b>>,
{
if let (Ok(l), Ok(r)) = (left.try_into(), right.try_into()) {
l.intersects(&r)
} else {
false
}
}

pub(crate) fn keyexpr_include<'b, L, R>(left: L, right: R) -> bool
where
L: TryInto<KeyExpr<'a>>,
R: TryInto<KeyExpr<'b>>,
{
if let (Ok(l), Ok(r)) = (left.try_into(), right.try_into()) {
l.includes(&r)
} else {
false
}
}
}

impl FromStr for KeyExpr<'static> {
Expand Down
4 changes: 2 additions & 2 deletions zenoh/src/net/routing/hat/client/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,12 @@ impl HatPubSubTrait for HatCode {
&& interest
.res
.as_ref()
.map(|res| KeyExpr::string_includes(&res.expr(), expr.full_expr()))
.map(|res| KeyExpr::keyexpr_include(res.expr(), expr.full_expr()))
.unwrap_or(true)
}) || face_hat!(face)
.remote_subs
.values()
.any(|sub| KeyExpr::string_intersects(&sub.expr(), expr.full_expr()))
.any(|sub| KeyExpr::keyexpr_intersect(sub.expr(), expr.full_expr()))
{
let key_expr = Resource::get_best_key(expr.prefix, expr.suffix, face.id);
route.insert(
Expand Down
4 changes: 2 additions & 2 deletions zenoh/src/net/routing/hat/client/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,12 @@ impl HatQueriesTrait for HatCode {
&& interest
.res
.as_ref()
.map(|res| KeyExpr::string_includes(&res.expr(), expr.full_expr()))
.map(|res| KeyExpr::keyexpr_include(res.expr(), expr.full_expr()))
.unwrap_or(true)
}) || face_hat!(face)
.remote_qabls
.values()
.any(|qbl| KeyExpr::string_intersects(&qbl.expr(), expr.full_expr()))
.any(|qbl| KeyExpr::keyexpr_intersect(qbl.expr(), expr.full_expr()))
{
let key_expr = Resource::get_best_key(expr.prefix, expr.suffix, face.id);
route.push(QueryTargetQabl {
Expand Down
4 changes: 2 additions & 2 deletions zenoh/src/net/routing/hat/p2p_peer/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,12 @@ impl HatPubSubTrait for HatCode {
&& interest
.res
.as_ref()
.map(|res| KeyExpr::string_includes(&res.expr(), expr.full_expr()))
.map(|res| KeyExpr::keyexpr_include(res.expr(), expr.full_expr()))
.unwrap_or(true)
}) || face_hat!(face)
.remote_subs
.values()
.any(|sub| KeyExpr::string_intersects(&sub.expr(), expr.full_expr()))
.any(|sub| KeyExpr::keyexpr_intersect(sub.expr(), expr.full_expr()))
{
let key_expr = Resource::get_best_key(expr.prefix, expr.suffix, face.id);
route.insert(
Expand Down
4 changes: 2 additions & 2 deletions zenoh/src/net/routing/hat/p2p_peer/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,12 @@ impl HatQueriesTrait for HatCode {
&& interest
.res
.as_ref()
.map(|res| KeyExpr::string_includes(&res.expr(), expr.full_expr()))
.map(|res| KeyExpr::keyexpr_include(res.expr(), expr.full_expr()))
.unwrap_or(true)
}) || face_hat!(face)
.remote_qabls
.values()
.any(|sub| KeyExpr::string_intersects(&sub.expr(), expr.full_expr()))
.any(|sub| KeyExpr::keyexpr_intersect(sub.expr(), expr.full_expr()))
{
let key_expr = Resource::get_best_key(expr.prefix, expr.suffix, face.id);
route.push(QueryTargetQabl {
Expand Down

0 comments on commit f219640

Please sign in to comment.