Class OortMap<K,V>
- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.cometd.oort.OortObject<T>
-
- org.cometd.oort.OortContainer<java.util.concurrent.ConcurrentMap<K,V>>
-
- org.cometd.oort.OortMap<K,V>
-
- Type Parameters:
K- the key typeV- the value type
- All Implemented Interfaces:
java.lang.Iterable<OortObject.Info<java.util.concurrent.ConcurrentMap<K,V>>>,java.util.EventListener,ConfigurableServerChannel.Initializer,Oort.CometListener,org.eclipse.jetty.util.component.Dumpable,org.eclipse.jetty.util.component.LifeCycle
- Direct Known Subclasses:
OortLongMap,OortStringMap
public abstract class OortMap<K,V> extends OortContainer<java.util.concurrent.ConcurrentMap<K,V>>
A specialized oort object whose entity is a
ConcurrentMap.OortMapspecializesOortObjectand allows optimized replication of map entries across the cluster: instead of replicating the whole map, that may be contain a lot of entries, only entries that are modified are replicated.Applications can use
putAndShare(Object, Object, Result)andremoveAndShare(Object, Result)to broadcast changes related to single entries, as well asOortObject.setAndShare(Object, Result)to change the whole map.When a single entry is changed,
OortMap.EntryListeners are notified.OortMap.DeltaListenerconverts whole map updates triggered byOortObject.setAndShare(Object, Result)into events forOortMap.EntryListeners, giving applications a single listener type to implement their business logic.The type parameter for keys,
K, must be a String to be able to use this class as-is, although usage ofOortStringMapis preferred. This is due to the fact that aMap<Long,Object>containing an entry{13:"foo"}is serialized in JSON as{"13":"foo"}because JSON field names must always be strings. When deserialized, it is restored as aMap<String,Object>, which is incompatible with the original type parameter for keys. To overcome this issue, subclasses may overrideOortObject.serialize(Object)andOortObject.deserialize(Object). MethodOortObject.serialize(Object)should convert the entity object to a format that retains enough type information forOortObject.deserialize(Object)to convert the JSON-deserialized entity object that has the wrong key type to an entity object that has the right key type, likeOortLongMapdoes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classOortMap.DeltaListener<K,V>An implementation ofOortObject.Listenerthat converts whole map events intoOortMap.EntryListenerevents.static classOortMap.Entry<K,V>A triple that holds the key, the previous value and the new value, used to notify entry updates:static interfaceOortMap.EntryListener<K,V>Listener for entry events that update the entity map, either locally or remotely.-
Nested classes/interfaces inherited from class org.cometd.oort.OortObject
OortObject.Data<T>, OortObject.Factory<T>, OortObject.Info<T>, OortObject.Listener<T>, OortObject.Merger<T,R>, OortObject.Result<R>
-
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener, org.eclipse.jetty.util.component.AbstractLifeCycle.StopException
-
Nested classes/interfaces inherited from interface org.cometd.bayeux.server.ConfigurableServerChannel.Initializer
ConfigurableServerChannel.Initializer.Persistent
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
org.eclipse.jetty.util.component.Dumpable.DumpableContainer
-
Nested classes/interfaces inherited from interface org.cometd.oort.Oort.CometListener
Oort.CometListener.Event
-
-
Field Summary
-
Fields inherited from class org.cometd.oort.OortObject
OORT_OBJECTS_CHANNEL
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedOortMap(Oort oort, java.lang.String name, OortObject.Factory<java.util.concurrent.ConcurrentMap<K,V>> factory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEntryListener(OortMap.EntryListener<K,V> listener)Vfind(K key)Returns the first non-null value mapped to the given key from the entity maps of all nodes.OortObject.Info<java.util.concurrent.ConcurrentMap<K,V>>findInfo(K key)Vget(K key)Returns the value mapped to the given key from the local entity map of this node.protected booleanisItemUpdate(java.util.Map<java.lang.String,java.lang.Object> data)protected voidonItem(OortObject.Info<java.util.concurrent.ConcurrentMap<K,V>> info, java.util.Map<java.lang.String,java.lang.Object> data)voidputAndShare(K key, V value, OortObject.Result<V> callback)Updates a single entry of the local entity map with the givenkeyandvalue, and broadcasts the operation to all nodes in the cluster.voidputIfAbsentAndShare(K key, V value, OortObject.Result<V> callback)Updates a single entry of the local entity map with the givenkeyandvalueif it does not exist yet, and broadcasts the operation to all nodes in the cluster.voidremoveAndShare(K key, OortObject.Result<V> callback)Removes the givenkeyfrom the local entity map, and broadcasts the operation to all nodes in the cluster.voidremoveEntryListener(OortMap.EntryListener<K,V> listener)voidremoveEntryListeners()-
Methods inherited from class org.cometd.oort.OortContainer
cometLeft, doStop, onObject
-
Methods inherited from class org.cometd.oort.OortObject
addListener, cometJoined, configureChannel, deserialize, doStart, dump, getChannelName, getFactory, getInfo, getInfoByObject, getInfos, getLocalSession, getName, getOort, iterator, merge, newInfo, notifyRemoved, notifyUpdated, pullInfo, pushInfo, removeListener, removeListeners, serialize, setAndShare, toString
-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addEventListener, getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, setEventListeners, start, stop
-
-
-
-
Constructor Detail
-
OortMap
protected OortMap(Oort oort, java.lang.String name, OortObject.Factory<java.util.concurrent.ConcurrentMap<K,V>> factory)
-
-
Method Detail
-
addEntryListener
public void addEntryListener(OortMap.EntryListener<K,V> listener)
-
removeEntryListener
public void removeEntryListener(OortMap.EntryListener<K,V> listener)
-
removeEntryListeners
public void removeEntryListeners()
-
putAndShare
public void putAndShare(K key, V value, OortObject.Result<V> callback)
Updates a single entry of the local entity map with the given
keyandvalue, and broadcasts the operation to all nodes in the cluster.Calling this method triggers notifications
OortMap.EntryListeners, both on this node and on remote nodes.The entry is guaranteed to be put not when this method returns, but when the
OortObject.Resultparameter is notified.- Parameters:
key- the key to associate the value tovalue- the value associated with the keycallback- the callback invoked with the old value, ornullif there is no interest in the old value- See Also:
putIfAbsentAndShare(Object, Object, Result),removeAndShare(Object, Result)
-
putIfAbsentAndShare
public void putIfAbsentAndShare(K key, V value, OortObject.Result<V> callback)
Updates a single entry of the local entity map with the given
keyandvalueif it does not exist yet, and broadcasts the operation to all nodes in the cluster.Calling this method triggers notifications
OortMap.EntryListeners, both on this node and on remote nodes, only if the key did not exist.The entry is guaranteed to be put not when this method returns, but when the
OortObject.Resultparameter is notified.- Parameters:
key- the key to associate the value tovalue- the value associated with the keycallback- the callback invoked with the old value, ornullif there is no interest in the old value- See Also:
putAndShare(Object, Object, Result)
-
removeAndShare
public void removeAndShare(K key, OortObject.Result<V> callback)
Removes the given
keyfrom the local entity map, and broadcasts the operation to all nodes in the cluster.Calling this method triggers notifications
OortMap.EntryListeners, both on this node and on remote nodes.The entry is guaranteed to be removed not when this method returns, but when the
OortObject.Resultparameter is notified.- Parameters:
key- the key to removecallback- the callback invoked with the value, ornullif there is no interest in the value- See Also:
putAndShare(Object, Object, Result)
-
get
public V get(K key)
Returns the value mapped to the given key from the local entity map of this node. Differently fromfind(Object), only the local entity map is scanned.- Parameters:
key- the key mapped to the value to return- Returns:
- the value mapped to the given key, or
nullif the local map does not contain the given key - See Also:
find(Object)
-
find
public V find(K key)
Returns the first non-null value mapped to the given key from the entity maps of all nodes. Differently fromget(Object), entity maps of all nodes are scanned.- Parameters:
key- the key mapped to the value to return- Returns:
- the value mapped to the given key, or
nullif the maps do not contain the given key - See Also:
get(Object)
-
findInfo
public OortObject.Info<java.util.concurrent.ConcurrentMap<K,V>> findInfo(K key)
- Parameters:
key- the key to search- Returns:
- the first
OortObject.Infowhose entity map contains the given key.
-
isItemUpdate
protected boolean isItemUpdate(java.util.Map<java.lang.String,java.lang.Object> data)
- Specified by:
isItemUpdatein classOortContainer<java.util.concurrent.ConcurrentMap<K,V>>
-
onItem
protected void onItem(OortObject.Info<java.util.concurrent.ConcurrentMap<K,V>> info, java.util.Map<java.lang.String,java.lang.Object> data)
- Specified by:
onItemin classOortContainer<java.util.concurrent.ConcurrentMap<K,V>>
-
-