Skip to content

Commit

Permalink
Add AlgorithmMetaDataQueryResultRows (#29087)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 19, 2023
1 parent a76d0d7 commit 5baf887
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,22 @@
* limitations under the License.
*/

package org.apache.shardingsphere.data.pipeline.common.pojo;
package org.apache.shardingsphere.infra.database.core.spi;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;

import java.util.Collection;

/**
* Data consistency check algorithm info.
* Database supported SPI.
*/
@RequiredArgsConstructor
@Getter
public final class DataConsistencyCheckAlgorithmInfo {
public interface DatabaseSupportedTypedSPI extends TypedSPI {

private final String type;

private final String typeAliases;

private final Collection<DatabaseType> supportedDatabaseTypes;

private final String description;
/**
* Get supported database types.
*
* @return supported database types
*/
Collection<DatabaseType> getSupportedDatabaseTypes();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.ral.query.algorithm;

import org.apache.shardingsphere.infra.algorithm.ShardingSphereAlgorithm;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseSupportedTypedSPI;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.annotation.SPIDescription;

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

/**
* Algorithm meta data query result row.
*/
public final class AlgorithmMetaDataQueryResultRow {

private final boolean containsDatabaseTypes;

private final String type;

private final String typeAliases;

private final String supportedDatabaseTypes;

private final String description;

public AlgorithmMetaDataQueryResultRow(final ShardingSphereAlgorithm algorithm) {
containsDatabaseTypes = algorithm instanceof DatabaseSupportedTypedSPI;
type = String.valueOf(algorithm.getType());
typeAliases = algorithm.getTypeAliases().stream().map(Object::toString).collect(Collectors.joining(","));
supportedDatabaseTypes = containsDatabaseTypes
? getSupportedDatabaseTypes(((DatabaseSupportedTypedSPI) algorithm).getSupportedDatabaseTypes()).stream().map(DatabaseType::getType).collect(Collectors.joining(","))
: "";
SPIDescription description = algorithm.getClass().getAnnotation(SPIDescription.class);
this.description = null == description ? "" : description.value();
}

private Collection<DatabaseType> getSupportedDatabaseTypes(final Collection<DatabaseType> supportedDatabaseTypes) {
return supportedDatabaseTypes.isEmpty() ? ShardingSphereServiceLoader.getServiceInstances(DatabaseType.class) : supportedDatabaseTypes;
}

/**
* To local data query result row.
*
* @return local data query result row
*/
public LocalDataQueryResultRow toLocalDataQueryResultRow() {
return containsDatabaseTypes ? new LocalDataQueryResultRow(type, typeAliases, supportedDatabaseTypes, description) : new LocalDataQueryResultRow(type, typeAliases, description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.ral.query.algorithm;

import org.apache.shardingsphere.infra.algorithm.ShardingSphereAlgorithm;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseSupportedTypedSPI;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;

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

/**
* Algorithm meta data query result rows.
*/
public final class AlgorithmMetaDataQueryResultRows {

private final boolean containsDatabaseTypes;

private final Collection<AlgorithmMetaDataQueryResultRow> rows;

public AlgorithmMetaDataQueryResultRows(final Class<? extends ShardingSphereAlgorithm> algorithmClass) {
containsDatabaseTypes = DatabaseSupportedTypedSPI.class.isAssignableFrom(algorithmClass);
rows = ShardingSphereServiceLoader.getServiceInstances(algorithmClass).stream().map(AlgorithmMetaDataQueryResultRow::new).collect(Collectors.toList());
}

/**
* Get rows.
*
* @return rows
*/
public Collection<LocalDataQueryResultRow> getRows() {
return rows.stream().map(AlgorithmMetaDataQueryResultRow::toLocalDataQueryResultRow).collect(Collectors.toList());
}

/**
* Get column names.
*
* @return column names
*/
public Collection<String> getColumnNames() {
return containsDatabaseTypes ? Arrays.asList("type", "type_aliases", "supported_database_types", "description") : Arrays.asList("type", "type_aliases", "description");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
package org.apache.shardingsphere.data.pipeline.core.consistencycheck.table;

import org.apache.shardingsphere.infra.algorithm.ShardingSphereAlgorithm;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;

import java.util.Collection;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseSupportedTypedSPI;

/**
* Table data consistency checker.
*/
public interface TableDataConsistencyChecker extends ShardingSphereAlgorithm, AutoCloseable {
public interface TableDataConsistencyChecker extends ShardingSphereAlgorithm, DatabaseSupportedTypedSPI, AutoCloseable {

/**
* Build table inventory checker.
Expand All @@ -44,13 +42,6 @@ default boolean isBreakOnInventoryCheckNotMatched() {
return true;
}

/**
* Get supported database types.
*
* @return supported database types
*/
Collection<DatabaseType> getSupportedDatabaseTypes();

@Override
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,29 @@

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

import org.apache.shardingsphere.data.pipeline.common.pojo.DataConsistencyCheckAlgorithmInfoRegistry;
import org.apache.shardingsphere.data.pipeline.core.consistencycheck.table.TableDataConsistencyChecker;
import org.apache.shardingsphere.distsql.handler.ral.query.QueryableRALExecutor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.distsql.handler.ral.query.algorithm.AlgorithmMetaDataQueryResultRows;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.migration.distsql.statement.ShowMigrationCheckAlgorithmsStatement;

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

/**
* Show migration check algorithms' executor.
*/
public final class ShowMigrationCheckAlgorithmsExecutor implements QueryableRALExecutor<ShowMigrationCheckAlgorithmsStatement> {

private final AlgorithmMetaDataQueryResultRows algorithmMetaDataQueryResultRows = new AlgorithmMetaDataQueryResultRows(TableDataConsistencyChecker.class);

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShowMigrationCheckAlgorithmsStatement sqlStatement) {
return DataConsistencyCheckAlgorithmInfoRegistry.getAllAlgorithmInfos().stream().map(
each -> new LocalDataQueryResultRow(each.getType(), each.getTypeAliases(),
each.getSupportedDatabaseTypes().stream().map(DatabaseType::getType).collect(Collectors.joining(",")), each.getDescription()))
.collect(Collectors.toList());
return algorithmMetaDataQueryResultRows.getRows();
}

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("type", "type_aliases", "supported_database_types", "description");
return algorithmMetaDataQueryResultRows.getColumnNames();
}

@Override
Expand Down

0 comments on commit 5baf887

Please sign in to comment.