Search in sources :

Example 21 with KeyHolder

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.
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 22 with KeyHolder

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();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 23 with KeyHolder

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.
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 24 with KeyHolder

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();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 25 with KeyHolder

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();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)42 Test (org.junit.Test)32 SQLException (java.sql.SQLException)28 DalHints (com.ctrip.platform.dal.dao.DalHints)27 ArrayList (java.util.ArrayList)13 Timestamp (java.sql.Timestamp)5 Map (java.util.Map)5 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)3 CombinedInsertTask (com.ctrip.platform.dal.dao.task.CombinedInsertTask)3 ExecutorService (java.util.concurrent.ExecutorService)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 BeforeClass (org.junit.BeforeClass)2 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)2 DalColumnMapRowMapper (com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper)1 DalDefaultJpaParser (com.ctrip.platform.dal.dao.helper.DalDefaultJpaParser)1 DalRowMapperExtractor (com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Future (java.util.concurrent.Future)1