use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testCombinedInsertAsyncCallback.
@Test
public void testCombinedInsertAsyncCallback() throws SQLException {
if (!diff.supportInsertValues)
return;
DalHints hints;
ClientTestModel[] entities = new ClientTestModel[3];
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[i] = model;
}
KeyHolder holder = new KeyHolder();
int res;
try {
hints = asyncHints();
res = dao.combinedInsert(hints, holder, Arrays.asList(entities));
res = assertInt(res, hints);
fail();
} catch (Exception e) {
}
for (int i = 0; i < mod; i++) {
int j = 1;
holder = null;
// By tabelShard
// holder = new KeyHolder();
hints = asyncHints();
res = dao.combinedInsert(hints.inTableShard(i), holder, Arrays.asList(entities));
res = assertInt(res, hints);
assertEquals((i + 1) + j++ * 3, getCount(i));
// By tableShardValue
// holder = new KeyHolder();
hints = intHints();
res = dao.combinedInsert(hints.setTableShardValue(i), holder, Arrays.asList(entities));
res = assertInt(res, hints);
assertEquals((i + 1) + j++ * 3, getCount(i));
// By shardColValue
// holder = new KeyHolder();
hints = asyncHints();
res = dao.combinedInsert(hints.setShardColValue("index", i), holder, Arrays.asList(entities));
res = assertInt(res, hints);
assertEquals((i + 1) + j++ * 3, getCount(i));
// By shardColValue
// holder = new KeyHolder();
hints = intHints();
res = dao.combinedInsert(hints.setShardColValue("tableIndex", i), holder, Arrays.asList(entities));
res = assertInt(res, hints);
assertEquals((i + 1) + j++ * 3, getCount(i));
}
// For combined insert, the shard id must be defined or change bd deduced.
}
use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testCrossShardInsert.
@Test
public void testCrossShardInsert() {
if (!diff.supportInsertValues)
return;
try {
deleteAllShards();
ClientTestModel p = new ClientTestModel();
ClientTestModel[] pList = new ClientTestModel[6];
p = new ClientTestModel();
p.setId(1);
p.setAddress("aaa");
p.setTableIndex(0);
pList[0] = p;
p = new ClientTestModel();
p.setId(2);
p.setAddress("aaa");
p.setTableIndex(1);
pList[1] = p;
p = new ClientTestModel();
p.setId(3);
p.setAddress("aaa");
p.setTableIndex(2);
pList[2] = p;
p = new ClientTestModel();
p.setId(4);
p.setAddress("aaa");
p.setTableIndex(3);
pList[3] = p;
p = new ClientTestModel();
p.setId(5);
p.setAddress("aaa");
p.setTableIndex(4);
pList[4] = p;
p = new ClientTestModel();
p.setId(5);
p.setAddress("aaa");
p.setTableIndex(5);
pList[5] = p;
//new KeyHolder();
KeyHolder keyHolder = null;
dao.combinedInsert(new DalHints(), keyHolder, Arrays.asList(pList));
assertEquals(2, getCount(0));
assertEquals(2, getCount(1));
assertEquals(1, getCount(2));
assertEquals(1, getCount(3));
} catch (Exception e) {
fail();
}
}
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.
}
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();
ClientTestModel[] pList = new ClientTestModel[6];
p = new ClientTestModel();
p.setId(1);
p.setAddress("aaa");
p.setTableIndex(0);
pList[0] = p;
p = new ClientTestModel();
p.setId(2);
p.setAddress("aaa");
p.setTableIndex(1);
pList[1] = p;
p = new ClientTestModel();
p.setId(3);
p.setAddress("aaa");
p.setTableIndex(2);
pList[2] = p;
p = new ClientTestModel();
p.setId(4);
p.setAddress("aaa");
p.setTableIndex(3);
pList[3] = p;
p = new ClientTestModel();
p.setId(5);
p.setAddress("aaa");
p.setTableIndex(4);
pList[4] = p;
p = new ClientTestModel();
p.setId(5);
p.setAddress("aaa");
p.setTableIndex(5);
pList[5] = p;
assertEquals(0, getCountByDb(dao, 0));
assertEquals(0, getCountByDb(dao, 1));
KeyHolder keyholder = createKeyHolder();
DalHints hints = new DalHints();
res = dao.combinedInsert(hints, keyholder, Arrays.asList(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();
ClientTestModel[] pList = new ClientTestModel[6];
p = new ClientTestModel();
p.setId(1);
p.setAddress("aaa");
p.setTableIndex(0);
pList[0] = p;
p = new ClientTestModel();
p.setId(2);
p.setAddress("aaa");
p.setTableIndex(1);
pList[1] = p;
p = new ClientTestModel();
p.setId(3);
p.setAddress("aaa");
p.setTableIndex(2);
pList[2] = p;
p = new ClientTestModel();
p.setId(4);
p.setAddress("aaa");
p.setTableIndex(3);
pList[3] = p;
p = new ClientTestModel();
p.setId(5);
p.setAddress("aaa");
p.setTableIndex(4);
pList[4] = p;
p = new ClientTestModel();
p.setId(5);
p.setAddress("aaa");
p.setTableIndex(5);
pList[5] = p;
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, Arrays.asList(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();
}
}
Aggregations