View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.metadata.statistics.builder.dialect;
19  
20  import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
21  import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
22  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
23  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
24  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
25  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
26  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
27  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
28  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
29  import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;
30  
31  import java.util.Collections;
32  import java.util.Map.Entry;
33  
34  /**
35   * ShardingSphere statistics builder for MySQL.
36   */
37  
38  public final class MySQLShardingSphereStatisticsBuilder implements ShardingSphereStatisticsBuilder {
39      
40      private static final String SHARDING_SPHERE = "shardingsphere";
41      
42      private static final String CLUSTER_INFORMATION = "cluster_information";
43      
44      private static final String SHARDING_TABLE_STATISTICS = "sharding_table_statistics";
45      
46      @Override
47      public ShardingSphereStatistics build(final ShardingSphereMetaData metaData) {
48          ShardingSphereStatistics result = new ShardingSphereStatistics();
49          for (Entry<String, ShardingSphereDatabase> entry : metaData.getDatabases().entrySet()) {
50              ShardingSphereDatabaseData databaseData = new ShardingSphereDatabaseData();
51              initSchemas(entry.getValue(), databaseData);
52              if (!databaseData.getSchemaData().isEmpty()) {
53                  result.putDatabase(entry.getKey(), databaseData);
54              }
55          }
56          return result;
57      }
58      
59      private void initSchemas(final ShardingSphereDatabase database, final ShardingSphereDatabaseData databaseData) {
60          for (Entry<String, ShardingSphereSchema> entry : database.getSchemas().entrySet()) {
61              if (SHARDING_SPHERE.equals(entry.getKey())) {
62                  ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
63                  initClusterInformationTable(schemaData);
64                  initShardingTableStatisticsTable(schemaData);
65                  databaseData.putSchema(SHARDING_SPHERE, schemaData);
66              }
67          }
68      }
69      
70      private void initClusterInformationTable(final ShardingSphereSchemaData schemaData) {
71          ShardingSphereTableData tableData = new ShardingSphereTableData(CLUSTER_INFORMATION);
72          tableData.getRows().add(new ShardingSphereRowData(Collections.singletonList(ShardingSphereVersion.VERSION)));
73          schemaData.putTable(CLUSTER_INFORMATION, tableData);
74      }
75      
76      private void initShardingTableStatisticsTable(final ShardingSphereSchemaData schemaData) {
77          schemaData.putTable(SHARDING_TABLE_STATISTICS, new ShardingSphereTableData(SHARDING_TABLE_STATISTICS));
78      }
79      
80      @Override
81      public String getDatabaseType() {
82          return "MySQL";
83      }
84  }