Skip to content

Commit

Permalink
replacing the TreeMap with CaseInsensitiveMap.(#28602)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhucong committed Sep 27, 2023
1 parent be9b6fb commit ffa8fa6
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, EncryptColumn> columns;
Expand All @@ -57,7 +57,7 @@ public EncryptTable(final EncryptTableRuleConfiguration config, final Map<String
@SuppressWarnings("rawtypes")
private Map<String, EncryptColumn> createEncryptColumns(final EncryptTableRuleConfiguration config, final Map<String, StandardEncryptAlgorithm> standardEncryptors,
final Map<String, AssistedEncryptAlgorithm> assistedEncryptors, final Map<String, LikeEncryptAlgorithm> likeEncryptors) {
Map<String, EncryptColumn> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, EncryptColumn> result = new CaseInsensitiveMap<>();
for (EncryptColumnRuleConfiguration each : config.getColumns()) {
result.put(each.getName(), createEncryptColumn(each, standardEncryptors, assistedEncryptors, likeEncryptors));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,7 +32,7 @@ public final class MaskTable {
private final Map<String, MaskColumn> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -62,7 +62,7 @@ private ShardingSphereSchema getSchema(final SQLStatementContext sqlStatementCon
}

private Map<String, Integer> getColumnLabelIndexMap(final QueryResult queryResult) throws SQLException {
Map<String, Integer> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, Integer> result = new CaseInsensitiveMap<>();
for (int i = 0; i < queryResult.getMetaData().getColumnCount(); i++) {
result.put(SQLUtils.getExactlyValue(queryResult.getMetaData().getColumnLabel(i + 1)), i + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -73,7 +73,7 @@ private boolean isNeedAggregateRewrite(final SQLStatementContext sqlStatementCon
}

private Map<String, Integer> getColumnLabelIndexMap(final QueryResult queryResult) throws SQLException {
Map<String, Integer> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, Integer> result = new CaseInsensitiveMap<>();
for (int i = queryResult.getMetaData().getColumnCount(); i > 0; i--) {
result.put(SQLUtils.getExactlyValue(queryResult.getMetaData().getColumnLabel(i)), i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,7 +43,6 @@
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.TreeSet;

/**
Expand All @@ -56,6 +56,13 @@ public final class TablesContext {

private final Collection<SimpleTableSegment> simpleTableSegments = new LinkedList<>();

/**
* -- GETTER --
* Get table names.
*
* @return table names
*/
@Getter
private final Collection<String> tableNames = new HashSet<>();

private final Collection<String> schemaNames = new HashSet<>();
Expand Down Expand Up @@ -112,15 +119,6 @@ private Map<String, Collection<SubqueryTableContext>> createSubqueryTables(final
return result;
}

/**
* Get table names.
*
* @return table names
*/
public Collection<String> getTableNames() {
return tableNames;
}

/**
* Find expression table name map by column segment.
*
Expand All @@ -132,7 +130,7 @@ public Map<String, String> findTableNamesByColumnSegment(final Collection<Column
if (1 == simpleTableSegments.size()) {
return findTableNameFromSingleTableByColumnSegment(columns);
}
Map<String, String> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, String> result = new CaseInsensitiveMap<>();
Map<String, Collection<String>> ownerColumnNames = getOwnerColumnNamesByColumnSegment(columns);
result.putAll(findTableNameFromSQL(ownerColumnNames));
Collection<String> noOwnerColumnNames = getNoOwnerColumnNamesByColumnSegment(columns);
Expand Down Expand Up @@ -163,15 +161,15 @@ private Map<String, String> findTableNameFromSubqueryByColumnSegment(final Colle

private Map<String, String> findTableNameFromSingleTableByColumnSegment(final Collection<ColumnSegment> columns) {
String tableName = simpleTableSegments.iterator().next().getTableName().getIdentifier().getValue();
Map<String, String> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, String> result = new CaseInsensitiveMap<>();
for (ColumnSegment each : columns) {
result.putIfAbsent(each.getExpression(), tableName);
}
return result;
}

private Map<String, Collection<String>> getOwnerColumnNamesByColumnSegment(final Collection<ColumnSegment> columns) {
Map<String, Collection<String>> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, Collection<String>> result = new CaseInsensitiveMap<>();
for (ColumnSegment each : columns) {
if (!each.getOwner().isPresent()) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

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;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.TreeMap;

/**
* Data type loader.
Expand All @@ -46,7 +46,7 @@ public Map<String, Integer> load(final DatabaseMetaData databaseMetaData, final
}

private Map<String, Integer> loadStandardDataTypes(final DatabaseMetaData databaseMetaData) throws SQLException {
Map<String, Integer> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, Integer> result = new CaseInsensitiveMap<>();
try (ResultSet resultSet = databaseMetaData.getTypeInfo()) {
while (resultSet.next()) {
result.put(resultSet.getString("TYPE_NAME"), resultSet.getInt("DATA_TYPE"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -83,7 +83,7 @@ public DatabaseMetaDataResultSet(final ResultSet resultSet, final Collection<Sha
}

private Map<String, Integer> initIndexMap() throws SQLException {
Map<String, Integer> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, Integer> result = new CaseInsensitiveMap<>();
for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
result.put(resultSetMetaData.getColumnLabel(i), i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<BinaryOperationExpression, SqlNode> {

private static final Map<String, SqlOperator> REGISTRY = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private static final Map<String, SqlOperator> REGISTRY = new CaseInsensitiveMap<>();

static {
register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<UnaryOperationExpression, SqlNode> {

private static final Map<String, SqlOperator> REGISTRY = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private static final Map<String, SqlOperator> REGISTRY = new CaseInsensitiveMap<>();

static {
register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AggregationProjectionSegment, SqlNode> {

private static final Map<String, SqlAggFunction> REGISTRY = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private static final Map<String, SqlAggFunction> REGISTRY = new CaseInsensitiveMap<>();

static {
register(SqlStdOperatorTable.MAX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -144,7 +144,7 @@ private void setColumnNames(final Iterator<Result> rows) {
}

private Map<String, String> parseResult(final Result result) {
Map<String, String> row = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, String> row = new CaseInsensitiveMap<>();
row.put(ROW_KEY_COLUMN_NAME, Bytes.toString(result.getRow()));
Long timestamp = null;
for (Cell each : result.listCells()) {
Expand Down

0 comments on commit ffa8fa6

Please sign in to comment.