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.data.pipeline.core.metadata.model;
19  
20  import lombok.EqualsAndHashCode;
21  import lombok.Getter;
22  import lombok.NonNull;
23  import lombok.ToString;
24  import lombok.extern.slf4j.Slf4j;
25  import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
26  
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Optional;
33  import java.util.stream.Collectors;
34  
35  /**
36   * Pipeline table meta data.
37   */
38  @EqualsAndHashCode(of = "name")
39  @ToString
40  @Slf4j
41  public final class PipelineTableMetaData {
42      
43      @NonNull
44      private final String name;
45      
46      private final Map<ShardingSphereIdentifier, PipelineColumnMetaData> columnMetaDataMap;
47      
48      @Getter
49      private final List<String> columnNames;
50      
51      @Getter
52      private final List<String> primaryKeyColumns;
53      
54      @Getter
55      private final Collection<PipelineIndexMetaData> uniqueIndexes;
56      
57      public PipelineTableMetaData(final String name, final Map<ShardingSphereIdentifier, PipelineColumnMetaData> columnMetaDataMap, final Collection<PipelineIndexMetaData> uniqueIndexes) {
58          this.name = name;
59          this.columnMetaDataMap = columnMetaDataMap;
60          List<PipelineColumnMetaData> columnMetaDataList = new ArrayList<>(columnMetaDataMap.values());
61          Collections.sort(columnMetaDataList);
62          columnNames = Collections.unmodifiableList(columnMetaDataList.stream().map(PipelineColumnMetaData::getName).collect(Collectors.toList()));
63          Optional<PipelineIndexMetaData> primaryKeyMetaData = uniqueIndexes.stream().filter(PipelineIndexMetaData::isPrimaryKey).findFirst();
64          primaryKeyColumns = primaryKeyMetaData.map(each -> each.getColumns().stream().map(PipelineColumnMetaData::getName).collect(Collectors.toList()))
65                  .orElseGet(() -> Collections.unmodifiableList(columnMetaDataList.stream().filter(PipelineColumnMetaData::isPrimaryKey)
66                          .map(PipelineColumnMetaData::getName).collect(Collectors.toList())));
67          this.uniqueIndexes = Collections.unmodifiableCollection(uniqueIndexes);
68      }
69      
70      /**
71       * Get column meta data.
72       *
73       * @param columnIndex the first column is 1, the second is 2, ...
74       * @return column meta data
75       */
76      public PipelineColumnMetaData getColumnMetaData(final int columnIndex) {
77          return getColumnMetaData(columnNames.get(columnIndex - 1));
78      }
79      
80      /**
81       * Get column meta data.
82       *
83       * @param columnName column name
84       * @return column meta data
85       */
86      public PipelineColumnMetaData getColumnMetaData(final String columnName) {
87          PipelineColumnMetaData result = columnMetaDataMap.get(new ShardingSphereIdentifier(columnName));
88          if (null == result) {
89              log.warn("Can not get column meta data for column name '{}', columnNames={}", columnName, columnNames);
90          }
91          return result;
92      }
93  }