
com.hazelcast.cache.impl.AbstractCacheProxy Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
*
* 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 com.hazelcast.cache.impl;
import com.hazelcast.config.CacheConfig;
import com.hazelcast.core.ICompletableFuture;
import com.hazelcast.map.impl.MapEntries;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.internal.serialization.SerializationService;
import com.hazelcast.partition.InternalPartitionService;
import com.hazelcast.spi.InternalCompletableFuture;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.Operation;
import com.hazelcast.spi.OperationFactory;
import com.hazelcast.spi.OperationService;
import com.hazelcast.util.ExceptionUtil;
import javax.cache.CacheException;
import javax.cache.expiry.ExpiryPolicy;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import static com.hazelcast.cache.impl.CacheProxyUtil.validateNotNull;
/**
* Hazelcast provides extension functionality to default spec interface {@link javax.cache.Cache}.
* {@link com.hazelcast.cache.ICache} is the designated interface.
* AbstractCacheProxyExtension provides implementation of various {@link com.hazelcast.cache.ICache} methods.
* Note: this partial implementation is used by server or embedded mode cache.
*
* @param the type of key.
* @param the type of value.
* @see com.hazelcast.cache.impl.CacheProxy
* @see com.hazelcast.cache.ICache
*/
abstract class AbstractCacheProxy
extends AbstractInternalCacheProxy {
protected AbstractCacheProxy(CacheConfig cacheConfig, NodeEngine nodeEngine, ICacheService cacheService) {
super(cacheConfig, nodeEngine, cacheService);
}
@Override
public InternalCompletableFuture getAsync(K key) {
return getAsync(key, null);
}
@Override
public InternalCompletableFuture getAsync(K key, ExpiryPolicy expiryPolicy) {
ensureOpen();
validateNotNull(key);
final Data keyData = serializationService.toData(key);
final Operation op = operationProvider.createGetOperation(keyData, expiryPolicy);
return invoke(op, keyData, false);
}
@Override
public InternalCompletableFuture putAsync(K key, V value) {
return putAsync(key, value, null);
}
@Override
public InternalCompletableFuture putAsync(K key, V value, ExpiryPolicy expiryPolicy) {
return putAsyncInternal(key, value, expiryPolicy, false, false);
}
@Override
public InternalCompletableFuture putIfAbsentAsync(K key, V value) {
return putIfAbsentAsyncInternal(key, value, null, false);
}
@Override
public InternalCompletableFuture putIfAbsentAsync(K key, V value, ExpiryPolicy expiryPolicy) {
return putIfAbsentAsyncInternal(key, value, expiryPolicy, false);
}
@Override
public ICompletableFuture getAndPutAsync(K key, V value) {
return getAndPutAsync(key, value, null);
}
@Override
public ICompletableFuture getAndPutAsync(K key, V value, ExpiryPolicy expiryPolicy) {
return putAsyncInternal(key, value, expiryPolicy, true, false);
}
@Override
public InternalCompletableFuture removeAsync(K key) {
return removeAsyncInternal(key, null, false, false, false);
}
@Override
public InternalCompletableFuture removeAsync(K key, V oldValue) {
return removeAsyncInternal(key, oldValue, true, false, false);
}
@Override
public ICompletableFuture getAndRemoveAsync(K key) {
return removeAsyncInternal(key, null, false, true, false);
}
@Override
public ICompletableFuture replaceAsync(K key, V value) {
return replaceAsyncInternal(key, null, value, null, false, false, false);
}
@Override
public ICompletableFuture replaceAsync(K key, V value, ExpiryPolicy expiryPolicy) {
return replaceAsyncInternal(key, null, value, expiryPolicy, false, false, false);
}
@Override
public ICompletableFuture replaceAsync(K key, V oldValue, V newValue) {
return replaceAsyncInternal(key, oldValue, newValue, null, true, false, false);
}
@Override
public ICompletableFuture replaceAsync(K key, V oldValue, V newValue, ExpiryPolicy expiryPolicy) {
return replaceAsyncInternal(key, oldValue, newValue, expiryPolicy, true, false, false);
}
@Override
public ICompletableFuture getAndReplaceAsync(K key, V value) {
return replaceAsyncInternal(key, null, value, null, false, true, false);
}
@Override
public ICompletableFuture getAndReplaceAsync(K key, V value, ExpiryPolicy expiryPolicy) {
return replaceAsyncInternal(key, null, value, expiryPolicy, false, true, false);
}
@Override
public V get(K key, ExpiryPolicy expiryPolicy) {
final Future f = getAsync(key, expiryPolicy);
try {
return f.get();
} catch (Throwable e) {
throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
}
}
@Override
public Map getAll(Set extends K> keys, ExpiryPolicy expiryPolicy) {
ensureOpen();
validateNotNull(keys);
if (keys.isEmpty()) {
return Collections.EMPTY_MAP;
}
final Set ks = new HashSet(keys.size());
for (K key : keys) {
final Data k = serializationService.toData(key);
ks.add(k);
}
final Map result = new HashMap();
final Collection partitions = getPartitionsForKeys(ks);
try {
OperationFactory factory = operationProvider.createGetAllOperationFactory(ks, expiryPolicy);
OperationService operationService = getNodeEngine().getOperationService();
Map responses = operationService.invokeOnPartitions(getServiceName(), factory, partitions);
for (Object response : responses.values()) {
MapEntries mapEntries = serializationService.toObject(response);
for (Map.Entry entry : mapEntries) {
final V value = serializationService.toObject(entry.getValue());
final K key = serializationService.toObject(entry.getKey());
result.put(key, value);
}
}
} catch (Throwable e) {
throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
}
return result;
}
@Override
public void put(K key, V value, ExpiryPolicy expiryPolicy) {
final InternalCompletableFuture
© 2015 - 2025 Weber Informatics LLC | Privacy Policy