GigaSpaces XAP 7.0 API

com.j_spaces.javax.cache
Interface CacheStore

All Superinterfaces:
CacheLoader
All Known Subinterfaces:
MapDataSource<K,V>
All Known Implementing Classes:
HibernateMapDataSource

Deprecated. Use ExternalDataSource.

@Deprecated
public interface CacheStore
extends CacheLoader

The CacheStore implementation is provided to ensure read-through and write-through behavior of the space. 
Whenever an object is written into space or removed from space, some method on CacheStore is called to perform the operation in the secondary storage. 

Note that this interface extends CacheLoader interface to ensure read-through semantic. Please see CacheLoader interface for information on how to load objects into the space.
The store(Object key,Object value) method is invoked whenever any of the following methods are called: IMap.put(Object key, Object value) - without using transaction JavaSpace.write(Entry entry, Transaction transaction, long lease) - with null transaction IJSpace.write(Object entry, Transaction transaction, long lease) - with null transaction IJSpace.update(Object updatedEntry, Transaction transaction, long lease, long timeout) - with null transaction IJSpace.update(Entry updatedEntry, Transaction transaction, long lease, long timeout) - with null transaction IJSpace.update(Object updatedEntry, Transaction transaction, long lease, long timeout, int updateModifiers) - with null transaction IJSpace.update(Entry updatedEntry, Transaction transaction, long lease, long timeout, int updateModifiers) - with null transaction IJSpace.update(Entry template, Entry newEntry, Transaction txn, long lease)- with null transaction IMap.putAll(Map collection) - without using transaction IJSpace.writeMultiple(Object[] entries, Transaction txn, long lease) - with null transaction IJSpace.writeMultiple(Object[] entries, Transaction txn, long lease, int modifiers) - with null transaction IJSpace.writeMultiple(Entry[] entries, Transaction txn, long lease) - with null transaction IJSpace.updateMultiple(Object[] entries, Transaction transaction, long[] leases, int updateModifiers) - with null transaction IJSpace.updateMultiple(Object[] entries, Transaction transaction, long[] leases) - with null transaction
The key and value are IGSEntry objects, so you should cast these into IGSEntry to retrieve the original object data. By extending your CacheStore implementation from AbstractCacheLoader you will be able to convert the IGSEntry to your original object using: AbstractCashLoader.getConvertor().toObject(IGSEntry entry) - this will return your original Entry Object AbstractCashLoader.getConvertor().toIGSEntry(Object obj)- This will transfer your original Object to IGSEntry. This is required for the CacheLoader.load() return value method.
JavaSpace API: Object myObject = getConvertor().toObject((IGSEntry) value);
Map API: IGSEntry.MapEntry mEntry = ((IGSEntry) key).getMapEntry(); Object myKey = mEntry.getKey(); Object myValue= mEntry.getValue();
With this version of the product (5.1) both the key and value will provide the same object as part of the store(Object key,Object value) method.

The storeAll(Map map) method is invoked whenever any of the following methods are called: IMap.putAll(Map collection) - when using transaction and CacheBulk is not implemented IJSpace.writeMultiple(Object[] entries, Transaction txn, long lease) - with transaction and CacheBulk is not implemented IJSpace.writeMultiple(Object[] entries, Transaction txn, long lease, int modifiers) - with transaction and CacheBulk is not implemented IJSpace.writeMultiple(Entry[] entries, Transaction txn, long lease) - with transaction and CacheBulk is not implemented IJSpace.updateMultiple(Object[] entries, Transaction transaction, long[] leases, int updateModifiers)- with transaction and CacheBulk is not implemented IJSpace.updateMultiple(Object[] entries, Transaction transaction, long[] leases) - with transaction - with transaction and CacheBulk is not implemented
The erase(Object key) method is invoked whenever any of the following methods are called: IMap.remove(Object key) - without using transaction JavaSpace.take(Object entry, Transaction transaction, long timeout) - with null transaction JavaSpace.takeIfExists(Object entry, Transaction transaction, long timeout) - with null transaction IJSpace.takeMultiple(Entry template, Transaction txn, int maxEntries) - with null transaction IJSpace.takeMultiple(Object template, Transaction txn, int maxEntries) - with null transaction
The key is IGSEntry object, so you should cast it into IGSEntry to retrieve the original object data - same as the store method.
The eraseAll(Collection keys) method is invoked whenever any of the following methods are called: IJSpace.takeMultiple(Entry template, Transaction txn, int maxEntries) - with transaction and CacheBulk is not implemented IJSpace.takeMultiple(Object template, Transaction txn, int maxEntries) - with transaction and CacheBulk is not implemented
Configuration CacheStore is configured in space schema XML configuration file as a persistent adapter.

The <persistent> section should include the following:

<persistent> <enabled>true</enabled> <!-- name of the storage adapter class to be used by the space --> <StorageAdapterClass>com.j_spaces.sadapter.cache.CacheAdapter</StorageAdapterClass> <!-- CacheLoader or CacheStore implementation class name that will be used by space --> <CacheLoaderClass>com.j_spaces.examples.jdbccachestore.spaceapi.CacheStoreImpl</CacheLoaderClass> <DataBaseName>jdbc:hsqldb:testDB</DataBaseName> <userName>sa</userName> <password></password> <StorageAdapterURL>${com.gs.home}/GenericPersistProperties</StorageAdapterURL> </persistent>


You may also use the following space properties: space-config.persistent.enabled=true space-config.persistent.CacheLoaderClass=com.j_spaces.examples.jdbccachestore.spaceapi.CacheStoreImpl space-config.persistent.StorageAdapterClass=com.j_spaces.sadapter.cache.CacheAdapter space-config.persistent.DataBaseName=jdbc:hsqldb:hsql://localhost:9003 space-config.persistent.userName=sa space-config.persistent.password= space-config.engine.cache_policy=0
You must use space-config.engine.cache_policy=0 (LRU policy) to activate the CacheStore and CacheLoader implementation.
It is recommended to configure also the following to control the space memory usage and the eviction behavior: space-config.engine.memory_usage.enabled=true space-config.engine.memory_usage.high_watermark_percentage=95 space-config.engine.memory_usage.write_only_block_percentage=85 space-config.engine.memory_usage.write_only_check_percentage=76 space-config.engine.memory_usage.low_watermark_percentage=75 space-config.engine.memory_usage.eviction_batch_size=500
The space-config.persistent.DataBaseName , space-config.persistent.userName and space-config.persistent.password will be passed into the CacheLifeCycleManager constructor to be used by the CacheStore implementation. Note that only one, CacheLoader or CacheStore, but not both, is allowed to be used since CacheStore extends CacheLoader interface.


Method Summary
 void erase(Object key)
          Deprecated. Remove the specified key from the underlying store if present.
 void eraseAll(Collection keys)
          Deprecated. Remove the specified keys from the underlying store if present
 void store(Object key, Object value)
          Deprecated. Store the specified values under the specified keys in the store.
 void storeAll(Map map)
          Deprecated. Store the specified values under the specified keys in the underlying store.
 
Methods inherited from interface com.j_spaces.javax.cache.CacheLoader
load, loadAll
 

Method Detail

store

void store(Object key,
           Object value)
Deprecated. 
Store the specified values under the specified keys in the store. This method should support both create and update for the specified key.

Parameters:
key - key to store the value under
value - value to be stored
Space API Implementation Example:

        public void store(Object key, Object value) {
                try {
                        Object myObject = getObject((IGSEntry) value);
                        Person obj = null;
                        Connection con = getConnection();
                        if (myObject instanceof Person) {
                                obj = (Person) myObject;
                                int keyValue = obj.getId().intValue();
                                storePerson(keyValue, obj,con );
                        }
                        con.close();
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                }
        }

        private void storePerson(int key, Person person, Connection con)
                        throws SQLException {
                PreparedStatement stPd = con.prepareStatement("select ID from "
                                + tableNames.get(Person.class.getName()) + " where ID = ? ");
                stPd.setInt(1, key);

                ResultSet rs = stPd.executeQuery();
                // check if we need to perform update or insert
                if (rs.next()) {
                        stPd.close();
                        stPd = con.prepareStatement("update "
                                        + tableNames.get(Person.class.getName())
                                        + " set FirstName =? , LastName =? where ID = ? ");
                        stPd.setString(1, person.getFirstName());
                        stPd.setString(2, person.getLastName());
                        stPd.setInt(3, key);
                        stPd.executeUpdate();
                        stPd.close();
                } else {
                        stPd.close();
                        stPd = con.prepareStatement("insert into "
                                        + tableNames.get(Person.class.getName())
                                        + " (FirstName, LastName, ID) values(?,?,?) ");
                        stPd.setString(1, person.getFirstName());
                        stPd.setString(2, person.getLastName());
                        stPd.setInt(3, key);
                        stPd.executeUpdate();
                        stPd.close();
                }
        }

        
        Map API Implementation Example:
        
        public void store(Object key, Object value) {
                PreparedStatement stPd;
                try {
                        int keyValue;
                        Person obj;
                        IGSEntry.MapEntry mEntry = ((IGSEntry) key).getMapEntry();

                        key = mEntry.getKey();
                        obj = (Person) mEntry.getValue();
                        keyValue = ((Integer) key).intValue();
                        stPd = dbConnection.prepareStatement("select ID from "  + this.tableName + " where ID = ? ");
                        stPd.setInt(1, keyValue);

                        // check if we need to perform update or insert
                        ResultSet rs = stPd.executeQuery();

                        if (rs.next()) {
                                stPd.close();
                                stPd = dbConnection.prepareStatement("update " + this.tableName
                                                + " set FirstName =? , LastName =? where ID = ? ");
                                stPd.setString(1, obj.getFirstName());
                                stPd.setString(2, obj.getLastName());
                                stPd.setInt(3, keyValue);
                                stPd.executeUpdate();
                                stPd.close();
                        } else {
                                stPd.close();
                                stPd = dbConnection.prepareStatement("insert into "
                                                + this.tableName
                                                + " (FirstName, LastName, ID) values(?,?,?) ");
                                stPd.setString(1, obj.getFirstName());
                                stPd.setString(2, obj.getLastName());
                                stPd.setInt(3, keyValue);
                                stPd.executeUpdate();
                                stPd.close();
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                }
        }

        
        

storeAll

void storeAll(Map map)
Deprecated. 
Store the specified values under the specified keys in the underlying store. This method is intended to support both key/value creation and value update for the specified keys.

Parameters:
map - a Map of any number of keys and values to store
Implementation Example:

        public void storeAll(Map map) {
                Connection connection = null;
                try {
                        Object key = null;
                        Object myObject = null;
                        Person obj = null;
                        connection = getConnection();
                        connection.setAutoCommit(false);
                        // All objects will be stored in one transaction
                        Iterator keyIter = map.keySet().iterator();
                        while (keyIter.hasNext()) {
                                key = keyIter.next();
                                myObject = getObject((IGSEntry) key);
                                if (myObject instanceof Person) {
                                        obj = (Person) myObject;
                                        int keyValue = obj.getId().intValue();
                                        storePerson(keyValue, obj, connection);
                                }
                        }
                        // commit transaction
                        connection.commit();
                        connection.close();

                } catch (Exception e) {
                        try {
                                connection.rollback();
                                connection.close();
                        } catch (SQLException e1) {
                                e1.printStackTrace();
                        }
                        throw new RuntimeException(e);
                }
        }


erase

void erase(Object key)
Deprecated. 
Remove the specified key from the underlying store if present.

Parameters:
key - key whose mapping is being removed from the cache
Space API Implementation Example:
        public void erase(Object key) {
                try {
                        System.out.println(" ********* erase Called ********* ");
                        Object myObject = getConvertor().toObject((IGSEntry) key);

                        if (myObject instanceof Person) {
                                Person obj = (Person) myObject;
                                int keyValue = obj.getId().intValue();
                                erasePerson(keyValue, getConnection());
                        }

                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                }
        }
        private void erasePerson(int keyValue, Connection con) throws SQLException {
                PreparedStatement stPd = con.prepareStatement("delete from "
                                + tableNames.get(Person.class.getName()) + " where ID = ? ");
                stPd.setInt(1, keyValue);
                stPd.executeUpdate();
                stPd.close();
        }


Map API Implementation Example:

public void erase(Object key) {
                try {
                        System.out.println(" ********* erase Called ********* ");
                        Object myObject = getObject((IGSEntry) key);
                        Connection con = getConnection();
                        if (myObject instanceof Person) {
                                Person obj = (Person) myObject;
                                int keyValue = obj.getId().intValue();
                                erasePerson(keyValue, con);
                        }
                        con.close();
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                }
        }
        private void erasePerson(int keyValue, Connection con) throws SQLException {
                PreparedStatement stPd = con.prepareStatement("delete from "
                                + tableNames.get(Person.class.getName()) + " where ID = ? ");
                stPd.setInt(1, keyValue);
                stPd.executeUpdate();
                stPd.close();
        }


eraseAll

void eraseAll(Collection keys)
Deprecated. 
Remove the specified keys from the underlying store if present

Parameters:
keys - keys whose mappings are being removed from the cache

GigaSpaces XAP 7.0 API

Copyright © GigaSpaces.