# ignite-329 Changed example.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/e6693d4c Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/e6693d4c Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/e6693d4c Branch: refs/heads/ignite-443 Commit: e6693d4ca7467c95e6737d87c13fa19a4dfc2e78 Parents: 7cd3cb4 Author: anovikov <anovi...@gridgain.com> Authored: Thu Mar 5 18:59:02 2015 +0700 Committer: anovikov <anovi...@gridgain.com> Committed: Thu Mar 5 18:59:02 2015 +0700 ---------------------------------------------------------------------- examples/config/store/example-database.script | 12 +++++++ .../config/store/example-jdbc-pojo-store.xml | 2 +- examples/config/store/initdb.script | 1 - .../store/CacheNodeWithStoreStartup.java | 17 +++++---- .../store/CacheStoreLoadDataExample.java | 2 +- .../store/jdbc/CacheJdbcPersonStore.java | 2 +- .../store/jdbc/CacheJdbcPojoPersonStore.java | 37 ++++++++++++++++---- .../cache/store/jdbc/CacheJdbcPojoStore.java | 5 ++- 8 files changed, 60 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e6693d4c/examples/config/store/example-database.script ---------------------------------------------------------------------- diff --git a/examples/config/store/example-database.script b/examples/config/store/example-database.script new file mode 100644 index 0000000..781e69c --- /dev/null +++ b/examples/config/store/example-database.script @@ -0,0 +1,12 @@ +create table PERSONS(id bigint not null, first_name varchar(50), last_name varchar(50), PRIMARY KEY(id)); + +insert into PERSONS(id, first_name, last_name) values(1, 'first-name-1', 'last-name-1'); +insert into PERSONS(id, first_name, last_name) values(2, 'first-name-2', 'last-name-2'); +insert into PERSONS(id, first_name, last_name) values(3, 'first-name-3', 'last-name-3'); +insert into PERSONS(id, first_name, last_name) values(4, 'first-name-4', 'last-name-4'); +insert into PERSONS(id, first_name, last_name) values(5, 'first-name-5', 'last-name-5'); +insert into PERSONS(id, first_name, last_name) values(6, 'first-name-6', 'last-name-6'); +insert into PERSONS(id, first_name, last_name) values(7, 'first-name-7', 'last-name-7'); +insert into PERSONS(id, first_name, last_name) values(8, 'first-name-8', 'last-name-8'); +insert into PERSONS(id, first_name, last_name) values(9, 'first-name-9', 'last-name-9'); +insert into PERSONS(id, first_name, last_name) values(10, 'first-name-10', 'last-name-10'); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e6693d4c/examples/config/store/example-jdbc-pojo-store.xml ---------------------------------------------------------------------- diff --git a/examples/config/store/example-jdbc-pojo-store.xml b/examples/config/store/example-jdbc-pojo-store.xml index 8eebbe4..80e5fe1 100644 --- a/examples/config/store/example-jdbc-pojo-store.xml +++ b/examples/config/store/example-jdbc-pojo-store.xml @@ -79,7 +79,7 @@ <property name="typeMetadata"> <list> <bean class="org.apache.ignite.cache.CacheTypeMetadata"> - <property name="databaseTable" value="PERSON"/> + <property name="databaseTable" value="PERSONS"/> <property name="keyType" value="java.lang.Long"/> <property name="valueType" value="org.apache.ignite.examples.datagrid.store.model.Person"/> <property name="keyFields"> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e6693d4c/examples/config/store/initdb.script ---------------------------------------------------------------------- diff --git a/examples/config/store/initdb.script b/examples/config/store/initdb.script deleted file mode 100644 index 8d768ba..0000000 --- a/examples/config/store/initdb.script +++ /dev/null @@ -1 +0,0 @@ -CREATE TABLE Person(id bigint not null, first_name varchar(50), last_name varchar(50), PRIMARY KEY(id)); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e6693d4c/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java index 4d39e86..568435e 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java @@ -74,16 +74,19 @@ public class CacheNodeWithStoreStartup { // Set atomicity as transaction, since we are showing transactions in example. cacheCfg.setAtomicityMode(TRANSACTIONAL); + // Set query indexing enabled for use query in example. + cacheCfg.setQueryIndexEnabled(true); + CacheStore<Long, Person> store; // Uncomment other cache stores to try them. -// store = new CacheDummyPersonStore(); + store = new CacheDummyPersonStore(); // store = new CacheJdbcPersonStore(); // store = new CacheHibernatePersonStore(); - // Uncomment two lines for try . - store = new CacheJdbcPojoPersonStore(); - cacheCfg.setTypeMetadata(typeMetadata()); + // Uncomment two lines for try CacheJdbcPojoStore. +// store = new CacheJdbcPojoPersonStore(); +// cacheCfg.setTypeMetadata(typeMetadata()); cacheCfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<>(store)); cacheCfg.setReadThrough(true); @@ -95,10 +98,13 @@ public class CacheNodeWithStoreStartup { return cfg; } + /** + * + */ private static Collection<CacheTypeMetadata> typeMetadata() { CacheTypeMetadata tm = new CacheTypeMetadata(); - tm.setDatabaseTable("PERSON"); + tm.setDatabaseTable("PERSONS"); tm.setKeyType("java.lang.Long"); tm.setValueType("org.apache.ignite.examples.datagrid.store.model.Person"); @@ -112,6 +118,5 @@ public class CacheNodeWithStoreStartup { )); return F.asList(tm); - } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e6693d4c/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreLoadDataExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreLoadDataExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreLoadDataExample.java index e456579..8a47c1a 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreLoadDataExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreLoadDataExample.java @@ -67,7 +67,7 @@ public class CacheStoreLoadDataExample { long end = System.currentTimeMillis(); - System.out.println(">>> Loaded " + ENTRY_COUNT + " keys with backups in " + (end - start) + "ms."); + System.out.println(">>> Loaded " + cache.size() +" keys with backups in " + (end - start) + "ms."); } } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e6693d4c/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPersonStore.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPersonStore.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPersonStore.java index 2faa226..252c037 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPersonStore.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPersonStore.java @@ -31,7 +31,7 @@ import java.util.*; /** * Example of {@link CacheStore} implementation that uses JDBC - * transaction with cache transactions and maps {@link UUID} to {@link Person}. + * transaction with cache transactions and maps {@link Long} to {@link Person}. * */ public class CacheJdbcPersonStore extends CacheStoreAdapter<Long, Person> { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e6693d4c/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java index 1996a4d..778e005 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java @@ -19,6 +19,7 @@ package org.apache.ignite.examples.datagrid.store.jdbc; import org.apache.ignite.*; import org.apache.ignite.cache.store.jdbc.*; +import org.apache.ignite.examples.datagrid.store.model.*; import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.lang.*; import org.h2.tools.*; @@ -26,35 +27,57 @@ import org.jetbrains.annotations.*; import javax.cache.integration.*; import java.io.*; +import java.sql.*; /** - * TODO: Add class description. + * Example of {@link CacheJdbcPojoStore} implementation that uses JDBC + * transaction with cache transactions and maps {@link Long} to {@link Person}. */ -public class CacheJdbcPojoPersonStore<K, V> extends CacheJdbcPojoStore { +public class CacheJdbcPojoPersonStore extends CacheJdbcPojoStore<Long, Person> { + /** + * Constructor. + * + * @throws IgniteException If failed. + */ public CacheJdbcPojoPersonStore() throws IgniteException { + // Construct example database in memory. dataSrc = org.h2.jdbcx.JdbcConnectionPool.create("jdbc:h2:mem:ExampleDb;DB_CLOSE_DELAY=-1", "sa", ""); - File script = U.resolveIgnitePath("examples/config/store/initdb.script"); + prepareDb(); + } + + /** + * Prepares database for example execution. This method will create a + * table called "PERSONS" so it can be used by store implementation. + * + * @throws IgniteException If failed. + */ + private void prepareDb() throws IgniteException { + File script = U.resolveIgnitePath("examples/config/store/example-database.script"); if (script == null) - throw new IgniteException("Failed to find initial database script: " + "examples/config/store/initdb.script"); + throw new IgniteException("Failed to find example database script: " + + "examples/config/store/example-database.script"); try { RunScript.execute(dataSrc.getConnection(), new FileReader(script)); } - catch (Exception e) { + catch (SQLException e) { throw new IgniteException("Failed to initialize database", e); } + catch (FileNotFoundException e) { + throw new IgniteException("Failed to find example database script: " + script.getPath(), e); + } } /** {@inheritDoc} */ - @Override public void loadCache(IgniteBiInClosure<Object, Object> clo, @Nullable Object... args) + @Override public void loadCache(IgniteBiInClosure<Long, Person> clo, @Nullable Object... args) throws CacheLoaderException { if (args == null || args.length == 0 || args[0] == null) throw new CacheLoaderException("Expected entry count parameter is not provided."); final int entryCnt = (Integer)args[0]; - super.loadCache(clo, "java.lang.Long", "select * from PERSON limit " + entryCnt); + super.loadCache(clo, "java.lang.Long", "select * from PERSONS limit " + entryCnt); } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e6693d4c/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java index 162f7de..620c396 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java @@ -33,8 +33,11 @@ import java.util.*; * Base class for {@link CacheStore} that implementation backed by JDBC and POJO via reflection. * * This implementation stores objects in underlying database using java beans mapping description via reflection. + * + * @param <K> the type of keys handled by this loader + * @param <V> the type of values generated by this loader */ -public class CacheJdbcPojoStore extends CacheAbstractJdbcStore<Object, Object> { +public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K, V> { /** * POJO methods cache. */