AdvancedModStrategy extends AbstractConditionStrategy { @Override public void initialize(Map<String, String> settings) { super.initialize(settings); String type = settings.get(TYPE); if(isShardByDb()) { if(!settings.containsKey(MOD)) { throw new IllegalArgumentException("Property " + MOD + " is required for shard by database"); } String zeroPaddingFormat = "%01d"; if(settings.containsKey(ZERO_PADDING)) { zeroPaddingFormat = "%0" + Integer.parseInt(settings.get(ZERO_PADDING)) + "d"; } dbLoactor = createLocator(type, Integer.parseInt(settings.get(MOD)), zeroPaddingFormat); } if(isShardByTable()) { if(!settings.containsKey(TABLE_MOD)) { throw new IllegalArgumentException("Property " + TABLE_MOD + " is required for shard by table"); } String tableZeroPaddingFormat = "%01d"; if(settings.containsKey(TABLE_ZERO_PADDING)) { tableZeroPaddingFormat = "%0" + Integer.parseInt(settings.get(TABLE_ZERO_PADDING)) + "d"; } Integer mod = Integer.parseInt(settings.get(TABLE_MOD)); tableLoactor = createLocator(type, mod, tableZeroPaddingFormat); Set<String> allShards = new HashSet<>(); for(int i = 0; i < mod; i++) { allShards.add(String.format(tableZeroPaddingFormat, i)); } setAllTableShards(allShards); } }  @Override void initialize(Map<String, String> settings); @Override Set<String> locateDbShardsByValue(ShardingContext ctx, Object shardValue); @Override Set<String> locateDbShards(ConditionContext ctx); @Override Set<String> locateTableShardsByValue(TableShardingContext ctx, Object tableShardValue); @Override Set<String> locateTableShards(TableConditionContext ctx); static final String TYPE; static final String MOD; static final String TABLE_MOD; }
@Test public void testWrongDbConfig() throws Exception { strategy = new AdvancedModStrategy(); Map<String, String> settings = new HashMap<>(); settings.put(AdvancedModStrategy.COLUMNS, columnName); try { strategy.initialize(settings); fail(); } catch (Exception e) { assertEquals(e.getClass(), IllegalArgumentException.class); } }
@Test public void testWrongTableConfig() throws Exception { strategy = new AdvancedModStrategy(); Map<String, String> settings = new HashMap<>(); settings.put(AdvancedModStrategy.TABLE_COLUMNS, columnName); try { strategy.initialize(settings); fail(); } catch (Exception e) { assertEquals(e.getClass(), IllegalArgumentException.class); } }