# ignite-32 Add store tests to suite.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/68dad5b7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/68dad5b7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/68dad5b7 Branch: refs/heads/ignite-188 Commit: 68dad5b772c28b3c216f3ddb14716741e4b3636d Parents: 3294c60 Author: anovikov <anovi...@gridgain.com> Authored: Fri Feb 6 15:32:50 2015 +0700 Committer: anovikov <anovi...@gridgain.com> Committed: Fri Feb 6 15:32:50 2015 +0700 ---------------------------------------------------------------------- .../store/jdbc/CacheAbstractJdbcStore.java | 11 +- .../core/src/test/config/store/jdbc/Ignite.xml | 50 +++++++ .../store/jdbc/CacheJdbcPojoStoreTest.java | 73 +++++++++- ...eJdbcStoreAbstractMultithreadedSelfTest.java | 98 +++++++++++-- .../store/jdbc/model/PersonComplexKey.java | 146 +++++++++++++++++++ .../ignite/testsuites/IgniteCacheTestSuite.java | 2 + 6 files changed, 360 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/68dad5b7/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java index 8e550f2..3d60a74 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java @@ -1448,9 +1448,16 @@ public abstract class CacheAbstractJdbcStore<K, V> extends CacheStore<K, V> impl ResultSet rs = stmt.executeQuery(); + ResultSetMetaData meta = rs.getMetaData(); + + Map<String, Integer> colIdxs = U.newHashMap(meta.getColumnCount()); + + for (int i = 1; i <= meta.getColumnCount(); i++) + colIdxs.put(meta.getColumnLabel(i), i); + while (rs.next()) { - K1 key = buildObject(em.keyType(), em.keyColumns(), em.loadColIdxs, rs); - V1 val = buildObject(em.valueType(), em.valueColumns(), em.loadColIdxs, rs); + K1 key = buildObject(em.keyType(), em.keyColumns(), colIdxs, rs); + V1 val = buildObject(em.valueType(), em.valueColumns(), colIdxs, rs); clo.apply(key, val); } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/68dad5b7/modules/core/src/test/config/store/jdbc/Ignite.xml ---------------------------------------------------------------------- diff --git a/modules/core/src/test/config/store/jdbc/Ignite.xml b/modules/core/src/test/config/store/jdbc/Ignite.xml index a1fc231..bb4502a 100644 --- a/modules/core/src/test/config/store/jdbc/Ignite.xml +++ b/modules/core/src/test/config/store/jdbc/Ignite.xml @@ -98,4 +98,54 @@ </list> </property> </bean> + <bean class="org.apache.ignite.cache.CacheTypeMetadata"> + <property name="databaseSchema" value="PUBLIC"/> + <property name="databaseTable" value="PERSON_COMPLEX"/> + <property name="keyType" value="org.apache.ignite.cache.store.jdbc.model.PersonComplexKey"/> + <property name="valueType" value="org.apache.ignite.cache.store.jdbc.model.Person"/> + <property name="keyFields"> + <list> + <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> + <property name="databaseName" value="ID"/> + <property name="databaseType" value="4"/> + <property name="javaName" value="id"/> + <property name="javaType" value="int"/> + </bean> + <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> + <property name="databaseName" value="ORG_ID"/> + <property name="databaseType" value="4"/> + <property name="javaName" value="orgId"/> + <property name="javaType" value="int"/> + </bean> + <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> + <property name="databaseName" value="CITY_ID"/> + <property name="databaseType" value="4"/> + <property name="javaName" value="cityId"/> + <property name="javaType" value="int"/> + </bean> + </list> + </property> + <property name="valueFields"> + <list> + <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> + <property name="databaseName" value="ID"/> + <property name="databaseType" value="4"/> + <property name="javaName" value="id"/> + <property name="javaType" value="java.lang.Integer"/> + </bean> + <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> + <property name="databaseName" value="ORG_ID"/> + <property name="databaseType" value="4"/> + <property name="javaName" value="orgId"/> + <property name="javaType" value="java.lang.Integer"/> + </bean> + <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> + <property name="databaseName" value="NAME"/> + <property name="databaseType" value="12"/> + <property name="javaName" value="name"/> + <property name="javaType" value="java.lang.String"/> + </bean> + </list> + </property> + </bean> </beans> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/68dad5b7/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java index 20805d1..0edc6f6 100644 --- a/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java +++ b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreTest.java @@ -90,16 +90,15 @@ public class CacheJdbcPojoStoreTest extends GridCommonAbstractTest { // PGPoolingDataSource ds = new PGPoolingDataSource(); // ds.setUser("postgres"); -// ds.setPassword("1"); -// ds.setServerName("192.168.1.47"); +// ds.setPassword("postgres"); +// ds.setServerName("ip"); // ds.setDatabaseName("postgres"); // store.setDataSource(ds); // MysqlDataSource ds = new MysqlDataSource(); -// ds.setURL("jdbc:mysql://192.168.1.12:3306/test"); -// ds.setUser("test"); -// ds.setPassword("1"); -// store.setDataSource(ds); +// ds.setURL("jdbc:mysql://ip:port/dbname"); +// ds.setUser("mysql"); +// ds.setPassword("mysql"); store.setDataSource(JdbcConnectionPool.create(DFLT_CONN_URL, "sa", "")); @@ -251,10 +250,28 @@ public class CacheJdbcPojoStoreTest extends GridCommonAbstractTest { U.closeQuiet(prnStmt); + PreparedStatement prnComplexStmt = conn.prepareStatement("INSERT INTO Person_Complex(id, org_id, city_id, name) 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); + + prnComplexStmt.addBatch(); + } + + prnComplexStmt.executeBatch(); + + U.closeQuiet(prnComplexStmt); + + conn.commit(); + U.closeQuiet(conn); final Collection<OrganizationKey> orgKeys = new ConcurrentLinkedQueue<>(); final Collection<PersonKey> prnKeys = new ConcurrentLinkedQueue<>(); + final Collection<PersonComplexKey> prnComplexKeys = new ConcurrentLinkedQueue<>(); IgniteBiInClosure<Object, Object> c = new CI2<Object, Object>() { @Override public void apply(Object k, Object v) { @@ -262,6 +279,17 @@ public class CacheJdbcPojoStoreTest extends GridCommonAbstractTest { orgKeys.add((OrganizationKey)k); else if (k instanceof PersonKey && v instanceof Person) prnKeys.add((PersonKey)k); + else if (k instanceof PersonComplexKey && v instanceof Person) { + PersonComplexKey key = (PersonComplexKey)k; + + Person val = (Person)v; + + assert key.getId() == val.getId(); + assert key.getOrgId() == val.getOrgId(); + assert ("name" + key.getId()).equals(val.getName()); + + prnComplexKeys.add((PersonComplexKey)k); + } } }; @@ -269,17 +297,36 @@ public class CacheJdbcPojoStoreTest extends GridCommonAbstractTest { assertEquals(ORGANIZATION_CNT, orgKeys.size()); assertEquals(PERSON_CNT, prnKeys.size()); + assertEquals(PERSON_CNT, prnComplexKeys.size()); - store.deleteAll(orgKeys); - store.deleteAll(prnKeys); + Collection<OrganizationKey> tmpOrgKeys = new ArrayList<>(orgKeys); + Collection<PersonKey> tmpPrnKeys = new ArrayList<>(prnKeys); + Collection<PersonComplexKey> 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()); } /** @@ -323,6 +370,15 @@ public class CacheJdbcPojoStoreTest extends GridCommonAbstractTest { ses.newSession(null); assertNull(store.load(k3)); + + OrganizationKey k4 = new OrganizationKey(4); + Organization v4 = new Organization(4, null, "City4"); + + assertNull(store.load(k4)); + + store.write(new CacheEntryImpl<>(k4, v4)); + + assertEquals(v4, store.load(k4)); } /** @@ -692,6 +748,7 @@ public class CacheJdbcPojoStoreTest extends GridCommonAbstractTest { stmt.executeUpdate("CREATE TABLE IF NOT EXISTS Organization (id integer not null, name varchar(50), city varchar(50), PRIMARY KEY(id))"); stmt.executeUpdate("CREATE TABLE IF NOT EXISTS Person (id integer not null, org_id integer, name varchar(50), PRIMARY KEY(id))"); + stmt.executeUpdate("CREATE TABLE IF NOT EXISTS Person_Complex (id integer not null, org_id integer not null, city_id integer not null, name varchar(50), PRIMARY KEY(id))"); conn.commit(); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/68dad5b7/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractMultithreadedSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractMultithreadedSelfTest.java b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractMultithreadedSelfTest.java index c9e1e77e..d1424c4 100644 --- a/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractMultithreadedSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcStoreAbstractMultithreadedSelfTest.java @@ -21,12 +21,14 @@ import org.apache.ignite.*; import org.apache.ignite.cache.*; import org.apache.ignite.cache.store.jdbc.model.*; import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.*; import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.spi.discovery.tcp.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; import org.apache.ignite.testframework.junits.common.*; +import org.apache.ignite.transactions.*; import org.jetbrains.annotations.*; import org.springframework.beans.*; import org.springframework.beans.factory.xml.*; @@ -42,6 +44,7 @@ import java.util.concurrent.*; import static org.apache.ignite.cache.CacheAtomicityMode.*; import static org.apache.ignite.cache.CacheMode.*; +import static org.apache.ignite.testframework.GridTestUtils.*; /** * @@ -55,13 +58,13 @@ public abstract class CacheJdbcStoreAbstractMultithreadedSelfTest<T extends Cach protected static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); /** Number of transactions. */ - private static final int TX_CNT = 1000; + private static final int TX_CNT = 200; /** Number of transactions. */ private static final int BATCH_CNT = 2000; /** Cache store. */ - protected T store; + protected static CacheAbstractJdbcStore store; /** {@inheritDoc} */ @Override protected void beforeTestsStarted() throws Exception { @@ -70,7 +73,6 @@ public abstract class CacheJdbcStoreAbstractMultithreadedSelfTest<T extends Cach /** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { - Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection(DFLT_CONN_URL, "sa", ""); Statement stmt = conn.createStatement(); @@ -81,16 +83,13 @@ public abstract class CacheJdbcStoreAbstractMultithreadedSelfTest<T extends Cach stmt.executeUpdate("CREATE TABLE Organization (id integer PRIMARY KEY, name varchar(50), city varchar(50))"); stmt.executeUpdate("CREATE TABLE Person (id integer PRIMARY KEY, org_id integer, name varchar(50))"); - stmt.executeUpdate("CREATE INDEX Org_Name_IDX On Organization (name)"); - stmt.executeUpdate("CREATE INDEX Org_Name_City_IDX On Organization (name, city)"); - stmt.executeUpdate("CREATE INDEX Person_Name_IDX1 On Person (name)"); - stmt.executeUpdate("CREATE INDEX Person_Name_IDX2 On Person (name desc)"); - conn.commit(); U.closeQuiet(stmt); U.closeQuiet(conn); + + startGrid(); } /** {@inheritDoc} */ @@ -164,9 +163,53 @@ public abstract class CacheJdbcStoreAbstractMultithreadedSelfTest<T extends Cach /** * @throws Exception If failed. */ - public void testMultithreadedPutAll() throws Exception { - startGrid(); + public void testMultithreadedPut() throws Exception { + IgniteInternalFuture<?> fut1 = runMultiThreadedAsync(new Callable<Object>() { + private final Random rnd = new Random(); + + @Override public Object call() throws Exception { + for (int i = 0; i < TX_CNT; i++) { + GridCache<Object, Object> cache = cache(); + + 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(), "Name" + id)); + } + + return null; + } + }, 4, "put"); + + IgniteInternalFuture<?> fut2 = runMultiThreadedAsync(new Callable<Object>() { + private final Random rnd = new Random(); + + @Override public Object call() throws Exception { + for (int i = 0; i < TX_CNT; i++) { + GridCache<Object, Object> cache = cache(); + + 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(), "Name" + id)); + } + + return null; + } + }, 8, "putIfAbsent"); + fut1.get(); + fut2.get(); + } + + /** + * @throws Exception If failed. + */ + public void testMultithreadedPutAll() throws Exception { multithreaded(new Callable<Object>() { private final Random rnd = new Random(); @@ -194,4 +237,39 @@ public abstract class CacheJdbcStoreAbstractMultithreadedSelfTest<T extends Cach } }, 8, "putAll"); } + + /** + * @throws Exception If failed. + */ + public void testMultithreadedExplicitTx() throws Exception { + runMultiThreaded(new Callable<Object>() { + private final Random rnd = new Random(); + + @Override public Object call() throws Exception { + for (int i = 0; i < TX_CNT; i++) { + GridCache<PersonKey, Person> cache = cache(); + + try (IgniteTx tx = cache.txStart()) { + cache.put(new PersonKey(1), new Person(1, rnd.nextInt(), "Name" + 1)); + cache.put(new PersonKey(2), new Person(2, rnd.nextInt(), "Name" + 2)); + cache.put(new PersonKey(3), new Person(3, rnd.nextInt(), "Name" + 3)); + + cache.get(new PersonKey(1)); + cache.get(new PersonKey(4)); + + Map<PersonKey, Person> map = U.newHashMap(2); + + map.put(new PersonKey(5), new Person(5, rnd.nextInt(), "Name" + 5)); + map.put(new PersonKey(6), new Person(6, rnd.nextInt(), "Name" + 6)); + + cache.putAll(map); + + tx.commit(); + } + } + + return null; + } + }, 8, "tx"); + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/68dad5b7/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/model/PersonComplexKey.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/model/PersonComplexKey.java b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/model/PersonComplexKey.java new file mode 100644 index 0000000..e797982 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/cache/store/jdbc/model/PersonComplexKey.java @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.cache.store.jdbc.model; + +import java.io.*; + +/** + * PersonComplexKey definition. + * + * Code generated by Apache Ignite Schema Load utility: 02/03/2015. + */ +public class PersonComplexKey implements Serializable { + /** */ + private static final long serialVersionUID = 0L; + + /** Value for id. */ + private int id; + + /** Value for org id. */ + private int orgId; + + /** Value for city id. */ + private int cityId; + + + /** + * Empty constructor. + */ + public PersonComplexKey() { + // No-op. + } + + /** + * Full constructor. + */ + public PersonComplexKey( + int id, + int orgId, + int cityId + ) { + this.id = id; + this.orgId = orgId; + this.cityId = cityId; + } + + /** + * Gets id. + * + * @return Value for id. + */ + public int getId() { + return id; + } + + /** + * Sets id. + * + * @param id New value for id. + */ + public void setId(int id) { + this.id = id; + } + + /** + * Gets orgId. + * + * @return Value for orgId. + */ + public int getOrgId() { + return orgId; + } + + /** + * Sets orgId. + * + * @param orgId New value for orgId. + */ + public void setOrgId(int orgId) { + this.orgId = orgId; + } + + /** + * Gets cityId. + */ + public int getCityId() { + return cityId; + } + + /** + * Sets city id. + * + * @param cityId New value for cityId. + */ + public void setCityId(int cityId) { + this.cityId = cityId; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + + if (!(o instanceof PersonComplexKey)) + return false; + + PersonComplexKey that = (PersonComplexKey)o; + + if (id != that.id || orgId != that.orgId || cityId != that.cityId) + return false; + + return true; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + int res = id; + + res = 31 * res + orgId; + res = 31 * res + cityId; + + return res; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "PersonKey [id=" + id + + ", orgId=" + orgId + + ", cityId=" + cityId + + "]"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/68dad5b7/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java index eb4e7ea..a126512 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java @@ -86,6 +86,8 @@ public class IgniteCacheTestSuite extends TestSuite { suite.addTestSuite(GridCacheConfigurationConsistencySelfTest.class); suite.addTestSuite(GridCacheJdbcBlobStoreSelfTest.class); suite.addTestSuite(GridCacheJdbcBlobStoreMultithreadedSelfTest.class); + suite.addTestSuite(CacheJdbcPojoStoreTest.class); + suite.addTestSuite(CacheJdbcPojoStoreMultitreadedSelfTest.class); suite.addTestSuite(GridCacheBalancingStoreSelfTest.class); suite.addTestSuite(GridCacheAffinityApiSelfTest.class); suite.addTestSuite(GridCacheStoreValueBytesSelfTest.class);