use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testQueryTopWithWhereClauseFailedCallback.
@Test
public void testQueryTopWithWhereClauseFailedCallback() throws SQLException {
String whereClause = "type=?";
StatementParameters parameters = new StatementParameters();
parameters.set(1, Types.SMALLINT, 10);
List<ClientTestModel> models;
DefaultResultCallback callback = new DefaultResultCallback();
DalHints hints = new DalHints().callbackWith(callback);
models = dao.queryTop(whereClause, parameters, hints, 2);
try {
callback.waitForDone();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertTrue(!callback.isSuccess());
assertNull(callback.getResult());
assertNotNull(callback.getError());
models = dao.queryTop(whereClause, parameters, new DalHints().inTableShard(1), 2);
assertTrue(null != models);
assertEquals(0, models.size());
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testInsertSingleAsyncCallback.
@Test
public void testInsertSingleAsyncCallback() throws SQLException {
ClientTestModel model = new ClientTestModel();
model.setQuantity(10 + 1 % 3);
model.setType(((Number) (1 % 3)).shortValue());
model.setAddress("CTRIP");
int res;
DalHints hints;
try {
res = dao.insert(new DalHints(), model);
fail();
} catch (Exception e) {
}
for (int i = 0; i < mod; i++) {
int j = 1;
// By tabelShard
hints = asyncHints();
res = dao.insert(hints.inTableShard(i), model);
res = assertInt(res, hints);
assertEquals((i + 1) + j++ * 1, getCount(i));
// By tableShardValue
hints = intHints();
res = dao.insert(hints.setTableShardValue(i), model);
res = assertInt(res, hints);
assertEquals((i + 1) + j++ * 1, getCount(i));
// By shardColValue
hints = asyncHints();
res = dao.insert(hints.setShardColValue("index", i), model);
res = assertInt(res, hints);
assertEquals((i + 1) + j++ * 1, getCount(i));
// By shardColValue
hints = intHints();
res = dao.insert(hints.setShardColValue("tableIndex", i), model);
res = assertInt(res, hints);
assertEquals((i + 1) + j++ * 1, getCount(i));
// By fields
hints = asyncHints();
model.setTableIndex(i);
res = dao.insert(hints, model);
res = assertInt(res, hints);
assertEquals((i + 1) + j++ * 1, getCount(i));
}
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testQueryFromWithWhereClauseFailedAsyncCallback.
@Test
public void testQueryFromWithWhereClauseFailedAsyncCallback() throws SQLException {
String whereClause = "type=?";
List<ClientTestModel> models = null;
DalHints hints;
for (int i = 0; i < mod; i++) {
StatementParameters parameters = new StatementParameters();
parameters.set(1, Types.SMALLINT, 1);
// By tabelShard
hints = asyncHints();
models = dao.queryFrom(whereClause, parameters, hints.inTableShard(i), 0, 10);
models = assertModels(models, hints);
assertTrue(null != models);
assertEquals(i + 1, models.size());
// By tableShardValue
hints = callbackHints();
models = dao.queryFrom(whereClause, parameters, hints.setTableShardValue(i), 0, 10);
models = assertModels(models, hints);
assertTrue(null != models);
assertEquals(i + 1, models.size());
// By shardColValue
hints = asyncHints();
models = dao.queryFrom(whereClause, parameters, hints.setShardColValue("index", i), 0, 10);
models = assertModels(models, hints);
assertTrue(null != models);
assertEquals(i + 1, models.size());
// By shardColValue
hints = callbackHints();
models = dao.queryFrom(whereClause, parameters, hints.setShardColValue("tableIndex", i), 0, 10);
models = assertModels(models, hints);
assertTrue(null != models);
assertEquals(i + 1, models.size());
}
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testBatchUpdateCallback.
@Test
public void testBatchUpdateCallback() throws SQLException {
DefaultResultCallback callback = new DefaultResultCallback();
DalHints hints = new DalHints().callbackWith(callback);
List<ClientTestModel> entities = new ArrayList<>();
for (int i = 0; i < 4; i++) {
ClientTestModel model = new ClientTestModel();
model.setId(i + 1);
model.setAddress("CTRIP");
entities.add(model);
}
int[] res;
try {
res = dao.batchUpdate(hints, entities);
callback.waitForDone();
assertTrue(!callback.isSuccess());
} catch (Exception e) {
fail();
}
// By fields same shard
// holder = new KeyHolder();
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");
entities.get(3).setTableIndex(0);
entities.get(3).setAddress("1234");
dao.batchUpdate(new DalHints(), entities);
List<ClientTestModel> result = dao.query("1=1", new StatementParameters(), new DalHints().inTableShard(0));
for (ClientTestModel m : result) assertEquals("1234", m.getAddress());
}
use of com.ctrip.platform.dal.dao.DalHints in project dal by ctripcorp.
the class BaseDalTabelDaoShardByTableTest method testDeleteMultipleAsyncCallback.
@Test
public void testDeleteMultipleAsyncCallback() throws SQLException {
DalHints hints;
List<ClientTestModel> entities = new ArrayList<>();
for (int i = 0; i < 3; i++) {
ClientTestModel model = new ClientTestModel();
model.setId(i + 1);
model.setQuantity(10 + 1 % 3);
model.setType(((Number) (1 % 3)).shortValue());
model.setAddress("CTRIP");
entities.add(model);
}
int[] res;
// By tabelShard
assertEquals(1, getCount(0));
hints = asyncHints();
res = dao.delete(hints.inTableShard(0), entities);
res = assertIntArray(res, hints);
assertEquals(0, getCount(0));
// By tableShardValue
assertEquals(2, getCount(1));
hints = intHints();
res = dao.delete(hints.setTableShardValue(1), entities);
res = assertIntArray(res, hints);
assertEquals(0, getCount(1));
// By shardColValue
assertEquals(3, getCount(2));
hints = asyncHints();
res = dao.delete(hints.setShardColValue("index", 2), entities);
res = assertIntArray(res, hints);
assertEquals(0, getCount(2));
// By shardColValue
assertEquals(4, getCount(3));
hints = intHints();
res = dao.delete(hints.setShardColValue("tableIndex", 3), entities);
res = assertIntArray(res, hints);
assertEquals(1, getCount(3));
// By fields same shard
// holder = new KeyHolder();
entities.get(0).setTableIndex(0);
entities.get(1).setTableIndex(1);
entities.get(2).setTableIndex(2);
dao.insert(new DalHints(), entities);
assertEquals(1, getCount(0));
assertEquals(1, getCount(1));
assertEquals(1, getCount(2));
entities.set(0, dao.queryFirst("1=1", new StatementParameters(), new DalHints().inTableShard(0)));
entities.set(1, dao.queryFirst("1=1", new StatementParameters(), new DalHints().inTableShard(1)));
entities.set(2, dao.queryFirst("1=1", new StatementParameters(), new DalHints().inTableShard(2)));
hints = intHints();
res = dao.delete(hints, entities);
res = assertIntArray(res, hints);
assertEquals(0, getCount(0));
assertEquals(0, getCount(1));
assertEquals(0, getCount(2));
}
Aggregations