Class HazelcastMapAdapter<T>

java.lang.Object
com.norconex.crawler.core.cluster.impl.hazelcast.HazelcastMapAdapter<T>
Type Parameters:
T - the type of values stored in the cache view
All Implemented Interfaces:
CacheMap<T>

public class HazelcastMapAdapter<T> extends Object implements CacheMap<T>
Hazelcast IMap-based implementation of the Cache interface. Adapter performs transparent conversion between values exposed as type T and the values actually stored in Hazelcast which may be plain String JSON (or raw String) or actual objects. This allows persisting everything as strings in the DB-backed MapStore while presenting typed objects to callers.
  • Constructor Details

    • HazelcastMapAdapter

      public HazelcastMapAdapter(com.hazelcast.map.IMap<String,?> hzMap, com.hazelcast.core.HazelcastInstance hzInstance, Class<T> type)
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface CacheMap<T>
    • put

      public void put(String key, T value)
      Specified by:
      put in interface CacheMap<T>
    • putAll

      public void putAll(Map<String,T> entries)
      Specified by:
      putAll in interface CacheMap<T>
    • get

      public Optional<T> get(String key)
      Specified by:
      get in interface CacheMap<T>
    • remove

      public void remove(String key)
      Specified by:
      remove in interface CacheMap<T>
    • clear

      public void clear()
      Specified by:
      clear in interface CacheMap<T>
    • computeIfAbsent

      public T computeIfAbsent(String key, Function<String,? extends T> mappingFunction)
      Specified by:
      computeIfAbsent in interface CacheMap<T>
    • computeIfPresent

      public Optional<T> computeIfPresent(String key, BiFunction<String,? super T,? extends T> remappingFunction)
      Specified by:
      computeIfPresent in interface CacheMap<T>
    • compute

      public Optional<T> compute(String key, BiFunction<String,? super T,? extends T> remappingFunction)
      Specified by:
      compute in interface CacheMap<T>
    • merge

      public T merge(String key, T value, BiFunction<? super T,? super T,? extends T> remappingFunction)
      Specified by:
      merge in interface CacheMap<T>
    • containsKey

      public boolean containsKey(String key)
      Specified by:
      containsKey in interface CacheMap<T>
    • getOrDefault

      public T getOrDefault(String key, T defaultValue)
      Specified by:
      getOrDefault in interface CacheMap<T>
    • putIfAbsent

      public T putIfAbsent(String key, T value)
      Description copied from interface: CacheMap
      If the specified key is not already associated with a value, associates it with the given value. This is equivalent to, for this cache:
       if (!cache.containsKey(key))
           return map.put(key, value);
       else
           return map.get(key);
      
      except that the action is performed atomically.
      Specified by:
      putIfAbsent in interface CacheMap<T>
      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
      Returns:
      the previous value associated with the specified key, or null if there was no mapping for the key.
    • replace

      public boolean replace(String key, T oldValue, T newValue)
      Specified by:
      replace in interface CacheMap<T>
    • query

      public List<T> query(QueryFilter filter)
      Description copied from interface: CacheMap
      Queries the cache for entries matching the given query filter. Note: This method loads all results into memory at once. For large result sets, consider using CacheMap.queryIterator(com.norconex.crawler.core.cluster.QueryFilter) instead.
      Specified by:
      query in interface CacheMap<T>
      Parameters:
      filter - the query filter
      Returns:
      a list of matching entries or an empty list (never null)
    • queryIterator

      public Iterator<T> queryIterator(QueryFilter filter)
      Description copied from interface: CacheMap
      Queries the cache for entries matching the given filter and returns an iterator for memory-efficient processing of results. This avoids loading all results into memory at once.
      Specified by:
      queryIterator in interface CacheMap<T>
      Parameters:
      filter - the query filter
      Returns:
      an iterator over the matching entries
    • count

      public long count(QueryFilter filter)
      Description copied from interface: CacheMap
      Counts the number of entries matching the given query filter.
      Specified by:
      count in interface CacheMap<T>
      Parameters:
      filter - the query filter
      Returns:
      the count of matching entries
    • size

      public long size()
      Description copied from interface: CacheMap
      Counts the number of entries in this cache.
      Specified by:
      size in interface CacheMap<T>
      Returns:
      the number of entries
    • delete

      public void delete(QueryFilter filter)
      Description copied from interface: CacheMap
      Deletes entries matching the given query filter.
      Specified by:
      delete in interface CacheMap<T>
      Parameters:
      filter - the query filter
    • forEach

      public void forEach(BiConsumer<String,? super T> action)
      Specified by:
      forEach in interface CacheMap<T>
    • keys

      public List<String> keys()
      Description copied from interface: CacheMap
      Returns all keys in this cache. For distributed caches, this includes keys from all nodes in the cluster, not just locally-owned keys.
      Specified by:
      keys in interface CacheMap<T>
      Returns:
      a list of all keys in the cache
    • loadAll

      public void loadAll()
      Description copied from interface: CacheMap
      Eagerly loads all entries from the backing store (e.g., JDBC) into this cache. For caches configured with LAZY initial load mode, this ensures sub­sequent CacheMap.size(), CacheMap.forEach(java.util.function.BiConsumer<java.lang.String, ? super T>), and CacheMap.keys() calls return correct results. If the cache already has its data loaded (e.g., EAGER mode), this is a no-op.
      Specified by:
      loadAll in interface CacheMap<T>
    • vendor

      public com.hazelcast.map.IMap<String,T> vendor()
      Return the underlying Hazelcast IMap as typed view. Use with caution: underlying values are stored as Objects/Strings.
      Returns:
      hazelcast IMap
    • isPersistent

      public boolean isPersistent()
      Description copied from interface: CacheMap
      Returns whether this cache persists data across restarts.
      Specified by:
      isPersistent in interface CacheMap<T>
      Returns:
      true if the cache is persistent, false if ephemeral
    • getName

      public String getName()
      Description copied from interface: CacheMap
      Gets the name of this cache.
      Specified by:
      getName in interface CacheMap<T>
      Returns:
      cache name