Search in sources :

Example 26 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 27 with KeyHolder

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

the class DalDirectClientTestStub method updateTestInsertWithKeyHolder.

/**
	 * Test the update function with key-holder
	 * 
	 * @throws SQLException
	 */
@Test
public void updateTestInsertWithKeyHolder() throws SQLException {
    if (!diff.supportGetGeneratedKeys)
        return;
    String insertSql = String.format("INSERT INTO %s VALUES(NULL, 10, 1, 'SH INFO', NULL)", TABLE_NAME);
    StatementParameters parameters = new StatementParameters();
    KeyHolder holder = new KeyHolder();
    DalHints hints = new DalHints();
    int count = client.update(insertSql, parameters, hints.setKeyHolder(holder));
    Assert.assertEquals(1, count);
    Assert.assertEquals(1, holder.size());
    Assert.assertTrue(holder.getKeyList().get(0).containsKey("GENERATED_KEY"));
    Assert.assertEquals(4, queryModelsByIds().size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Example 28 with KeyHolder

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

the class KeyHolderTest method testMergeSequential.

@Test
public void testMergeSequential() {
    ExecutorService service = null;
    service = new ThreadPoolExecutor(5, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
    KeyHolder test = new KeyHolder();
    test.setSize(6);
    test.requireMerge();
    KeyHolder tmpHolder = new KeyHolder();
    tmpHolder.addKey(buildKey(0));
    tmpHolder.addKey(buildKey(1));
    tmpHolder.addKey(buildKey(2));
    test.addPatial(new Integer[] { 0, 1, 2 }, tmpHolder);
    tmpHolder = new KeyHolder();
    tmpHolder.addKey(buildKey(3));
    tmpHolder.addKey(buildKey(4));
    tmpHolder.addKey(buildKey(5));
    test.addPatial(new Integer[] { 3, 4, 5 }, tmpHolder);
    try {
        int i = 0;
        for (Number value : test.getIdList()) {
            assertEquals(i++, value);
        }
    } catch (SQLException e) {
        fail();
    }
}
Also used : SQLException(java.sql.SQLException) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 29 with KeyHolder

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

the class KeyHolderTest method testGetKeyList.

@Test
public void testGetKeyList() {
    KeyHolder test = new KeyHolder();
    test.addKey(buildKey(1));
    assertEquals(1, test.size());
    test.addKey(buildKey(1));
    assertEquals(2, test.size());
}
Also used : KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Example 30 with KeyHolder

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

the class KeyHolderTest method testMergeParallel.

@Test
public void testMergeParallel() {
    ExecutorService service = null;
    service = new ThreadPoolExecutor(5, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
    KeyHolder test = new KeyHolder();
    test.setSize(30);
    test.requireMerge();
    List<Future<Boolean>> fList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        fList.add(service.submit(new KeyHolderTask(test, new Integer[] { i * 3, i * 3 + 1, i * 3 + 2 })));
    }
    for (Future<Boolean> f : fList) try {
        f.get();
    } catch (InterruptedException | ExecutionException e) {
        fail();
    }
    assertTrue(test.isRequireMerge());
    assertTrue(test.isMerged());
    assertEquals(30, test.size());
    try {
        assertEquals(30, test.getIdList().size());
    } catch (SQLException e1) {
        fail();
    }
    try {
        List<Number> ids = test.getIdList();
        for (int i = 0; i < 30; i++) assertEquals(i, ids.get(i));
    } catch (SQLException e) {
        fail();
    }
    service.shutdown();
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) 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