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.metadata.persist.service.config.database.datasource;
19  
20  import com.google.common.base.Strings;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
23  import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
24  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
25  import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
26  import org.apache.shardingsphere.metadata.persist.node.metadata.DataSourceMetaDataNode;
27  import org.apache.shardingsphere.metadata.persist.service.config.database.DatabaseBasedPersistService;
28  import org.apache.shardingsphere.mode.spi.PersistRepository;
29  
30  import java.util.Collection;
31  import java.util.LinkedHashMap;
32  import java.util.LinkedList;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Map.Entry;
36  
37  /**
38   * Data source unit persist service.
39   */
40  @RequiredArgsConstructor
41  public final class DataSourceUnitPersistService implements DatabaseBasedPersistService<Map<String, DataSourcePoolProperties>> {
42      
43      private static final String DEFAULT_VERSION = "0";
44      
45      private final PersistRepository repository;
46      
47      @Override
48      public void persist(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
49          for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
50              String activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey());
51              List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey()));
52              repository.persist(DataSourceMetaDataNode.getDataSourceUnitVersionNode(databaseName, entry.getKey(), versions.isEmpty()
53                      ? DEFAULT_VERSION
54                      : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue())));
55              if (Strings.isNullOrEmpty(activeVersion)) {
56                  repository.persist(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION);
57              }
58          }
59      }
60      
61      @SuppressWarnings("unchecked")
62      @Override
63      public Map<String, DataSourcePoolProperties> load(final String databaseName) {
64          Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>();
65          for (String each : repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitsNode(databaseName))) {
66              String dataSourceValue = repository.getDirectly(DataSourceMetaDataNode.getDataSourceUnitVersionNode(databaseName, each, getDataSourceActiveVersion(databaseName, each)));
67              if (!Strings.isNullOrEmpty(dataSourceValue)) {
68                  result.put(each, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class)));
69              }
70          }
71          return result;
72      }
73      
74      @SuppressWarnings("unchecked")
75      @Override
76      public Map<String, DataSourcePoolProperties> load(final String databaseName, final String name) {
77          Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>();
78          String dataSourceValue = repository.getDirectly(DataSourceMetaDataNode.getDataSourceUnitVersionNode(databaseName, name, getDataSourceActiveVersion(databaseName, name)));
79          if (!Strings.isNullOrEmpty(dataSourceValue)) {
80              result.put(name, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class)));
81          }
82          return result;
83      }
84      
85      @Override
86      public void delete(final String databaseName, final String name) {
87          repository.delete(DataSourceMetaDataNode.getDataSourceUnitNode(databaseName, name));
88      }
89      
90      @Override
91      public Collection<MetaDataVersion> deleteConfigurations(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
92          Collection<MetaDataVersion> result = new LinkedList<>();
93          for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
94              String delKey = DataSourceMetaDataNode.getDataSourceUnitNode(databaseName, entry.getKey());
95              repository.delete(delKey);
96              result.add(new MetaDataVersion(delKey));
97          }
98          return result;
99      }
100     
101     @Override
102     public Collection<MetaDataVersion> persistConfigurations(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
103         Collection<MetaDataVersion> result = new LinkedList<>();
104         for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
105             List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey()));
106             String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
107             repository.persist(DataSourceMetaDataNode.getDataSourceUnitVersionNode(databaseName, entry.getKey(), nextActiveVersion),
108                     YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue())));
109             if (Strings.isNullOrEmpty(getDataSourceActiveVersion(databaseName, entry.getKey()))) {
110                 repository.persist(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION);
111             }
112             result.add(new MetaDataVersion(DataSourceMetaDataNode.getDataSourceUnitNode(databaseName, entry.getKey()), getDataSourceActiveVersion(databaseName, entry.getKey()), nextActiveVersion));
113         }
114         return result;
115     }
116     
117     private String getDataSourceActiveVersion(final String databaseName, final String dataSourceName) {
118         return repository.getDirectly(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, dataSourceName));
119     }
120 }