Search in sources :

Example 16 with KeyHolder

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

the class KeyHolderTest method testAddPatial.

@Test
public void testAddPatial() {
    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);
    assertTrue(test.isRequireMerge());
    assertTrue(test.isMerged());
}
Also used : KeyHolder(com.ctrip.platform.dal.dao.KeyHolder) Test(org.junit.Test)

Example 17 with KeyHolder

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

the class KeyHolderTest method testSize.

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

Example 18 with KeyHolder

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

the class BaseDalTableDaoShardByDbTest method testCrossShardInsertAsync.

@Test
public void testCrossShardInsertAsync() {
    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().asyncExecution();
        res = dao.combinedInsert(hints, keyholder, Arrays.asList(pList));
        assertEquals(0, res);
        res = getInt(hints);
        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 19 with KeyHolder

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

the class BaseDalTableDaoShardByDbTest method testInsertMultipleAsListWithKeyHolderAsyncCallback.

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

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

the class BaseDalTableDaoShardByDbTest method testCombinedInsertAsyncCallback.

@Test
public void testCombinedInsertAsyncCallback() throws SQLException {
    if (!diff.supportInsertValues)
        return;
    DalHints hints;
    IntCallback callback;
    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;
        // By shard
        hints = new DalHints().asyncExecution();
        holder = createKeyHolder();
        res = dao.combinedInsert(hints.inShard(i), holder, entities);
        assertEquals(0, res);
        res = getInt(hints);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertKeyHolder(holder);
        // By shardValue
        callback = new IntCallback();
        hints = new DalHints().callbackWith(callback);
        holder = createKeyHolder();
        res = dao.combinedInsert(hints.setShardValue(i), holder, entities);
        assertEquals(0, res);
        res = callback.getInt();
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertKeyHolder(holder);
        // By shardColValue
        hints = new DalHints().asyncExecution();
        holder = createKeyHolder();
        res = dao.combinedInsert(hints.setShardColValue("index", i), holder, entities);
        assertEquals(0, res);
        res = getInt(hints);
        assertResEquals(3, res);
        assertEquals(3 + j++ * 3, getCountByDb(dao, i));
        assertKeyHolder(holder);
        // By shardColValue
        callback = new IntCallback();
        hints = new DalHints().callbackWith(callback);
        holder = createKeyHolder();
        res = dao.combinedInsert(hints.setShardColValue("tableIndex", i), holder, entities);
        assertEquals(0, res);
        res = callback.getInt();
        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)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