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.AccessLevel;
21  import lombok.EqualsAndHashCode;
22  import lombok.Getter;
23  import lombok.NonNull;
24  import lombok.RequiredArgsConstructor;
25  import lombok.ToString;
26  import lombok.extern.slf4j.Slf4j;
27  import org.apache.shardingsphere.infra.metadata.caseinsensitive.CaseInsensitiveIdentifier;
28  
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.Collections;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.stream.Collectors;
35  
36  /**
37   * Pipeline table meta data.
38   */
39  @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
40  @Slf4j
41  @EqualsAndHashCode(of = "name")
42  @ToString
43  public final class PipelineTableMetaData {
44      
45      @NonNull
46      private final String name;
47      
48      private final Map<CaseInsensitiveIdentifier, PipelineColumnMetaData> columnMetaDataMap;
49      
50      @Getter
51      private final List<String> columnNames;
52      
53      @Getter
54      private final List<String> primaryKeyColumns;
55      
56      @Getter
57      private final Collection<PipelineIndexMetaData> uniqueIndexes;
58      
59      public PipelineTableMetaData(final String name, final Map<CaseInsensitiveIdentifier, PipelineColumnMetaData> columnMetaDataMap, final Collection<PipelineIndexMetaData> uniqueIndexes) {
60          this.name = name;
61          this.columnMetaDataMap = columnMetaDataMap;
62          List<PipelineColumnMetaData> columnMetaDataList = new ArrayList<>(columnMetaDataMap.values());
63          Collections.sort(columnMetaDataList);
64          columnNames = Collections.unmodifiableList(columnMetaDataList.stream().map(PipelineColumnMetaData::getName).collect(Collectors.toList()));
65          primaryKeyColumns = 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      // TODO Remove it. Get column meta data by column name for incremental dumper, since columns ordering might be changed.
77      public PipelineColumnMetaData getColumnMetaData(final int columnIndex) {
78          return getColumnMetaData(columnNames.get(columnIndex - 1));
79      }
80      
81      /**
82       * Get column meta data.
83       *
84       * @param columnName column name
85       * @return column meta data
86       */
87      public PipelineColumnMetaData getColumnMetaData(final String columnName) {
88          PipelineColumnMetaData result = columnMetaDataMap.get(new CaseInsensitiveIdentifier(columnName));
89          if (null == result) {
90              log.warn("Can not get column meta data for column name '{}', columnNames={}", columnName, columnNames);
91          }
92          return result;
93      }
94  }