|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.j_spaces.javax.cache.AbstractCacheLoader
public abstract class AbstractCacheLoader
This class provides methods that convert the user's original object to GigaSpaces internal Entry representation - the IGSEntry
.
You should extend your custom CacheLoader
or CacheStore
from the AbstractCacheLoader and implement the relevant methods.
When using the AbstractCacheLoader.getConverter().toObject(IGSEntry) your Entry must include getter and setter methods.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface com.j_spaces.javax.cache.CacheLifeCycleManager |
---|
CacheLifeCycleManager.LifeCycleProperty |
Constructor Summary | |
---|---|
AbstractCacheLoader()
Deprecated. |
Method Summary | |
---|---|
void |
dropTable(String className)
Deprecated. drop table represented by this class name. |
Properties |
init(String dbURL,
String user,
String password,
Properties properties)
Deprecated. Called when space is started |
abstract Object |
load(Object key)
Deprecated. Loads value from secondary data storage in case if it's not in space cache. |
abstract Map |
loadAll(Collection keys)
Deprecated. Loads multiple values from secondary data storage in case they are not in cache. |
void |
shutdown()
Deprecated. Called when space is shut down |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public AbstractCacheLoader()
Method Detail |
---|
public Properties init(String dbURL, String user, String password, Properties properties)
CacheLifeCycleManager
init
in interface CacheLifeCycleManager
dbURL
- The StorageAdapterURL space config/schema property valueuser
- The userName space config/schema property valuepassword
- The password space config/schema property valueproperties
- Properties taken from property file
CacheLifeCycleManager.LifeCycleProperty
between the
underlying driver and the cache adapter; can be null
.public void shutdown()
CacheLifeCycleManager
shutdown
in interface CacheLifeCycleManager
public void dropTable(String className)
CacheLifeCycleManager
dropTable
in interface CacheLifeCycleManager
className
- representing the table to be dropped.public abstract Object load(Object key)
CacheLoader
load
in interface CacheLoader
key
- Key (an IGSEntry) of the object to load.
null
Space API Implementation Example:public Object load(Object key) { Object values[] = null; Object loaded_object = null; try { Object myObject = getObject((IGSEntry) key); int keyValue = 0; if (myObject instanceof Person) { Person person = null; // This is the template we got - must have ID person = (Person) myObject; // getting primary key value - another option could be via // ((IGSEntry) key).getPrimaryKeyName() keyValue = person.getId().intValue(); // Constructing the query Connection con = getConnection(); PreparedStatement stP = con.prepareStatement( "select * from " + tableNames.get(((IGSEntry) key) .getClassName()) + " where ID = ? "); stP.setInt(1, keyValue); ResultSet rs = stP.executeQuery(); int sz = rs.getMetaData().getColumnCount(); values = new Object[sz]; while (rs.next()) { for (int i = 0; i < sz; i++) { values[i] = rs.getObject(i + 1); } // here we construct the loaded Person object loaded_object = new Person(String.valueOf(values[0]), String.valueOf(values[1]), Integer.valueOf(String .valueOf(values[2]))); break; } rs.close(); rs.close(); con.close(); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } return getConvertor().toIGSEntry(loaded_object); }
Map API Implementation Example:public Object load(Object key) { Object values[] = null; Person loaded_person = null; Connection con = null; try { con = getConnection(); int keyValue = 0; Map.Entry mEntry = ((IGSEntry) key).getMapEntry(); if (mEntry != null) { keyValue = ((Integer) mEntry.getKey()).intValue(); } else { keyValue = ((Integer) getId((IGSEntry) key)).intValue(); } PreparedStatement stP = con.prepareStatement("select * from " + tableNames.get(Person.class.getName()) + " where ID = ? "); stP.setInt(1, keyValue); ResultSet rs = stP.executeQuery(); int sz = rs.getMetaData().getColumnCount(); values = new Object[sz]; while (rs.next()) { for (int i = 0; i < sz; i++) { values[i] = rs.getObject(i + 1); } // HERE WE MAP table row to Person object loaded_person = new Person(String.valueOf(values[0]), String .valueOf(values[1]), Integer.valueOf(String .valueOf(values[2]))); break; } rs.close(); con.close(); } catch (Exception e) { try { con.close(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } return getConvertor().toIGSEntry(loaded_person); }
public abstract Map loadAll(Collection keys)
CacheLoader
loadAll
in interface CacheLoader
keys
- Collection of (CacheQuery) Keys to load by.
Space API Implementation Examplepublic Map loadAll(Collection keys) { HashMap map = new HashMap(); Iterator keyIter = keys.iterator(); while (keyIter.hasNext()) { CacheQuery exprationEntry = (CacheQuery) keyIter.next(); Object query = exprationEntry.getQuery(); String querystr = null; // null template if (query instanceof IGSEntry) { IGSEntry igsentry = (IGSEntry) query; if (!tableNames.containsKey(igsentry.getClassName())) { System.out.println("Do not have mapping for class " + igsentry.getClassName()); if (igsentry.getClassName().equals("java.lang.Object")) { System.out.println("HERE YOU CAN LOAD DATA INTO THE SPACE WHEN IT IS STARTED!"); } // return empty map - indicates no objects loaded from database return map; } querystr = "select * from " + tableNames.get(igsentry.getClassName()); } // non null template or SQLQuery used if (query instanceof SQLQuery) { SQLQuery sqlquery = (SQLQuery) query; if (!tableNames.containsKey(sqlquery.getClassName())) { System.out.println("Do not have mapping for class " + sqlquery.getClassName()); // return empty map - indicates no objects loaded from database return map; } querystr = "select * from " + tableNames.get(sqlquery.getClassName()); querystr += " where " + sqlquery.getQuery(); } try { Connection con = getConnection(); PreparedStatement stP = con.prepareStatement( querystr); ResultSet rs = stP.executeQuery(); IGSEntry value = null; while (rs.next()) { Integer id = new Integer(rs.getInt(3)); Person person = new Person(rs.getString(1), rs.getString(2), id); value = getConvertor().toIGSEntry(person); map.put(value, value); } rs.close(); con.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } return map; }
Map API Implementation Examplepublic Map loadAll(Collection keys) { HashMap map = new HashMap(); Iterator keyIter = keys.iterator(); while (keyIter.hasNext()) { CacheQuery cacheQuery = (CacheQuery) keyIter.next(); Object query = cacheQuery.getQuery(); String sqlstr = null; sqlstr = "select * from Person"; Map.Entry mapentry = null; if (query instanceof Map.Entry) { mapentry = (Map.Entry)query; sqlstr += " where id = " + Integer.valueOf(mapentry.getKey().toString()).intValue(); } else if (query instanceof IGSEntry) { // All entries - for example IMap.values() call IGSEntry igsentry = (IGSEntry)query; if (igsentry.getClassName().equals("java.lang.Object")) { return map; } } else if (query instanceof SQLQuery) { SQLQuery sqlquery = (SQLQuery)query; if (sqlquery.getClassName().equals("java.lang.Object")) { return map; } sqlstr += " where " + sqlquery.getQuery(); } PreparedStatement stP =null; ResultSet rs = null; Connection con = null; try { con = getConnection(); stP = con.prepareStatement(sqlstr); rs = stP.executeQuery(); while (rs.next()) { Integer id = new Integer(rs.getInt(3)); Person person = new Person(rs.getString(1), rs.getString(2), id); IGSEntry igsEntry = getConvertor().toIGSEntry(person); map.put( igsEntry , igsEntry ); } rs.close(); con.close(); } catch (Exception e) { try { rs.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { con.close(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); throw new RuntimeException(e); } } return map; }
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |