Skip to content

Commit

Permalink
Add ShardingSphereDefaultStatisticsBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 16, 2025
1 parent 885c09c commit 96a2d97
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 56 deletions.
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.infra.metadata.statistics.builder;

import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;

import java.util.Collections;

/**
* ShardingSphere default statistics builder.
*/
public final class ShardingSphereDefaultStatisticsBuilder {

private static final String SHARDINGSPHERE = "shardingsphere";

private static final String CLUSTER_INFORMATION = "cluster_information";

private static final String SHARDING_TABLE_STATISTICS = "sharding_table_statistics";

public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) {
ShardingSphereDatabaseData result = new ShardingSphereDatabaseData();
if (database.containsSchema(SHARDINGSPHERE)) {
ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
buildClusterInformationTable(schemaData);
buildShardingTableStatisticsTable(schemaData);
result.putSchema(SHARDINGSPHERE, schemaData);
}
return result;
}

private void buildClusterInformationTable(final ShardingSphereSchemaData schemaData) {
ShardingSphereTableData tableData = new ShardingSphereTableData(CLUSTER_INFORMATION);
tableData.getRows().add(new ShardingSphereRowData(Collections.singletonList(ShardingSphereVersion.VERSION)));
schemaData.putTable(CLUSTER_INFORMATION, tableData);
}

private void buildShardingTableStatisticsTable(final ShardingSphereSchemaData schemaData) {
schemaData.putTable(SHARDING_TABLE_STATISTICS, new ShardingSphereTableData(SHARDING_TABLE_STATISTICS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,20 @@

package org.apache.shardingsphere.infra.metadata.statistics.builder.dialect;

import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereDefaultStatisticsBuilder;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;

import java.util.Collections;

/**
* ShardingSphere statistics builder for MySQL.
*/

public final class MySQLShardingSphereStatisticsBuilder implements ShardingSphereStatisticsBuilder {

private static final String SHARDINGSPHERE = "shardingsphere";

private static final String CLUSTER_INFORMATION = "cluster_information";

private static final String SHARDING_TABLE_STATISTICS = "sharding_table_statistics";

@Override
public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) {
ShardingSphereDatabaseData result = new ShardingSphereDatabaseData();
if (database.containsSchema(SHARDINGSPHERE)) {
ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
initClusterInformationTable(schemaData);
initShardingTableStatisticsTable(schemaData);
result.putSchema(SHARDINGSPHERE, schemaData);
}
return result;
}

private void initClusterInformationTable(final ShardingSphereSchemaData schemaData) {
ShardingSphereTableData tableData = new ShardingSphereTableData(CLUSTER_INFORMATION);
tableData.getRows().add(new ShardingSphereRowData(Collections.singletonList(ShardingSphereVersion.VERSION)));
schemaData.putTable(CLUSTER_INFORMATION, tableData);
}

private void initShardingTableStatisticsTable(final ShardingSphereSchemaData schemaData) {
schemaData.putTable(SHARDING_TABLE_STATISTICS, new ShardingSphereTableData(SHARDING_TABLE_STATISTICS));
return new ShardingSphereDefaultStatisticsBuilder().build(database);
}

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

package org.apache.shardingsphere.infra.metadata.statistics.builder.dialect;

import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereDefaultStatisticsBuilder;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -40,12 +38,6 @@

public final class PostgreSQLShardingSphereStatisticsBuilder implements ShardingSphereStatisticsBuilder {

private static final String SHARDINGSPHERE = "shardingsphere";

private static final String CLUSTER_INFORMATION = "cluster_information";

private static final String SHARDING_TABLE_STATISTICS = "sharding_table_statistics";

private static final Map<String, Collection<String>> INIT_DATA_SCHEMA_TABLES = new LinkedHashMap<>();

static {
Expand All @@ -54,13 +46,7 @@ public final class PostgreSQLShardingSphereStatisticsBuilder implements Sharding

@Override
public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) {
ShardingSphereDatabaseData result = new ShardingSphereDatabaseData();
if (null != database.getSchema(SHARDINGSPHERE)) {
ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
initClusterInformationTable(schemaData);
initShardingTableStatisticsTable(schemaData);
result.putSchema(SHARDINGSPHERE, schemaData);
}
ShardingSphereDatabaseData result = new ShardingSphereDefaultStatisticsBuilder().build(database);
for (Entry<String, Collection<String>> entry : INIT_DATA_SCHEMA_TABLES.entrySet()) {
ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
if (null != database.getSchema(entry.getKey())) {
Expand All @@ -71,16 +57,6 @@ public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) {
return result;
}

private void initClusterInformationTable(final ShardingSphereSchemaData schemaData) {
ShardingSphereTableData tableData = new ShardingSphereTableData(CLUSTER_INFORMATION);
tableData.getRows().add(new ShardingSphereRowData(Collections.singletonList(ShardingSphereVersion.VERSION)));
schemaData.putTable(CLUSTER_INFORMATION, tableData);
}

private void initShardingTableStatisticsTable(final ShardingSphereSchemaData schemaData) {
schemaData.putTable(SHARDING_TABLE_STATISTICS, new ShardingSphereTableData(SHARDING_TABLE_STATISTICS));
}

private void initTables(final ShardingSphereSchema schema, final Collection<String> tables, final ShardingSphereSchemaData schemaData) {
for (ShardingSphereTable each : schema.getAllTables()) {
if (tables.contains(each.getName().toLowerCase())) {
Expand Down

0 comments on commit 96a2d97

Please sign in to comment.