Skip to content

Commit 87e155d

Browse files
authored
fix: fix the cache for g function (casbin#240)
Signed-off-by: Yixiang Zhao <[email protected]>
1 parent a334647 commit 87e155d

File tree

3 files changed

+39
-52
lines changed

3 files changed

+39
-52
lines changed

src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java

+21-36
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ public class CoreEnforcer {
5757
boolean autoNotifyWatcher = true;
5858
boolean autoNotifyDispatcher = true;
5959

60-
// cached instance of AviatorEvaluatorInstance
61-
AviatorEvaluatorInstance aviatorEval;
62-
63-
// detect changes in Model so that we can invalidate AviatorEvaluatorInstance cache
64-
int modelModCount;
65-
6660
void initialize() {
6761
rmMap = new HashMap<>();
6862
eft = new DefaultEffector();
@@ -129,7 +123,6 @@ public void loadModel() {
129123
model.loadModel(this.modelPath);
130124
model.printModel();
131125
fm = FunctionMap.loadFunctionMap();
132-
aviatorEval = null;
133126
}
134127

135128
/**
@@ -149,7 +142,6 @@ public Model getModel() {
149142
public void setModel(Model model) {
150143
this.model = model;
151144
fm = FunctionMap.loadFunctionMap();
152-
aviatorEval = null;
153145
}
154146

155147
/**
@@ -399,37 +391,31 @@ private boolean enforce(String matcher, Object... rvals) {
399391
if (!enabled) {
400392
return true;
401393
}
402-
if (aviatorEval == null || modelModCount != model.getModCount()) {
403-
synchronized (this) {
404-
if (aviatorEval == null || modelModCount != model.getModCount()) {
405-
// AviatorEvaluator instance must be rebuild
406-
Map<String, AviatorFunction> functions = new HashMap<>();
407-
for (Map.Entry<String, AviatorFunction> entry : fm.fm.entrySet()) {
408-
String key = entry.getKey();
409-
AviatorFunction function = entry.getValue();
410-
411-
functions.put(key, function);
412-
}
413-
if (model.model.containsKey("g")) {
414-
for (Map.Entry<String, Assertion> entry : model.model.get("g").entrySet()) {
415-
String key = entry.getKey();
416-
Assertion ast = entry.getValue();
417-
418-
RoleManager rm = ast.rm;
419-
functions.put(key, BuiltInFunctions.generateGFunction(key, rm));
420-
}
421-
}
422394

423-
aviatorEval = AviatorEvaluator.newInstance();
424-
for (AviatorFunction f : functions.values()) {
425-
aviatorEval.addFunction(f);
426-
}
427-
fm.setAviatorEval(aviatorEval);
395+
Map<String, AviatorFunction> functions = new HashMap<>();
396+
for (Map.Entry<String, AviatorFunction> entry : fm.fm.entrySet()) {
397+
String key = entry.getKey();
398+
AviatorFunction function = entry.getValue();
428399

429-
modelModCount = model.getModCount();
430-
}
400+
functions.put(key, function);
401+
}
402+
if (model.model.containsKey("g")) {
403+
for (Map.Entry<String, Assertion> entry : model.model.get("g").entrySet()) {
404+
String key = entry.getKey();
405+
Assertion ast = entry.getValue();
406+
407+
RoleManager rm = ast.rm;
408+
functions.put(key, BuiltInFunctions.generateGFunction(key, rm));
409+
}
410+
}
411+
AviatorEvaluatorInstance aviatorEval = AviatorEvaluator.newInstance();
412+
for (AviatorFunction f : functions.values()) {
413+
if (aviatorEval.containsFunction(f.getName())) {
414+
aviatorEval.removeFunction(f.getName());
431415
}
416+
aviatorEval.addFunction(f);
432417
}
418+
fm.setAviatorEval(aviatorEval);
433419

434420
String expString;
435421
if (matcher == null || "".equals(matcher)) {
@@ -599,7 +585,6 @@ private boolean validateEnforceSection(String section, Object... rvals) {
599585
* need to call it explicitly if you manipulate directly Model.
600586
*/
601587
public void resetExpressionEvaluator() {
602-
aviatorEval = null;
603588
fm.setAviatorEval(null);
604589
}
605590

src/main/java/org/casbin/jcasbin/main/ManagementEnforcer.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,6 @@ public boolean addGroupingPolicies(String[][] rules) {
589589
public boolean addNamedGroupingPolicy(String ptype, List<String> params) {
590590
boolean ruleAdded = addPolicy("g", ptype, params);
591591

592-
aviatorEval = null;
593592
fm.setAviatorEval(null);
594593
return ruleAdded;
595594
}
@@ -695,7 +694,6 @@ public boolean removeFilteredGroupingPolicy(int fieldIndex, String... fieldValue
695694
public boolean removeNamedGroupingPolicy(String ptype, List<String> params) {
696695
boolean ruleRemoved = removePolicy("g", ptype, params);
697696

698-
aviatorEval = null;
699697
fm.setAviatorEval(null);
700698
return ruleRemoved;
701699
}
@@ -745,7 +743,6 @@ public boolean removeNamedGroupingPolicies(String ptype, String[][] rules) {
745743
public boolean removeFilteredNamedGroupingPolicy(String ptype, int fieldIndex, String... fieldValues) {
746744
boolean ruleRemoved = removeFilteredPolicy("g", ptype, fieldIndex, fieldValues);
747745

748-
aviatorEval = null;
749746
fm.setAviatorEval(null);
750747
return ruleRemoved;
751748
}
@@ -758,7 +755,7 @@ public boolean removeFilteredNamedGroupingPolicy(String ptype, int fieldIndex, S
758755
*/
759756
public void addFunction(String name, CustomFunction function) {
760757
fm.addFunction(name, function);
761-
aviatorEval = null;
758+
762759
fm.setAviatorEval(null);
763760
}
764761

src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -341,31 +341,37 @@ public static boolean allMatch(String key1, String key2) {
341341
* @return the function.
342342
*/
343343
public static AviatorFunction generateGFunction(String name, RoleManager rm) {
344-
return new AbstractVariadicFunction() {
345344

345+
Map<String, AviatorBoolean> memorized = new HashMap<>();
346+
347+
return new AbstractVariadicFunction() {
346348
@Override
347349
public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args) {
348-
Map<String, AviatorBoolean> memorized = new HashMap<>();
349-
350350
int len = args.length;
351351
if(len < 2){
352352
return AviatorBoolean.valueOf(false);
353353
}
354-
String key = "";
355354
Object name1Obj = FunctionUtils.getJavaObject(args[0], env);
356355
String name2 = FunctionUtils.getStringValue(args[1], env);
357356
Sequence name1List = null;
358357
String name1 = null;
359358
if (name1Obj instanceof java.util.List) {
360359
name1List = RuntimeUtils.seq(name1Obj,env);
361-
for (Object obj : name1List) {
362-
key += ";" + obj;
363-
}
364-
key += ";" + name2;
365-
}
366-
else{
360+
} else{
367361
name1 = (String) name1Obj;
368-
key = ";" + name1 + ";" + name2;
362+
}
363+
364+
String key = "";
365+
for (int i = 0; i < len; i++) {
366+
Object nameObj = FunctionUtils.getJavaObject(args[i], env);
367+
if (nameObj instanceof java.util.List) {
368+
Sequence nameList = RuntimeUtils.seq(name, env);
369+
for (Object obj : nameList) {
370+
key += ";" + obj;
371+
}
372+
} else {
373+
key += ";" + nameObj;
374+
}
369375
}
370376

371377
AviatorBoolean value = memorized.get(key);
@@ -408,7 +414,6 @@ public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args
408414
} else {
409415
value = AviatorBoolean.valueOf(false);
410416
}
411-
412417
memorized.put(key, value);
413418
return value;
414419
}

0 commit comments

Comments
 (0)