# IGNITE-330 Store demo.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/52ee2353 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/52ee2353 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/52ee2353 Branch: refs/heads/ignite-368 Commit: 52ee2353941d6559e73c93d272e6a1172aa36f5b Parents: 435543f Author: AKuznetsov <akuznet...@gridgain.com> Authored: Sun Mar 15 21:25:55 2015 +0700 Committer: AKuznetsov <akuznet...@gridgain.com> Committed: Sun Mar 15 21:25:55 2015 +0700 ---------------------------------------------------------------------- examples/config/store/example-database.script | 22 +-- .../config/store/example-jdbc-pojo-store.xml | 142 ------------------- .../store/CacheNodeWithStoreStartup.java | 45 ++++-- .../datagrid/store/CacheStoreExample.java | 19 +++ .../store/jdbc/CacheJdbcPojoPersonStore.java | 11 -- .../datagrid/store/model/PersonKey.java | 97 ------------- 6 files changed, 66 insertions(+), 270 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/52ee2353/examples/config/store/example-database.script ---------------------------------------------------------------------- diff --git a/examples/config/store/example-database.script b/examples/config/store/example-database.script index 1397f28..80e418b 100644 --- a/examples/config/store/example-database.script +++ b/examples/config/store/example-database.script @@ -13,15 +13,15 @@ -- See the License for the specific language governing permissions and -- limitations under the License. -create table PERSONS(id bigint not null, first_name varchar(50), last_name varchar(50), PRIMARY KEY(id)); +create table PERSON(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'); +insert into PERSON(id, first_name, last_name) values(1, 'first-name-1', 'last-name-1'); +insert into PERSON(id, first_name, last_name) values(2, 'first-name-2', 'last-name-2'); +insert into PERSON(id, first_name, last_name) values(3, 'first-name-3', 'last-name-3'); +insert into PERSON(id, first_name, last_name) values(4, 'first-name-4', 'last-name-4'); +insert into PERSON(id, first_name, last_name) values(5, 'first-name-5', 'last-name-5'); +insert into PERSON(id, first_name, last_name) values(6, 'first-name-6', 'last-name-6'); +insert into PERSON(id, first_name, last_name) values(7, 'first-name-7', 'last-name-7'); +insert into PERSON(id, first_name, last_name) values(8, 'first-name-8', 'last-name-8'); +insert into PERSON(id, first_name, last_name) values(9, 'first-name-9', 'last-name-9'); +insert into PERSON(id, first_name, last_name) values(10, 'first-name-10', 'last-name-10'); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/52ee2353/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 deleted file mode 100644 index 4fc089f..0000000 --- a/examples/config/store/example-jdbc-pojo-store.xml +++ /dev/null @@ -1,142 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> - -<!-- - 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. - --> - -<!-- - Ignite Spring configuration file to startup Ignite cache. - - When starting a standalone node, you need to execute the following command: - {IGNITE_HOME}/bin/ignite.{bat|sh} examples/config/example-jdbc-pojo-store.xml - - When starting Ignite from Java IDE, pass path to this file to Ignition: - Ignition.start("examples/config/example-jdbc-pojo-store.xml"); ---> -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:util="http://www.springframework.org/schema/util" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation=" - http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/util - http://www.springframework.org/schema/util/spring-util.xsd"> - <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> - <!-- Set to true to enable distributed class loading for examples, default is false. --> - <property name="peerClassLoadingEnabled" value="true"/> - - <property name="marshaller"> - <bean class="org.apache.ignite.marshaller.optimized.OptimizedMarshaller"> - <!-- Set to false to allow non-serializable objects in examples, default is true. --> - <property name="requireSerializable" value="false"/> - </bean> - </property> - - <!-- Cache configuration. --> - <property name="cacheConfiguration"> - <list> - <bean class="org.apache.ignite.configuration.CacheConfiguration"> - <property name="name" value="partitioned"/> - <property name="cacheMode" value="PARTITIONED"/> - <property name="atomicityMode" value="ATOMIC"/> - <property name="distributionMode" value="PARTITIONED_ONLY"/> - <property name="preloadMode" value="SYNC"/> - <property name="backups" value="1"/> - - <property name="cacheStoreFactory"> - <bean class="javax.cache.configuration.FactoryBuilder$SingletonFactory"> - <constructor-arg> - <bean class="org.apache.ignite.examples.datagrid.store.jdbc.CacheJdbcPojoPersonStore"/> - </constructor-arg> - </bean> - </property> - - <property name="typeMetadata"> - <list> - <bean class="org.apache.ignite.cache.CacheTypeMetadata"> - <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"> - <list> - <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> - <property name="databaseName" value="ID"/> - <property name="databaseType"> - <util:constant static-field="java.sql.Types.BIGINT"/> - </property> - <property name="javaName" value="id"/> - </bean> - </list> - </property> - <property name="valueFields"> - <list> - <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> - <property name="databaseName" value="ID"/> - <property name="databaseType"> - <util:constant static-field="java.sql.Types.BIGINT"/> - </property> - <property name="javaName" value="id"/> - <property name="javaType" value="long"/> - </bean> - <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> - <property name="databaseName" value="FIRST_NAME"/> - <property name="databaseType"> - <util:constant static-field="java.sql.Types.VARCHAR"/> - </property> - <property name="javaName" value="firstName"/> - <property name="javaType" value="java.lang.String"/> - </bean> - <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> - <property name="databaseName" value="LAST_NAME"/> - <property name="databaseType"> - <util:constant static-field="java.sql.Types.VARCHAR"/> - </property> - <property name="javaName" value="lastName"/> - <property name="javaType" value="java.lang.String"/> - </bean> - </list> - </property> - </bean> - </list> - </property> - </bean> - </list> - </property> - - <!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. --> - <property name="discoverySpi"> - <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi"> - <property name="ipFinder"> - <!-- - Ignite provides several options for automatic discovery that can be used - instead os static IP based discovery. For information on all options refer - to our documentation: http://doc.gridgain.org/latest/Automatic+Node+Discovery - --> - <!-- Uncomment static IP finder to enable static-based discovery of initial nodes. --> - <!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">--> - <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder"> - <property name="addresses"> - <list> - <!-- In distributed environment, replace with actual host IP address. --> - <value>127.0.0.1:47500..47501</value> - </list> - </property> - </bean> - </property> - </bean> - </property> - </bean> -</beans> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/52ee2353/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 8d2cb7f..6304193 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 @@ -22,6 +22,8 @@ import org.apache.ignite.cache.*; import org.apache.ignite.cache.store.*; import org.apache.ignite.configuration.*; import org.apache.ignite.examples.datagrid.store.dummy.*; +import org.apache.ignite.examples.datagrid.store.hibernate.*; +import org.apache.ignite.examples.datagrid.store.jdbc.*; import org.apache.ignite.examples.datagrid.store.model.*; import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.spi.discovery.tcp.*; @@ -35,9 +37,24 @@ import java.util.*; import static org.apache.ignite.cache.CacheAtomicityMode.*; /** - * Starts up an empty node with example cache configuration. + * Starts up an empty node with example cache and store configuration. */ public class CacheNodeWithStoreStartup { + /** Use org.apache.ignite.examples.datagrid.store.dummy.CacheDummyPersonStore to run example. */ + public static final String DUMMY = "DUMMY"; + + /** Use org.apache.ignite.examples.datagrid.store.jdbc.CacheJdbcPersonStore to run example. */ + public static final String SIMPLE_JDBC = "SIMPLE_JDBC"; + + /** Use org.apache.ignite.examples.datagrid.store.hibernate.CacheHibernatePersonStore to run example. */ + public static final String HIBERNATE = "HIBERNATE"; + + /** Use org.apache.ignite.examples.datagrid.store.jdbc.CacheJdbcPojoPersonStore to run example. */ + public static final String AUTO = "AUTO"; + + /** Store to use. */ + public static final String STORE = DUMMY; + /** * Start up an empty node with specified cache configuration. * @@ -78,14 +95,24 @@ public class CacheNodeWithStoreStartup { CacheStore<Long, Person> store; - // Uncomment other cache stores to try them. - store = new CacheDummyPersonStore(); - // store = new CacheJdbcPersonStore(); - // store = new CacheHibernatePersonStore(); + switch (STORE) { + case DUMMY: + store = new CacheDummyPersonStore(); + break; + + case SIMPLE_JDBC: + store = new CacheJdbcPersonStore(); + break; + + case HIBERNATE: + store = new CacheHibernatePersonStore(); + break; - // Uncomment two lines for try CacheJdbcPojoStore. - // store = new CacheJdbcPojoPersonStore(); - // cacheCfg.setTypeMetadata(typeMetadata()); + default: + store = new CacheJdbcPojoPersonStore(); + cacheCfg.setTypeMetadata(typeMetadata()); + break; + } cacheCfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<>(store)); cacheCfg.setReadThrough(true); @@ -103,7 +130,7 @@ public class CacheNodeWithStoreStartup { private static Collection<CacheTypeMetadata> typeMetadata() { CacheTypeMetadata tm = new CacheTypeMetadata(); - tm.setDatabaseTable("PERSONS"); + tm.setDatabaseTable("PERSON"); tm.setKeyType("java.lang.Long"); tm.setValueType("org.apache.ignite.examples.datagrid.store.model.Person"); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/52ee2353/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java index ce1076d..051d5fc 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java @@ -24,6 +24,8 @@ import org.apache.ignite.transactions.*; import java.util.*; +import static org.apache.ignite.examples.datagrid.store.CacheNodeWithStoreStartup.*; + /** * Demonstrates usage of cache with underlying persistent store configured. * <p> @@ -48,6 +50,7 @@ public class CacheStoreExample { try (Ignite ignite = Ignition.start(cfg)) { System.out.println(); System.out.println(">>> Cache store example started."); + System.out.println(">>> Store: " + STORE); IgniteCache<Long, Person> cache = ignite.jcache(null); @@ -71,6 +74,22 @@ public class CacheStoreExample { } System.out.println("Read value after commit: " + cache.get(id)); + + // If example run with CacheJdbcPojoStore. + // Example of CacheJdbcPojoStore special features. + if (STORE.equals(AUTO)) { + System.out.println(">>> Example of CacheJdbcPojoStore special feature: load from DB with custom SQL."); + + cache.clear(); + + System.out.println("Cache size: " + cache.size()); + + // Load values from DB into store with custom SQL. + cache.loadCache(null, "java.lang.Long", "select * from PERSON where id = 2"); + + System.out.println("Cache size: " + cache.size()); + System.out.println("Person: " + cache.get(2L)); + } } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/52ee2353/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 0160203..a0d91c5 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 @@ -82,15 +82,4 @@ public class CacheJdbcPojoPersonStore extends CacheJdbcPojoStore<Long, Person> { throw new IgniteException("Failed to find example database script: " + script.getPath(), e); } } - - /** {@inheritDoc} */ - @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 PERSONS limit " + entryCnt); - } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/52ee2353/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/PersonKey.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/PersonKey.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/PersonKey.java deleted file mode 100644 index 74da78b..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/PersonKey.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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.examples.datagrid.store.model; - -import java.io.*; - -/** - * PersonKey definition. - * - * Code generated by Apache Ignite Schema Import utility: 02/24/2015. - */ -public class PersonKey implements Serializable { - /** */ - private static final long serialVersionUID = 0L; - - /** Value for id. */ - private long id; - - /** - * Empty constructor. - */ - public PersonKey() { - // No-op. - } - - /** - * Full constructor. - */ - public PersonKey( - long id - ) { - this.id = id; - } - - /** - * Gets id. - * - * @return Value for id. - */ - public long getId() { - return id; - } - - /** - * Sets id. - * - * @param id New value for id. - */ - public void setId(long id) { - this.id = id; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (!(o instanceof PersonKey)) - return false; - - PersonKey that = (PersonKey)o; - - if (id != that.id) - return false; - - return true; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = (int)(id ^ (id >>> 32)); - - return res; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "PersonKey [id=" + id + - "]"; - } -} -