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(); |
Yuta HIGUCHI | e8bfe61 | 2014-06-08 14:20:51 -0700 | [diff] [blame^] | 187 | try { |
| 188 | Output output = new Output(buffer, -1); |
| 189 | kryo.writeClassAndObject(output, value); |
| 190 | byte[] valueBytes = output.toBytes(); |
| 191 | return valueBytes; |
| 192 | } finally { |
| 193 | kryoFactory.deleteKryo(kryo); |
| 194 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
Pavlin Radoslavov | 7439db1 | 2014-04-17 13:50:29 -0700 | [diff] [blame] | 198 | * Deserialize the value. |
| 199 | * |
| 200 | * @param kryo the Kryo instance to use for the deserialization. |
| 201 | * @param valueBytes the buffer with the serialized value. |
| 202 | * @return the deserialized value. |
| 203 | */ |
| 204 | private V deserializeValue(Kryo kryo, byte[] valueBytes) { |
| 205 | V value; |
| 206 | |
| 207 | // |
| 208 | // Decode the value |
| 209 | // |
| 210 | Input input = new Input(valueBytes); |
| 211 | Object objValue = kryo.readClassAndObject(input); |
| 212 | try { |
| 213 | value = typeV.cast(objValue); |
| 214 | } catch (ClassCastException e) { |
| 215 | log.error("Received notification value cast failed", e); |
| 216 | return null; |
| 217 | } |
| 218 | |
| 219 | return value; |
| 220 | } |
| 221 | |
| 222 | /** |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 223 | * Remove an entry from the channel. |
| 224 | * |
| 225 | * @param key the key of the entry to remove. |
| 226 | */ |
| 227 | @Override |
| 228 | public void removeEntry(K key) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 229 | // |
| 230 | // Remove the entry: |
| 231 | // - Key : Type <K> |
| 232 | // - Value : Serialized Value (byte[]) |
| 233 | // |
| 234 | channelMap.removeAsync(key); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Update an entry in the channel. |
| 239 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 240 | * @param key the key of the entry to update. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 241 | * @param value the value of the entry to update. |
| 242 | */ |
| 243 | @Override |
| 244 | public void updateEntry(K key, V value) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 245 | // NOTE: Adding an entry with an existing key automatically updates it |
| 246 | addEntry(key, value); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get an entry from the channel. |
| 251 | * |
| 252 | * @param key the key of the entry to get. |
| 253 | * @return the entry if found, otherwise null. |
| 254 | */ |
| 255 | @Override |
| 256 | @Deprecated |
| 257 | public V getEntry(K key) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 258 | byte[] valueBytes = channelMap.get(key); |
| 259 | if (valueBytes == null) { |
| 260 | return null; |
| 261 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 262 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 263 | // |
| 264 | // Decode the value |
| 265 | // |
Pavlin Radoslavov | 7439db1 | 2014-04-17 13:50:29 -0700 | [diff] [blame] | 266 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | e8bfe61 | 2014-06-08 14:20:51 -0700 | [diff] [blame^] | 267 | try { |
| 268 | V value = deserializeValue(kryo, valueBytes); |
| 269 | return value; |
| 270 | } finally { |
| 271 | kryoFactory.deleteKryo(kryo); |
| 272 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get all entries in the channel. |
| 277 | * |
| 278 | * @return all entries that are currently in the channel. |
| 279 | */ |
| 280 | @Override |
| 281 | @Deprecated |
| 282 | public Collection<V> getAllEntries() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 283 | Collection<V> allEntries = new LinkedList<V>(); |
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 | if (channelMap == null) { |
| 286 | return allEntries; // Nothing found |
| 287 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 288 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 289 | // |
| 290 | // Get all entries |
| 291 | // |
| 292 | Collection<byte[]> values = channelMap.values(); |
| 293 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | e8bfe61 | 2014-06-08 14:20:51 -0700 | [diff] [blame^] | 294 | try { |
| 295 | for (byte[] valueBytes : values) { |
| 296 | // |
| 297 | // Decode the value |
| 298 | // |
| 299 | V value = deserializeValue(kryo, valueBytes); |
| 300 | allEntries.add(value); |
| 301 | } |
| 302 | } finally { |
| 303 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 304 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 305 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 306 | return allEntries; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Remove all entries in the channel. |
| 311 | */ |
| 312 | @Override |
| 313 | @Deprecated |
| 314 | public void removeAllEntries() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 315 | // |
| 316 | // Remove all entries |
| 317 | // |
| 318 | // NOTE: We remove the entries one-by-one so the per-entry |
| 319 | // notifications will be delivered. |
| 320 | // |
| 321 | // channelMap.clear(); |
| 322 | Set<K> keySet = channelMap.keySet(); |
| 323 | for (K key : keySet) { |
| 324 | channelMap.removeAsync(key); |
| 325 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Class for receiving event notifications for the channel. |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 330 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 331 | * The datagrid map is: |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 332 | * - Key: Type K |
| 333 | * - Value: Serialized V (byte[]) |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 334 | */ |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 335 | private class MapEntryListener implements EntryListener<K, byte[]> { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 336 | /** |
| 337 | * Receive a notification that an entry is added. |
| 338 | * |
| 339 | * @param event the notification event for the entry. |
| 340 | */ |
| 341 | @Override |
| 342 | public void entryAdded(EntryEvent<K, byte[]> event) { |
| 343 | // |
| 344 | // Decode the value |
| 345 | // |
| 346 | byte[] valueBytes = event.getValue(); |
| 347 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 348 | try { |
| 349 | V value = deserializeValue(kryo, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 350 | |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 351 | // |
| 352 | // Deliver the notification |
| 353 | // |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 354 | final Iterator<IEventChannelListener<K, V>> it = listeners.iterator(); |
| 355 | while (it.hasNext()) { |
| 356 | final IEventChannelListener<K, V> listener = it.next(); |
| 357 | if (it.hasNext()) { |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 358 | // Each listener should get a deep copy of the value |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 359 | // TODO: compare which is faster |
| 360 | // - kryo.copy(value) |
| 361 | // - deserializeValue(kryo, valueBytes) |
| 362 | listener.entryAdded(kryo.copy(value)); |
| 363 | } else { |
| 364 | // Last listener can use the value |
| 365 | listener.entryAdded(value); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 366 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 367 | } |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 368 | } finally { |
| 369 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 370 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 371 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 372 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 373 | /** |
| 374 | * Receive a notification that an entry is removed. |
| 375 | * |
| 376 | * @param event the notification event for the entry. |
| 377 | */ |
| 378 | @Override |
| 379 | public void entryRemoved(EntryEvent<K, byte[]> event) { |
| 380 | // |
| 381 | // Decode the value |
| 382 | // |
| 383 | byte[] valueBytes = event.getValue(); |
| 384 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 385 | try { |
| 386 | V value = deserializeValue(kryo, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 387 | |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 388 | // |
| 389 | // Deliver the notification |
| 390 | // |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 391 | final Iterator<IEventChannelListener<K, V>> it = listeners.iterator(); |
| 392 | while (it.hasNext()) { |
| 393 | final IEventChannelListener<K, V> listener = it.next(); |
| 394 | if (it.hasNext()) { |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 395 | // Each listener should get a deep copy of the value |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 396 | listener.entryRemoved(kryo.copy(value)); |
| 397 | } else { |
| 398 | // Last listener can use the value |
| 399 | listener.entryRemoved(value); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 400 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 401 | } |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 402 | } finally { |
| 403 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 404 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 405 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 406 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 407 | /** |
| 408 | * Receive a notification that an entry is updated. |
| 409 | * |
| 410 | * @param event the notification event for the entry. |
| 411 | */ |
| 412 | @Override |
| 413 | public void entryUpdated(EntryEvent<K, byte[]> event) { |
| 414 | // |
| 415 | // Decode the value |
| 416 | // |
| 417 | byte[] valueBytes = event.getValue(); |
| 418 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 419 | try { |
| 420 | V value = deserializeValue(kryo, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 421 | |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 422 | // |
| 423 | // Deliver the notification |
| 424 | // |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 425 | final Iterator<IEventChannelListener<K, V>> it = listeners.iterator(); |
| 426 | while (it.hasNext()) { |
| 427 | final IEventChannelListener<K, V> listener = it.next(); |
| 428 | if (it.hasNext()) { |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 429 | // Each listener should get a deep copy of the value |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 430 | listener.entryUpdated(kryo.copy(value)); |
| 431 | } else { |
| 432 | // Last listener can use the value |
| 433 | listener.entryUpdated(value); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 434 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 435 | } |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 436 | } finally { |
| 437 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 438 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 439 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 440 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 441 | /** |
| 442 | * Receive a notification that an entry is evicted. |
| 443 | * |
| 444 | * @param event the notification event for the entry. |
| 445 | */ |
| 446 | @Override |
| 447 | public void entryEvicted(EntryEvent<K, byte[]> event) { |
| 448 | // NOTE: We don't use eviction for this map |
| 449 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 450 | } |
| 451 | } |