Sharding Pattern (#1056)
* Create sharding module * Add Unit Tests * Fix readme hyperlink * Fix check-style issue
This commit is contained in:
39
sharding/src/test/java/com/iluwatar/sharding/AppTest.java
Normal file
39
sharding/src/test/java/com/iluwatar/sharding/AppTest.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.sharding;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for App class.
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.sharding;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for HashShardManager class.
|
||||
*/
|
||||
public class HashShardManagerTest {
|
||||
|
||||
private HashShardManager hashShardManager;
|
||||
|
||||
/**
|
||||
* Initialize hashShardManager instance.
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
hashShardManager = new HashShardManager();
|
||||
var shard1 = new Shard(1);
|
||||
var shard2 = new Shard(2);
|
||||
var shard3 = new Shard(3);
|
||||
hashShardManager.addNewShard(shard1);
|
||||
hashShardManager.addNewShard(shard2);
|
||||
hashShardManager.addNewShard(shard3);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStoreData() {
|
||||
var data = new Data(1, "test", Data.DataType.type1);
|
||||
hashShardManager.storeData(data);
|
||||
Assert.assertEquals(data, hashShardManager.getShardById(1).getDataById(1));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.sharding;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for LookupShardManager class.
|
||||
*/
|
||||
public class LookupShardManagerTest {
|
||||
|
||||
private LookupShardManager lookupShardManager;
|
||||
|
||||
/**
|
||||
* Initialize lookupShardManager instance.
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
lookupShardManager = new LookupShardManager();
|
||||
var shard1 = new Shard(1);
|
||||
var shard2 = new Shard(2);
|
||||
var shard3 = new Shard(3);
|
||||
lookupShardManager.addNewShard(shard1);
|
||||
lookupShardManager.addNewShard(shard2);
|
||||
lookupShardManager.addNewShard(shard3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStoreData() {
|
||||
try {
|
||||
var data = new Data(1, "test", Data.DataType.type1);
|
||||
lookupShardManager.storeData(data);
|
||||
var field = LookupShardManager.class.getDeclaredField("lookupMap");
|
||||
field.setAccessible(true);
|
||||
Map<Integer, Integer> lookupMap = (Map<Integer, Integer>) field.get(lookupShardManager);
|
||||
var shardId = lookupMap.get(1);
|
||||
var shard = lookupShardManager.getShardById(shardId);
|
||||
Assert.assertEquals(data, shard.getDataById(1));
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
Assert.fail("Fail to modify field access.");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.sharding;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for RangeShardManager class.
|
||||
*/
|
||||
public class RangeShardManagerTest {
|
||||
|
||||
private RangeShardManager rangeShardManager;
|
||||
|
||||
/**
|
||||
* Initialize rangeShardManager instance.
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
rangeShardManager = new RangeShardManager();
|
||||
var shard1 = new Shard(1);
|
||||
var shard2 = new Shard(2);
|
||||
var shard3 = new Shard(3);
|
||||
rangeShardManager.addNewShard(shard1);
|
||||
rangeShardManager.addNewShard(shard2);
|
||||
rangeShardManager.addNewShard(shard3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStoreData() {
|
||||
var data = new Data(1, "test", Data.DataType.type1);
|
||||
rangeShardManager.storeData(data);
|
||||
Assert.assertEquals(data, rangeShardManager.getShardById(1).getDataById(1));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.sharding;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for ShardManager class.
|
||||
*/
|
||||
public class ShardManagerTest {
|
||||
|
||||
private ShardManager shardManager;
|
||||
|
||||
/**
|
||||
* Initialize shardManager instance.
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
shardManager = new TestShardManager();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddNewShard() {
|
||||
try {
|
||||
var shard = new Shard(1);
|
||||
shardManager.addNewShard(shard);
|
||||
var field = ShardManager.class.getDeclaredField("shardMap");
|
||||
field.setAccessible(true);
|
||||
Map<Integer, Shard> map = (Map<Integer, Shard>) field.get(shardManager);
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertEquals(shard, map.get(1));
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
Assert.fail("Fail to modify field access.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveShardById() {
|
||||
try {
|
||||
var shard = new Shard(1);
|
||||
shardManager.addNewShard(shard);
|
||||
boolean flag = shardManager.removeShardById(1);
|
||||
var field = ShardManager.class.getDeclaredField("shardMap");
|
||||
field.setAccessible(true);
|
||||
Map<Integer, Shard> map = (Map<Integer, Shard>) field.get(shardManager);
|
||||
Assert.assertEquals(true, flag);
|
||||
Assert.assertEquals(0, map.size());
|
||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||
Assert.fail("Fail to modify field access.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetShardById() {
|
||||
Shard shard = new Shard(1);
|
||||
shardManager.addNewShard(shard);
|
||||
Shard tmpShard = shardManager.getShardById(1);
|
||||
Assert.assertEquals(shard, tmpShard);
|
||||
}
|
||||
|
||||
class TestShardManager extends ShardManager {
|
||||
|
||||
@Override
|
||||
public int storeData(Data data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int allocateShard(Data data) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
83
sharding/src/test/java/com/iluwatar/sharding/ShardTest.java
Normal file
83
sharding/src/test/java/com/iluwatar/sharding/ShardTest.java
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.sharding;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for Shard class.
|
||||
*/
|
||||
public class ShardTest {
|
||||
|
||||
private Data data;
|
||||
|
||||
private Shard shard;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
data = new Data(1, "test", Data.DataType.type1);
|
||||
shard = new Shard(1);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {}
|
||||
|
||||
@Test
|
||||
public void testStoreData() {
|
||||
try {
|
||||
shard.storeData(data);
|
||||
var field = Shard.class.getDeclaredField("dataStore");
|
||||
field.setAccessible(true);
|
||||
Map<Integer, Data> dataMap = (Map<Integer, Data>) field.get(shard);
|
||||
Assert.assertEquals(1, dataMap.size());
|
||||
Assert.assertEquals(data, dataMap.get(1));
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
Assert.fail("Fail to modify field access.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearData() {
|
||||
try {
|
||||
Map<Integer, Data> dataMap = new HashMap<>();
|
||||
dataMap.put(1, data);
|
||||
var field = Shard.class.getDeclaredField("dataStore");
|
||||
field.setAccessible(true);
|
||||
field.set(shard, dataMap);
|
||||
shard.clearData();
|
||||
dataMap = (Map<Integer, Data>) field.get(shard);
|
||||
Assert.assertEquals(0, dataMap.size());
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
Assert.fail("Fail to modify field access.");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user