Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datagrid; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 3 | import java.util.Collection; |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame^] | 4 | import java.util.Iterator; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 5 | import java.util.LinkedList; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 6 | import java.util.Set; |
Pavlin Radoslavov | 1c8d809 | 2014-02-14 15:47:24 -0800 | [diff] [blame] | 7 | import java.util.concurrent.CopyOnWriteArrayList; |
Pavlin Radoslavov | bcf1433 | 2014-03-27 18:15:30 -0700 | [diff] [blame] | 8 | import java.util.concurrent.TimeUnit; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 9 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.util.serializers.KryoFactory; |
| 11 | |
Jonathan Hart | 14c4a76 | 2014-04-15 10:35:57 -0700 | [diff] [blame] | 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
| 14 | |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 15 | import com.esotericsoftware.kryo.Kryo; |
| 16 | import com.esotericsoftware.kryo.io.Input; |
| 17 | import com.esotericsoftware.kryo.io.Output; |
| 18 | import com.hazelcast.core.EntryEvent; |
| 19 | import com.hazelcast.core.EntryListener; |
| 20 | import com.hazelcast.core.HazelcastInstance; |
| 21 | import com.hazelcast.core.IMap; |
| 22 | |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 23 | /** |
| 24 | * A datagrid event channel that uses Hazelcast as a datagrid. |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 25 | * |
| 26 | * @param <K> The class type of the key. |
| 27 | * @param <V> The class type of the value. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 28 | */ |
| 29 | public class HazelcastEventChannel<K, V> implements IEventChannel<K, V> { |
Jonathan Hart | 14c4a76 | 2014-04-15 10:35:57 -0700 | [diff] [blame] | 30 | private static final Logger log = |
| 31 | LoggerFactory.getLogger(HazelcastEventChannel.class); |
| 32 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 33 | private final HazelcastInstance hazelcastInstance; // The Hazelcast instance |
| 34 | private final String channelName; // The event channel name |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 35 | private final Class<K> typeK; // The class type of the key |
| 36 | private final Class<V> typeV; // The class type of the value |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 37 | private IMap<K, byte[]> channelMap; // The Hazelcast channel map |
Pavlin Radoslavov | 1c8d809 | 2014-02-14 15:47:24 -0800 | [diff] [blame] | 38 | // The channel listeners |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 39 | private final CopyOnWriteArrayList<IEventChannelListener<K, V>> listeners = |
| 40 | new CopyOnWriteArrayList<>(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 41 | |
| 42 | // The map entry listener |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 43 | private final EntryListener<K, byte[]> mapEntryListener = new MapEntryListener(); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 44 | private String mapListenerId; // The map listener ID |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 45 | |
| 46 | // TODO: We should use a single global KryoFactory instance |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 47 | private final KryoFactory kryoFactory = new KryoFactory(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 48 | |
| 49 | // Maximum serialized event size |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 50 | private static final int MAX_BUFFER_SIZE = 64 * 1024; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Constructor for a given event channel name. |
| 54 | * |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 55 | * @param newHazelcastInstance the Hazelcast instance to use. |
| 56 | * @param newChannelName the event channel name. |
| 57 | * @param newTypeK the type of the Key in the Key-Value store. |
| 58 | * @param newTypeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 59 | */ |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 60 | public HazelcastEventChannel(HazelcastInstance newHazelcastInstance, |
| 61 | String newChannelName, Class<K> newTypeK, |
| 62 | Class<V> newTypeV) { |
| 63 | hazelcastInstance = newHazelcastInstance; |
| 64 | channelName = newChannelName; |
| 65 | typeK = newTypeK; |
| 66 | typeV = newTypeV; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Verify the key and value types of a channel. |
| 71 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 72 | * @param typeKToVerify the type of the key to verify. |
| 73 | * @param typeVToVerify the type of the value to verify. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 74 | * @return true if the key and value types of the channel match, |
| 75 | * otherwise false. |
| 76 | */ |
| 77 | @Override |
Jonathan Hart | 14c4a76 | 2014-04-15 10:35:57 -0700 | [diff] [blame] | 78 | public boolean verifyKeyValueTypes(Class<?> typeKToVerify, |
| 79 | Class<?> typeVToVerify) { |
Ray Milkey | 7301965 | 2014-03-24 11:12:44 -0700 | [diff] [blame] | 80 | return (typeK.equals(typeKToVerify)) && (typeV.equals(typeVToVerify)); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Cleanup and destroy the channel. |
| 85 | */ |
| 86 | @Override |
| 87 | protected void finalize() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 88 | shutdown(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Startup the channel operation. |
| 93 | */ |
| 94 | @Override |
| 95 | public void startup() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 96 | if (channelMap == null) { |
| 97 | channelMap = hazelcastInstance.getMap(channelName); |
| 98 | mapListenerId = channelMap.addEntryListener(mapEntryListener, |
| 99 | true); |
| 100 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Shutdown the channel operation. |
| 105 | */ |
| 106 | @Override |
| 107 | public void shutdown() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 108 | if (channelMap != null) { |
| 109 | channelMap.removeEntryListener(mapListenerId); |
| 110 | channelMap = null; |
| 111 | mapListenerId = null; |
| 112 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Add event channel listener. |
| 117 | * |
| 118 | * @param listener the listener to add. |
| 119 | */ |
| 120 | @Override |
| 121 | public void addListener(IEventChannelListener<K, V> listener) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 122 | if (listeners.contains(listener)) { |
| 123 | return; // Nothing to do: already a listener |
| 124 | } |
| 125 | listeners.add(listener); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Remove event channel listener. |
| 130 | * |
| 131 | * @param listener the listener to remove. |
| 132 | */ |
| 133 | @Override |
| 134 | public void removeListener(IEventChannelListener<K, V> listener) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 135 | listeners.remove(listener); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Add an entry to the channel. |
| 140 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 141 | * @param key the key of the entry to add. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 142 | * @param value the value of the entry to add. |
| 143 | */ |
| 144 | @Override |
| 145 | public void addEntry(K key, V value) { |
Pavlin Radoslavov | bcf1433 | 2014-03-27 18:15:30 -0700 | [diff] [blame] | 146 | byte[] valueBytes = serializeValue(value); |
| 147 | // |
| 148 | // Put the entry in the map: |
| 149 | // - Key : Type <K> |
| 150 | // - Value : Serialized Value (byte[]) |
| 151 | // |
| 152 | channelMap.putAsync(key, valueBytes); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Add a transient entry to the channel. |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 157 | * <p/> |
Pavlin Radoslavov | bcf1433 | 2014-03-27 18:15:30 -0700 | [diff] [blame] | 158 | * The added entry is transient and will automatically timeout after 1ms. |
| 159 | * |
| 160 | * @param key the key of the entry to add. |
| 161 | * @param value the value of the entry to add. |
| 162 | */ |
| 163 | @Override |
| 164 | public void addTransientEntry(K key, V value) { |
| 165 | byte[] valueBytes = serializeValue(value); |
| 166 | // |
| 167 | // Put the entry in the map: |
| 168 | // - Key : Type <K> |
| 169 | // - Value : Serialized Value (byte[]) |
| 170 | // - Timeout: 1ms |
| 171 | // |
| 172 | channelMap.putAsync(key, valueBytes, 1L, TimeUnit.MILLISECONDS); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Serialize the value. |
| 177 | * |
| 178 | * @param value the value to serialize. |
| 179 | * @return the serialized value. |
| 180 | */ |
| 181 | private byte[] serializeValue(V value) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 182 | // |
| 183 | // Encode the value |
| 184 | // |
| 185 | byte[] buffer = new byte[MAX_BUFFER_SIZE]; |
| 186 | Kryo kryo = kryoFactory.newKryo(); |
| 187 | Output output = new Output(buffer, -1); |
Jonathan Hart | 14c4a76 | 2014-04-15 10:35:57 -0700 | [diff] [blame] | 188 | kryo.writeClassAndObject(output, value); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 189 | byte[] valueBytes = output.toBytes(); |
| 190 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 191 | |
Pavlin Radoslavov | bcf1433 | 2014-03-27 18:15:30 -0700 | [diff] [blame] | 192 | return valueBytes; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
Pavlin Radoslavov | 7439db1 | 2014-04-17 13:50:29 -0700 | [diff] [blame] | 196 | * Deserialize the value. |
| 197 | * |
| 198 | * @param kryo the Kryo instance to use for the deserialization. |
| 199 | * @param valueBytes the buffer with the serialized value. |
| 200 | * @return the deserialized value. |
| 201 | */ |
| 202 | private V deserializeValue(Kryo kryo, byte[] valueBytes) { |
| 203 | V value; |
| 204 | |
| 205 | // |
| 206 | // Decode the value |
| 207 | // |
| 208 | Input input = new Input(valueBytes); |
| 209 | Object objValue = kryo.readClassAndObject(input); |
| 210 | try { |
| 211 | value = typeV.cast(objValue); |
| 212 | } catch (ClassCastException e) { |
| 213 | log.error("Received notification value cast failed", e); |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | return value; |
| 218 | } |
| 219 | |
| 220 | /** |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 221 | * Remove an entry from the channel. |
| 222 | * |
| 223 | * @param key the key of the entry to remove. |
| 224 | */ |
| 225 | @Override |
| 226 | public void removeEntry(K key) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 227 | // |
| 228 | // Remove the entry: |
| 229 | // - Key : Type <K> |
| 230 | // - Value : Serialized Value (byte[]) |
| 231 | // |
| 232 | channelMap.removeAsync(key); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Update an entry in the channel. |
| 237 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 238 | * @param key the key of the entry to update. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 239 | * @param value the value of the entry to update. |
| 240 | */ |
| 241 | @Override |
| 242 | public void updateEntry(K key, V value) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 243 | // NOTE: Adding an entry with an existing key automatically updates it |
| 244 | addEntry(key, value); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get an entry from the channel. |
| 249 | * |
| 250 | * @param key the key of the entry to get. |
| 251 | * @return the entry if found, otherwise null. |
| 252 | */ |
| 253 | @Override |
| 254 | @Deprecated |
| 255 | public V getEntry(K key) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 256 | byte[] valueBytes = channelMap.get(key); |
| 257 | if (valueBytes == null) { |
| 258 | return null; |
| 259 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 260 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 261 | // |
| 262 | // Decode the value |
| 263 | // |
Pavlin Radoslavov | 7439db1 | 2014-04-17 13:50:29 -0700 | [diff] [blame] | 264 | Kryo kryo = kryoFactory.newKryo(); |
| 265 | V value = deserializeValue(kryo, valueBytes); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 266 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 267 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 268 | return value; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Get all entries in the channel. |
| 273 | * |
| 274 | * @return all entries that are currently in the channel. |
| 275 | */ |
| 276 | @Override |
| 277 | @Deprecated |
| 278 | public Collection<V> getAllEntries() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 279 | Collection<V> allEntries = new LinkedList<V>(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 280 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 281 | if (channelMap == null) { |
| 282 | return allEntries; // Nothing found |
| 283 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 284 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 285 | // |
| 286 | // Get all entries |
| 287 | // |
| 288 | Collection<byte[]> values = channelMap.values(); |
| 289 | Kryo kryo = kryoFactory.newKryo(); |
| 290 | for (byte[] valueBytes : values) { |
| 291 | // |
| 292 | // Decode the value |
| 293 | // |
Pavlin Radoslavov | 7439db1 | 2014-04-17 13:50:29 -0700 | [diff] [blame] | 294 | V value = deserializeValue(kryo, valueBytes); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 295 | allEntries.add(value); |
| 296 | } |
| 297 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 298 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 299 | return allEntries; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Remove all entries in the channel. |
| 304 | */ |
| 305 | @Override |
| 306 | @Deprecated |
| 307 | public void removeAllEntries() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 308 | // |
| 309 | // Remove all entries |
| 310 | // |
| 311 | // NOTE: We remove the entries one-by-one so the per-entry |
| 312 | // notifications will be delivered. |
| 313 | // |
| 314 | // channelMap.clear(); |
| 315 | Set<K> keySet = channelMap.keySet(); |
| 316 | for (K key : keySet) { |
| 317 | channelMap.removeAsync(key); |
| 318 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Class for receiving event notifications for the channel. |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 323 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 324 | * The datagrid map is: |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 325 | * - Key: Type K |
| 326 | * - Value: Serialized V (byte[]) |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 327 | */ |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 328 | private class MapEntryListener implements EntryListener<K, byte[]> { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 329 | /** |
| 330 | * Receive a notification that an entry is added. |
| 331 | * |
| 332 | * @param event the notification event for the entry. |
| 333 | */ |
| 334 | @Override |
| 335 | public void entryAdded(EntryEvent<K, byte[]> event) { |
| 336 | // |
| 337 | // Decode the value |
| 338 | // |
| 339 | byte[] valueBytes = event.getValue(); |
| 340 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 341 | try { |
| 342 | V value = deserializeValue(kryo, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 343 | |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 344 | // |
| 345 | // Deliver the notification |
| 346 | // |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame^] | 347 | final Iterator<IEventChannelListener<K, V>> it = listeners.iterator(); |
| 348 | while (it.hasNext()) { |
| 349 | final IEventChannelListener<K, V> listener = it.next(); |
| 350 | if (it.hasNext()) { |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 351 | // Each listener should get a deep copy of the value |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame^] | 352 | // TODO: compare which is faster |
| 353 | // - kryo.copy(value) |
| 354 | // - deserializeValue(kryo, valueBytes) |
| 355 | listener.entryAdded(kryo.copy(value)); |
| 356 | } else { |
| 357 | // Last listener can use the value |
| 358 | listener.entryAdded(value); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 359 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 360 | } |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 361 | } finally { |
| 362 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 363 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 364 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 365 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 366 | /** |
| 367 | * Receive a notification that an entry is removed. |
| 368 | * |
| 369 | * @param event the notification event for the entry. |
| 370 | */ |
| 371 | @Override |
| 372 | public void entryRemoved(EntryEvent<K, byte[]> event) { |
| 373 | // |
| 374 | // Decode the value |
| 375 | // |
| 376 | byte[] valueBytes = event.getValue(); |
| 377 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 378 | try { |
| 379 | V value = deserializeValue(kryo, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 380 | |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 381 | // |
| 382 | // Deliver the notification |
| 383 | // |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame^] | 384 | final Iterator<IEventChannelListener<K, V>> it = listeners.iterator(); |
| 385 | while (it.hasNext()) { |
| 386 | final IEventChannelListener<K, V> listener = it.next(); |
| 387 | if (it.hasNext()) { |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 388 | // Each listener should get a deep copy of the value |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame^] | 389 | listener.entryRemoved(kryo.copy(value)); |
| 390 | } else { |
| 391 | // Last listener can use the value |
| 392 | listener.entryRemoved(value); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 393 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 394 | } |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 395 | } finally { |
| 396 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 397 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 398 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 399 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 400 | /** |
| 401 | * Receive a notification that an entry is updated. |
| 402 | * |
| 403 | * @param event the notification event for the entry. |
| 404 | */ |
| 405 | @Override |
| 406 | public void entryUpdated(EntryEvent<K, byte[]> event) { |
| 407 | // |
| 408 | // Decode the value |
| 409 | // |
| 410 | byte[] valueBytes = event.getValue(); |
| 411 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 412 | try { |
| 413 | V value = deserializeValue(kryo, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 414 | |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 415 | // |
| 416 | // Deliver the notification |
| 417 | // |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame^] | 418 | final Iterator<IEventChannelListener<K, V>> it = listeners.iterator(); |
| 419 | while (it.hasNext()) { |
| 420 | final IEventChannelListener<K, V> listener = it.next(); |
| 421 | if (it.hasNext()) { |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 422 | // Each listener should get a deep copy of the value |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame^] | 423 | listener.entryUpdated(kryo.copy(value)); |
| 424 | } else { |
| 425 | // Last listener can use the value |
| 426 | listener.entryUpdated(value); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 427 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 428 | } |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 429 | } finally { |
| 430 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 431 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 432 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 433 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 434 | /** |
| 435 | * Receive a notification that an entry is evicted. |
| 436 | * |
| 437 | * @param event the notification event for the entry. |
| 438 | */ |
| 439 | @Override |
| 440 | public void entryEvicted(EntryEvent<K, byte[]> event) { |
| 441 | // NOTE: We don't use eviction for this map |
| 442 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 443 | } |
| 444 | } |