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