pub fn load_rules(rules: Vec<Arc<Rule>>) -> bool {
    let mut rule_map: RuleMap = HashMap::new();
    // todo: validate rules here,
    // neglect invalid rules,
    // instead of dealing with them in
    // `on_rule_update`
    for rule in rules {
        let entry = rule_map
            .entry(rule.resource.clone())
            .or_insert(HashSet::new());
        entry.insert(rule);
    }

    let mut global_rule_map = RULE_MAP.lock().unwrap();
    if &*global_rule_map == &rule_map {
        logging::info!(
            "[Flow] Load rules is the same with current rules, so ignore load operation."
        );
        return false;
    }
    // when rule_map is different with global one, update the global one
    // ignore invalid rules
    let mut valid_rules_map = HashMap::with_capacity(rule_map.len());
    for (res, rules) in &rule_map {
        let mut valid_rules = HashSet::new();
        for rule in rules {
            match rule.is_valid() {
                Ok(_) => {
                    valid_rules.insert(Arc::clone(&rule));
                }
                Err(err) => logging::warn!(
                    "[Flow load_rules] Ignoring invalid flow rule {:?}, reason: {:?}",
                    rule,
                    err
                ),
            }
        }
        if valid_rules.len() > 0 {
            valid_rules_map.insert(res.clone(), valid_rules);
        }
    }

    let start = utils::curr_time_nanos();
    let mut controller_map = CONTROLLER_MAP.lock().unwrap();
    let mut valid_controller_map = HashMap::with_capacity(valid_rules_map.len());

    // build controller_map according to valid rules
    for (res, rules) in valid_rules_map.iter() {
        let mut placeholder = Vec::new();
        let new_tcs_of_res = build_resource_traffic_shaping_controller(
            res,
            rules,
            controller_map.get_mut(res).unwrap_or(&mut placeholder),
        );
        if new_tcs_of_res.len() > 0 {
            valid_controller_map.insert(res.clone(), new_tcs_of_res);
        }
    }
    *controller_map = valid_controller_map;
    *global_rule_map = rule_map;
    drop(global_rule_map);
    drop(controller_map);
    logging::debug!(
        "[Flow load_rules] Time statistic(ns) for updating flow rule, time cost {}",
        utils::curr_time_nanos() - start
    );
    log_rule_update(&valid_rules_map);
    return true;
}
/// `load_rules` loads the given flow rules to the rule manager, while all previous rules will be replaced.
/// The returned `bool` indicates whether do real load operation, if the rules is the same with previous rules, return false
// This func acquires locks on global `RULE_MAP` and `CONTROLLER_MAP`,
// please release your locks on them before calling this func
`load_rules` loads the given flow rules to the rule manager, while all previous rules will be replaced.
` load_rules ` loads the given flow rules to the rule manager while all previous rules will be replaced .
pub fn load_rules(rules: Vec<Arc<Rule>>) -> bool {
    let mut rule_map: RuleMap = HashMap::new();
    // todo: validate rules here,
    // neglect invalid rules,
    // instead of dealing with them in
    // `on_rule_update`
    for rule in rules {
        let entry = rule_map
            .entry(rule.resource.clone())
            .or_insert(HashSet::new());
        entry.insert(rule);
    }
    let mut global_rule_map = RULE_MAP.lock().unwrap();
    if &*global_rule_map == &rule_map {
        logging::info!(
            "[Flow] Load rules is the same with current rules, so ignore load operation."
        );
        return false;
    }
    // when rule_map is different with global one, update the global one
    // ignore invalid rules
    let mut valid_rules_map = HashMap::with_capacity(rule_map.len());
    for (res, rules) in &rule_map {
        let mut valid_rules = HashSet::new();
        for rule in rules {
            match rule.is_valid() {
                Ok(_) => {
                    valid_rules.insert(Arc::clone(&rule));
                }
                Err(err) => logging::warn!(
                    "[Flow load_rules] Ignoring invalid flow rule {:?}, reason: {:?}",
                    rule,
                    err
                ),
            }
        }
        if valid_rules.len() > 0 {
            valid_rules_map.insert(res.clone(), valid_rules);
        }
    }
    let start = utils::curr_time_nanos();
    let mut controller_map = CONTROLLER_MAP.lock().unwrap();
    let mut valid_controller_map = HashMap::with_capacity(valid_rules_map.len());
    // build controller_map according to valid rules
    for (res, rules) in valid_rules_map.iter() {
        let mut placeholder = Vec::new();
        let new_tcs_of_res = build_resource_traffic_shaping_controller(
            res,
            rules,
            controller_map.get_mut(res).unwrap_or(&mut placeholder),
        );
        if new_tcs_of_res.len() > 0 {
            valid_controller_map.insert(res.clone(), new_tcs_of_res);
        }
    }
    *controller_map = valid_controller_map;
    *global_rule_map = rule_map;
    drop(global_rule_map);
    drop(controller_map);
    logging::debug!(
        "[Flow load_rules] Time statistic(ns) for updating flow rule, time cost {}",
        utils::curr_time_nanos() - start
    );
    log_rule_update(&valid_rules_map);
    return true;
}
pub fn load_rules ( rules : Vec < Arc < Rule > > ) -> bool { let mut rule_map : RuleMap = HashMap :: new ( ) ; // todo: validate rules here, // neglect invalid rules, // instead of dealing with them in // `on_rule_update` for rule in rules { let entry = rule_map . entry ( rule . resource . clone ( ) ) . or_insert ( HashSet :: new ( ) ) ; entry . insert ( rule ) ; } let mut global_rule_map = RULE_MAP . lock ( ) . unwrap ( ) ; if & * global_rule_map == & rule_map { logging :: info ! ( " " ) ; return false ; } // when rule_map is different with global one, update the global one // ignore invalid rules let mut valid_rules_map = HashMap :: with_capacity ( rule_map . len ( ) ) ; for ( res , rules ) in & rule_map { let mut valid_rules = HashSet :: new ( ) ; for rule in rules { match rule . is_valid ( ) { Ok ( _ ) => { valid_rules . insert ( Arc :: clone ( & rule ) ) ; } Err ( err ) => logging :: warn ! ( " " rule err ) , } } if valid_rules . len ( ) > 0 { valid_rules_map . insert ( res . clone ( ) , valid_rules ) ; } } let start = utils :: curr_time_nanos ( ) ; let mut controller_map = CONTROLLER_MAP . lock ( ) . unwrap ( ) ; let mut valid_controller_map = HashMap :: with_capacity ( valid_rules_map . len ( ) ) ; // build controller_map according to valid rules for ( res , rules ) in valid_rules_map . iter ( ) { let mut placeholder = Vec :: new ( ) ; let new_tcs_of_res = build_resource_traffic_shaping_controller ( res , rules , controller_map . get_mut ( res ) . unwrap_or ( & mut placeholder ) , ) ; if new_tcs_of_res . len ( ) > 0 { valid_controller_map . insert ( res . clone ( ) , new_tcs_of_res ) ; } } * controller_map = valid_controller_map ; * global_rule_map = rule_map ; drop ( global_rule_map ) ; drop ( controller_map ) ; logging :: debug ! ( " " utils curr_time_nanos ( ) start ) ; log_rule_update ( & valid_rules_map ) ; return true ; }
`load_rules` loads the given flow rules to the rule manager, while all previous rules will be replaced.
` load_rules ` loads the given flow rules to the rule manager while all previous rules will be replaced .
rules
Vec<Arc<Rule>>
rules
Vec<Arc<Rule>>