Class ContinuousQueryCache<K,V>

java.lang.Object
com.loomcache.server.datastructures.ContinuousQueryCache<K,V>
Type Parameters:
K - the key type
V - the value type
All Implemented Interfaces:
DistributedMap.MapChangeListener<K,V>

public class ContinuousQueryCache<K,V> extends Object implements DistributedMap.MapChangeListener<K,V>
Continuous Query Cache — a server-side filtered view of a DistributedMap that auto-updates via map change events.

On creation, the CQC scans the source map and populates an in-memory view with all entries matching the predicate. It then registers as a DistributedMap.MapChangeListener to receive live updates:

  • New entries matching the predicate are added to the view
  • Updated entries are re-evaluated: added, updated, or removed from the view
  • Removed/evicted/expired entries are removed from the view
  • Map clear empties the view

The view is read-only — all writes go through the source map. Call destroy() to deregister the listener and release resources.

Thread safety: The view uses ConcurrentHashMap; listener dispatch uses CopyOnWriteArrayList. All operations are safe for concurrent access.

Since:
1.1
  • Constructor Details

    • ContinuousQueryCache

      public ContinuousQueryCache(String name, int instanceNumber, DistributedMap<K,V> sourceMap, MapPredicate<K,V> predicate)
      Create a CQC on the given map with the specified filter predicate.

      Registers as a listener first, then scans existing entries, ensuring no entries are missed due to concurrent mutations.

      Parameters:
      name - the CQC name for identification
      instanceNumber - the node instance number for logging
      sourceMap - the map to create a filtered view of
      predicate - the filter predicate (entries matching this are included)
  • Method Details

    • get

      public @Nullable V get(K key)
      Get a value from the filtered view.
      Parameters:
      key - the key to look up
      Returns:
      the value if the entry exists in the view, or null
    • containsKey

      public boolean containsKey(K key)
      Check if a key exists in the filtered view.
    • containsValue

      public boolean containsValue(V value)
      Check if a value exists in the filtered view.
    • size

      public int size()
      Get the number of entries in the filtered view.
    • isEmpty

      public boolean isEmpty()
      Check if the filtered view is empty.
    • keySet

      public Set<K> keySet()
      Get an unmodifiable set of keys in the filtered view.
    • values

      public Collection<V> values()
      Get an unmodifiable collection of values in the filtered view.
    • getAll

      public Map<K,V> getAll()
      Get an unmodifiable snapshot of all entries in the filtered view.
    • getSourceMapName

      public String getSourceMapName()
      The name of the source map this CQC is derived from.
    • addEntryListener

      public String addEntryListener(ContinuousQueryCache.CqcListener<K,V> listener)
      Add a listener for changes to the CQC view.
      Parameters:
      listener - the listener to register
      Returns:
      a subscription ID for removal
    • removeEntryListener

      public boolean removeEntryListener(ContinuousQueryCache.CqcListener<K,V> listener)
      Remove a CQC listener.
    • destroy

      public void destroy()
      Deregister from the source map and release resources. After this call, the view is frozen and no longer receives updates.
    • getEventsProcessed

      public long getEventsProcessed()
    • getPredicateEvaluations

      public long getPredicateEvaluations()
    • getViewUpdates

      public long getViewUpdates()
    • getStatistics

      public ContinuousQueryCache.CqcStatistics getStatistics()
      Get a snapshot of CQC statistics.
    • onEntryAdded

      public void onEntryAdded(String mapName, K key, V value)
      Specified by:
      onEntryAdded in interface DistributedMap.MapChangeListener<K,V>
    • onEntryUpdated

      public void onEntryUpdated(String mapName, K key, @Nullable V oldValue, V newValue)
      Specified by:
      onEntryUpdated in interface DistributedMap.MapChangeListener<K,V>
    • onEntryRemoved

      public void onEntryRemoved(String mapName, K key, V oldValue)
      Specified by:
      onEntryRemoved in interface DistributedMap.MapChangeListener<K,V>
    • onEntryEvicted

      public void onEntryEvicted(String mapName, K key, V value)
      Description copied from interface: DistributedMap.MapChangeListener
      Called when an entry is evicted by memory pressure or eviction policy (LRU/LFU/FIFO/RANDOM). Eviction is involuntary removal triggered by capacity limits, distinct from explicit removal.
      Specified by:
      onEntryEvicted in interface DistributedMap.MapChangeListener<K,V>
      Parameters:
      mapName - the map name
      key - the evicted key
      value - the evicted value
    • onEntryExpired

      public void onEntryExpired(String mapName, K key, V value)
      Description copied from interface: DistributedMap.MapChangeListener
      Called when an entry expires due to TTL (time-to-live). Expiration is involuntary removal triggered by time, distinct from explicit removal.
      Specified by:
      onEntryExpired in interface DistributedMap.MapChangeListener<K,V>
      Parameters:
      mapName - the map name
      key - the expired key
      value - the expired value
    • onMapCleared

      public void onMapCleared(String mapName)
      Description copied from interface: DistributedMap.MapChangeListener
      Called when the entire map is cleared via DistributedMap.clear(). Fired once per clear operation, not per entry.
      Specified by:
      onMapCleared in interface DistributedMap.MapChangeListener<K,V>
      Parameters:
      mapName - the map name