Skip to content

Commit 9032ccb

Browse files
committed
compatible DatabaseSet
1 parent ac45e99 commit 9032ccb

File tree

15 files changed

+229
-63
lines changed

15 files changed

+229
-63
lines changed

dal-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.ctrip.platform</groupId>
66
<artifactId>dal-client</artifactId>
7-
<version>2.0.3</version>
7+
<version>2.0.4</version>
88

99
<properties>
1010
<java.version>1.8</java.version>

dal-client/src/main/java/com/ctrip/platform/dal/dao/configure/ClusterDatabaseSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* @author c7ch23en
2121
*/
22-
public class ClusterDatabaseSet implements DatabaseSet {
22+
public class ClusterDatabaseSet extends DatabaseSet {
2323

2424
private String databaseSetName;
2525
private Cluster cluster;

dal-client/src/main/java/com/ctrip/platform/dal/dao/configure/DatabaseSet.java

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,81 @@
1414
import com.ctrip.platform.dal.exceptions.DalException;
1515
import com.ctrip.platform.dal.sharding.idgen.IIdGeneratorConfig;
1616

17-
public interface DatabaseSet {
18-
19-
String getName();
20-
21-
String getProvider();
22-
23-
DatabaseCategory getDatabaseCategory();
24-
25-
boolean isShardingSupported();
26-
27-
boolean isTableShardingSupported(String tableName);
28-
29-
Map<String, DataBase> getDatabases();
30-
31-
void validate(String shard) throws SQLException;
32-
33-
Set<String> getAllShards();
34-
35-
Set<String> getAllTableShards(String tableName) throws SQLException;
36-
37-
DalShardingStrategy getStrategy() throws SQLException;
38-
39-
List<DataBase> getMasterDbs();
40-
41-
List<DataBase> getSlaveDbs();
42-
43-
List<DataBase> getMasterDbs(String shard);
44-
45-
List<DataBase> getSlaveDbs(String shard);
46-
47-
IIdGeneratorConfig getIdGenConfig();
17+
public abstract class DatabaseSet implements IDatabaseSet {
18+
19+
@Override
20+
public String getName() {
21+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
22+
}
23+
24+
@Override
25+
public String getProvider() {
26+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
27+
}
28+
29+
@Override
30+
public DatabaseCategory getDatabaseCategory() {
31+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
32+
}
33+
34+
@Override
35+
public boolean isShardingSupported() {
36+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
37+
}
38+
39+
@Override
40+
public boolean isTableShardingSupported(String tableName) {
41+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
42+
}
43+
44+
@Override
45+
public Map<String, DataBase> getDatabases() {
46+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
47+
}
48+
49+
@Override
50+
public void validate(String shard) throws SQLException {
51+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
52+
}
53+
54+
@Override
55+
public Set<String> getAllShards() {
56+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
57+
}
58+
59+
@Override
60+
public Set<String> getAllTableShards(String tableName) throws SQLException {
61+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
62+
}
63+
64+
@Override
65+
public DalShardingStrategy getStrategy() throws SQLException {
66+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
67+
}
68+
69+
@Override
70+
public List<DataBase> getMasterDbs() {
71+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
72+
}
73+
74+
@Override
75+
public List<DataBase> getSlaveDbs() {
76+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
77+
}
78+
79+
@Override
80+
public List<DataBase> getMasterDbs(String shard) {
81+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
82+
}
83+
84+
@Override
85+
public List<DataBase> getSlaveDbs(String shard) {
86+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
87+
}
88+
89+
@Override
90+
public IIdGeneratorConfig getIdGenConfig() {
91+
throw new UnsupportedOperationException("This is an abstract DatabaseSet.");
92+
}
4893

4994
}

dal-client/src/main/java/com/ctrip/platform/dal/dao/configure/DefaultDatabaseSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import com.ctrip.platform.dal.exceptions.DalException;
1515
import com.ctrip.platform.dal.sharding.idgen.IIdGeneratorConfig;
1616

17-
public class DefaultDatabaseSet implements DatabaseSet {
17+
public class DefaultDatabaseSet extends DatabaseSet {
1818
private static final String CLASS = "class";
1919
private static final String ENTRY_SEPARATOR = ";";
2020
private static final String KEY_VALUE_SEPARATOR = "=";
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.ctrip.platform.dal.dao.configure;
2+
3+
import com.ctrip.platform.dal.common.enums.DatabaseCategory;
4+
import com.ctrip.platform.dal.dao.strategy.DalShardingStrategy;
5+
import com.ctrip.platform.dal.sharding.idgen.IIdGeneratorConfig;
6+
7+
import java.sql.SQLException;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
public interface IDatabaseSet {
13+
14+
String getName();
15+
16+
String getProvider();
17+
18+
DatabaseCategory getDatabaseCategory();
19+
20+
boolean isShardingSupported();
21+
22+
boolean isTableShardingSupported(String tableName);
23+
24+
Map<String, DataBase> getDatabases();
25+
26+
void validate(String shard) throws SQLException;
27+
28+
Set<String> getAllShards();
29+
30+
Set<String> getAllTableShards(String tableName) throws SQLException;
31+
32+
DalShardingStrategy getStrategy() throws SQLException;
33+
34+
List<DataBase> getMasterDbs();
35+
36+
List<DataBase> getSlaveDbs();
37+
38+
List<DataBase> getMasterDbs(String shard);
39+
40+
List<DataBase> getSlaveDbs(String shard);
41+
42+
IIdGeneratorConfig getIdGenConfig();
43+
44+
}

dal-client/src/main/java/com/ctrip/platform/dal/dao/datasource/DataSourceLocator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public DataSource getDataSource(DataSourceIdentity id) {
7777

7878
public void removeDataSource(DataSourceIdentity id) {
7979
DataSource ds = cache.remove(id);
80+
provider.unregister(id);
8081
if (ds instanceof RefreshableDataSource) {
8182
((RefreshableDataSource) ds).close();
8283
}
@@ -96,8 +97,7 @@ private DataSource createDataSource(DataSourceIdentity id) throws SQLException {
9697
return ds;
9798
}
9899

99-
public void setup(Cluster cluster) {
100-
}
100+
public void setup(Cluster cluster) {}
101101

102102
public static Map<String, Integer> getActiveConnectionNumber() {
103103
Map<String, Integer> map = new HashMap<>();

dal-client/src/main/java/com/ctrip/platform/dal/dao/task/InsertTaskAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void processIdentityField(DalHints hints, List<Map<String, ?>> pojos) {
139139
if (identityInsertDisabled || null == pojo.get(identityFieldName)) {
140140
Number id = idGenerator.nextId();
141141
checkIdentityTypes(identityFieldType, id);
142-
pojo.put(identityFieldName, idGenerator.nextId());
142+
pojo.put(identityFieldName, id);
143143
}
144144
}
145145
}

dal-cluster-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.ctrip.framework.dal</groupId>
77
<artifactId>dal-cluster-client</artifactId>
8-
<version>2.0.3</version>
8+
<version>2.0.4</version>
99
<!-- TODO: mvn package sources & javadoc -->
1010
<properties>
1111
<java.version>1.8</java.version>

dal-cluster-client/src/main/java/com/ctrip/framework/dal/cluster/client/config/ClusterConfigXMLConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface ClusterConfigXMLConstants {
1313
String DATABASE = "Database";
1414
String SHARD_STRATEGIES = "ShardStrategies";
1515
String MOD_STRATEGY = "ModStrategy";
16+
String USER_HINT_STRATEGY = "UserHintStrategy";
1617
String CUSTOM_STRATEGY = "CustomStrategy";
1718
String PROPERTY = "Property";
1819
String TABLES = "Tables";

dal-cluster-client/src/main/java/com/ctrip/framework/dal/cluster/client/config/ClusterConfigXMLParser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.ctrip.framework.dal.cluster.client.sharding.idgen.ClusterIdGeneratorConfig;
99
import com.ctrip.framework.dal.cluster.client.sharding.strategy.ModShardStrategy;
1010
import com.ctrip.framework.dal.cluster.client.sharding.strategy.ShardStrategy;
11+
import com.ctrip.framework.dal.cluster.client.sharding.strategy.UserHintStrategy;
1112
import com.ctrip.framework.dal.cluster.client.util.ServiceLoaderUtils;
1213
import com.ctrip.framework.dal.cluster.client.util.StringUtils;
1314
import org.w3c.dom.Document;
@@ -139,6 +140,8 @@ private void setAttributesForDatabase(DatabaseConfigImpl databaseConfig, Node da
139140
private void parseShardStrategies(ClusterConfigImpl clusterConfig, Node strategiesNode) {
140141
for (Node modStrategyNode : getChildNodes(strategiesNode, MOD_STRATEGY))
141142
parseModStrategy(clusterConfig, modStrategyNode);
143+
for (Node modStrategyNode : getChildNodes(strategiesNode, USER_HINT_STRATEGY))
144+
parseUserHintStrategy(clusterConfig, modStrategyNode);
142145
for (Node customStrategyNode : getChildNodes(strategiesNode, CUSTOM_STRATEGY))
143146
parseCustomStrategy(clusterConfig, customStrategyNode);
144147
}
@@ -147,6 +150,10 @@ private void parseModStrategy(ClusterConfigImpl clusterConfig, Node strategyNode
147150
parseShardStrategy(clusterConfig, strategyNode, new ModShardStrategy());
148151
}
149152

153+
private void parseUserHintStrategy(ClusterConfigImpl clusterConfig, Node strategyNode) {
154+
parseShardStrategy(clusterConfig, strategyNode, new UserHintStrategy());
155+
}
156+
150157
private void parseCustomStrategy(ClusterConfigImpl clusterConfig, Node strategyNode) {
151158
String className = getAttribute(strategyNode, CLASS);
152159
try {

0 commit comments

Comments
 (0)