Advertisement
tpeierls

IMap.replace(...) example

Jun 13th, 2011
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.32 KB | None | 0 0
  1. package com.hazelcast.util;
  2.  
  3. import com.hazelcast.config.*;
  4. import com.hazelcast.core.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7.  
  8. /**
  9.  * IMap.replace does not result in a call to MapStore.store, even though
  10.  * IMap.put does. Hazelcast 1.9.3.1.
  11.  */
  12. public class IMapReplaceExample {
  13.  
  14.     public static void main(String... args) {
  15.         Config config = new Config();
  16.         config
  17.             .getMapConfig("myMap")
  18.             .setMapStoreConfig(new MapStoreConfig()
  19.                 //.setWriteDelaySeconds(1)
  20.                 .setImplementation(myMapStore));
  21.         HazelcastInstance hc = Hazelcast.newHazelcastInstance(config);
  22.         try {
  23.             IMap<String, Long> myMap = hc.getMap("myMap");
  24.             myMap.put("one", 1L);
  25.             System.err.printf("main: Replacing 1 with 111.%n");
  26.             myMap.replace("one", 1L, 111L);
  27.             System.err.printf("main: Flushing map%n");
  28.             myMap.flush();
  29.             System.err.printf("main: myMap.get(one) returns %d%n", myMap.get("one"));
  30.             System.err.printf("main: Unconditional replace of value.%n");
  31.             myMap.replace("one", 1L);
  32.             System.err.printf("main: myMap.get(one) returns %d%n", myMap.get("one"));
  33.         } finally {
  34.             Hazelcast.shutdownAll();
  35.         }
  36.     }
  37.  
  38.     static final ConcurrentMap<String, Long> store = new ConcurrentHashMap<String, Long>();
  39.     static final MapStore<String, Long> myMapStore = new SimpleMapStore<String, Long>(store);
  40.  
  41.     static class SimpleMapStore<K, V> implements MapStore<K, V> {
  42.  
  43.         SimpleMapStore(ConcurrentMap<K, V> store) {
  44.             this.store = store;
  45.         }
  46.  
  47.         @Override public V load(K key) {
  48.             V value = store.get(key);
  49.             System.err.printf("SimpleMapStore.load(%s) = %s%n", key, value);
  50.             return value;
  51.         }
  52.  
  53.  
  54.         @Override public Map<K, V> loadAll(Collection<K> keys) {
  55.             Map<K, V> result = new HashMap<K, V>();
  56.             for (K key : keys) {
  57.                 V value = store.get(key);
  58.                 if (value != null) {
  59.                     result.put(key, value);
  60.                 }
  61.             }
  62.             System.err.printf("SimpleMapStore.loadAll(%s) = %s%n", keys, result);
  63.             return result;
  64.         }
  65.  
  66.         @Override public Set<K> loadAllKeys() {
  67.             Set<K> keys = store.keySet();
  68.             System.err.printf("SimpleMapStore.loadAllKeys() = %s%n", keys);
  69.             return keys;
  70.         }
  71.  
  72.  
  73.         //
  74.         // MapStore methods
  75.         //
  76.  
  77.         @Override public void store(K key, V value) {
  78.             System.err.printf("SimpleMapStore.store(%s, %s)%n", key, value);
  79.             store.put(key, value);
  80.         }
  81.  
  82.         @Override public void delete(K key) {
  83.             System.err.printf("SimpleMapStore.delete(%s)%n", key);
  84.             store.remove(key);
  85.         }
  86.  
  87.         @Override public void storeAll(Map<K, V> map) {
  88.             System.err.printf("SimpleMapStore.storeAll(%s)%n", map);
  89.             store.putAll(map);
  90.         }
  91.  
  92.         @Override public void deleteAll(Collection<K> keys) {
  93.             System.err.printf("SimpleMapStore.deleteAll(%s)%n", keys);
  94.             for (K key : keys) {
  95.                 store.remove(key);
  96.             }
  97.         }
  98.  
  99.         private final ConcurrentMap<K, V> store;
  100.     };
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement