Skip to content

Commit

Permalink
Move build role links to internal (#140)
Browse files Browse the repository at this point in the history
* move build role links process to internal_api

* bump version
  • Loading branch information
GopherJ authored May 11, 2020
1 parent edb35cb commit 598a9bd
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casbin"
version = "0.7.4"
version = "0.7.5"
authors = ["Joey <[email protected]>", "Cheng JIANG <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Add this package to `Cargo.toml` of your project. (Check https://crates.io/crate

```toml
[dependencies]
casbin = { version = "0.7.4", default-features = false, features = ["runtime-async-std", "logging"] }
casbin = { version = "0.7.5", default-features = false, features = ["runtime-async-std", "logging"] }
async-std = { version = "1.5.0", features = ["attributes"] }
env_logger = "0.7.1"
```
Expand Down
5 changes: 5 additions & 0 deletions src/cached_enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ impl CoreApi for CachedEnforcer {
fn has_auto_notify_watcher_enabled(&self) -> bool {
self.enforcer.has_auto_notify_watcher_enabled()
}

#[inline]
fn has_auto_build_role_links_enabled(&self) -> bool {
self.enforcer.has_auto_build_role_links_enabled()
}
}

impl CachedApi for CachedEnforcer {
Expand Down
1 change: 1 addition & 0 deletions src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ pub trait CoreApi: Sized + Send + Sync {
fn has_auto_save_enabled(&self) -> bool;
#[cfg(feature = "watcher")]
fn has_auto_notify_watcher_enabled(&self) -> bool;
fn has_auto_build_role_links_enabled(&self) -> bool;
}
11 changes: 10 additions & 1 deletion src/enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ impl CoreApi for Enforcer {

fn add_matching_fn(&mut self, f: fn(String, String) -> bool) -> Result<()> {
self.rm.write().unwrap().add_matching_fn(f);
self.build_role_links()
if self.auto_build_role_links {
self.build_role_links()?;
}

Ok(())
}

async fn set_model<M: TryIntoModel>(&mut self, m: M) -> Result<()> {
Expand Down Expand Up @@ -559,6 +563,11 @@ impl CoreApi for Enforcer {
fn has_auto_notify_watcher_enabled(&self) -> bool {
self.auto_notify_watcher
}

#[inline]
fn has_auto_build_role_links_enabled(&self) -> bool {
self.auto_build_role_links
}
}

#[cfg(test)]
Expand Down
170 changes: 155 additions & 15 deletions src/internal_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ use crate::{
Result,
};

#[cfg(any(feature = "watcher", feature = "cached", feature = "logging"))]
#[cfg(any(
feature = "watcher",
feature = "cached",
feature = "logging",
feature = "incremental"
))]
use crate::emitter::EventData;

#[cfg(feature = "cached")]
Expand Down Expand Up @@ -66,18 +71,31 @@ impl InternalApi for Enforcer {
}

let rule_added = self.get_mut_model().add_policy(sec, ptype, {
#[cfg(any(feature = "watcher", feature = "logging"))]
#[cfg(any(feature = "watcher", feature = "logging", feature = "incremental"))]
{
rule.clone()
}
#[cfg(all(not(feature = "watcher"), not(feature = "logging")))]
#[cfg(all(
not(feature = "watcher"),
not(feature = "logging"),
not(feature = "incremental")
))]
{
rule
}
});
#[cfg(any(feature = "watcher", feature = "logging"))]
{
let event_data = EventData::AddPolicy(sec.to_owned(), ptype.to_owned(), rule);
let event_data = EventData::AddPolicy(sec.to_owned(), ptype.to_owned(), {
#[cfg(feature = "incremental")]
{
rule.clone()
}
#[cfg(not(feature = "incremental"))]
{
rule
}
});
#[cfg(feature = "watcher")]
{
if rule_added && self.has_auto_notify_watcher_enabled() {
Expand All @@ -91,6 +109,21 @@ impl InternalApi for Enforcer {
}
}
}
if sec != "g" || !self.has_auto_build_role_links_enabled() {
return Ok(rule_added);
}
#[cfg(not(feature = "incremental"))]
{
self.build_role_links()?;
}
#[cfg(feature = "incremental")]
{
self.build_incremental_role_links(EventData::AddPolicy(
sec.to_owned(),
ptype.to_owned(),
rule,
))?;
}

Ok(rule_added)
}
Expand All @@ -111,18 +144,31 @@ impl InternalApi for Enforcer {
}

let rules_added = self.get_mut_model().add_policies(sec, ptype, {
#[cfg(any(feature = "watcher", feature = "logging"))]
#[cfg(any(feature = "watcher", feature = "logging", feature = "incremental"))]
{
rules.clone()
}
#[cfg(all(not(feature = "watcher"), not(feature = "logging")))]
#[cfg(all(
not(feature = "watcher"),
not(feature = "logging"),
not(feature = "incremental")
))]
{
rules
}
});
#[cfg(any(feature = "watcher", feature = "logging"))]
{
let event_data = EventData::AddPolicies(sec.to_owned(), ptype.to_owned(), rules);
let event_data = EventData::AddPolicies(sec.to_owned(), ptype.to_owned(), {
#[cfg(feature = "incremental")]
{
rules.clone()
}
#[cfg(not(feature = "incremental"))]
{
rules
}
});
#[cfg(feature = "watcher")]
{
if rules_added && self.has_auto_notify_watcher_enabled() {
Expand All @@ -136,6 +182,21 @@ impl InternalApi for Enforcer {
}
}
}
if sec != "g" || !self.has_auto_build_role_links_enabled() {
return Ok(rules_added);
}
#[cfg(not(feature = "incremental"))]
{
self.build_role_links()?;
}
#[cfg(feature = "incremental")]
{
self.build_incremental_role_links(EventData::AddPolicies(
sec.to_owned(),
ptype.to_owned(),
rules,
))?;
}

Ok(rules_added)
}
Expand All @@ -156,18 +217,31 @@ impl InternalApi for Enforcer {
}

let rule_removed = self.get_mut_model().remove_policy(sec, ptype, {
#[cfg(any(feature = "watcher", feature = "logging"))]
#[cfg(any(feature = "watcher", feature = "logging", feature = "incremental"))]
{
rule.clone()
}
#[cfg(all(not(feature = "watcher"), not(feature = "logging")))]
#[cfg(all(
not(feature = "watcher"),
not(feature = "logging"),
not(feature = "incremental")
))]
{
rule
}
});
#[cfg(any(feature = "watcher", feature = "logging"))]
{
let event_data = EventData::RemovePolicy(sec.to_owned(), ptype.to_owned(), rule);
let event_data = EventData::RemovePolicy(sec.to_owned(), ptype.to_owned(), {
#[cfg(feature = "incremental")]
{
rule.clone()
}
#[cfg(not(feature = "incremental"))]
{
rule
}
});
#[cfg(feature = "watcher")]
{
if rule_removed && self.has_auto_notify_watcher_enabled() {
Expand All @@ -181,6 +255,21 @@ impl InternalApi for Enforcer {
}
}
}
if sec != "g" || !self.has_auto_build_role_links_enabled() {
return Ok(rule_removed);
}
#[cfg(not(feature = "incremental"))]
{
self.build_role_links()?;
}
#[cfg(feature = "incremental")]
{
self.build_incremental_role_links(EventData::RemovePolicy(
sec.to_owned(),
ptype.to_owned(),
rule,
))?;
}

Ok(rule_removed)
}
Expand All @@ -201,19 +290,32 @@ impl InternalApi for Enforcer {
}

let rules_removed = self.get_mut_model().remove_policies(sec, ptype, {
#[cfg(any(feature = "watcher", feature = "logging"))]
#[cfg(any(feature = "watcher", feature = "logging", feature = "incremental"))]
{
rules.clone()
}
#[cfg(all(not(feature = "watcher"), not(feature = "logging")))]
#[cfg(all(
not(feature = "watcher"),
not(feature = "logging"),
not(feature = "incremental")
))]
{
rules
}
});

#[cfg(any(feature = "watcher", feature = "logging"))]
{
let event_data = EventData::RemovePolicies(sec.to_owned(), ptype.to_owned(), rules);
let event_data = EventData::RemovePolicies(sec.to_owned(), ptype.to_owned(), {
#[cfg(feature = "incremental")]
{
rules.clone()
}
#[cfg(not(feature = "incremental"))]
{
rules
}
});
#[cfg(feature = "watcher")]
{
if rules_removed && self.has_auto_notify_watcher_enabled() {
Expand All @@ -227,6 +329,21 @@ impl InternalApi for Enforcer {
}
}
}
if sec != "g" || !self.has_auto_build_role_links_enabled() {
return Ok(rules_removed);
}
#[cfg(not(feature = "incremental"))]
{
self.build_role_links()?;
}
#[cfg(feature = "incremental")]
{
self.build_incremental_role_links(EventData::RemovePolicies(
sec.to_owned(),
ptype.to_owned(),
rules,
))?;
}

Ok(rules_removed)
}
Expand All @@ -253,8 +370,16 @@ impl InternalApi for Enforcer {

#[cfg(any(feature = "watcher", feature = "logging"))]
{
let event_data =
EventData::RemoveFilteredPolicy(sec.to_owned(), ptype.to_owned(), rules.clone());
let event_data = EventData::RemoveFilteredPolicy(sec.to_owned(), ptype.to_owned(), {
#[cfg(feature = "incremental")]
{
rules.clone()
}
#[cfg(not(feature = "incremental"))]
{
rules
}
});
#[cfg(feature = "watcher")]
{
if rules_removed && self.has_auto_notify_watcher_enabled() {
Expand All @@ -268,6 +393,21 @@ impl InternalApi for Enforcer {
}
}
}
if sec != "g" || !self.has_auto_build_role_links_enabled() {
return Ok((rules_removed, rules));
}
#[cfg(not(feature = "incremental"))]
{
self.build_role_links()?;
}
#[cfg(feature = "incremental")]
{
self.build_incremental_role_links(EventData::RemoveFilteredPolicy(
sec.to_owned(),
ptype.to_owned(),
rules.clone(),
))?;
}

Ok((rules_removed, rules))
}
Expand Down
Loading

1 comment on commit 598a9bd

@GopherJ
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: 598a9bd Previous: edb35cb Ratio
b_benchmark_abac_model 6421 ns/iter (± 1363) 7365 ns/iter (± 5415) 0.87
b_benchmark_basic_model 6693 ns/iter (± 1448) 7779 ns/iter (± 2086) 0.86
b_benchmark_key_match 24267 ns/iter (± 4058) 25842 ns/iter (± 2251) 0.94
b_benchmark_priority_model 7745 ns/iter (± 1304) 9009 ns/iter (± 964) 0.86
b_benchmark_raw 7 ns/iter (± 1) 8 ns/iter (± 2) 0.88
b_benchmark_rbac_model 19798 ns/iter (± 3247) 22233 ns/iter (± 14057) 0.89
b_benchmark_rbac_model_large 56061649 ns/iter (± 6381520) 70838101 ns/iter (± 5314903) 0.79
b_benchmark_rbac_model_medium 5440739 ns/iter (± 911091) 6651449 ns/iter (± 833509) 0.82
b_benchmark_rbac_model_small 546371 ns/iter (± 76074) 647565 ns/iter (± 62181) 0.84
b_benchmark_rbac_model_with_domains 11058 ns/iter (± 1914) 13125 ns/iter (± 2928) 0.84
b_benchmark_rbac_with_deny 32953 ns/iter (± 5589) 37197 ns/iter (± 2004) 0.89
b_benchmark_rbac_with_resource_roles 8678 ns/iter (± 1428) 10076 ns/iter (± 816) 0.86

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.