Skip to content

Commit

Permalink
# ignite-329 Changed example.
Browse files Browse the repository at this point in the history
  • Loading branch information
nva committed Mar 5, 2015
1 parent 7cd3cb4 commit e6693d4
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 18 deletions.
12 changes: 12 additions & 0 deletions 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');
2 changes: 1 addition & 1 deletion examples/config/store/example-jdbc-pojo-store.xml
Expand Up @@ -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">
Expand Down
1 change: 0 additions & 1 deletion examples/config/store/initdb.script

This file was deleted.

Expand Up @@ -74,16 +74,19 @@ public static IgniteConfiguration configure() throws IgniteException {
// 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);
Expand All @@ -95,10 +98,13 @@ public static IgniteConfiguration configure() throws IgniteException {
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");
Expand All @@ -112,6 +118,5 @@ private static Collection<CacheTypeMetadata> typeMetadata() {
));

return F.asList(tm);

}
}
Expand Up @@ -67,7 +67,7 @@ public static void main(String[] args) throws IgniteException {

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.");
}
}
}
Expand Up @@ -31,7 +31,7 @@

/**
* 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> {
Expand Down
Expand Up @@ -19,42 +19,65 @@

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.*;
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);
}
}
Expand Up @@ -33,8 +33,11 @@
* 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.
*/
Expand Down

0 comments on commit e6693d4

Please sign in to comment.