Search in sources :

Example 1 with Organization

use of org.apache.ignite.cache.store.jdbc.model.Organization 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 2 with Organization

use of org.apache.ignite.cache.store.jdbc.model.Organization 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 3 with Organization

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

the class CacheJdbcPojoStoreTest method testWriteRetry.

/**
 * @throws Exception If failed.
 */
@Test
public void testWriteRetry() throws Exception {
    CacheJdbcPojoStore<Object, Object> store = store();
    // Special dialect that will skip updates, to test write retry.
    store.setDialect(new H2Dialect() {

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean hasMerge() {
            return false;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String updateQuery(String tblName, Collection<String> keyCols, Iterable<String> valCols) {
            return super.updateQuery(tblName, keyCols, valCols) + " AND 1 = 0";
        }
    });
    inject(store);
    Connection conn = store.openConnection(false);
    PreparedStatement orgStmt = conn.prepareStatement("INSERT INTO Organization(id, name, city) VALUES (?, ?, ?)");
    orgStmt.setInt(1, 1);
    orgStmt.setString(2, "name" + 1);
    orgStmt.setString(3, "city" + 1);
    orgStmt.executeUpdate();
    U.closeQuiet(orgStmt);
    conn.commit();
    OrganizationKey k1 = new OrganizationKey(1);
    Organization v1 = new Organization(1, "Name1", "City1");
    ses.newSession(null);
    try {
        store.write(new CacheEntryImpl<>(wrap(k1), wrap(v1)));
        fail("CacheWriterException wasn't thrown.");
    } catch (CacheWriterException e) {
        if (!e.getMessage().startsWith("Failed insert entry in database, violate a unique index or primary key") || e.getSuppressed().length != 2)
            throw e;
    }
}
Also used : Organization(org.apache.ignite.cache.store.jdbc.model.Organization) H2Dialect(org.apache.ignite.cache.store.jdbc.dialect.H2Dialect) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) OrganizationKey(org.apache.ignite.cache.store.jdbc.model.OrganizationKey) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheWriterException(javax.cache.integration.CacheWriterException) Test(org.junit.Test) GridAbstractCacheStoreSelfTest(org.apache.ignite.testframework.junits.cache.GridAbstractCacheStoreSelfTest) BinaryTest(org.apache.ignite.cache.store.jdbc.model.BinaryTest)

Example 4 with Organization

use of org.apache.ignite.cache.store.jdbc.model.Organization 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)

Aggregations

Organization (org.apache.ignite.cache.store.jdbc.model.Organization)4 OrganizationKey (org.apache.ignite.cache.store.jdbc.model.OrganizationKey)4 Test (org.junit.Test)4 Person (org.apache.ignite.cache.store.jdbc.model.Person)3 PersonKey (org.apache.ignite.cache.store.jdbc.model.PersonKey)3 MalformedURLException (java.net.MalformedURLException)2 Connection (java.sql.Connection)2 Date (java.sql.Date)2 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)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 GridAbstractCacheStoreSelfTest (org.apache.ignite.testframework.junits.cache.GridAbstractCacheStoreSelfTest)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 Nullable (org.jetbrains.annotations.Nullable)2 BeansException (org.springframework.beans.BeansException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1