use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testUpdateMultipleAsyncCallback.
@Test
public void testUpdateMultipleAsyncCallback() throws SQLException {
DalHints hints;
IntCallback callback;
hints = new DalHints();
List<ClientTestModel> entities = new ArrayList<>();
for (int i = 0; i < 3; i++) {
ClientTestModel model = new ClientTestModel();
model.setId(i + 1);
model.setAddress("CTRIP");
entities.add(model);
}
int[] ress;
try {
ress = dao.update(hints, entities);
fail();
} catch (Exception e) {
}
int res;
// By shard
hints = new DalHints().asyncExecution();
entities.get(0).setAddress("test1");
res = dao.update(hints.inShard(0), entities.get(0));
assertEquals(0, res);
res = getInt(hints);
assertResEquals(1, res);
assertEquals("test1", dao.queryByPk(1, new DalHints().inShard(0)).getAddress());
// By shardValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
entities.get(1).setQuantity(-11);
res = dao.update(hints.setShardValue(1), entities.get(1));
assertEquals(0, res);
res = callback.getInt();
assertResEquals(1, res);
assertEquals(-11, dao.queryByPk(2, new DalHints().inShard(1)).getQuantity().intValue());
// By shardColValue
hints = new DalHints().asyncExecution();
entities.get(2).setType((short) 3);
res = dao.update(hints.setShardColValue("index", 2), entities.get(2));
assertEquals(0, res);
res = getInt(hints);
assertResEquals(1, res);
assertEquals((short) 3, dao.queryByPk(3, new DalHints().inShard(0)).getType().shortValue());
// By shardColValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
entities.get(0).setAddress("testa");
res = dao.update(hints.setShardColValue("tableIndex", 3), entities.get(0));
assertEquals(0, res);
res = callback.getInt();
assertResEquals(1, res);
assertEquals("testa", dao.queryByPk(1, new DalHints().inShard(1)).getAddress());
// By fields same shard
// holder = createKeyHolder();
hints = new DalHints().asyncExecution();
entities.get(0).setTableIndex(0);
entities.get(0).setAddress("1234");
entities.get(1).setTableIndex(0);
entities.get(1).setAddress("1234");
entities.get(2).setTableIndex(0);
entities.get(2).setAddress("1234");
ress = dao.update(hints, entities);
assertNull(ress);
res = getInt(hints);
assertResEquals(3, res);
List<ClientTestModel> result = dao.query("1=1", new StatementParameters(), new DalHints().inShard(0));
for (ClientTestModel m : result) assertEquals("1234", m.getAddress());
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testCrossShardDeleteCallback.
@Test
public void testCrossShardDeleteCallback() {
try {
int[] res;
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(1);
p.setAddress("aaa");
p.setTableIndex(1);
pList[1] = p;
p = new ClientTestModel();
p.setId(2);
p.setAddress("aaa");
p.setTableIndex(2);
pList[2] = p;
p = new ClientTestModel();
p.setId(2);
p.setAddress("aaa");
p.setTableIndex(3);
pList[3] = p;
p = new ClientTestModel();
p.setId(3);
p.setAddress("aaa");
p.setTableIndex(5);
pList[4] = p;
p = new ClientTestModel();
p.setId(3);
p.setAddress("aaa");
p.setTableIndex(6);
pList[5] = p;
IntCallback callback = new IntCallback();
DalHints hints = new DalHints().callbackWith(callback);
res = dao.batchDelete(hints, Arrays.asList(pList));
assertNull(res);
res = callback.getIntArray();
assertEquals(0, getCountByDb(dao, 0));
assertEquals(0, getCountByDb(dao, 1));
assertResEquals(new int[] { 1, 1, 1, 1, 1, 1 }, res);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testDeleteWithWhereClauseShardsAsync.
@Test
public void testDeleteWithWhereClauseShardsAsync() throws SQLException {
String whereClause = "type=?";
StatementParameters parameters = new StatementParameters();
parameters.set(1, Types.SMALLINT, 1);
DalHints hints = new DalHints().asyncExecution();
int res;
// By shards
Set<String> shards = new HashSet<>();
shards.add("0");
shards.add("1");
assertEquals(3, getCountByDb(dao, 0));
assertEquals(3, getCountByDb(dao, 1));
res = dao.delete(whereClause, parameters, hints.inShards(shards));
assertEquals(0, res);
res = getInt(hints);
assertResEquals(6, res);
assertEquals(0, dao.query(whereClause, parameters, new DalHints().inShards(shards)).size());
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTableDaoShardByDbTest method testBatchInsertAsyncCallback.
@Test
public void testBatchInsertAsyncCallback() throws SQLException {
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);
}
int[] res;
try {
res = dao.batchInsert(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.batchInsert(hints.inShard(i), entities);
assertNull(res);
res = getIntArray(hints);
assertResEquals(new int[] { 1, 1, 1 }, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
// By shardValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
res = dao.batchInsert(hints.setShardValue(i), entities);
assertNull(res);
res = callback.getIntArray();
assertResEquals(new int[] { 1, 1, 1 }, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
// By shardColValue
hints = new DalHints().asyncExecution();
res = dao.batchInsert(hints.setShardColValue("index", i), entities);
assertNull(res);
res = getIntArray(hints);
assertResEquals(new int[] { 1, 1, 1 }, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
// By shardColValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
res = dao.batchInsert(hints.setShardColValue("tableIndex", i), entities);
assertNull(res);
res = callback.getIntArray();
assertResEquals(new int[] { 1, 1, 1 }, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
}
// 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 BaseDalTableDaoShardByDbTest method testInsertMultipleAsListWithKeyHolderAsyncCallback.
@Test
public void testInsertMultipleAsListWithKeyHolderAsyncCallback() 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);
}
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
hints = new DalHints().asyncExecution();
holder = createKeyHolder();
res = dao.insert(hints.inShard(i), holder, entities);
assertNull(res);
res = getIntArray(hints);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
// By shardValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
holder = createKeyHolder();
res = dao.insert(hints.setShardValue(i), holder, entities);
assertNull(res);
res = callback.getIntArray();
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
// By shardColValue
hints = new DalHints().asyncExecution();
holder = createKeyHolder();
res = dao.insert(hints.setShardColValue("index", i), holder, entities);
assertNull(res);
res = getIntArray(hints);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
// By shardColValue
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
holder = createKeyHolder();
res = dao.insert(hints.setShardColValue("tableIndex", i), holder, entities);
assertNull(res);
res = callback.getIntArray();
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
// By fields same shard
hints = new DalHints().asyncExecution();
holder = createKeyHolder();
entities.get(0).setTableIndex(i);
entities.get(1).setTableIndex(i);
entities.get(2).setTableIndex(i);
res = dao.insert(hints, holder, entities);
assertNull(res);
res = getIntArray(hints);
assertResEquals(3, res);
assertEquals(3 + j++ * 3, getCountByDb(dao, i));
assertResEquals(3, res);
assertKeyHolder(holder);
}
deleteAllShardsByDb(dao, mod);
// By fields not same shard
callback = new IntCallback();
hints = new DalHints().callbackWith(callback);
holder = createKeyHolder();
entities.get(0).setTableIndex(0);
entities.get(1).setTableIndex(1);
entities.get(2).setTableIndex(2);
res = dao.insert(hints, holder, entities);
assertNull(res);
res = callback.getIntArray();
assertResEquals(3, res);
assertEquals(2, getCountByDb(dao, 0));
assertEquals(1, getCountByDb(dao, 1));
assertResEquals(3, res);
assertKeyHolder(holder);
}
Aggregations