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;
19  
20  import com.google.common.base.Strings;
21  import org.apache.shardingsphere.data.pipeline.core.context.PipelineContextKey;
22  import org.apache.shardingsphere.data.pipeline.core.job.api.PipelineAPIFactory;
23  import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
24  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
25  import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
26  
27  import java.util.Collections;
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  import java.util.Map.Entry;
31  
32  /**
33   * Pipeline data source persist service.
34   */
35  public final class PipelineDataSourcePersistService implements PipelineMetaDataPersistService<Map<String, DataSourcePoolProperties>> {
36      
37      private final YamlDataSourceConfigurationSwapper swapper = new YamlDataSourceConfigurationSwapper();
38      
39      @Override
40      @SuppressWarnings("unchecked")
41      public Map<String, DataSourcePoolProperties> load(final PipelineContextKey contextKey, final String jobType) {
42          String dataSourcesProps = PipelineAPIFactory.getPipelineGovernanceFacade(contextKey).getMetaDataFacade().getDataSource().load(jobType);
43          if (Strings.isNullOrEmpty(dataSourcesProps)) {
44              return Collections.emptyMap();
45          }
46          Map<String, Map<String, Object>> yamlDataSources = YamlEngine.unmarshal(dataSourcesProps, Map.class);
47          Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>(yamlDataSources.size(), 1F);
48          yamlDataSources.forEach((key, value) -> result.put(key, swapper.swapToDataSourcePoolProperties(value)));
49          return result;
50      }
51      
52      @Override
53      public void persist(final PipelineContextKey contextKey, final String jobType, final Map<String, DataSourcePoolProperties> propsMap) {
54          Map<String, Map<String, Object>> dataSourceMap = new LinkedHashMap<>(propsMap.size(), 1F);
55          for (Entry<String, DataSourcePoolProperties> entry : propsMap.entrySet()) {
56              dataSourceMap.put(entry.getKey(), swapper.swapToMap(entry.getValue()));
57          }
58          PipelineAPIFactory.getPipelineGovernanceFacade(contextKey).getMetaDataFacade().getDataSource().persist(jobType, YamlEngine.marshal(dataSourceMap));
59      }
60  }