Skip to content

Commit bcf7ed8

Browse files
committed
Store sharded tables in a map
1 parent abf6266 commit bcf7ed8

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/test/java/examples/sharding/ShardingTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.io.InputStreamReader;
2727
import java.sql.Connection;
2828
import java.sql.DriverManager;
29+
import java.util.HashMap;
30+
import java.util.Map;
2931

3032
import examples.sharding.TableCodesDynamicSqlSupport.TableCodes;
3133
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
@@ -45,9 +47,11 @@
4547
class ShardingTest {
4648
private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
4749
private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
50+
private final Map<String, TableCodes> shards = new HashMap<>();
4851

4952
private SqlSessionFactory sqlSessionFactory;
5053

54+
5155
@BeforeEach
5256
void setup() throws Exception {
5357
Class.forName(JDBC_DRIVER);
@@ -151,16 +155,14 @@ private SelectStatementProvider buildSelect(int id) {
151155
}
152156

153157
private TableCodes calculateTable(int id) {
154-
// it might be better to lookup instances in a Map rather than creating a new instance
155-
// every time
156-
return tableCodes.withName(calculateTableName(id));
157-
}
158-
159-
private String calculateTableName(int id) {
160158
if (id % 2 == 0) {
161-
return "tableCodes_even";
159+
return shards.computeIfAbsent("even", this::newTableInstance);
162160
} else {
163-
return "tableCodes_odd";
161+
return shards.computeIfAbsent("odd", this::newTableInstance);
164162
}
165163
}
164+
165+
private TableCodes newTableInstance(String suffix) {
166+
return tableCodes.withName("tableCodes_" + suffix);
167+
}
166168
}

src/test/kotlin/examples/kotlin/mybatis3/sharding/KShardingTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
2929
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
3030
class KShardingTest {
3131
private lateinit var sqlSessionFactory: SqlSessionFactory
32+
private val shards = mutableMapOf<String, KTableCodesTableDynamicSQLSupport.TableCodes>()
3233

3334
@BeforeAll
3435
fun setup() {
@@ -131,13 +132,12 @@ class KShardingTest {
131132
return selectOneString(selectStatement)
132133
}
133134

134-
private fun calculateTable(id: Int) = tableCodes.withName(calculateTableName(id))
135-
136-
private fun calculateTableName(id: Int): String {
137-
return if (id.mod(2) == 0) {
138-
"tableCodes_even"
135+
private fun calculateTable(id: Int) =
136+
if (id % 2 == 0) {
137+
shards.computeIfAbsent("even", this::newTableInstance)
139138
} else {
140-
"tableCodes_odd"
139+
shards.computeIfAbsent("odd", this::newTableInstance)
141140
}
142-
}
141+
142+
private fun newTableInstance(suffix: String)= tableCodes.withName("tableCodes_$suffix")
143143
}

0 commit comments

Comments
 (0)