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.database.schema.model.ShardingSphereTable;
25  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
26  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
27  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
28  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
29  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
30  import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;
31  
32  import java.util.Arrays;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.LinkedHashMap;
36  import java.util.Map;
37  import java.util.Map.Entry;
38  
39  /**
40   * ShardingSphere statistics builder for PostgreSQL.
41   */
42  
43  public final class PostgreSQLShardingSphereStatisticsBuilder implements ShardingSphereStatisticsBuilder {
44      
45      private static final String SHARDING_SPHERE = "shardingsphere";
46      
47      private static final String CLUSTER_INFORMATION = "cluster_information";
48      
49      private static final String SHARDING_TABLE_STATISTICS = "sharding_table_statistics";
50      
51      private static final Map<String, Collection<String>> INIT_DATA_SCHEMA_TABLES = new LinkedHashMap<>();
52      
53      static {
54          INIT_DATA_SCHEMA_TABLES.put("pg_catalog", Arrays.asList("pg_class", "pg_namespace"));
55      }
56      
57      @Override
58      public ShardingSphereStatistics build(final ShardingSphereMetaData metaData) {
59          ShardingSphereStatistics result = new ShardingSphereStatistics();
60          for (Entry<String, ShardingSphereDatabase> entry : metaData.getDatabases().entrySet()) {
61              ShardingSphereDatabaseData databaseData = new ShardingSphereDatabaseData();
62              initSchemas(entry.getValue(), databaseData);
63              if (!databaseData.getSchemaData().isEmpty()) {
64                  result.putDatabase(entry.getKey(), databaseData);
65              }
66          }
67          return result;
68      }
69      
70      private void initSchemas(final ShardingSphereDatabase database, final ShardingSphereDatabaseData databaseData) {
71          for (Entry<String, ShardingSphereSchema> entry : database.getSchemas().entrySet()) {
72              if (SHARDING_SPHERE.equals(entry.getKey())) {
73                  ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
74                  initClusterInformationTable(schemaData);
75                  initShardingTableStatisticsTable(schemaData);
76                  databaseData.putSchema(SHARDING_SPHERE, schemaData);
77              }
78              if (INIT_DATA_SCHEMA_TABLES.containsKey(entry.getKey())) {
79                  ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
80                  initTables(entry.getValue(), INIT_DATA_SCHEMA_TABLES.get(entry.getKey()), schemaData);
81                  databaseData.putSchema(entry.getKey(), schemaData);
82              }
83          }
84      }
85      
86      private void initClusterInformationTable(final ShardingSphereSchemaData schemaData) {
87          ShardingSphereTableData tableData = new ShardingSphereTableData(CLUSTER_INFORMATION);
88          tableData.getRows().add(new ShardingSphereRowData(Collections.singletonList(ShardingSphereVersion.VERSION)));
89          schemaData.putTable(CLUSTER_INFORMATION, tableData);
90      }
91      
92      private void initShardingTableStatisticsTable(final ShardingSphereSchemaData schemaData) {
93          schemaData.putTable(SHARDING_TABLE_STATISTICS, new ShardingSphereTableData(SHARDING_TABLE_STATISTICS));
94      }
95      
96      private void initTables(final ShardingSphereSchema schema, final Collection<String> tables, final ShardingSphereSchemaData schemaData) {
97          for (Entry<String, ShardingSphereTable> entry : schema.getTables().entrySet()) {
98              if (tables.contains(entry.getValue().getName())) {
99                  ShardingSphereTableData tableData = new ShardingSphereTableData(entry.getValue().getName());
100                 schemaData.putTable(entry.getKey(), tableData);
101             }
102         }
103     }
104     
105     @Override
106     public String getDatabaseType() {
107         return "PostgreSQL";
108     }
109 }