refactor(hyperswitch_constraint_graph): Removal of lifetime from the Constraint Graph framework (#5132)

This commit is contained in:
Prajjwal Kumar
2024-06-28 15:06:47 +05:30
committed by GitHub
parent bb9a97154c
commit 6df8f0646b
10 changed files with 104 additions and 86 deletions

View File

@ -12,28 +12,34 @@ use crate::{
},
};
pub enum DomainIdOrIdentifier<'a> {
pub enum DomainIdOrIdentifier {
DomainId(DomainId),
DomainIdentifier(DomainIdentifier<'a>),
DomainIdentifier(DomainIdentifier),
}
impl<'a> From<&'a str> for DomainIdOrIdentifier<'a> {
fn from(value: &'a str) -> Self {
impl From<String> for DomainIdOrIdentifier {
fn from(value: String) -> Self {
Self::DomainIdentifier(DomainIdentifier::new(value))
}
}
impl From<DomainId> for DomainIdOrIdentifier<'_> {
impl From<DomainIdentifier> for DomainIdOrIdentifier {
fn from(value: DomainIdentifier) -> Self {
Self::DomainIdentifier(value)
}
}
impl From<DomainId> for DomainIdOrIdentifier {
fn from(value: DomainId) -> Self {
Self::DomainId(value)
}
}
#[derive(Debug)]
pub struct ConstraintGraphBuilder<'a, V: ValueNode> {
domain: DenseMap<DomainId, DomainInfo<'a>>,
pub struct ConstraintGraphBuilder<V: ValueNode> {
domain: DenseMap<DomainId, DomainInfo>,
nodes: DenseMap<NodeId, Node<V>>,
edges: DenseMap<EdgeId, Edge>,
domain_identifier_map: FxHashMap<DomainIdentifier<'a>, DomainId>,
domain_identifier_map: FxHashMap<DomainIdentifier, DomainId>,
value_map: FxHashMap<NodeValue<V>, NodeId>,
edges_map: FxHashMap<(NodeId, NodeId, Option<DomainId>), EdgeId>,
node_info: DenseMap<NodeId, Option<&'static str>>,
@ -41,7 +47,7 @@ pub struct ConstraintGraphBuilder<'a, V: ValueNode> {
}
#[allow(clippy::new_without_default)]
impl<'a, V> ConstraintGraphBuilder<'a, V>
impl<V> ConstraintGraphBuilder<V>
where
V: ValueNode,
{
@ -58,7 +64,7 @@ where
}
}
pub fn build(self) -> ConstraintGraph<'a, V> {
pub fn build(self) -> ConstraintGraph<V> {
ConstraintGraph {
domain: self.domain,
domain_identifier_map: self.domain_identifier_map,
@ -72,7 +78,7 @@ where
fn retrieve_domain_from_identifier(
&self,
domain_ident: DomainIdentifier<'_>,
domain_ident: DomainIdentifier,
) -> Result<DomainId, GraphError<V>> {
self.domain_identifier_map
.get(&domain_ident)
@ -82,7 +88,7 @@ where
pub fn make_domain(
&mut self,
domain_identifier: &'a str,
domain_identifier: String,
domain_description: &str,
) -> Result<DomainId, GraphError<V>> {
let domain_identifier = DomainIdentifier::new(domain_identifier);
@ -93,7 +99,7 @@ where
.map_or_else(
|| {
let domain_id = self.domain.push(DomainInfo {
domain_identifier,
domain_identifier: domain_identifier.clone(),
domain_description: domain_description.to_string(),
});
self.domain_identifier_map
@ -123,7 +129,7 @@ where
})
}
pub fn make_edge<'short, T: Into<DomainIdOrIdentifier<'short>>>(
pub fn make_edge<T: Into<DomainIdOrIdentifier>>(
&mut self,
pred_id: NodeId,
succ_id: NodeId,
@ -188,7 +194,7 @@ where
nodes: &[(NodeId, Relation, Strength)],
info: Option<&'static str>,
metadata: Option<M>,
domain: Option<&str>,
domain: Option<String>,
) -> Result<NodeId, GraphError<V>> {
nodes
.iter()
@ -202,7 +208,13 @@ where
.push(metadata.map(|meta| -> Arc<dyn Metadata> { Arc::new(meta) }));
for (node_id, relation, strength) in nodes {
self.make_edge(*node_id, aggregator_id, *strength, *relation, domain)?;
self.make_edge(
*node_id,
aggregator_id,
*strength,
*relation,
domain.clone(),
)?;
}
Ok(aggregator_id)
@ -213,7 +225,7 @@ where
nodes: &[(NodeId, Relation, Strength)],
info: Option<&'static str>,
metadata: Option<M>,
domain: Option<&str>,
domain: Option<String>,
) -> Result<NodeId, GraphError<V>> {
nodes
.iter()
@ -227,7 +239,13 @@ where
.push(metadata.map(|meta| -> Arc<dyn Metadata> { Arc::new(meta) }));
for (node_id, relation, strength) in nodes {
self.make_edge(*node_id, aggregator_id, *strength, *relation, domain)?;
self.make_edge(
*node_id,
aggregator_id,
*strength,
*relation,
domain.clone(),
)?;
}
Ok(aggregator_id)

View File

@ -26,9 +26,9 @@ struct CheckNodeContext<'a, V: ValueNode, C: CheckingContext<Value = V>> {
}
#[derive(Debug)]
pub struct ConstraintGraph<'a, V: ValueNode> {
pub domain: DenseMap<DomainId, DomainInfo<'a>>,
pub domain_identifier_map: FxHashMap<DomainIdentifier<'a>, DomainId>,
pub struct ConstraintGraph<V: ValueNode> {
pub domain: DenseMap<DomainId, DomainInfo>,
pub domain_identifier_map: FxHashMap<DomainIdentifier, DomainId>,
pub nodes: DenseMap<NodeId, Node<V>>,
pub edges: DenseMap<EdgeId, Edge>,
pub value_map: FxHashMap<NodeValue<V>, NodeId>,
@ -36,7 +36,7 @@ pub struct ConstraintGraph<'a, V: ValueNode> {
pub node_metadata: DenseMap<NodeId, Option<Arc<dyn Metadata>>>,
}
impl<'a, V> ConstraintGraph<'a, V>
impl<V> ConstraintGraph<V>
where
V: ValueNode,
{
@ -70,7 +70,7 @@ where
strength: Strength,
memo: &mut Memoization<V>,
cycle_map: &mut CycleCheck,
domains: Option<&[&str]>,
domains: Option<&[String]>,
) -> Result<(), GraphError<V>>
where
C: CheckingContext<Value = V>,
@ -81,7 +81,7 @@ where
.iter()
.map(|domain_ident| {
self.domain_identifier_map
.get(&DomainIdentifier::new(domain_ident))
.get(&DomainIdentifier::new(domain_ident.to_string()))
.copied()
.ok_or(GraphError::DomainNotFound)
})
@ -482,15 +482,15 @@ where
Ok(())
}
pub fn combine<'b>(g1: &'b Self, g2: &'b Self) -> Result<Self, GraphError<V>> {
pub fn combine(g1: &Self, g2: &Self) -> Result<Self, GraphError<V>> {
let mut node_builder = builder::ConstraintGraphBuilder::new();
let mut g1_old2new_id = DenseMap::<NodeId, NodeId>::new();
let mut g2_old2new_id = DenseMap::<NodeId, NodeId>::new();
let mut g1_old2new_domain_id = DenseMap::<DomainId, DomainId>::new();
let mut g2_old2new_domain_id = DenseMap::<DomainId, DomainId>::new();
let add_domain = |node_builder: &mut builder::ConstraintGraphBuilder<'a, V>,
domain: DomainInfo<'a>|
let add_domain = |node_builder: &mut builder::ConstraintGraphBuilder<V>,
domain: DomainInfo|
-> Result<DomainId, GraphError<V>> {
node_builder.make_domain(
domain.domain_identifier.into_inner(),
@ -498,7 +498,7 @@ where
)
};
let add_node = |node_builder: &mut builder::ConstraintGraphBuilder<'a, V>,
let add_node = |node_builder: &mut builder::ConstraintGraphBuilder<V>,
node: &Node<V>|
-> Result<NodeId, GraphError<V>> {
match &node.node_type {
@ -553,14 +553,14 @@ where
.domain
.map(|domain_id| g1.domain.get(domain_id).ok_or(GraphError::DomainNotFound))
.transpose()?
.map(|domain| domain.domain_identifier);
.map(|domain| domain.domain_identifier.clone());
node_builder.make_edge(
*new_pred_id,
*new_succ_id,
edge.strength,
edge.relation,
domain_ident.as_deref(),
domain_ident,
)?;
}
@ -575,14 +575,14 @@ where
.domain
.map(|domain_id| g2.domain.get(domain_id).ok_or(GraphError::DomainNotFound))
.transpose()?
.map(|domain| domain.domain_identifier);
.map(|domain| domain.domain_identifier.clone());
node_builder.make_edge(
*new_pred_id,
*new_succ_id,
edge.strength,
edge.relation,
domain_ident.as_deref(),
domain_ident,
)?;
}
@ -604,7 +604,7 @@ mod viz {
format!("N{}", node_id.get_id())
}
impl<'a, V> ConstraintGraph<'a, V>
impl<V> ConstraintGraph<V>
where
V: ValueNode + NodeViz,
<V as ValueNode>::Key: NodeViz,

View File

@ -139,36 +139,36 @@ pub struct DomainId(usize);
impl_entity!(DomainId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DomainIdentifier<'a>(&'a str);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DomainIdentifier(String);
impl<'a> DomainIdentifier<'a> {
pub fn new(identifier: &'a str) -> Self {
impl DomainIdentifier {
pub fn new(identifier: String) -> Self {
Self(identifier)
}
pub fn into_inner(&self) -> &'a str {
self.0
pub fn into_inner(&self) -> String {
self.0.clone()
}
}
impl<'a> From<&'a str> for DomainIdentifier<'a> {
fn from(value: &'a str) -> Self {
impl From<String> for DomainIdentifier {
fn from(value: String) -> Self {
Self(value)
}
}
impl<'a> Deref for DomainIdentifier<'a> {
type Target = str;
fn deref(&self) -> &'a Self::Target {
self.0
}
}
// impl Deref for DomainIdentifier {
// type Target = &String;
//
// fn deref(&self) -> Self::Target {
// self.0
// }
// }
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DomainInfo<'a> {
pub domain_identifier: DomainIdentifier<'a>,
pub struct DomainInfo {
pub domain_identifier: DomainIdentifier,
pub domain_description: String,
}