Skip to content

Commit

Permalink
remove useless code: MasterSlaveDataSource.getDataSourceName
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 2, 2017
1 parent 7371f41 commit b97d7c5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractDataSourceAdapter;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.connection.MasterSlaveConnection;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import lombok.Getter;

Expand Down Expand Up @@ -96,29 +95,6 @@ public Map<String, DataSource> getAllDataSources() {
return result;
}

/**
* Get data source name from master-slave data source.
*
* @param dataSourceName data source name
* @param sqlType SQL type
* @return data source name from master-slave data source
*/
public static String getDataSourceName(final String dataSourceName, final SQLType sqlType) {
return isMasterRoute(sqlType) ? getMasterDataSourceName(dataSourceName) : getSlaveDataSourceName(dataSourceName);
}

private static boolean isMasterRoute(final SQLType sqlType) {
return SQLType.DQL != sqlType || DML_FLAG.get() || HintManagerHolder.isMasterRouteOnly();
}

private static String getMasterDataSourceName(final String dataSourceName) {
return Joiner.on("-").join(dataSourceName, "MASTER");
}

private static String getSlaveDataSourceName(final String dataSourceName) {
return Joiner.on("-").join(dataSourceName, "SLAVE");
}

/**
* reset DML flag.
*/
Expand All @@ -143,6 +119,10 @@ public NamedDataSource getDataSource(final SQLType sqlType) {
return new NamedDataSource(selectedSourceName, result);
}

private boolean isMasterRoute(final SQLType sqlType) {
return SQLType.DQL != sqlType || DML_FLAG.get() || HintManagerHolder.isMasterRouteOnly();
}

@Override
public Connection getConnection() throws SQLException {
return new MasterSlaveConnection(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,6 @@ public void reset() {
MasterSlaveDataSource.resetDMLFlag();
}

@Test
public void assertGetDataSourceNameForSelect() {
assertThat(MasterSlaveDataSource.getDataSourceName("test_ds", SQLType.DQL), is("test_ds-SLAVE"));
}

@Test
public void assertGetDataSourceNameForInsert() {
assertThat(MasterSlaveDataSource.getDataSourceName("test_ds", SQLType.DML), is("test_ds-MASTER"));
}

@Test
public void assertGetDataSourceNameForDmlFlagIsTrue() {
masterSlaveDataSource.getDataSource(SQLType.DML);
assertThat(MasterSlaveDataSource.getDataSourceName("test_ds", SQLType.DQL), is("test_ds-MASTER"));
}

@Test
public void assertGetDataSourceNameForMasterRouteOnly() {
HintManager hintManager = HintManager.getInstance();
hintManager.setMasterRouteOnly();
assertThat(MasterSlaveDataSource.getDataSourceName("test_ds", SQLType.DQL), is("test_ds-MASTER"));
}

@Test
public void assertGetDataSourceForDML() {
assertThat(masterSlaveDataSource.getDataSource(SQLType.DML).getDataSource(), is(masterDataSource));
Expand Down

0 comments on commit b97d7c5

Please sign in to comment.