Class OortMap<K,V>

java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
Type Parameters:
K - the key type
V - the value type
All Implemented Interfaces:
Iterable<OortObject.Info<ConcurrentMap<K,V>>>, 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<ConcurrentMap<K,V>>

A specialized oort object whose entity is a ConcurrentMap.

OortMap specializes OortObject and 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) and removeAndShare(Object, Result) to broadcast changes related to single entries, as well as OortObject.setAndShare(Object, Result) to change the whole map.

When a single entry is changed, OortMap.EntryListeners are notified. OortMap.DeltaListener converts whole map updates triggered by OortObject.setAndShare(Object, Result) into events for OortMap.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 of OortStringMap is preferred. This is due to the fact that a Map<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 a Map<String,Object>, which is incompatible with the original type parameter for keys. To overcome this issue, subclasses may override OortObject.serialize(Object) and OortObject.deserialize(Object). Method OortObject.serialize(Object) should convert the entity object to a format that retains enough type information for OortObject.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, like OortLongMap does.

  • Constructor Details

  • Method Details

    • 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 key and value, 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.Result parameter is notified.

      Parameters:
      key - the key to associate the value to
      value - the value associated with the key
      callback - the callback invoked with the old value, or null if there is no interest in the old value
      See Also:
    • putIfAbsentAndShare

      public void putIfAbsentAndShare(K key, V value, OortObject.Result<V> callback)

      Updates a single entry of the local entity map with the given key and value if 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.Result parameter is notified.

      Parameters:
      key - the key to associate the value to
      value - the value associated with the key
      callback - the callback invoked with the old value, or null if there is no interest in the old value
      See Also:
    • removeAndShare

      public void removeAndShare(K key, OortObject.Result<V> callback)

      Removes the given key from 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.Result parameter is notified.

      Parameters:
      key - the key to remove
      callback - the callback invoked with the value, or null if there is no interest in the value
      See Also:
    • get

      public V get(K key)
      Returns the value mapped to the given key from the local entity map of this node. Differently from find(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 null if the local map does not contain the given key
      See Also:
    • 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 from get(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 null if the maps do not contain the given key
      See Also:
    • findInfo

      public OortObject.Info<ConcurrentMap<K,V>> findInfo(K key)
      Parameters:
      key - the key to search
      Returns:
      the first OortObject.Info whose entity map contains the given key.
    • isItemUpdate

      protected boolean isItemUpdate(Map<String,Object> data)
      Specified by:
      isItemUpdate in class OortContainer<ConcurrentMap<K,V>>
    • onItem

      protected void onItem(OortObject.Info<ConcurrentMap<K,V>> info, Map<String,Object> data)
      Specified by:
      onItem in class OortContainer<ConcurrentMap<K,V>>