Search in sources :

Example 51 with KeyHolder

use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTableTest method testInsertMultipleAsListWithKeyHolder.

/**
 * Test Insert multiple entities with key-holder
 * @throws SQLException
 */
private void testInsertMultipleAsListWithKeyHolder(int shardId, DalHints oldhints) throws SQLException {
    DalHints hints;
    List<ClientTestModel> entities = createListNoId(3);
    KeyHolder holder = new KeyHolder();
    int[] res;
    try {
        hints = copy(oldhints);
        res = dao.insert(hints, holder, entities);
        assertIntArray(res, hints);
        Assert.fail();
    } catch (Exception e) {
    }
    for (int i = 0; i < tableMod; i++) {
        int j = 1;
        // By tabelShard
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.insert(hints.inTableShard(i), holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        if (INSERT_PK_BACK_ALLOWED) {
            // check ID set back
            holder = createKeyHolder();
            IdentitySetBackHelper.clearId(entities);
            hints = copy(oldhints);
            res = dao.insert(hints.inTableShard(i).setIdentityBack(), holder, entities);
            res = assertIntArray(res, hints);
            assertResEquals(3, res);
            Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
            IdentitySetBackHelper.assertIdentity(dao, copy(oldhints).inTableShard(i), entities);
        }
        // By tableShardValue
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.insert(hints.setTableShardValue(i), holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // By shardColValue
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.insert(hints.setShardColValue("table", i), holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // By shardColValue
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.insert(hints.setShardColValue("tableIndex", i), holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // By fields same shard
        holder = createKeyHolder();
        entities.get(0).setTableIndex(i);
        entities.get(1).setTableIndex(i);
        entities.get(2).setTableIndex(i);
        hints = copy(oldhints);
        res = dao.insert(hints, holder, entities);
        res = assertIntArray(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
    }
    deleteAllShards(shardId);
    // By fields not same shard
    holder = createKeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(2).setTableIndex(2);
    hints = copy(oldhints);
    res = dao.insert(hints, holder, entities);
    res = assertIntArray(res, hints);
    Assert.assertEquals(1, getCount(shardId, 0));
    Assert.assertEquals(1, getCount(shardId, 1));
    Assert.assertEquals(1, getCount(shardId, 2));
    assertResEquals(3, res);
    assertKeyHolder(holder);
    deleteAllShards(shardId);
    // Check set ID back
    if (INSERT_PK_BACK_ALLOWED) {
        holder = createKeyHolder();
        entities.get(0).setTableIndex(0);
        entities.get(1).setTableIndex(1);
        entities.get(2).setTableIndex(2);
        IdentitySetBackHelper.clearId(entities);
        hints = copy(oldhints);
        res = dao.insert(hints.setIdentityBack(), holder, entities);
        res = assertIntArray(res, hints);
        Assert.assertEquals(1, getCount(shardId, 0));
        Assert.assertEquals(1, getCount(shardId, 1));
        Assert.assertEquals(1, getCount(shardId, 2));
        assertResEquals(3, res);
        assertKeyHolder(holder);
        IdentitySetBackHelper.assertIdentity(dao, copy(oldhints), entities);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException)

Example 52 with KeyHolder

use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTableTest method testCrossShardCombinedInsertSetPkBack.

private void testCrossShardCombinedInsertSetPkBack(DalHints oldhints) throws SQLException {
    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;
                }
            }
        }
        KeyHolder keyHolder = createKeyHolder();
        DalHints hints = copy(oldhints);
        IdentitySetBackHelper.clearId(Arrays.asList(pList));
        int res = dao.combinedInsert(hints.setIdentityBack(), keyHolder, Arrays.asList(pList));
        assertInt(res, hints);
        assertKeyHolderCrossShard(keyHolder);
        for (int i = 0; i < mod; i++) {
            for (int j = 0; j < tableMod; j++) {
                Assert.assertEquals(j + 1, getCount(i, j));
            }
        }
        IdentitySetBackHelper.assertIdentity(dao, copy(oldhints), Arrays.asList(pList));
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException)

Example 53 with KeyHolder

use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTableTest method testCombinedInsert.

/**
 * Test Insert multiple entities with one SQL Statement
 * @throws SQLException
 */
private void testCombinedInsert(int shardId, DalHints oldhints) throws SQLException {
    DalHints hints;
    List<ClientTestModel> entities = create(3);
    KeyHolder holder = createKeyHolder();
    int res;
    try {
        hints = copy(oldhints);
        res = dao.combinedInsert(hints, holder, entities);
        res = assertInt(res, hints);
        Assert.fail();
    } catch (Exception e) {
    }
    for (int i = 0; i < tableMod; i++) {
        int j = 1;
        holder = null;
        // By tabelShard
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.combinedInsert(hints.inTableShard(i), holder, entities);
        res = assertInt(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // Check set ID back
        if (INSERT_PK_BACK_ALLOWED) {
            holder = createKeyHolder();
            hints = copy(oldhints);
            IdentitySetBackHelper.clearId(entities);
            res = dao.combinedInsert(hints.inTableShard(i).setIdentityBack(), holder, entities);
            res = assertInt(res, hints);
            assertResEquals(3, res);
            Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
            assertKeyHolder(holder);
            IdentitySetBackHelper.assertIdentity(dao, copy(oldhints).inTableShard(i), entities);
        }
        // By tableShardValue
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.combinedInsert(hints.setTableShardValue(i), holder, entities);
        res = assertInt(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // By shardColValue
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.combinedInsert(hints.setShardColValue("table", i), holder, entities);
        res = assertInt(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, i));
        assertKeyHolder(holder);
        // By shardColValue
        holder = createKeyHolder();
        hints = copy(oldhints);
        res = dao.combinedInsert(hints.setShardColValue("tableIndex", i), holder, entities);
        res = assertInt(res, hints);
        assertResEquals(3, res);
        Assert.assertEquals((i + 1) + j++ * 3, getCount(shardId, 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) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) SQLException(java.sql.SQLException)

Example 54 with KeyHolder

use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTableTest method testInsertMultipleAsListWithKeyHolderByFieldsSetPkBack.

private void testInsertMultipleAsListWithKeyHolderByFieldsSetPkBack(DalHints hints) throws SQLException {
    reset();
    List<ClientTestModel> entities = createListNoId(3);
    int[] res;
    KeyHolder holder = createKeyHolder();
    deleteAllShardsByDbTable(dao, mod, tableMod);
    // By fields not same shard
    holder = createKeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(0).setDbIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(1).setDbIndex(1);
    entities.get(2).setTableIndex(2);
    entities.get(2).setDbIndex(2);
    hints = copy(hints);
    IdentitySetBackHelper.clearId(entities);
    res = dao.insert(hints.setIdentityBack(), holder, entities);
    res = assertIntArray(res, hints);
    assertResEquals(3, res);
    Assert.assertEquals(1, getCount(0, 0));
    Assert.assertEquals(1, getCount(1, 1));
    Assert.assertEquals(1, getCount(0, 2));
    assertKeyHolder(holder);
    IdentitySetBackHelper.assertIdentity(dao, hints, entities);
}
Also used : KeyHolder(com.ctrip.platform.dal.dao.KeyHolder)

Example 55 with KeyHolder

use of com.ctrip.platform.dal.dao.KeyHolder in project dal by ctripcorp.

the class BaseDalTableDaoShardByDbTest method testInsertMultipleAsListWithPkInsertBack.

/**
 * Test Insert multiple entities one by one
 * @throws SQLException
 */
@Test
public void testInsertMultipleAsListWithPkInsertBack() throws SQLException {
    if (!INSERT_PK_BACK_ALLOWED)
        return;
    List<ClientTestModel> entities = new ArrayList<ClientTestModel>();
    for (int i = 0; i < 3; i++) {
        ClientTestModel model = new ClientTestModel();
        model.setQuantity(10 + i % 3);
        model.setType(((Number) (i % 3)).shortValue());
        model.setAddress("CTRIP");
        entities.add(model);
    }
    int[] res;
    for (int i = 0; i < mod; i++) {
        int j = 1;
        // By shard
        IdentitySetBackHelper.clearId(entities);
        res = dao.insert(new DalHints().inShard(i).setIdentityBack().setKeyHolder(new KeyHolder()), entities);
        IdentitySetBackHelper.assertIdentity(dao, entities, i);
        // By fields same shard
        IdentitySetBackHelper.clearId(entities);
        entities.get(0).setTableIndex(i);
        entities.get(1).setTableIndex(i);
        entities.get(2).setTableIndex(i);
        res = dao.insert(new DalHints().setIdentityBack().setKeyHolder(new KeyHolder()), entities);
        IdentitySetBackHelper.assertIdentity(dao, entities, i);
    }
    deleteAllShardsByDb(dao, mod);
    // By fields not same shard
    entities.get(0).setTableIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(2).setTableIndex(2);
    res = dao.insert(new DalHints().continueOnError().setIdentityBack().setKeyHolder(new KeyHolder()), entities);
    for (ClientTestModel model : entities) {
        assertEquals(dao.queryByPk(model, new DalHints()).getAddress(), model.getAddress());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Aggregations

KeyHolder (com.ctrip.platform.dal.dao.KeyHolder)60 Test (org.junit.Test)46 DalHints (com.ctrip.platform.dal.dao.DalHints)45 SQLException (java.sql.SQLException)32 ArrayList (java.util.ArrayList)20 DalTableDao (com.ctrip.platform.dal.dao.DalTableDao)7 Timestamp (java.sql.Timestamp)5 Map (java.util.Map)5 CombinedInsertTask (com.ctrip.platform.dal.dao.task.CombinedInsertTask)4 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)2 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