Search in sources :

Example 56 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();
        List<ClientTestModel> pList = create6Entities();
        assertEquals(0, getCountByDb(dao, 0));
        assertEquals(0, getCountByDb(dao, 1));
        KeyHolder keyholder = createKeyHolder();
        DalHints hints = new DalHints();
        res = dao.combinedInsert(hints, keyholder, 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 57 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();
        List<ClientTestModel> pList = create6Entities();
        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, 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)

Example 58 with KeyHolder

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

the class BaseDalTableDaoShardByDbTest method testInsertMultipleAsListWithKeyHolder.

/**
 * Test Insert multiple entities with key-holder
 * @throws SQLException
 */
@Test
public void testInsertMultipleAsListWithKeyHolder() throws SQLException {
    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
        holder = createKeyHolder();
        res = dao.insert(new DalHints().inShard(i), holder, entities);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertResEquals(3, res);
        assertKeyHolder(holder);
        // By shardValue
        holder = createKeyHolder();
        res = dao.insert(new DalHints().setShardValue(i), holder, entities);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertResEquals(3, res);
        assertKeyHolder(holder);
        // By shardColValue
        holder = createKeyHolder();
        res = dao.insert(new DalHints().setShardColValue("index", i), holder, entities);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertResEquals(3, res);
        assertKeyHolder(holder);
        // By shardColValue
        holder = createKeyHolder();
        res = dao.insert(new DalHints().setShardColValue("tableIndex", i), holder, entities);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertResEquals(3, res);
        assertKeyHolder(holder);
        // By fields same shard
        holder = createKeyHolder();
        entities.get(0).setTableIndex(i);
        entities.get(1).setTableIndex(i);
        entities.get(2).setTableIndex(i);
        res = dao.insert(new DalHints(), holder, entities);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertResEquals(3, res);
        assertKeyHolder(holder);
    }
    deleteAllShardsByDb(dao, mod);
    // By fields not same shard
    holder = createKeyHolder();
    entities.get(0).setTableIndex(0);
    entities.get(1).setTableIndex(1);
    entities.get(2).setTableIndex(2);
    res = dao.insert(new DalHints(), holder, entities);
    assertResEquals(3, res);
    assertEquals(2, getCountByDb(dao, 0));
    assertEquals(1, getCountByDb(dao, 1));
    assertResEquals(3, res);
    assertKeyHolder(holder);
}
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 59 with KeyHolder

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

the class BaseDalTableDaoShardByDbTest method testInsertSingleWithPkInsertBack.

/**
 * Test Insert multiple entities one by one
 * @throws SQLException
 */
@Test
public void testInsertSingleWithPkInsertBack() throws SQLException {
    if (!INSERT_PK_BACK_ALLOWED)
        return;
    ClientTestModel model = new ClientTestModel();
    model.setQuantity(10 + 1 % 3);
    model.setType(((Number) (1 % 3)).shortValue());
    model.setAddress("CTRIP");
    int res;
    for (int i = 0; i < mod; i++) {
        int j = 1;
        // By shard
        model.setId(-1);
        model.setAddress("CTRIP" + j++);
        res = dao.insert(new DalHints().inShard(i).setIdentityBack().setKeyHolder(new KeyHolder()), model);
        assertTrue(model.getId() > 0);
        assertEquals(dao.queryByPk(model, new DalHints().inShard(i)).getAddress(), model.getAddress());
        // By shardValue
        model.setId(-1);
        model.setAddress("CTRIP" + j++);
        res = dao.insert(new DalHints().setShardValue(i).setIdentityBack().setKeyHolder(new KeyHolder()), model);
        assertTrue(model.getId() > 0);
        assertEquals(dao.queryByPk(model, new DalHints().setShardValue(i)).getAddress(), model.getAddress());
        // By shardColValue
        model.setId(-1);
        model.setAddress("CTRIP" + j++);
        res = dao.insert(new DalHints().setShardColValue("index", i).setIdentityBack().setKeyHolder(new KeyHolder()), model);
        assertTrue(model.getId() > 0);
        assertEquals(dao.queryByPk(model, new DalHints().setShardColValue("index", i)).getAddress(), model.getAddress());
        // By shardColValue
        model.setId(-1);
        model.setAddress("CTRIP" + j++);
        dao.insert(new DalHints().setShardColValue("tableIndex", i).setIdentityBack().setKeyHolder(new KeyHolder()), model);
        assertTrue(model.getId() > 0);
        assertEquals(dao.queryByPk(model, new DalHints().setShardColValue("tableIndex", i)).getAddress(), model.getAddress());
        // By fields
        model.setId(-1);
        model.setAddress("CTRIP" + j++);
        model.setTableIndex(i);
        res = dao.insert(new DalHints().setIdentityBack().setKeyHolder(new KeyHolder()), model);
        assertTrue(model.getId() > 0);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Example 60 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)

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