Skip to content

Commit

Permalink
refactor "SHOW KEY GENERATE ALGORITHM IMPLEMENTATIONS" module (#29395)
Browse files Browse the repository at this point in the history
  • Loading branch information
dongzl authored Dec 13, 2023
1 parent 2a1f87d commit eb2bed8
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@
#

org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingAlgorithmImplementationsExecutor
org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingKeyGenerateAlgorithmImplementationsExecutor
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,3 @@ NONE
IMPLEMENTATIONS
: I M P L E M E N T A T I O N S
;

GENERATE
: G E N E R A T E
;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,3 @@ import BaseRule;
showShardingAlgorithmImplementations
: SHOW SHARDING ALGORITHM IMPLEMENTATIONS
;

showShardingKeyGenerateAlgorithmImplementations
: SHOW SHARDING KEY GENERATE ALGORITHM IMPLEMENTATIONS
;
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ execute
| showUnusedShardingAuditors
| countShardingRule
| showShardingAlgorithmImplementations
| showShardingKeyGenerateAlgorithmImplementations
) SEMI_? EOF
;
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.ShowShardingAlgorithmsContext;
import org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.ShowShardingAuditorsContext;
import org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.ShowShardingKeyGeneratorsContext;
import org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.ShowShardingKeyGenerateAlgorithmImplementationsContext;
import org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.ShowShardingTableNodesContext;
import org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.ShowShardingTableReferenceRulesContext;
import org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.ShowShardingTableRulesContext;
Expand Down Expand Up @@ -90,7 +89,6 @@
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAlgorithmImplementationsStatement;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAlgorithmsStatement;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAuditorsStatement;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingKeyGenerateAlgorithmImplementationsStatement;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingKeyGeneratorsStatement;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingTableNodesStatement;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingTableReferenceRulesStatement;
Expand Down Expand Up @@ -409,9 +407,4 @@ public ASTNode visitCountShardingRule(final CountShardingRuleContext ctx) {
public ASTNode visitShowShardingAlgorithmImplementations(final ShowShardingAlgorithmImplementationsContext ctx) {
return new ShowShardingAlgorithmImplementationsStatement();
}

@Override
public ASTNode visitShowShardingKeyGenerateAlgorithmImplementations(final ShowShardingKeyGenerateAlgorithmImplementationsContext ctx) {
return new ShowShardingKeyGenerateAlgorithmImplementationsStatement();
}
}
12 changes: 12 additions & 0 deletions parser/distsql/engine/src/main/antlr4/imports/Keyword.g4
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,15 @@ IMPLEMENTATIONS
OF
: O F
;

KEY
: K E Y
;

GENERATE
: G E N E R A T E
;

ALGORITHM
: A L G O R I T H M
;
4 changes: 4 additions & 0 deletions parser/distsql/engine/src/main/antlr4/imports/RALStatement.g4
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ showServiceProviderImplementations
: SHOW IMPLEMENTATIONS OF serviceProviderInterface
;

showKeyGenerateAlgorithmImplementations
: SHOW KEY GENERATE ALGORITHM IMPLEMENTATIONS
;

transmissionRule
: LP_ readDefinition? (COMMA_? writeDefinition)? (COMMA_? streamChannel)? RP_
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ execute
| lockCluster
| unlockCluster
| showServiceProviderImplementations
| showKeyGenerateAlgorithmImplementations
) SEMI_? EOF
;
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowDistVariableContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowDistVariablesContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowLogicalTablesContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowKeyGenerateAlgorithmImplementationsContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowMigrationRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowRulesUsedStorageUnitContext;
import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowServiceProviderImplementationsContext;
Expand Down Expand Up @@ -84,6 +85,7 @@
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowComputeNodesStatement;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowDistVariableStatement;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowDistVariablesStatement;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowKeyGenerateAlgorithmImplementationsStatement;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowMigrationRuleStatement;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowServiceProviderImplementationsStatement;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowTableMetaDataStatement;
Expand Down Expand Up @@ -408,4 +410,9 @@ private String getQuotedContent(final ParseTree context) {
public ASTNode visitShowServiceProviderImplementations(final ShowServiceProviderImplementationsContext ctx) {
return new ShowServiceProviderImplementationsStatement(getIdentifierValue(ctx.serviceProviderInterface()));
}

@Override
public ASTNode visitShowKeyGenerateAlgorithmImplementations(final ShowKeyGenerateAlgorithmImplementationsContext ctx) {
return new ShowKeyGenerateAlgorithmImplementationsStatement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.distsql.statement;
package org.apache.shardingsphere.distsql.statement.ral.queryable;

import org.apache.shardingsphere.distsql.statement.ral.QueryableRALStatement;

/**
* Show sharding key generate algorithm implementations statement.
* Show key generate algorithm implementations statement.
*/
public final class ShowShardingKeyGenerateAlgorithmImplementationsStatement extends QueryableRALStatement {
public final class ShowKeyGenerateAlgorithmImplementationsStatement extends QueryableRALStatement {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.distsql.handler.query;
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import org.apache.shardingsphere.distsql.handler.ral.query.QueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowKeyGenerateAlgorithmImplementationsStatement;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.keygen.core.algorithm.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingKeyGenerateAlgorithmImplementationsStatement;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;

/**
* Show sharding key generate algorithm implementations executor.
* Show key generate algorithm implementations executor.
*/
public final class ShowShardingKeyGenerateAlgorithmImplementationsExecutor implements QueryableRALExecutor<ShowShardingKeyGenerateAlgorithmImplementationsStatement> {
public final class ShowKeyGenerateAlgorithmImplementationsExecutor implements QueryableRALExecutor<ShowKeyGenerateAlgorithmImplementationsStatement> {

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("name", "type", "class_path");
}

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShowShardingKeyGenerateAlgorithmImplementationsStatement sqlStatement) {
public Collection<LocalDataQueryResultRow> getRows(final ShowKeyGenerateAlgorithmImplementationsStatement sqlStatement) {
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
Collection<KeyGenerateAlgorithm> keyGenerateAlgorithms = ShardingSphereServiceLoader.getServiceInstances(KeyGenerateAlgorithm.class);
for (KeyGenerateAlgorithm each : keyGenerateAlgorithms) {
Expand All @@ -48,7 +48,7 @@ public Collection<LocalDataQueryResultRow> getRows(final ShowShardingKeyGenerate
}

@Override
public Class<ShowShardingKeyGenerateAlgorithmImplementationsStatement> getType() {
return ShowShardingKeyGenerateAlgorithmImplementationsStatement.class;
public Class<ShowKeyGenerateAlgorithmImplementationsStatement> getType() {
return ShowKeyGenerateAlgorithmImplementationsStatement.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.ShowStatus
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.ShowTableMetaDataExecutor
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.ShowMigrationRuleExecutor
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.ShowServiceProviderImplementationsExecutor
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.ShowKeyGenerateAlgorithmImplementationsExecutor
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.proxy.backend.handler.distsql.fixture;

import org.apache.shardingsphere.keygen.core.algorithm.KeyGenerateAlgorithm;
import org.apache.shardingsphere.keygen.core.context.KeyGenerateContext;

import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public final class DistSQLKeyGenerateAlgorithmImplementationsFixture implements KeyGenerateAlgorithm {

@Override
public Collection<Comparable<?>> generateKeys(final KeyGenerateContext keyGenerateContext, final int keyGenerateCount) {
return IntStream.range(0, keyGenerateCount).mapToObj(each -> 0L).collect(Collectors.toList());

}

@Override
public String getType() {
return "DISTSQL.IMPLEMENTATIONS.FIXTURE";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.distsql.query;
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import org.apache.shardingsphere.distsql.handler.ral.query.QueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowKeyGenerateAlgorithmImplementationsStatement;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingAlgorithmImplementationsExecutor;
import org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingKeyGenerateAlgorithmImplementationsExecutor;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAlgorithmImplementationsStatement;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingKeyGenerateAlgorithmImplementationsStatement;
import org.junit.jupiter.api.Test;

import java.util.Collection;
Expand All @@ -33,23 +30,23 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.mock;

class ShowShardingKeyGenerateAlgorithmImplementationsExecutorTest {
class ShowKeyGenerateAlgorithmImplementationsExecutorTest {

@Test
void assertGetRowData() {
QueryableRALExecutor<ShowShardingKeyGenerateAlgorithmImplementationsStatement> executor = new ShowShardingKeyGenerateAlgorithmImplementationsExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(mock(ShowShardingKeyGenerateAlgorithmImplementationsStatement.class));
QueryableRALExecutor<ShowKeyGenerateAlgorithmImplementationsStatement> executor = new ShowKeyGenerateAlgorithmImplementationsExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(mock(ShowKeyGenerateAlgorithmImplementationsStatement.class));
assertFalse(actual.isEmpty());
Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
LocalDataQueryResultRow row = iterator.next();
assertThat(row.getCell(1), is("DistSQLKeyGenerateAlgorithmFixture"));
assertThat(row.getCell(2), is("DISTSQL.FIXTURE"));
assertThat(row.getCell(3), is("org.apache.shardingsphere.sharding.distsql.fixture.keygen.DistSQLKeyGenerateAlgorithmFixture"));
assertThat(row.getCell(1), is("DistSQLKeyGenerateAlgorithmImplementationsFixture"));
assertThat(row.getCell(2), is("DISTSQL.IMPLEMENTATIONS.FIXTURE"));
assertThat(row.getCell(3), is("org.apache.shardingsphere.proxy.backend.handler.distsql.fixture.DistSQLKeyGenerateAlgorithmImplementationsFixture"));
}

@Test
void assertGetColumnNames() {
QueryableRALExecutor<ShowShardingAlgorithmImplementationsStatement> executor = new ShowShardingAlgorithmImplementationsExecutor();
QueryableRALExecutor<ShowKeyGenerateAlgorithmImplementationsStatement> executor = new ShowKeyGenerateAlgorithmImplementationsExecutor();
Collection<String> columns = executor.getColumnNames();
assertThat(columns.size(), is(3));
Iterator<String> iterator = columns.iterator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.shardingsphere.proxy.backend.handler.distsql.fixture.DistSQLKeyGenerateAlgorithmImplementationsFixture

0 comments on commit eb2bed8

Please sign in to comment.