use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testCrossShardInsert.
@Test
public void testCrossShardInsert() {
if (!diff.supportInsertValues)
return;
try {
int res = 0;
deleteAllShardsByDb(dao, mod);
ClientTestModel p = new ClientTestModel();
List<ClientTestModel> pList = create6Entities();
assertEquals(0, getCountByDb(dao, 0));
assertEquals(0, getCountByDb(dao, 1));
KeyHolder keyholder = createKeyHolder();
DalHints hints = new DalHints();
res = dao.combinedInsert(hints, keyholder, pList);
assertResEquals(6, res);
assertEquals(3, getCountByDb(dao, 0));
assertEquals(3, getCountByDb(dao, 1));
assertKeyHolderCrossShard(keyholder);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testCrossShardInsertCallback.
@Test
public void testCrossShardInsertCallback() {
if (!diff.supportInsertValues)
return;
try {
int res = 0;
deleteAllShardsByDb(dao, mod);
ClientTestModel p = new ClientTestModel();
List<ClientTestModel> pList = create6Entities();
assertEquals(0, getCountByDb(dao, 0));
assertEquals(0, getCountByDb(dao, 1));
KeyHolder keyholder = createKeyHolder();
IntCallback callback = new IntCallback();
DalHints hints = new DalHints().callbackWith(callback);
res = dao.combinedInsert(hints, keyholder, pList);
assertEquals(0, res);
res = callback.getInt();
assertResEquals(6, res);
assertEquals(3, getCountByDb(dao, 0));
assertEquals(3, getCountByDb(dao, 1));
assertKeyHolderCrossShard(keyholder);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
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 BaseDalTableDaoShardByDbTest method testInsertSingleWithPkInsertBack.
/**
* Test Insert multiple entities one by one
* @throws SQLException
*/
@Test
public void testInsertSingleWithPkInsertBack() throws SQLException {
if (!INSERT_PK_BACK_ALLOWED)
return;
ClientTestModel model = new ClientTestModel();
model.setQuantity(10 + 1 % 3);
model.setType(((Number) (1 % 3)).shortValue());
model.setAddress("CTRIP");
int res;
for (int i = 0; i < mod; i++) {
int j = 1;
// By shard
model.setId(-1);
model.setAddress("CTRIP" + j++);
res = dao.insert(new DalHints().inShard(i).setIdentityBack().setKeyHolder(new KeyHolder()), model);
assertTrue(model.getId() > 0);
assertEquals(dao.queryByPk(model, new DalHints().inShard(i)).getAddress(), model.getAddress());
// By shardValue
model.setId(-1);
model.setAddress("CTRIP" + j++);
res = dao.insert(new DalHints().setShardValue(i).setIdentityBack().setKeyHolder(new KeyHolder()), model);
assertTrue(model.getId() > 0);
assertEquals(dao.queryByPk(model, new DalHints().setShardValue(i)).getAddress(), model.getAddress());
// By shardColValue
model.setId(-1);
model.setAddress("CTRIP" + j++);
res = dao.insert(new DalHints().setShardColValue("index", i).setIdentityBack().setKeyHolder(new KeyHolder()), model);
assertTrue(model.getId() > 0);
assertEquals(dao.queryByPk(model, new DalHints().setShardColValue("index", i)).getAddress(), model.getAddress());
// By shardColValue
model.setId(-1);
model.setAddress("CTRIP" + j++);
dao.insert(new DalHints().setShardColValue("tableIndex", i).setIdentityBack().setKeyHolder(new KeyHolder()), model);
assertTrue(model.getId() > 0);
assertEquals(dao.queryByPk(model, new DalHints().setShardColValue("tableIndex", i)).getAddress(), model.getAddress());
// By fields
model.setId(-1);
model.setAddress("CTRIP" + j++);
model.setTableIndex(i);
res = dao.insert(new DalHints().setIdentityBack().setKeyHolder(new KeyHolder()), model);
assertTrue(model.getId() > 0);
}
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testCombinedInsert.
/**
* Test Insert multiple entities with one SQL Statement
* @throws SQLException
*/
@Test
public void testCombinedInsert() throws SQLException {
if (!diff.supportInsertValues)
return;
List<ClientTestModel> entities = new ArrayList<>();
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.combinedInsert(new DalHints(), holder, entities);
fail();
} catch (Exception e) {
}
for (int i = 0; i < mod; i++) {
int j = 1;
holder = null;
// By shard
holder = createKeyHolder();
res = dao.combinedInsert(new DalHints().inShard(i), holder, entities);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertKeyHolder(holder);
// By shardValue
holder = createKeyHolder();
res = dao.combinedInsert(new DalHints().setShardValue(i), holder, entities);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertKeyHolder(holder);
// By shardColValue
holder = createKeyHolder();
res = dao.combinedInsert(new DalHints().setShardColValue("index", i), holder, entities);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertKeyHolder(holder);
// By shardColValue
holder = createKeyHolder();
res = dao.combinedInsert(new DalHints().setShardColValue("tableIndex", i), holder, entities);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertKeyHolder(holder);
}
// For combined insert, the shard id must be defined or change bd deduced.
}
Aggregations