This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 0af2fcb Reduce duplicate overhead in Grantee (#16381)
0af2fcb is described below
commit 0af2fcb6bbdc5ab4a03e0df74603da3cb6546f20
Author: 吴伟杰 <[email protected]>
AuthorDate: Sat Mar 26 00:11:11 2022 +0800
Reduce duplicate overhead in Grantee (#16381)
---
.../infra/metadata/user/Grantee.java | 36 +++++++++++-----------
.../infra/metadata/user/GranteeTest.java | 27 ++++++++++++----
2 files changed, 39 insertions(+), 24 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/Grantee.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/Grantee.java
index f82d8bf..f9fca35 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/Grantee.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/Grantee.java
@@ -20,19 +20,32 @@ package org.apache.shardingsphere.infra.metadata.user;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
/**
* Grantee.
*/
-@RequiredArgsConstructor
public final class Grantee {
@Getter
private final String username;
+ @Getter
private final String hostname;
+ private final boolean isUnlimitedHost;
+
+ private final int hashCode;
+
+ private final String toString;
+
+ public Grantee(final String username, final String hostname) {
+ this.username = username;
+ this.hostname = Strings.isNullOrEmpty(hostname) ? "%" : hostname;
+ isUnlimitedHost = "%".equals(this.hostname);
+ hashCode = isUnlimitedHost ? username.toUpperCase().hashCode() :
Objects.hashCode(username.toUpperCase(), hostname.toUpperCase());
+ toString = username + "@" + hostname;
+ }
+
@Override
public boolean equals(final Object obj) {
if (obj instanceof Grantee) {
@@ -43,29 +56,16 @@ public final class Grantee {
}
private boolean isPermittedHost(final Grantee grantee) {
- return grantee.hostname.equalsIgnoreCase(hostname) ||
isUnlimitedHost();
- }
-
- private boolean isUnlimitedHost() {
- return Strings.isNullOrEmpty(hostname) || "%".equals(hostname);
- }
-
- /**
- * Get host name.
- *
- * @return host name
- */
- public String getHostname() {
- return Strings.isNullOrEmpty(hostname) ? "%" : hostname;
+ return isUnlimitedHost || grantee.hostname.equalsIgnoreCase(hostname);
}
@Override
public int hashCode() {
- return isUnlimitedHost() ? Objects.hashCode(username.toUpperCase()) :
Objects.hashCode(username.toUpperCase(), hostname.toUpperCase());
+ return hashCode;
}
@Override
public String toString() {
- return String.format("%s@%s", username, hostname);
+ return toString;
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/user/GranteeTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/user/GranteeTest.java
index c6fdaa4..5671330 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/user/GranteeTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/user/GranteeTest.java
@@ -21,22 +21,38 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public final class GranteeTest {
@Test
+ public void assertGetUsername() {
+ Grantee grantee = new Grantee("foo", "");
+ assertThat(grantee.getUsername(), is("foo"));
+ }
+
+ @Test
+ public void assertGetHostname() {
+ Grantee grantee = new Grantee("name", "%");
+ Grantee grantee1 = new Grantee("name", "");
+ assertThat(grantee.getHostname(), is("%"));
+ assertThat(grantee1.getHostname(), is("%"));
+ }
+
+ @Test
public void assertEquals() {
Grantee grantee = new Grantee("name", "%");
Grantee grantee1 = new Grantee("name", "");
Grantee grantee2 = new Grantee("name", "127.0.0.1");
assertTrue(grantee.equals(grantee1));
assertTrue(grantee.equals(grantee2));
+ assertFalse(grantee.equals(new Object()));
}
@Test
- public void assertHashcode() {
+ public void assertHashCode() {
Grantee grantee = new Grantee("name", "%");
Grantee grantee1 = new Grantee("name", "");
Grantee grantee2 = new Grantee("name", "127.0.0.1");
@@ -45,10 +61,9 @@ public final class GranteeTest {
}
@Test
- public void assertHostname() {
- Grantee grantee = new Grantee("name", "%");
- Grantee grantee1 = new Grantee("name", "");
- assertThat(grantee.getHostname(), is("%"));
- assertThat(grantee1.getHostname(), is("%"));
+ public void assertToString() {
+ assertThat(new Grantee("name", "127.0.0.1").toString(),
is("[email protected]"));
+ assertThat(new Grantee("name", "%").toString(), is("name@%"));
+ assertThat(new Grantee("name", "").toString(), is("name@"));
}
}