Skip to content

Commit

Permalink
Add CountRQLExecutor (#29722)
Browse files Browse the repository at this point in the history
* Add CountRQLExecutor

* Add CountRQLExecutor

* For code format
  • Loading branch information
terrymanu authored Jan 14, 2024
1 parent 04c1532 commit cd6ff45
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,24 @@

import org.apache.shardingsphere.broadcast.distsql.statement.CountBroadcastRuleStatement;
import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.distsql.handler.type.rql.CountRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;

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

/**
* Count broadcast rule executor.
*/
public final class CountBroadcastRuleExecutor implements RQLExecutor<CountBroadcastRuleStatement> {
public final class CountBroadcastRuleExecutor extends CountRQLExecutor<CountBroadcastRuleStatement, BroadcastRule> {

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("rule_name", "database", "count");
public CountBroadcastRuleExecutor() {
super(BroadcastRule.class);
}

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final CountBroadcastRuleStatement sqlStatement) {
Optional<BroadcastRule> rule = database.getRuleMetaData().findSingleRule(BroadcastRule.class);
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
rule.ifPresent(optional -> result.add(new LocalDataQueryResultRow("broadcast_table", database.getName(), optional.getConfiguration().getTables().size())));
return result;
protected Collection<LocalDataQueryResultRow> generateRows(final BroadcastRule rule, final String databaseName) {
return Collections.singleton(new LocalDataQueryResultRow("broadcast_table", databaseName, rule.getConfiguration().getTables().size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,26 @@

package org.apache.shardingsphere.encrypt.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.distsql.handler.type.rql.CountRQLExecutor;
import org.apache.shardingsphere.encrypt.distsql.statement.CountEncryptRuleStatement;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;

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

/**
* Count encrypt rule executor.
*/
public final class CountEncryptRuleExecutor implements RQLExecutor<CountEncryptRuleStatement> {
public final class CountEncryptRuleExecutor extends CountRQLExecutor<CountEncryptRuleStatement, EncryptRule> {

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("rule_name", "database", "count");
public CountEncryptRuleExecutor() {
super(EncryptRule.class);
}

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final CountEncryptRuleStatement sqlStatement) {
Optional<EncryptRule> rule = database.getRuleMetaData().findSingleRule(EncryptRule.class);
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
rule.ifPresent(optional -> fillRows(result, optional, database.getName()));
return result;
}

private void fillRows(final Collection<LocalDataQueryResultRow> result, final EncryptRule rule, final String databaseName) {
result.add(new LocalDataQueryResultRow("encrypt", databaseName, rule.getLogicTableMapper().getTableNames().size()));
protected Collection<LocalDataQueryResultRow> generateRows(final EncryptRule rule, final String databaseName) {
return Collections.singleton(new LocalDataQueryResultRow("encrypt", databaseName, rule.getLogicTableMapper().getTableNames().size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,26 @@

package org.apache.shardingsphere.mask.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.distsql.handler.type.rql.CountRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mask.distsql.statement.CountMaskRuleStatement;
import org.apache.shardingsphere.mask.rule.MaskRule;

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

/**
* Count mask rule executor.
*/
public final class CountMaskRuleExecutor implements RQLExecutor<CountMaskRuleStatement> {
public final class CountMaskRuleExecutor extends CountRQLExecutor<CountMaskRuleStatement, MaskRule> {

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("rule_name", "database", "count");
public CountMaskRuleExecutor() {
super(MaskRule.class);
}

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final CountMaskRuleStatement sqlStatement) {
Optional<MaskRule> rule = database.getRuleMetaData().findSingleRule(MaskRule.class);
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
rule.ifPresent(optional -> fillRows(result, optional, database.getName()));
return result;
}

private void fillRows(final Collection<LocalDataQueryResultRow> result, final MaskRule rule, final String databaseName) {
result.add(new LocalDataQueryResultRow("mask", databaseName, rule.getLogicTableMapper().getTableNames().size()));
protected Collection<LocalDataQueryResultRow> generateRows(final MaskRule rule, final String databaseName) {
return Collections.singleton(new LocalDataQueryResultRow("mask", databaseName, rule.getLogicTableMapper().getTableNames().size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,26 @@

package org.apache.shardingsphere.readwritesplitting.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.distsql.handler.type.rql.CountRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.readwritesplitting.distsql.statement.CountReadwriteSplittingRuleStatement;
import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;

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

/**
* Count readwrite-splitting rule executor.
*/
public final class CountReadwriteSplittingRuleExecutor implements RQLExecutor<CountReadwriteSplittingRuleStatement> {
public final class CountReadwriteSplittingRuleExecutor extends CountRQLExecutor<CountReadwriteSplittingRuleStatement, ReadwriteSplittingRule> {

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("rule_name", "database", "count");
public CountReadwriteSplittingRuleExecutor() {
super(ReadwriteSplittingRule.class);
}

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final CountReadwriteSplittingRuleStatement sqlStatement) {
Optional<ReadwriteSplittingRule> rule = database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class);
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
rule.ifPresent(optional -> fillRows(result, optional, database.getName()));
return result;
}

private void fillRows(final Collection<LocalDataQueryResultRow> result, final ReadwriteSplittingRule rule, final String databaseName) {
result.add(new LocalDataQueryResultRow("readwrite_splitting", databaseName, rule.getDataSourceMapper().size()));
protected Collection<LocalDataQueryResultRow> generateRows(final ReadwriteSplittingRule rule, final String databaseName) {
return Collections.singleton(new LocalDataQueryResultRow("readwrite_splitting", databaseName, rule.getDataSourceMapper().size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,26 @@

package org.apache.shardingsphere.shadow.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.distsql.handler.type.rql.CountRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.shadow.distsql.statement.CountShadowRuleStatement;
import org.apache.shardingsphere.shadow.rule.ShadowRule;

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

/**
* Count shadow rule executor.
*/
public final class CountShadowRuleExecutor implements RQLExecutor<CountShadowRuleStatement> {
public final class CountShadowRuleExecutor extends CountRQLExecutor<CountShadowRuleStatement, ShadowRule> {

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("rule_name", "database", "count");
public CountShadowRuleExecutor() {
super(ShadowRule.class);
}

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final CountShadowRuleStatement sqlStatement) {
Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
rule.ifPresent(optional -> fillRows(result, optional, database.getName()));
return result;
}

private void fillRows(final Collection<LocalDataQueryResultRow> result, final ShadowRule rule, final String databaseName) {
result.add(new LocalDataQueryResultRow("shadow", databaseName, rule.getDataSourceMapper().size()));
protected Collection<LocalDataQueryResultRow> generateRows(final ShadowRule rule, final String databaseName) {
return Collections.singleton(new LocalDataQueryResultRow("shadow", databaseName, rule.getDataSourceMapper().size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,28 @@

package org.apache.shardingsphere.sharding.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.distsql.handler.type.rql.CountRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.distsql.statement.CountShardingRuleStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;

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

/**
* Count sharding rule executor.
*/
public final class CountShardingRuleExecutor implements RQLExecutor<CountShardingRuleStatement> {
public final class CountShardingRuleExecutor extends CountRQLExecutor<CountShardingRuleStatement, ShardingRule> {

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("rule_name", "database", "count");
public CountShardingRuleExecutor() {
super(ShardingRule.class);
}

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final CountShardingRuleStatement sqlStatement) {
Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
rule.ifPresent(optional -> fillRows(result, optional, database.getName()));
return result;
}

private void fillRows(final Collection<LocalDataQueryResultRow> result, final ShardingRule rule, final String databaseName) {
fillRows(result, "sharding_table", databaseName, rule.getTableRules().size());
fillRows(result, "sharding_table_reference", databaseName, ((ShardingRuleConfiguration) rule.getConfiguration()).getBindingTableGroups().size());
}

private void fillRows(final Collection<LocalDataQueryResultRow> result, final String ruleName, final String databaseName, final int count) {
result.add(new LocalDataQueryResultRow(ruleName, databaseName, count));
protected Collection<LocalDataQueryResultRow> generateRows(final ShardingRule rule, final String databaseName) {
return Arrays.asList(new LocalDataQueryResultRow("sharding_table", databaseName, rule.getTableRules().size()),
new LocalDataQueryResultRow("sharding_table_reference", databaseName, ((ShardingRuleConfiguration) rule.getConfiguration()).getBindingTableGroups().size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.distsql.handler.type.rql;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.statement.rql.RQLStatement;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

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

/**
* Count RQL executor.
*
* @param <T> type of RQL statement
* @param <R> type of ShardingSphere rule
*/
@RequiredArgsConstructor
public abstract class CountRQLExecutor<T extends RQLStatement, R extends ShardingSphereRule> implements RQLExecutor<T> {

private final Class<R> ruleClass;

@Override
public final Collection<String> getColumnNames() {
return Arrays.asList("rule_name", "database", "count");
}

@Override
public final Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final T sqlStatement) {
Optional<R> rule = database.getRuleMetaData().findSingleRule(ruleClass);
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
rule.ifPresent(optional -> result.addAll(generateRows(optional, database.getName())));
return result;
}

protected abstract Collection<LocalDataQueryResultRow> generateRows(R rule, String databaseName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,26 @@

package org.apache.shardingsphere.single.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.distsql.handler.type.rql.CountRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.single.distsql.statement.rql.CountSingleTableStatement;
import org.apache.shardingsphere.single.rule.SingleRule;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

/**
* Count single table executor.
*/
public final class CountSingleTableExecutor implements RQLExecutor<CountSingleTableStatement> {
public final class CountSingleTableExecutor extends CountRQLExecutor<CountSingleTableStatement, SingleRule> {

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("database", "count");
public CountSingleTableExecutor() {
super(SingleRule.class);
}

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final CountSingleTableStatement sqlStatement) {
SingleRule rule = database.getRuleMetaData().getSingleRule(SingleRule.class);
return Collections.singleton(new LocalDataQueryResultRow(database.getName(), rule.getLogicTableMapper().getTableNames().size()));
protected Collection<LocalDataQueryResultRow> generateRows(final SingleRule rule, final String databaseName) {
return Collections.singleton(new LocalDataQueryResultRow("single", databaseName, rule.getLogicTableMapper().getTableNames().size()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ void assertGetRowData() {
assertThat(actual.size(), is(1));
Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
LocalDataQueryResultRow row = iterator.next();
assertThat(row.getCell(1), is("db_1"));
assertThat(row.getCell(2), is(2));
assertThat(row.getCell(1), is("single"));
assertThat(row.getCell(2), is("db_1"));
assertThat(row.getCell(3), is(2));
}

@Test
void assertGetColumnNames() {
Collection<String> columns = new CountSingleTableExecutor().getColumnNames();
assertThat(columns.size(), is(2));
assertThat(columns.size(), is(3));
Iterator<String> iterator = columns.iterator();
assertThat(iterator.next(), is("rule_name"));
assertThat(iterator.next(), is("database"));
assertThat(iterator.next(), is("count"));
}
Expand Down

0 comments on commit cd6ff45

Please sign in to comment.