From ffa8fa62334f14e5688dd28e57ed93a4d6a27230 Mon Sep 17 00:00:00 2001 From: Cong Hu Date: Wed, 27 Sep 2023 19:08:08 +0800 Subject: [PATCH 1/2] replacing the TreeMap with CaseInsensitiveMap.(#28602) --- .../encrypt/rule/EncryptTable.java | 6 ++--- .../shardingsphere/mask/rule/MaskTable.java | 4 ++-- .../merge/ddl/ShardingDDLResultMerger.java | 4 ++-- .../merge/dql/ShardingDQLResultMerger.java | 4 ++-- .../context/segment/table/TablesContext.java | 24 +++++++++---------- .../database/datatype/DataTypeLoader.java | 4 ++-- .../resultset/DatabaseMetaDataResultSet.java | 6 ++--- .../BinaryOperationExpressionConverter.java | 4 ++-- .../UnaryOperationExpressionConverter.java | 4 ++-- .../impl/AggregationProjectionConverter.java | 4 ++-- .../hbase/result/query/HBaseGetResultSet.java | 4 ++-- 11 files changed, 33 insertions(+), 35 deletions(-) diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java index e230de7450d1b..871e63a5f5694 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.encrypt.rule; import lombok.Getter; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration; import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration; import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; @@ -35,14 +36,13 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; -import java.util.TreeMap; /** * Encrypt table. */ +@Getter public final class EncryptTable { - @Getter private final String table; private final Map columns; @@ -57,7 +57,7 @@ public EncryptTable(final EncryptTableRuleConfiguration config, final Map createEncryptColumns(final EncryptTableRuleConfiguration config, final Map standardEncryptors, final Map assistedEncryptors, final Map likeEncryptors) { - Map result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map result = new CaseInsensitiveMap<>(); for (EncryptColumnRuleConfiguration each : config.getColumns()) { result.put(each.getName(), createEncryptColumn(each, standardEncryptors, assistedEncryptors, likeEncryptors)); } diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskTable.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskTable.java index 1e3f24ab927d1..3157c8196b4ca 100644 --- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskTable.java +++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskTable.java @@ -17,12 +17,12 @@ package org.apache.shardingsphere.mask.rule; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; import java.util.Map; import java.util.Optional; -import java.util.TreeMap; /** * Mask table. @@ -32,7 +32,7 @@ public final class MaskTable { private final Map columns; public MaskTable(final MaskTableRuleConfiguration config) { - columns = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + columns = new CaseInsensitiveMap<>(); for (MaskColumnRuleConfiguration each : config.getColumns()) { columns.put(each.getLogicColumn(), new MaskColumn(each.getLogicColumn(), each.getMaskAlgorithm())); } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/ddl/ShardingDDLResultMerger.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/ddl/ShardingDDLResultMerger.java index 5d0a8c33f7203..2ebafed58b112 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/ddl/ShardingDDLResultMerger.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/ddl/ShardingDDLResultMerger.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.merge.ddl; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.FetchStatementContext; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; @@ -34,7 +35,6 @@ import java.sql.SQLException; import java.util.List; import java.util.Map; -import java.util.TreeMap; /** * DDL result merger for Sharding. @@ -62,7 +62,7 @@ private ShardingSphereSchema getSchema(final SQLStatementContext sqlStatementCon } private Map getColumnLabelIndexMap(final QueryResult queryResult) throws SQLException { - Map result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map result = new CaseInsensitiveMap<>(); for (int i = 0; i < queryResult.getMetaData().getColumnCount(); i++) { result.put(SQLUtils.getExactlyValue(queryResult.getMetaData().getColumnLabel(i + 1)), i + 1); } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMerger.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMerger.java index 9fbd36d2bb5e2..62c9081919b78 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMerger.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMerger.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.sharding.merge.dql; import lombok.RequiredArgsConstructor; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.shardingsphere.infra.binder.context.segment.select.orderby.OrderByItem; import org.apache.shardingsphere.infra.binder.context.segment.select.pagination.PaginationContext; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; @@ -45,7 +46,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.TreeMap; /** * DQL result merger for Sharding. @@ -73,7 +73,7 @@ private boolean isNeedAggregateRewrite(final SQLStatementContext sqlStatementCon } private Map getColumnLabelIndexMap(final QueryResult queryResult) throws SQLException { - Map result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map result = new CaseInsensitiveMap<>(); for (int i = queryResult.getMetaData().getColumnCount(); i > 0; i--) { result.put(SQLUtils.getExactlyValue(queryResult.getMetaData().getColumnLabel(i)), i); } diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java index 09836ebed261d..783d0de795ae8 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java @@ -20,6 +20,7 @@ import com.google.common.base.Preconditions; import lombok.Getter; import lombok.ToString; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.shardingsphere.infra.binder.context.segment.select.subquery.SubqueryTableContext; import org.apache.shardingsphere.infra.binder.context.segment.select.subquery.engine.SubqueryTableContextEngine; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; @@ -42,7 +43,6 @@ import java.util.LinkedList; import java.util.Map; import java.util.Optional; -import java.util.TreeMap; import java.util.TreeSet; /** @@ -56,6 +56,13 @@ public final class TablesContext { private final Collection simpleTableSegments = new LinkedList<>(); + /** + * -- GETTER -- + * Get table names. + * + * @return table names + */ + @Getter private final Collection tableNames = new HashSet<>(); private final Collection schemaNames = new HashSet<>(); @@ -112,15 +119,6 @@ private Map> createSubqueryTables(final return result; } - /** - * Get table names. - * - * @return table names - */ - public Collection getTableNames() { - return tableNames; - } - /** * Find expression table name map by column segment. * @@ -132,7 +130,7 @@ public Map findTableNamesByColumnSegment(final Collection result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map result = new CaseInsensitiveMap<>(); Map> ownerColumnNames = getOwnerColumnNamesByColumnSegment(columns); result.putAll(findTableNameFromSQL(ownerColumnNames)); Collection noOwnerColumnNames = getNoOwnerColumnNamesByColumnSegment(columns); @@ -163,7 +161,7 @@ private Map findTableNameFromSubqueryByColumnSegment(final Colle private Map findTableNameFromSingleTableByColumnSegment(final Collection columns) { String tableName = simpleTableSegments.iterator().next().getTableName().getIdentifier().getValue(); - Map result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map result = new CaseInsensitiveMap<>(); for (ColumnSegment each : columns) { result.putIfAbsent(each.getExpression(), tableName); } @@ -171,7 +169,7 @@ private Map findTableNameFromSingleTableByColumnSegment(final Co } private Map> getOwnerColumnNamesByColumnSegment(final Collection columns) { - Map> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map> result = new CaseInsensitiveMap<>(); for (ColumnSegment each : columns) { if (!each.getOwner().isPresent()) { continue; diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/database/datatype/DataTypeLoader.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/database/datatype/DataTypeLoader.java index 343ad397a04a9..7671e9f6cbd93 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/database/datatype/DataTypeLoader.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/database/datatype/DataTypeLoader.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.infra.database.core.metadata.database.datatype; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; @@ -24,7 +25,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; -import java.util.TreeMap; /** * Data type loader. @@ -46,7 +46,7 @@ public Map load(final DatabaseMetaData databaseMetaData, final } private Map loadStandardDataTypes(final DatabaseMetaData databaseMetaData) throws SQLException { - Map result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map result = new CaseInsensitiveMap<>(); try (ResultSet resultSet = databaseMetaData.getTypeInfo()) { while (resultSet.next()) { result.put(resultSet.getString("TYPE_NAME"), resultSet.getInt("DATA_TYPE")); diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java index 6d1c2a7682223..4bbc4b180f630 100644 --- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java +++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java @@ -18,9 +18,10 @@ package org.apache.shardingsphere.driver.jdbc.core.resultset; import lombok.EqualsAndHashCode; +import org.apache.commons.collections4.map.CaseInsensitiveMap; +import org.apache.shardingsphere.driver.jdbc.exception.connection.ResultSetClosedException; import org.apache.shardingsphere.driver.jdbc.exception.syntax.ColumnIndexOutOfRangeException; import org.apache.shardingsphere.driver.jdbc.exception.syntax.ColumnLabelNotFoundException; -import org.apache.shardingsphere.driver.jdbc.exception.connection.ResultSetClosedException; import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedDatabaseMetaDataResultSet; import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.util.ResultSetUtils; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; @@ -43,7 +44,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.TreeMap; /** * Database meta data result set. @@ -83,7 +83,7 @@ public DatabaseMetaDataResultSet(final ResultSet resultSet, final Collection initIndexMap() throws SQLException { - Map result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map result = new CaseInsensitiveMap<>(); for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { result.put(resultSetMetaData.getColumnLabel(i), i); } diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/BinaryOperationExpressionConverter.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/BinaryOperationExpressionConverter.java index 729913874f219..922946feb156c 100644 --- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/BinaryOperationExpressionConverter.java +++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/BinaryOperationExpressionConverter.java @@ -24,6 +24,7 @@ import org.apache.calcite.sql.SqlOperator; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment; import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter; @@ -34,14 +35,13 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.TreeMap; /** * Binary operation expression converter. */ public final class BinaryOperationExpressionConverter implements SQLSegmentConverter { - private static final Map REGISTRY = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + private static final Map REGISTRY = new CaseInsensitiveMap<>(); static { register(); diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/UnaryOperationExpressionConverter.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/UnaryOperationExpressionConverter.java index cc95a87b6bd2d..1b51dbc066c6c 100644 --- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/UnaryOperationExpressionConverter.java +++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/expression/impl/UnaryOperationExpressionConverter.java @@ -23,6 +23,7 @@ import org.apache.calcite.sql.SqlOperator; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.UnaryOperationExpression; import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.SQLSegmentConverter; import org.apache.shardingsphere.sqlfederation.compiler.converter.segment.expression.ExpressionConverter; @@ -31,14 +32,13 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.TreeMap; /** * Unary operation expression converter. */ public final class UnaryOperationExpressionConverter implements SQLSegmentConverter { - private static final Map REGISTRY = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + private static final Map REGISTRY = new CaseInsensitiveMap<>(); static { register(); diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/projection/impl/AggregationProjectionConverter.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/projection/impl/AggregationProjectionConverter.java index 7349c8ef64dbf..4a217f1e90ad7 100644 --- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/projection/impl/AggregationProjectionConverter.java +++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/converter/segment/projection/impl/AggregationProjectionConverter.java @@ -26,6 +26,7 @@ import org.apache.calcite.sql.SqlSelectKeyword; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationDistinctProjectionSegment; import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment; @@ -39,14 +40,13 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.TreeMap; /** * Aggregation projection converter. */ public final class AggregationProjectionConverter implements SQLSegmentConverter { - private static final Map REGISTRY = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + private static final Map REGISTRY = new CaseInsensitiveMap<>(); static { register(SqlStdOperatorTable.MAX); diff --git a/proxy/backend/type/hbase/src/main/java/org/apache/shardingsphere/proxy/backend/hbase/result/query/HBaseGetResultSet.java b/proxy/backend/type/hbase/src/main/java/org/apache/shardingsphere/proxy/backend/hbase/result/query/HBaseGetResultSet.java index 278777c929b08..3c1829daa41b7 100644 --- a/proxy/backend/type/hbase/src/main/java/org/apache/shardingsphere/proxy/backend/hbase/result/query/HBaseGetResultSet.java +++ b/proxy/backend/type/hbase/src/main/java/org/apache/shardingsphere/proxy/backend/hbase/result/query/HBaseGetResultSet.java @@ -19,6 +19,7 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.map.CaseInsensitiveMap; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.client.Get; @@ -51,7 +52,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.TreeMap; import java.util.stream.Collectors; /** @@ -144,7 +144,7 @@ private void setColumnNames(final Iterator rows) { } private Map parseResult(final Result result) { - Map row = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map row = new CaseInsensitiveMap<>(); row.put(ROW_KEY_COLUMN_NAME, Bytes.toString(result.getRow())); Long timestamp = null; for (Cell each : result.listCells()) { From 3d78f7354a3a2de7eb43b92d379aa78895d881cf Mon Sep 17 00:00:00 2001 From: Cong Hu Date: Sun, 8 Oct 2023 08:55:49 +0800 Subject: [PATCH 2/2] Optimize comment . --- .../infra/binder/context/segment/table/TablesContext.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java index 783d0de795ae8..c83f30ce12c41 100644 --- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java +++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java @@ -57,8 +57,7 @@ public final class TablesContext { private final Collection simpleTableSegments = new LinkedList<>(); /** - * -- GETTER -- - * Get table names. + * Get table names. * * @return table names */