Yuta HIGUCHI

Conditionally update Cache

Change-Id: I97b4e537c15110b8962d585421cd4f4a14a82841
...@@ -73,6 +73,7 @@ public class DistributedDeviceStore ...@@ -73,6 +73,7 @@ public class DistributedDeviceStore
73 private IMap<byte[], byte[]> rawDevicePorts; 73 private IMap<byte[], byte[]> rawDevicePorts;
74 private LoadingCache<DeviceId, Optional<Map<PortNumber, Port>>> devicePorts; 74 private LoadingCache<DeviceId, Optional<Map<PortNumber, Port>>> devicePorts;
75 75
76 + @Override
76 @Activate 77 @Activate
77 public void activate() { 78 public void activate() {
78 super.activate(); 79 super.activate();
...@@ -361,5 +362,4 @@ public class DistributedDeviceStore ...@@ -361,5 +362,4 @@ public class DistributedDeviceStore
361 } 362 }
362 363
363 // TODO cache serialized DeviceID if we suffer from serialization cost 364 // TODO cache serialized DeviceID if we suffer from serialization cost
364 -
365 } 365 }
......
...@@ -6,6 +6,7 @@ import com.hazelcast.core.EntryAdapter; ...@@ -6,6 +6,7 @@ import com.hazelcast.core.EntryAdapter;
6 import com.hazelcast.core.EntryEvent; 6 import com.hazelcast.core.EntryEvent;
7 import com.hazelcast.core.HazelcastInstance; 7 import com.hazelcast.core.HazelcastInstance;
8 import com.hazelcast.core.MapEvent; 8 import com.hazelcast.core.MapEvent;
9 +
9 import org.apache.felix.scr.annotations.Activate; 10 import org.apache.felix.scr.annotations.Activate;
10 import org.apache.felix.scr.annotations.Component; 11 import org.apache.felix.scr.annotations.Component;
11 import org.apache.felix.scr.annotations.Reference; 12 import org.apache.felix.scr.annotations.Reference;
...@@ -86,8 +87,12 @@ public abstract class AbstractDistributedStore<E extends Event, D extends StoreD ...@@ -86,8 +87,12 @@ public abstract class AbstractDistributedStore<E extends Event, D extends StoreD
86 87
87 @Override 88 @Override
88 public void entryUpdated(EntryEvent<byte[], byte[]> event) { 89 public void entryUpdated(EntryEvent<byte[], byte[]> event) {
89 - cache.put(storeService.<K>deserialize(event.getKey()), 90 + K key = storeService.<K>deserialize(event.getKey());
90 - Optional.of(storeService.<V>deserialize(event.getValue()))); 91 + final V oldVal = storeService.<V>deserialize(event.getOldValue());
92 + Optional<V> oldValue = Optional.fromNullable(oldVal);
93 + final V newVal = storeService.<V>deserialize(event.getValue());
94 + Optional<V> newValue = Optional.of(newVal);
95 + cache.asMap().replace(key, oldValue, newValue);
91 } 96 }
92 97
93 @Override 98 @Override
...@@ -97,7 +102,10 @@ public abstract class AbstractDistributedStore<E extends Event, D extends StoreD ...@@ -97,7 +102,10 @@ public abstract class AbstractDistributedStore<E extends Event, D extends StoreD
97 102
98 @Override 103 @Override
99 public void entryAdded(EntryEvent<byte[], byte[]> event) { 104 public void entryAdded(EntryEvent<byte[], byte[]> event) {
100 - entryUpdated(event); 105 + K key = storeService.<K>deserialize(event.getKey());
106 + final V newVal = storeService.<V>deserialize(event.getValue());
107 + Optional<V> newValue = Optional.of(newVal);
108 + cache.asMap().putIfAbsent(key, newValue);
101 } 109 }
102 } 110 }
103 111
......