1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
39
40 @RequiredArgsConstructor
41 public final class DataSourceNodePersistService 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.getDataSourceNodeVersionsNode(databaseName, entry.getKey()));
52 repository.persist(DataSourceMetaDataNode.getDataSourceNodeVersionNode(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.getDataSourceNodeActiveVersionNode(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.getDataSourceNodesNode(databaseName))) {
66 String dataSourceValue = repository.getDirectly(DataSourceMetaDataNode.getDataSourceNodeVersionNode(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.getDataSourceNodeVersionNode(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.getDataSourceNodeNode(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.getDataSourceNodeNode(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.getDataSourceNodeVersionsNode(databaseName, entry.getKey()));
106 String nextActiveVersion = versions.isEmpty() ? DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1);
107 repository.persist(DataSourceMetaDataNode.getDataSourceNodeVersionNode(databaseName, entry.getKey(), nextActiveVersion),
108 YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue())));
109 if (Strings.isNullOrEmpty(getDataSourceActiveVersion(databaseName, entry.getKey()))) {
110 repository.persist(DataSourceMetaDataNode.getDataSourceNodeActiveVersionNode(databaseName, entry.getKey()), DEFAULT_VERSION);
111 }
112 result.add(new MetaDataVersion(DataSourceMetaDataNode.getDataSourceNodeNode(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.getDataSourceNodeActiveVersionNode(databaseName, dataSourceName));
119 }
120 }