This is an automated email from the ASF dual-hosted git repository.
panjuan 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 bdc4ededa01 Remove AbstractLockJudgeEngine (#19329)
bdc4ededa01 is described below
commit bdc4ededa0114c40ac1743aa7020222ba25e4d95
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Jul 18 19:04:48 2022 +0800
Remove AbstractLockJudgeEngine (#19329)
---
.../mode/manager/lock/AbstractLockJudgeEngine.java | 74 ----------------------
.../lock/ShardingSphereLockJudgeEngine.java | 18 ++++--
.../sql/common/statement/SQLStatementType.java | 40 +++++++++++-
.../sql/common/statement/SQLStatementTypeTest.java | 46 +++++++++-----
4 files changed, 80 insertions(+), 98 deletions(-)
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/AbstractLockJudgeEngine.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/AbstractLockJudgeEngine.java
deleted file mode 100644
index 8825d882c7e..00000000000
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/AbstractLockJudgeEngine.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mode.manager.lock;
-
-import lombok.Getter;
-import org.apache.shardingsphere.infra.lock.LockContext;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
-
-import java.util.Collections;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * Abstract lock judge engine.
- */
-public abstract class AbstractLockJudgeEngine implements LockJudgeEngine {
-
- private static final Set<Class<? extends SQLStatement>>
IGNORABLE_SQL_STATEMENT_CLASSES_STOP_WRITING = Collections.newSetFromMap(new
ConcurrentHashMap<>());
-
- @Getter
- private LockContext lockContext;
-
- @Override
- public void init(final LockContext lockContext) {
- this.lockContext = lockContext;
- }
-
- /**
- * Is write statement.
- *
- * @param sqlStatement sql statement
- * @return is write statement or not
- */
- protected boolean isWriteStatement(final SQLStatement sqlStatement) {
- Class<? extends SQLStatement> sqlStatementClass =
sqlStatement.getClass();
- if
(IGNORABLE_SQL_STATEMENT_CLASSES_STOP_WRITING.contains(sqlStatementClass)) {
- return false;
- }
- if (sqlStatement instanceof SelectStatement) {
- catchIgnorable(sqlStatementClass);
- return false;
- }
- if (sqlStatement instanceof DMLStatement) {
- return true;
- }
- if (sqlStatement instanceof DDLStatement) {
- return true;
- }
- catchIgnorable(sqlStatementClass);
- return false;
- }
-
- private void catchIgnorable(final Class<? extends SQLStatement>
sqlStatementClass) {
- IGNORABLE_SQL_STATEMENT_CLASSES_STOP_WRITING.add(sqlStatementClass);
- }
-}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/ShardingSphereLockJudgeEngine.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/ShardingSphereLockJudgeEngine.java
index 235af9c82a2..b76c22ff1c9 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/ShardingSphereLockJudgeEngine.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/ShardingSphereLockJudgeEngine.java
@@ -17,15 +17,22 @@
package org.apache.shardingsphere.mode.manager.lock;
-import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.lock.LockContext;
import
org.apache.shardingsphere.mode.manager.lock.definition.LockDefinitionFactory;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatementType;
/**
* Lock judge engine for ShardingSphere.
*/
-@RequiredArgsConstructor
-public final class ShardingSphereLockJudgeEngine extends
AbstractLockJudgeEngine {
+public final class ShardingSphereLockJudgeEngine implements LockJudgeEngine {
+
+ private LockContext lockContext;
+
+ @Override
+ public void init(final LockContext lockContext) {
+ this.lockContext = lockContext;
+ }
/**
* Is locked.
@@ -36,9 +43,6 @@ public final class ShardingSphereLockJudgeEngine extends
AbstractLockJudgeEngine
*/
@Override
public boolean isLocked(final String databaseName, final
SQLStatementContext<?> sqlStatementContext) {
- if (isWriteStatement(sqlStatementContext.getSqlStatement())) {
- return
getLockContext().isLocked(LockDefinitionFactory.newDatabaseLockDefinition(databaseName));
- }
- return false;
+ return
SQLStatementType.involvesDataChanges(sqlStatementContext.getSqlStatement()) &&
lockContext.isLocked(LockDefinitionFactory.newDatabaseLockDefinition(databaseName));
}
}
diff --git
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/SQLStatementType.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/SQLStatementType.java
index 25eb3add9c7..3fd7c599e45 100644
---
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/SQLStatementType.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/SQLStatementType.java
@@ -17,10 +17,48 @@
package org.apache.shardingsphere.sql.parser.sql.common.statement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.ConcurrentHashMap;
+
/**
* SQL statement type.
*/
public enum SQLStatementType {
- DML, DDL, TCL, DCL, DAL, RL
+ DML, DDL, TCL, DCL, DAL, RL;
+
+ private static final Collection<Class<? extends SQLStatement>>
INVOLVE_DATA_CHANGES_STATEMENTS = Collections.newSetFromMap(new
ConcurrentHashMap<>());
+
+ private static final Collection<Class<? extends SQLStatement>>
NOT_INVOLVE_DATA_CHANGES_STATEMENTS = Collections.newSetFromMap(new
ConcurrentHashMap<>());
+
+ /**
+ * Judge whether involves data changes.
+ *
+ * @param sqlStatement SQL statement
+ * @return is statement involves data changes or not
+ */
+ public static boolean involvesDataChanges(final SQLStatement sqlStatement)
{
+ Class<? extends SQLStatement> sqlStatementClass =
sqlStatement.getClass();
+ if (NOT_INVOLVE_DATA_CHANGES_STATEMENTS.contains(sqlStatementClass)) {
+ return false;
+ }
+ if (INVOLVE_DATA_CHANGES_STATEMENTS.contains(sqlStatementClass)) {
+ return true;
+ }
+ if (sqlStatement instanceof SelectStatement) {
+ NOT_INVOLVE_DATA_CHANGES_STATEMENTS.add(sqlStatementClass);
+ return false;
+ }
+ if (sqlStatement instanceof DMLStatement || sqlStatement instanceof
DDLStatement) {
+ INVOLVE_DATA_CHANGES_STATEMENTS.add(sqlStatementClass);
+ return true;
+ }
+ NOT_INVOLVE_DATA_CHANGES_STATEMENTS.add(sqlStatementClass);
+ return false;
+ }
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/lock/AbstractLockJudgeEngineTest.java
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/statement/SQLStatementTypeTest.java
similarity index 51%
rename from
shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/lock/AbstractLockJudgeEngineTest.java
rename to
shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/statement/SQLStatementTypeTest.java
index cac9e41ff3b..63671f66843 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/lock/AbstractLockJudgeEngineTest.java
+++
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/statement/SQLStatementTypeTest.java
@@ -15,36 +15,50 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.manager.lock;
+package org.apache.shardingsphere.sql.parser.sql.common.statement;
-import org.apache.shardingsphere.infra.lock.LockContext;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ShowStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
-import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
-public final class AbstractLockJudgeEngineTest {
+public class SQLStatementTypeTest {
- private AbstractLockJudgeEngine engine;
+ @Test
+ public void assertInvolvesDataChangesWithSelectStatement() {
+
assertFalse(SQLStatementType.involvesDataChanges(mock(SelectStatement.class)));
+ }
+
+ @Test
+ public void assertNotInvolvesDataChangesWithUpdateStatement() {
+
assertTrue(SQLStatementType.involvesDataChanges(mock(UpdateStatement.class)));
+ }
+
+ @Test
+ public void assertNotInvolvesDataChangesWithDDLStatement() {
+
assertTrue(SQLStatementType.involvesDataChanges(mock(CreateTableStatement.class)));
+ }
+
+ @Test
+ public void assertInvolvesDataChangesWithOtherStatement() {
+
assertFalse(SQLStatementType.involvesDataChanges(mock(ShowStatement.class)));
+ }
- @Before
- public void setUp() {
- engine = (AbstractLockJudgeEngine)
LockJudgeEngineBuilder.build(mock(LockContext.class));
+ @Test
+ public void assertInvolvesDataChangesWithCache() {
+
assertFalse(SQLStatementType.involvesDataChanges(mock(SelectStatement.class)));
+
assertFalse(SQLStatementType.involvesDataChanges(mock(SelectStatement.class)));
}
@Test
- public void assertIsWriteDDLStatement() {
- assertTrue(engine.isWriteStatement(mock(DDLStatement.class)));
- assertTrue(engine.isWriteStatement(mock(InsertStatement.class)));
- assertTrue(engine.isWriteStatement(mock(UpdateStatement.class)));
- assertTrue(engine.isWriteStatement(mock(DeleteStatement.class)));
- assertFalse(engine.isWriteStatement(mock(SelectStatement.class)));
+ public void assertNotInvolvesDataChangesWithCache() {
+
assertTrue(SQLStatementType.involvesDataChanges(mock(DeleteStatement.class)));
+
assertTrue(SQLStatementType.involvesDataChanges(mock(DeleteStatement.class)));
}
}