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

io.github.bucket4j.grid.hazelcast.HazelcastCompareAndSwapBasedProxyManager Maven / Gradle / Ivy

The newest version!
/*-
 * ========================LICENSE_START=================================
 * Bucket4j
 * %%
 * Copyright (C) 2015 - 2022 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==================================
 */
package io.github.bucket4j.grid.hazelcast;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import com.hazelcast.map.IMap;

import io.github.bucket4j.distributed.proxy.ClientSideConfig;
import io.github.bucket4j.distributed.proxy.generic.compare_and_swap.AbstractCompareAndSwapBasedProxyManager;
import io.github.bucket4j.distributed.proxy.generic.compare_and_swap.AsyncCompareAndSwapOperation;
import io.github.bucket4j.distributed.proxy.generic.compare_and_swap.CompareAndSwapOperation;
import io.github.bucket4j.distributed.remote.RemoteBucketState;
import io.github.bucket4j.grid.hazelcast.Bucket4jHazelcast.HazelcastCompareAndSwapBasedProxyManagerBuilder;

public class HazelcastCompareAndSwapBasedProxyManager extends AbstractCompareAndSwapBasedProxyManager {

    private final IMap map;

    HazelcastCompareAndSwapBasedProxyManager(HazelcastCompareAndSwapBasedProxyManagerBuilder builder) {
        super(builder.getClientSideConfig());
        this.map = builder.map;
    }

    /**
     * @deprecated use {@link Bucket4jHazelcast#casBasedBuilder(IMap)}
     */
    @Deprecated
    public HazelcastCompareAndSwapBasedProxyManager(IMap map) {
        this(map, ClientSideConfig.getDefault());
    }

    /**
     * @deprecated use {@link Bucket4jHazelcast#casBasedBuilder(IMap)}
     */
    @Deprecated
    public HazelcastCompareAndSwapBasedProxyManager(IMap map, ClientSideConfig clientSideConfig) {
        super(clientSideConfig);
        this.map = Objects.requireNonNull(map);
    }

    @Override
    protected CompletableFuture removeAsync(K key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean isAsyncModeSupported() {
        // Because Hazelcast IMap does not provide "replaceAsync" API.
        return false;
    }

    @Override
    public void removeProxy(K key) {
        map.remove(key);
    }

    @Override
    protected CompareAndSwapOperation beginCompareAndSwapOperation(K key) {
        return new CompareAndSwapOperation() {
            @Override
            public Optional getStateData(Optional timeoutNanos) {
                byte[] data = map.get(key);
                return Optional.ofNullable(data);
            }

            @Override
            public boolean compareAndSwap(byte[] originalData, byte[] newData, RemoteBucketState newState, Optional timeoutNanos) {
                if (originalData == null) {
                    return map.putIfAbsent(key, newData) == null;
                } else {
                    return map.replace(key, originalData, newData);
                }
            }
        };
    }

    @Override
    protected AsyncCompareAndSwapOperation beginAsyncCompareAndSwapOperation(K key) {
        throw new UnsupportedOperationException();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy