All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.as.clustering.infinispan.client.ManagedRemoteCache Maven / Gradle / Ivy

There is a newer version: 34.0.0.Final
Show newest version
/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.jboss.as.clustering.infinispan.client;

import java.net.SocketAddress;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.UnaryOperator;

import javax.management.ObjectName;

import jakarta.transaction.TransactionManager;

import org.infinispan.client.hotrod.CacheTopologyInfo;
import org.infinispan.client.hotrod.DataFormat;
import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.MetadataValue;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.ServerStatistics;
import org.infinispan.client.hotrod.StreamingRemoteCache;
import org.infinispan.client.hotrod.configuration.Configuration;
import org.infinispan.client.hotrod.impl.ClientStatistics;
import org.infinispan.client.hotrod.impl.InternalRemoteCache;
import org.infinispan.client.hotrod.impl.RemoteCacheSupport;
import org.infinispan.client.hotrod.impl.operations.OperationsFactory;
import org.infinispan.client.hotrod.impl.operations.PingResponse;
import org.infinispan.client.hotrod.impl.operations.RetryAwareCompletionStage;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.commons.util.CloseableIteratorCollection;
import org.infinispan.commons.util.CloseableIteratorSet;
import org.infinispan.commons.util.IntSet;
import org.infinispan.query.dsl.Query;
import org.reactivestreams.Publisher;
import org.wildfly.clustering.infinispan.client.RemoteCache;
import org.wildfly.clustering.infinispan.client.RemoteCacheContainer;
import org.wildfly.clustering.server.Registrar;
import org.wildfly.clustering.server.Registration;

/**
 * {@link RemoteCache} decorator that handles registration on {@link #start()} and deregistration on {@link #stop()}.
 * @author Paul Ferraro
 */
public class ManagedRemoteCache extends RemoteCacheSupport implements InternalRemoteCache, RemoteCache, UnaryOperator {

    private final Registrar registrar;
    private final AtomicReference registration;
    private final RemoteCacheContainer container;
    private final RemoteCacheManager manager;
    private final InternalRemoteCache cache;

    public ManagedRemoteCache(RemoteCacheContainer container, RemoteCacheManager manager, org.infinispan.client.hotrod.RemoteCache cache, Registrar registrar) {
        this(container, manager, (InternalRemoteCache) cache, registrar, new AtomicReference<>());
    }

    private ManagedRemoteCache(RemoteCacheContainer container, RemoteCacheManager manager, InternalRemoteCache cache, Registrar registrar, AtomicReference registration) {
        this.container = container;
        this.manager = manager;
        this.cache = cache;
        this.registrar = registrar;
        this.registration = registration;
    }

    @Override
    public boolean isTransactional() {
        return this.cache.isTransactional();
    }

    @Override
    public TransactionManager getTransactionManager() {
        return this.cache.getTransactionManager();
    }

    @Override
    public void start() {
        if (this.registration.getAndUpdate(this) == null) {
            this.cache.start();
        }
    }

    @Override
    public Registration apply(Registration registration) {
        return (registration == null) ? this.registrar.register(this.getName()) : registration;
    }

    @Override
    public void stop() {
        try (Registration registration = this.registration.getAndSet(null)) {
            if (registration != null) {
                this.cache.stop();
            }
        }
    }

    @Override
    public RemoteCacheContainer getRemoteCacheContainer() {
        return this.container;
    }

    @Deprecated
    @Override
    public RemoteCacheManager getRemoteCacheManager() {
        return this.manager;
    }

    @Override
    public void addClientListener(Object listener) {
        this.cache.addClientListener(listener);
    }

    @Override
    public void addClientListener(Object listener, Object[] filterFactoryParams, Object[] converterFactoryParams) {
        this.cache.addClientListener(listener, filterFactoryParams, converterFactoryParams);
    }

    @Override
    public ClientStatistics clientStatistics() {
        return this.cache.clientStatistics();
    }

    @Override
    public CloseableIteratorSet> entrySet(IntSet segments) {
        return this.cache.entrySet(segments);
    }

    @Override
    public  T execute(String taskName, Map params) {
        return this.cache.execute(taskName, params);
    }

    @Override
    public CacheTopologyInfo getCacheTopologyInfo() {
        return this.cache.getCacheTopologyInfo();
    }

    @Override
    public DataFormat getDataFormat() {
        return this.cache.getDataFormat();
    }

    @Deprecated
    @Override
    public Set getListeners() {
        return this.cache.getListeners();
    }

    @Override
    public String getProtocolVersion() {
        return this.cache.getProtocolVersion();
    }

    @Override
    public CloseableIteratorSet keySet(IntSet segments) {
        return this.cache.keySet(segments);
    }

    @Override
    public void removeClientListener(Object listener) {
        this.cache.removeClientListener(listener);
    }

    @Override
    public CompletableFuture replaceWithVersionAsync(K key, V newValue, long version, long lifespanSeconds, TimeUnit lifespanTimeUnit, long maxIdle, TimeUnit maxIdleTimeUnit) {
        return this.cache.replaceWithVersionAsync(key, newValue, version, lifespanSeconds, lifespanTimeUnit, maxIdle, maxIdleTimeUnit);
    }

    @Override
    public CloseableIterator> retrieveEntries(String filterConverterFactory, Object[] filterConverterParams, Set segments, int batchSize) {
        return this.cache.retrieveEntries(filterConverterFactory, filterConverterParams, segments, batchSize);
    }

    @Override
    public CloseableIterator> retrieveEntriesByQuery(Query filterQuery, Set segments, int batchSize) {
        return this.cache.retrieveEntriesByQuery(filterQuery, segments, batchSize);
    }

    @Override
    public CloseableIterator>> retrieveEntriesWithMetadata(Set segments, int batchSize) {
        return this.cache.retrieveEntriesWithMetadata(segments, batchSize);
    }

    @Override
    public  Publisher> publishEntries(String filterConverterFactory, Object[] filterConverterParams, Set segments, int batchSize) {
        return this.cache.publishEntries(filterConverterFactory, filterConverterParams, segments, batchSize);
    }

    @Override
    public  Publisher> publishEntriesByQuery(Query filterQuery, Set segments, int batchSize) {
        return this.cache.publishEntriesByQuery(filterQuery, segments, batchSize);
    }

    @Override
    public Publisher>> publishEntriesWithMetadata(Set segments, int batchSize) {
        return this.cache.publishEntriesWithMetadata(segments, batchSize);
    }

    @Override
    public ServerStatistics serverStatistics() {
        return this.cache.serverStatistics();
    }

    @Override
    public StreamingRemoteCache streaming() {
        return this.cache.streaming();
    }

    @Override
    public CloseableIteratorCollection values(IntSet segments) {
        return this.cache.values(segments);
    }

    @Override
    public  InternalRemoteCache withDataFormat(DataFormat dataFormat) {
        return new ManagedRemoteCache<>(this.container, this.manager, this.cache.withDataFormat(dataFormat), this.registrar, this.registration);
    }

    @Override
    public InternalRemoteCache withFlags(Flag... flags) {
        return new ManagedRemoteCache<>(this.container, this.manager, this.cache.withFlags(flags), this.registrar, this.registration);
    }

    @Override
    public String getName() {
        return this.cache.getName();
    }

    @Override
    public String getVersion() {
        return this.cache.getVersion();
    }

    @Override
    public CompletableFuture clearAsync() {
        return this.cache.clearAsync();
    }

    @Override
    public boolean isEmpty() {
        return this.cache.isEmpty();
    }

    @Override
    public boolean containsValue(Object value) {
        return this.cache.containsValue(value);
    }

    @Override
    public CompletableFuture computeAsync(K key, BiFunction remappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
        return this.cache.computeAsync(key, remappingFunction, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
    }

    @Override
    public CompletableFuture computeIfAbsentAsync(K key, Function mappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
        return this.cache.computeIfAbsentAsync(key, mappingFunction, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
    }

    @Override
    public CompletableFuture computeIfPresentAsync(K key, BiFunction remappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
        return this.cache.computeIfPresentAsync(key, remappingFunction, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
    }

    @Override
    public CompletableFuture containsKeyAsync(K key) {
        return this.cache.containsKeyAsync(key);
    }

    @Override
    public CompletableFuture> getAllAsync(Set keys) {
        return this.cache.getAllAsync(keys);
    }

    @Override
    public CompletableFuture getAsync(K key) {
        return this.cache.getAsync(key);
    }

    @Override
    public CompletableFuture> getWithMetadataAsync(K key) {
        return this.cache.getWithMetadataAsync(key);
    }

    @Override
    public CompletableFuture mergeAsync(K key, V value, BiFunction remappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) {
        return this.cache.mergeAsync(key, value, remappingFunction, lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit);
    }

    @Override
    public CompletableFuture putAllAsync(Map data, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
        return this.cache.putAllAsync(data, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
    }

    @Override
    public CompletableFuture putAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
        return this.cache.putAsync(key, value, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
    }

    @Override
    public CompletableFuture putIfAbsentAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
        return this.cache.putIfAbsentAsync(key, value, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
    }

    @Override
    public CompletableFuture removeAsync(Object key) {
        return this.cache.removeAsync(key);
    }

    @Override
    public CompletableFuture removeAsync(Object key, Object value) {
        return this.cache.removeAsync(key, value);
    }

    @Override
    public CompletableFuture removeWithVersionAsync(K key, long version) {
        return this.cache.removeWithVersionAsync(key, version);
    }

    @Override
    public void replaceAll(BiFunction function) {
        this.cache.replaceAll(function);
    }

    @Override
    public CompletableFuture replaceAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
        return this.cache.replaceAsync(key, value, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
    }

    @Override
    public CompletableFuture replaceAsync(K key, V oldValue, V newValue, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
        return this.cache.replaceAsync(key, oldValue, newValue, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
    }

    @Override
    public CompletableFuture sizeAsync() {
        return this.cache.sizeAsync();
    }

    @Override
    public CompletionStage serverStatisticsAsync() {
        return this.cache.serverStatisticsAsync();
    }

    @Override
    public CloseableIterator keyIterator(IntSet segments) {
        return this.cache.keyIterator(segments);
    }

    @Override
    public CloseableIterator> entryIterator(IntSet segments) {
        return this.cache.entryIterator(segments);
    }

    @Override
    public RetryAwareCompletionStage> getWithMetadataAsync(K key, SocketAddress preferredAddress) {
        return this.cache.getWithMetadataAsync(key, preferredAddress);
    }

    @Override
    public boolean hasForceReturnFlag() {
        return this.cache.hasForceReturnFlag();
    }

    @Override
    public void resolveStorage(boolean objectStorage) {
        this.cache.resolveStorage(objectStorage);
    }

    @Override
    public void init(OperationsFactory operationsFactory, Configuration configuration, ObjectName jmxParent) {
        this.cache.init(operationsFactory, configuration, jmxParent);
    }

    @Override
    public void init(OperationsFactory operationsFactory, Configuration configuration) {
        this.cache.init(operationsFactory, configuration);
    }

    @Override
    public OperationsFactory getOperationsFactory() {
        return this.cache.getOperationsFactory();
    }

    @Override
    public boolean isObjectStorage() {
        return this.cache.isObjectStorage();
    }

    @Override
    public byte[] keyToBytes(Object object) {
        return this.cache.keyToBytes(object);
    }

    @Override
    public CompletionStage ping() {
        return this.cache.ping();
    }

    @Override
    public SocketAddress addNearCacheListener(Object listener, int bloomBits) {
        return this.cache.addNearCacheListener(listener, bloomBits);
    }

    @Override
    public CompletionStage updateBloomFilter() {
        return this.cache.updateBloomFilter();
    }
}