You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/07/28 03:12:07 UTC
[shardingsphere] branch master updated: Reduce redundant overhead in AuthorityChecker (#19620)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 57c9ba82889 Reduce redundant overhead in AuthorityChecker (#19620)
57c9ba82889 is described below
commit 57c9ba82889b9f5ae23f14cfe38a0d56a964f7fe
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Thu Jul 28 11:11:59 2022 +0800
Reduce redundant overhead in AuthorityChecker (#19620)
* Reduce redundant overhead in AuthorityChecker
* Remove unused mock in AuthorityCheckerTest
---
.../shardingsphere/authority/checker/AuthorityChecker.java | 8 +++++---
.../shardingsphere/authority/checker/AuthorityCheckerTest.java | 9 ---------
2 files changed, 5 insertions(+), 12 deletions(-)
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
index 11647b9cac1..11471513a9a 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
@@ -47,7 +47,6 @@ import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQ
import java.util.Collections;
import java.util.List;
import java.util.Map;
-import java.util.Objects;
import java.util.Optional;
import java.util.function.BiPredicate;
@@ -78,8 +77,11 @@ public final class AuthorityChecker implements SQLChecker<AuthorityRule> {
return new SQLCheckResult(false, String.format("Unknown database '%s'", currentDatabase));
}
PrivilegeType privilegeType = getPrivilege(sqlStatementContext.getSqlStatement());
- String errorMessage = Objects.isNull(privilegeType) ? "" : String.format("Access denied for operation %s", privilegeType.name());
- return privileges.map(optional -> new SQLCheckResult(optional.hasPrivileges(Collections.singletonList(privilegeType)), errorMessage)).orElseGet(() -> new SQLCheckResult(false, ""));
+ return privileges.map(optional -> {
+ boolean isPassed = optional.hasPrivileges(Collections.singletonList(privilegeType));
+ String errorMessage = isPassed || null == privilegeType ? "" : String.format("Access denied for operation %s", privilegeType.name());
+ return new SQLCheckResult(isPassed, errorMessage);
+ }).orElseGet(() -> new SQLCheckResult(false, ""));
}
@Override
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
index b3e784dbcca..6d7bb02bb48 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
@@ -25,14 +25,9 @@ import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementConte
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.executor.check.SQLChecker;
import org.apache.shardingsphere.infra.executor.check.SQLCheckerFactory;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Answers;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collection;
import java.util.Collections;
@@ -43,12 +38,8 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
-@RunWith(MockitoJUnitRunner.class)
public final class AuthorityCheckerTest {
- @Mock(answer = Answers.RETURNS_DEEP_STUBS)
- private ShardingSphereDatabase database;
-
@SuppressWarnings("unchecked")
@Test
public void assertCheckSchemaByAllPrivilegesPermitted() {