Search in sources :

Example 1 with Person

use of org.apache.ignite.cache.store.jdbc.model.Person in project ignite by apache.

the class CacheJdbcPojoStoreTest method testParallelLoad.

/**
 * @throws Exception If failed.
 */
@Test
public void testParallelLoad() throws Exception {
    Connection conn = store.openConnection(false);
    PreparedStatement prnComplexStmt = conn.prepareStatement("INSERT INTO Person_Complex(id, org_id, city_id, name, salary) VALUES (?, ?, ?, ?, ?)");
    for (int i = 0; i < 8; i++) {
        prnComplexStmt.setInt(1, (i >> 2) & 1);
        prnComplexStmt.setInt(2, (i >> 1) & 1);
        prnComplexStmt.setInt(3, i % 2);
        prnComplexStmt.setString(4, "name");
        prnComplexStmt.setInt(5, 1000 + i * 500);
        prnComplexStmt.addBatch();
    }
    prnComplexStmt.executeBatch();
    U.closeQuiet(prnComplexStmt);
    conn.commit();
    U.closeQuiet(conn);
    final Collection<Object> prnComplexKeys = new ConcurrentLinkedQueue<>();
    IgniteBiInClosure<Object, Object> c = new CI2<Object, Object>() {

        @Override
        public void apply(Object k, Object v) {
            if (binaryEnable) {
                if (k instanceof BinaryObject && v instanceof BinaryObject) {
                    BinaryObject key = (BinaryObject) k;
                    BinaryObject val = (BinaryObject) v;
                    String keyType = key.type().typeName();
                    String valType = val.type().typeName();
                    if (PersonComplexKey.class.getName().equals(keyType) && Person.class.getName().equals(valType))
                        prnComplexKeys.add(key);
                }
            } else {
                if (k instanceof PersonComplexKey && v instanceof Person)
                    prnComplexKeys.add(k);
                else
                    fail("Unexpected entry [key=" + k + ", value=" + v + "]");
            }
        }
    };
    store.setParallelLoadCacheMinimumThreshold(2);
    store.loadCache(c);
    assertEquals(8, prnComplexKeys.size());
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) PersonComplexKey(org.apache.ignite.cache.store.jdbc.model.PersonComplexKey) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) BinaryObject(org.apache.ignite.binary.BinaryObject) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CI2(org.apache.ignite.internal.util.typedef.CI2) Person(org.apache.ignite.cache.store.jdbc.model.Person) Test(org.junit.Test) GridAbstractCacheStoreSelfTest(org.apache.ignite.testframework.junits.cache.GridAbstractCacheStoreSelfTest) BinaryTest(org.apache.ignite.cache.store.jdbc.model.BinaryTest)

Example 2 with Person

use of org.apache.ignite.cache.store.jdbc.model.Person in project ignite by apache.

the class CacheJdbcStoreAbstractMultithreadedSelfTest method testMultithreadedPut.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultithreadedPut() throws Exception {
    IgniteInternalFuture<?> fut1 = runMultiThreadedAsync(new Callable<Object>() {

        private final Random rnd = new Random();

        @Nullable
        @Override
        public Object call() throws Exception {
            for (int i = 0; i < TX_CNT; i++) {
                IgniteCache<Object, Object> cache = jcache();
                int id = rnd.nextInt(1000);
                if (rnd.nextBoolean())
                    cache.put(new OrganizationKey(id), new Organization(id, "Name" + id, "City" + id));
                else
                    cache.put(new PersonKey(id), new Person(id, rnd.nextInt(), new Date(System.currentTimeMillis()), "Name" + id, 1, Gender.random()));
            }
            return null;
        }
    }, 4, "put");
    IgniteInternalFuture<?> fut2 = runMultiThreadedAsync(new Callable<Object>() {

        private final Random rnd = new Random();

        @Nullable
        @Override
        public Object call() throws Exception {
            for (int i = 0; i < TX_CNT; i++) {
                IgniteCache<Object, Object> cache = jcache();
                int id = rnd.nextInt(1000);
                if (rnd.nextBoolean())
                    cache.putIfAbsent(new OrganizationKey(id), new Organization(id, "Name" + id, "City" + id));
                else
                    cache.putIfAbsent(new PersonKey(id), new Person(id, rnd.nextInt(), new Date(System.currentTimeMillis()), "Name" + id, i, Gender.random()));
            }
            return null;
        }
    }, 8, "putIfAbsent");
    fut1.get();
    fut2.get();
}
Also used : Organization(org.apache.ignite.cache.store.jdbc.model.Organization) PersonKey(org.apache.ignite.cache.store.jdbc.model.PersonKey) IgniteCache(org.apache.ignite.IgniteCache) MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BeansException(org.springframework.beans.BeansException) Date(java.sql.Date) OrganizationKey(org.apache.ignite.cache.store.jdbc.model.OrganizationKey) Random(java.util.Random) Person(org.apache.ignite.cache.store.jdbc.model.Person) Nullable(org.jetbrains.annotations.Nullable) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 3 with Person

use of org.apache.ignite.cache.store.jdbc.model.Person in project ignite by apache.

the class CacheJdbcStoreAbstractMultithreadedSelfTest method testMultithreadedPutAll.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultithreadedPutAll() throws Exception {
    multithreaded(new Callable<Object>() {

        private final Random rnd = new Random();

        @Nullable
        @Override
        public Object call() throws Exception {
            for (int i = 0; i < TX_CNT; i++) {
                int cnt = rnd.nextInt(BATCH_CNT);
                List<Integer> ids = new ArrayList<>(cnt);
                for (int j = 0; j < cnt; j++) {
                    int id = rnd.nextInt(5000);
                    if (!ids.contains(id))
                        ids.add(id);
                }
                Collections.sort(ids);
                Map<Object, Object> map = U.newLinkedHashMap(cnt);
                for (Integer id : ids) {
                    if (rnd.nextBoolean())
                        map.put(new OrganizationKey(id), new Organization(id, "Name" + id, "City" + id));
                    else
                        map.put(new PersonKey(id), new Person(id, rnd.nextInt(), new Date(System.currentTimeMillis()), "Name" + id, 1, Gender.random()));
                }
                IgniteCache<Object, Object> cache = jcache();
                cache.putAll(map);
            }
            return null;
        }
    }, 8, "putAll");
}
Also used : Organization(org.apache.ignite.cache.store.jdbc.model.Organization) PersonKey(org.apache.ignite.cache.store.jdbc.model.PersonKey) IgniteCache(org.apache.ignite.IgniteCache) MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BeansException(org.springframework.beans.BeansException) Date(java.sql.Date) OrganizationKey(org.apache.ignite.cache.store.jdbc.model.OrganizationKey) Random(java.util.Random) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Person(org.apache.ignite.cache.store.jdbc.model.Person) Nullable(org.jetbrains.annotations.Nullable) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 4 with Person

use of org.apache.ignite.cache.store.jdbc.model.Person in project ignite by apache.

the class CacheJdbcPojoStoreTest method testLoadCache.

/**
 * @throws Exception If failed.
 */
@Test
public void testLoadCache() throws Exception {
    Connection conn = store.openConnection(false);
    PreparedStatement orgStmt = conn.prepareStatement("INSERT INTO Organization(id, name, city) VALUES (?, ?, ?)");
    for (int i = 0; i < ORGANIZATION_CNT; i++) {
        orgStmt.setInt(1, i);
        orgStmt.setString(2, "name" + i);
        orgStmt.setString(3, "city" + i % 10);
        orgStmt.addBatch();
    }
    orgStmt.executeBatch();
    U.closeQuiet(orgStmt);
    conn.commit();
    PreparedStatement prnStmt = conn.prepareStatement("INSERT INTO Person(id, org_id, name) VALUES (?, ?, ?)");
    for (int i = 0; i < PERSON_CNT; i++) {
        prnStmt.setInt(1, i);
        prnStmt.setInt(2, i % 100);
        prnStmt.setString(3, "name" + i);
        prnStmt.addBatch();
    }
    prnStmt.executeBatch();
    conn.commit();
    U.closeQuiet(prnStmt);
    PreparedStatement prnComplexStmt = conn.prepareStatement("INSERT INTO Person_Complex(id, org_id, city_id, name, salary) VALUES (?, ?, ?, ?, ?)");
    for (int i = 0; i < PERSON_CNT; i++) {
        prnComplexStmt.setInt(1, i);
        prnComplexStmt.setInt(2, i % 500);
        prnComplexStmt.setInt(3, i % 100);
        prnComplexStmt.setString(4, "name" + i);
        if (i > 0)
            prnComplexStmt.setInt(5, 1000 + i * 500);
        else
            // Add person with null salary
            prnComplexStmt.setNull(5, Types.INTEGER);
        prnComplexStmt.addBatch();
    }
    prnComplexStmt.executeBatch();
    U.closeQuiet(prnComplexStmt);
    conn.commit();
    U.closeQuiet(prnStmt);
    PreparedStatement binaryStmt = conn.prepareStatement("INSERT INTO Binary_Entries(key, val) VALUES (?, ?)");
    byte[] bytes = new byte[16];
    for (byte i = 0; i < 16; i++) bytes[i] = i;
    binaryStmt.setInt(1, 1);
    binaryStmt.setBinaryStream(2, new ByteArrayInputStream(bytes));
    binaryStmt.addBatch();
    binaryStmt.executeBatch();
    U.closeQuiet(binaryStmt);
    conn.commit();
    U.closeQuiet(conn);
    final Collection<Object> orgKeys = new ConcurrentLinkedQueue<>();
    final Collection<Object> prnKeys = new ConcurrentLinkedQueue<>();
    final Collection<Object> prnComplexKeys = new ConcurrentLinkedQueue<>();
    final Collection<Object> binaryTestVals = new ConcurrentLinkedQueue<>();
    IgniteBiInClosure<Object, Object> c = new CI2<Object, Object>() {

        @Override
        public void apply(Object k, Object v) {
            if (binaryEnable) {
                if (k instanceof BinaryObject && v instanceof BinaryObject) {
                    BinaryObject key = (BinaryObject) k;
                    BinaryObject val = (BinaryObject) v;
                    String keyType = key.type().typeName();
                    String valType = val.type().typeName();
                    if (OrganizationKey.class.getName().equals(keyType) && Organization.class.getName().equals(valType))
                        orgKeys.add(key);
                    if (PersonKey.class.getName().equals(keyType) && Person.class.getName().equals(valType))
                        prnKeys.add(key);
                    if (PersonComplexKey.class.getName().equals(keyType) && Person.class.getName().equals(valType))
                        prnComplexKeys.add(key);
                    if (BinaryTestKey.class.getName().equals(keyType) && BinaryTest.class.getName().equals(valType))
                        binaryTestVals.add(val.field("bytes"));
                }
            } else {
                if (k instanceof OrganizationKey && v instanceof Organization)
                    orgKeys.add(k);
                else if (k instanceof PersonKey && v instanceof Person)
                    prnKeys.add(k);
                else if (k instanceof BinaryTestKey && v instanceof BinaryTest)
                    binaryTestVals.add(((BinaryTest) v).getBytes());
                else if (k instanceof PersonComplexKey && v instanceof Person) {
                    PersonComplexKey key = (PersonComplexKey) k;
                    Person val = (Person) v;
                    assertTrue("Key ID should be the same as value ID", key.getId() == val.getId());
                    assertTrue("Key orgID should be the same as value orgID", key.getOrgId() == val.getOrgId());
                    assertEquals("name" + key.getId(), val.getName());
                    prnComplexKeys.add(k);
                }
            }
        }
    };
    store.loadCache(c);
    assertEquals(ORGANIZATION_CNT, orgKeys.size());
    assertEquals(PERSON_CNT, prnKeys.size());
    assertEquals(PERSON_CNT, prnComplexKeys.size());
    assertEquals(1, binaryTestVals.size());
    assertTrue(Arrays.equals(bytes, (byte[]) binaryTestVals.iterator().next()));
    Collection<Object> tmpOrgKeys = new ArrayList<>(orgKeys);
    Collection<Object> tmpPrnKeys = new ArrayList<>(prnKeys);
    Collection<Object> tmpPrnComplexKeys = new ArrayList<>(prnComplexKeys);
    orgKeys.clear();
    prnKeys.clear();
    prnComplexKeys.clear();
    store.loadCache(c, OrganizationKey.class.getName(), "SELECT name, city, id FROM ORGANIZATION", PersonKey.class.getName(), "SELECT org_id, id, name FROM Person WHERE id < 1000");
    assertEquals(ORGANIZATION_CNT, orgKeys.size());
    assertEquals(1000, prnKeys.size());
    assertEquals(0, prnComplexKeys.size());
    store.deleteAll(tmpOrgKeys);
    store.deleteAll(tmpPrnKeys);
    store.deleteAll(tmpPrnComplexKeys);
    orgKeys.clear();
    prnKeys.clear();
    prnComplexKeys.clear();
    store.loadCache(c);
    assertTrue(orgKeys.isEmpty());
    assertTrue(prnKeys.isEmpty());
    assertTrue(prnComplexKeys.isEmpty());
}
Also used : Organization(org.apache.ignite.cache.store.jdbc.model.Organization) BinaryTest(org.apache.ignite.cache.store.jdbc.model.BinaryTest) PersonKey(org.apache.ignite.cache.store.jdbc.model.PersonKey) PersonComplexKey(org.apache.ignite.cache.store.jdbc.model.PersonComplexKey) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) CI2(org.apache.ignite.internal.util.typedef.CI2) OrganizationKey(org.apache.ignite.cache.store.jdbc.model.OrganizationKey) BinaryObject(org.apache.ignite.binary.BinaryObject) ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryTestKey(org.apache.ignite.cache.store.jdbc.model.BinaryTestKey) BinaryObject(org.apache.ignite.binary.BinaryObject) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Person(org.apache.ignite.cache.store.jdbc.model.Person) Test(org.junit.Test) GridAbstractCacheStoreSelfTest(org.apache.ignite.testframework.junits.cache.GridAbstractCacheStoreSelfTest) BinaryTest(org.apache.ignite.cache.store.jdbc.model.BinaryTest)

Example 5 with Person

use of org.apache.ignite.cache.store.jdbc.model.Person in project ignite by apache.

the class CacheJdbcPojoStoreAbstractSelfTest method checkPutRemove.

/**
 * Check put in cache and store it in db.
 *
 * @throws Exception If failed.
 */
private void checkPutRemove() throws Exception {
    boolean binaryMarshaller = marshaller() instanceof BinaryMarshaller || marshaller() == null;
    IgniteCache<Object, Person> c1 = grid().cache(CACHE_NAME);
    Connection conn = getConnection();
    try {
        PreparedStatement stmt = conn.prepareStatement("SELECT ID, ORG_ID, BIRTHDAY, NAME, GENDER FROM PERSON WHERE ID = ?");
        stmt.setInt(1, -1);
        ResultSet rs = stmt.executeQuery();
        assertFalse("Unexpected non empty result set", rs.next());
        U.closeQuiet(rs);
        Date testDate = Date.valueOf("2001-05-05");
        Gender testGender = Gender.random();
        Person val = new Person(-1, -2, testDate, "Person-to-test-put-insert", 999, testGender);
        Object key = builtinKeys ? Integer.valueOf(-1) : new PersonKey(-1);
        // Test put-insert.
        c1.put(key, val);
        rs = stmt.executeQuery();
        assertTrue("Unexpected empty result set", rs.next());
        assertEquals(-1, rs.getInt(1));
        assertEquals(-2, rs.getInt(2));
        assertEquals(testDate, rs.getDate(3));
        assertEquals("Person-to-test-put-insert", rs.getString(4));
        assertEquals(testGender.toString(), binaryMarshaller ? Gender.values()[rs.getInt(5)].toString() : rs.getString(5));
        assertFalse("Unexpected more data in result set", rs.next());
        U.closeQuiet(rs);
        // Test put-update.
        testDate = Date.valueOf("2016-04-04");
        c1.put(key, new Person(-1, -3, testDate, "Person-to-test-put-update", 999, testGender));
        rs = stmt.executeQuery();
        assertTrue("Unexpected empty result set", rs.next());
        assertEquals(-1, rs.getInt(1));
        assertEquals(-3, rs.getInt(2));
        assertEquals(testDate, rs.getDate(3));
        assertEquals("Person-to-test-put-update", rs.getString(4));
        assertEquals(testGender.toString(), binaryMarshaller ? Gender.values()[rs.getInt(5)].toString() : rs.getString(5));
        assertFalse("Unexpected more data in result set", rs.next());
        // Test remove.
        c1.remove(key);
        rs = stmt.executeQuery();
        assertFalse("Unexpected non-empty result set", rs.next());
        U.closeQuiet(rs);
    } finally {
        U.closeQuiet(conn);
    }
}
Also used : BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) PersonKey(org.apache.ignite.cache.store.jdbc.model.PersonKey) Connection(java.sql.Connection) DelegatingConnection(org.apache.commons.dbcp.DelegatingConnection) ResultSet(java.sql.ResultSet) DelegatingPreparedStatement(org.apache.commons.dbcp.DelegatingPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Gender(org.apache.ignite.cache.store.jdbc.model.Gender) Person(org.apache.ignite.cache.store.jdbc.model.Person) Date(java.sql.Date)

Aggregations

Person (org.apache.ignite.cache.store.jdbc.model.Person)5 PersonKey (org.apache.ignite.cache.store.jdbc.model.PersonKey)4 Test (org.junit.Test)4 Connection (java.sql.Connection)3 Date (java.sql.Date)3 PreparedStatement (java.sql.PreparedStatement)3 Organization (org.apache.ignite.cache.store.jdbc.model.Organization)3 OrganizationKey (org.apache.ignite.cache.store.jdbc.model.OrganizationKey)3 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 IgniteCache (org.apache.ignite.IgniteCache)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 BinaryObject (org.apache.ignite.binary.BinaryObject)2 BinaryTest (org.apache.ignite.cache.store.jdbc.model.BinaryTest)2 PersonComplexKey (org.apache.ignite.cache.store.jdbc.model.PersonComplexKey)2 CI2 (org.apache.ignite.internal.util.typedef.CI2)2 GridAbstractCacheStoreSelfTest (org.apache.ignite.testframework.junits.cache.GridAbstractCacheStoreSelfTest)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2