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

io.github.bucket4j.grid.infinispan.InfinispanProxyManager Maven / Gradle / Ivy

The newest version!
/*-
 * ========================LICENSE_START=================================
 * Bucket4j
 * %%
 * Copyright (C) 2015 - 2020 Vladimir Bukhtoyarov
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */
/*
 *
 * Copyright 2015-2018 Vladimir Bukhtoyarov
 *
 *       Licensed under the Apache License, Version 2.0 (the "License");
 *       you may not use this file except in compliance with the License.
 *       You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package io.github.bucket4j.grid.infinispan;

import io.github.bucket4j.distributed.proxy.AbstractProxyManager;
import io.github.bucket4j.distributed.proxy.ClientSideConfig;
import io.github.bucket4j.distributed.remote.*;
import org.infinispan.commons.CacheException;
import org.infinispan.functional.FunctionalMap.ReadWriteMap;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import static io.github.bucket4j.distributed.serialization.InternalSerializationHelper.deserializeResult;

/**
 * The extension of Bucket4j library addressed to support Infinispan in-memory computing platform.
 */
public class InfinispanProxyManager extends AbstractProxyManager {

    private final InfinispanProcessor REMOVE_BUCKET_ENTRY_PROCESSOR = new InfinispanProcessor<>(new byte[0]);
    private final ReadWriteMap readWriteMap;

    InfinispanProxyManager(Bucket4jInfinispan.InfinispanProxyManagerBuilder builder) {
        super(builder.getClientSideConfig());
        this.readWriteMap = builder.readWriteMap;
    }

    /**
     * @deprecated use {@link Bucket4jInfinispan#entryProcessorBasedBuilder(ReadWriteMap)}
     */
    @Deprecated
    public InfinispanProxyManager(ReadWriteMap readWriteMap) {
        this(readWriteMap, ClientSideConfig.getDefault());
    }

    /**
     * @deprecated use {@link Bucket4jInfinispan#entryProcessorBasedBuilder(ReadWriteMap)}
     */
    @Deprecated
    public InfinispanProxyManager(ReadWriteMap readWriteMap, ClientSideConfig clientSideConfig) {
        super(clientSideConfig);
        this.readWriteMap = Objects.requireNonNull(readWriteMap);
    }

    @Override
    public  CommandResult execute(K key, Request request) {
        InfinispanProcessor entryProcessor = new InfinispanProcessor<>(request);
        try {
            CompletableFuture resultFuture = readWriteMap.eval(key, entryProcessor);
            return (CommandResult) resultFuture.thenApply(resultBytes -> deserializeResult(resultBytes, request.getBackwardCompatibilityVersion())).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new CacheException(e);
        }
    }

    @Override
    public boolean isAsyncModeSupported() {
        return true;
    }

    @Override
    public void removeProxy(K key) {
        try {
            removeAsync(key).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new CacheException(e);
        }
    }

    @Override
    protected CompletableFuture removeAsync(K key) {
        try {
            CompletableFuture resultFuture = readWriteMap.eval(key, REMOVE_BUCKET_ENTRY_PROCESSOR);
            return resultFuture.thenApply(resultBytes -> null);
        } catch (Throwable t) {
            CompletableFuture fail = new CompletableFuture<>();
            fail.completeExceptionally(t);
            return fail;
        }
    }

    @Override
    public  CompletableFuture> executeAsync(K key, Request request) {
        try {
            InfinispanProcessor entryProcessor = new InfinispanProcessor<>(request);
            CompletableFuture resultFuture = readWriteMap.eval(key, entryProcessor);
            return resultFuture.thenApply(resultBytes -> deserializeResult(resultBytes, request.getBackwardCompatibilityVersion()));
        } catch (Throwable t) {
            CompletableFuture> fail = new CompletableFuture<>();
            fail.completeExceptionally(t);
            return fail;
        }
    }

    @Override
    public boolean isExpireAfterWriteSupported() {
        return true;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy