use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testInsertMultipleAsListAsyncCallback.
@Test
public void testInsertMultipleAsListAsyncCallback() throws SQLException {
DalHints hints;
IntCallback callback;
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);
}
int[] res;
try {
res = dao.insert(new DalHints(), entities);
fail();
} catch (Exception e) {
}
for (int i = 0; i < mod; i++) {
int j = 1;
// By shard
hints = new DalHints().asyncExecution();
res = dao.insert(hints.inShard(i), entities);
assertNull(res);
res = getIntArray(hints);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
// By shardValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
res = dao.insert(hints.setShardValue(i), entities);
assertNull(res);
res = callback.getIntArray();
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
// By shardColValue
hints = new DalHints().asyncExecution();
res = dao.insert(hints.setShardColValue("index", i), entities);
assertNull(res);
res = getIntArray(hints);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
// By shardColValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
res = dao.insert(hints.setShardColValue("tableIndex", i), entities);
assertNull(res);
res = callback.getIntArray();
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
// By fields same shard
hints = new DalHints().asyncExecution();
entities.get(0).setTableIndex(i);
entities.get(1).setTableIndex(i);
entities.get(2).setTableIndex(i);
res = dao.insert(hints, entities);
assertNull(res);
res = getIntArray(hints);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
}
deleteAllShardsByDb(dao, mod);
// By fields not same shard
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
entities.get(0).setTableIndex(0);
entities.get(1).setTableIndex(1);
entities.get(2).setTableIndex(2);
res = dao.insert(hints.continueOnError(), entities);
assertNull(res);
res = callback.getIntArray();
assertResEquals(3, res);
assertEquals(2, getCountByDb(dao, 0));
assertEquals(1, getCountByDb(dao, 1));
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testCombinedInsertAsyncCallback.
@Test
public void testCombinedInsertAsyncCallback() throws SQLException {
if (!diff.supportInsertValues)
return;
DalHints hints;
IntCallback callback;
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;
// By shard
hints = new DalHints().asyncExecution();
holder = createKeyHolder();
res = dao.combinedInsert(hints.inShard(i), holder, entities);
assertEquals(0, res);
res = getInt(hints);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertKeyHolder(holder);
// By shardValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
holder = createKeyHolder();
res = dao.combinedInsert(hints.setShardValue(i), holder, entities);
assertEquals(0, res);
res = callback.getInt();
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertKeyHolder(holder);
// By shardColValue
hints = new DalHints().asyncExecution();
holder = createKeyHolder();
res = dao.combinedInsert(hints.setShardColValue("index", i), holder, entities);
assertEquals(0, res);
res = getInt(hints);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertKeyHolder(holder);
// By shardColValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
holder = createKeyHolder();
res = dao.combinedInsert(hints.setShardColValue("tableIndex", i), holder, entities);
assertEquals(0, res);
res = callback.getInt();
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.
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTableTest method testQueryByPk.
private void testQueryByPk(int shardId, DalHints oldHints) throws SQLException {
ClientTestModel model = null;
DalHints hints;
for (int i = 0; i < tableMod; i++) {
int id = 1;
// By tabelShard
hints = copy(oldHints);
if (i % 2 == 0)
model = dao.queryByPk(1, hints.inTableShard(String.valueOf(i)));
else
model = dao.queryByPk(1, hints.inTableShard(i));
model = assertModel(model, hints);
assertQueryByPk(shardId, model, i, id);
// By tableShardValue
hints = copy(oldHints);
if (i % 2 == 0)
model = dao.queryByPk(1, hints.setTableShardValue(String.valueOf(i)));
else
model = dao.queryByPk(1, hints.setTableShardValue(i));
model = assertModel(model, hints);
assertQueryByPk(shardId, model, i, id);
// By shardColValue
hints = copy(oldHints);
if (i % 2 == 0)
model = dao.queryByPk(1, hints.setShardColValue("table", String.valueOf(i)));
else
model = dao.queryByPk(1, hints.setShardColValue("table", i));
model = assertModel(model, hints);
assertQueryByPk(shardId, model, i, id);
// By shardColValue
hints = copy(oldHints);
if (i % 2 == 0)
model = dao.queryByPk(1, hints.setShardColValue("tableIndex", String.valueOf(i)));
else
model = dao.queryByPk(1, hints.setShardColValue("tableIndex", i));
model = assertModel(model, hints);
assertQueryByPk(shardId, model, i, id);
}
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTableTest method testDeleteWithWhereClause.
@Test
public void testDeleteWithWhereClause() throws SQLException {
testDeleteWithWhereClause(new DalHints());
testDeleteWithWhereClause(asyncHints());
testDeleteWithWhereClause(intHints());
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTableTest method testCrossShardBatchInsert.
public void testCrossShardBatchInsert(DalHints oldhints) {
try {
deleteAllShardsByDbTable(dao, mod, tableMod);
ClientTestModel[] pList = new ClientTestModel[mod * (1 + tableMod) * tableMod / 2];
int x = 0;
for (int i = 0; i < mod; i++) {
for (int j = 0; j < tableMod; j++) {
for (int k = 0; k < j + 1; k++) {
ClientTestModel p = new ClientTestModel();
p = new ClientTestModel();
p.setId(1 + k);
p.setAddress("aaa");
p.setDbIndex(i);
p.setTableIndex(j);
pList[x++] = p;
}
}
}
DalHints hints = copy(oldhints);
int[] res = dao.batchInsert(hints, Arrays.asList(pList));
assertIntArray(res, hints);
for (int i = 0; i < mod; i++) {
for (int j = 0; j < tableMod; j++) {
Assert.assertEquals(j + 1, getCount(i, j));
}
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
Aggregations