use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testInsertMultipleAsListWithKeyHolder.
/**
* Test Insert multiple entities with key-holder
* @throws SQLException
*/
@Test
public void testInsertMultipleAsListWithKeyHolder() throws SQLException {
List<ClientTestModel> entities = new ArrayList<ClientTestModel>();
for (int i = 0; i < 3; i++) {
ClientTestModel model = new ClientTestModel();
model.setQuantity(10 + 1 % 3);
model.setType(((Number) (1 % 3)).shortValue());
model.setAddress("CTRIP");
entities.add(model);
}
KeyHolder holder = createKeyHolder();
int[] res;
try {
res = dao.insert(new DalHints(), holder, entities);
fail();
} catch (Exception e) {
}
for (int i = 0; i < mod; i++) {
int j = 1;
// By shard
holder = createKeyHolder();
res = dao.insert(new DalHints().inShard(i), holder, entities);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
// By shardValue
holder = createKeyHolder();
res = dao.insert(new DalHints().setShardValue(i), holder, entities);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
// By shardColValue
holder = createKeyHolder();
res = dao.insert(new DalHints().setShardColValue("index", i), holder, entities);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
// By shardColValue
holder = createKeyHolder();
res = dao.insert(new DalHints().setShardColValue("tableIndex", i), holder, entities);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
// By fields same shard
holder = createKeyHolder();
entities.get(0).setTableIndex(i);
entities.get(1).setTableIndex(i);
entities.get(2).setTableIndex(i);
res = dao.insert(new DalHints(), holder, entities);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
}
deleteAllShardsByDb(dao, mod);
// By fields not same shard
holder = createKeyHolder();
entities.get(0).setTableIndex(0);
entities.get(1).setTableIndex(1);
entities.get(2).setTableIndex(2);
res = dao.insert(new DalHints(), holder, entities);
assertResEquals(3, res);
assertEquals(2, getCountByDb(dao, 0));
assertEquals(1, getCountByDb(dao, 1));
assertResEquals(3, res);
assertKeyHolder(holder);
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class DalDirectClientTestStub method updateTestInsertWithKeyHolder.
/**
* Test the update function with key-holder
*
* @throws SQLException
*/
@Test
public void updateTestInsertWithKeyHolder() throws SQLException {
if (!diff.supportGetGeneratedKeys)
return;
String insertSql = String.format("INSERT INTO %s VALUES(NULL, 10, 1, 'SH INFO', NULL)", TABLE_NAME);
StatementParameters parameters = new StatementParameters();
KeyHolder holder = new KeyHolder();
DalHints hints = new DalHints();
int count = client.update(insertSql, parameters, hints.setKeyHolder(holder));
Assert.assertEquals(1, count);
Assert.assertEquals(1, holder.size());
Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
Assert.assertEquals(4, queryModelsByIds().size());
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class KeyHolderTest method testMergeSequential.
@Test
public void testMergeSequential() {
ExecutorService service = null;
service = new ThreadPoolExecutor(5, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
KeyHolder test = new KeyHolder();
test.setSize(6);
test.requireMerge();
KeyHolder tmpHolder = new KeyHolder();
tmpHolder.addKey(buildKey(0));
tmpHolder.addKey(buildKey(1));
tmpHolder.addKey(buildKey(2));
test.addPatial(new Integer[] { 0, 1, 2 }, tmpHolder);
tmpHolder = new KeyHolder();
tmpHolder.addKey(buildKey(3));
tmpHolder.addKey(buildKey(4));
tmpHolder.addKey(buildKey(5));
test.addPatial(new Integer[] { 3, 4, 5 }, tmpHolder);
try {
int i = 0;
for (Number value : test.getIdList()) {
assertEquals(i++, value);
}
} catch (SQLException e) {
fail();
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class KeyHolderTest method testGetKeyList.
@Test
public void testGetKeyList() {
KeyHolder test = new KeyHolder();
test.addKey(buildKey(1));
assertEquals(1, test.size());
test.addKey(buildKey(1));
assertEquals(2, test.size());
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class KeyHolderTest method testMergeParallel.
@Test
public void testMergeParallel() {
ExecutorService service = null;
service = new ThreadPoolExecutor(5, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
KeyHolder test = new KeyHolder();
test.setSize(30);
test.requireMerge();
List<Future<Boolean>> fList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
fList.add(service.submit(new KeyHolderTask(test, new Integer[] { i * 3, i * 3 + 1, i * 3 + 2 })));
}
for (Future<Boolean> f : fList) try {
f.get();
} catch (InterruptedException | ExecutionException e) {
fail();
}
assertTrue(test.isRequireMerge());
assertTrue(test.isMerged());
assertEquals(30, test.size());
try {
assertEquals(30, test.getIdList().size());
} catch (SQLException e1) {
fail();
}
try {
List<Number> ids = test.getIdList();
for (int i = 0; i < 30; i++) assertEquals(i, ids.get(i));
} catch (SQLException e) {
fail();
}
service.shutdown();
}
Aggregations