/*
* Copyright (c) 2008-2013, 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.client.cache.impl;
import com.hazelcast.cache.impl.AbstractHazelcastCacheManager;
import com.hazelcast.cache.impl.CacheEntryProcessorResult;
import com.hazelcast.cache.impl.CacheEventListenerAdaptor;
import com.hazelcast.cache.impl.CacheProxyUtil;
import com.hazelcast.cache.impl.client.CacheAddEntryListenerRequest;
import com.hazelcast.cache.impl.client.CacheContainsKeyRequest;
import com.hazelcast.cache.impl.client.CacheEntryProcessorRequest;
import com.hazelcast.cache.impl.client.CacheListenerRegistrationRequest;
import com.hazelcast.cache.impl.client.CacheLoadAllRequest;
import com.hazelcast.cache.impl.client.CacheRemoveEntryListenerRequest;
import com.hazelcast.client.nearcache.ClientNearCache;
import com.hazelcast.client.spi.ClientContext;
import com.hazelcast.client.spi.ClientInvocationService;
import com.hazelcast.client.spi.EventHandler;
import com.hazelcast.config.CacheConfig;
import com.hazelcast.core.ICompletableFuture;
import com.hazelcast.instance.MemberImpl;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.util.ExceptionUtil;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.cache.configuration.CacheEntryListenerConfiguration;
import javax.cache.configuration.Configuration;
import javax.cache.expiry.ExpiryPolicy;
import javax.cache.integration.CompletionListener;
import javax.cache.processor.EntryProcessor;
import javax.cache.processor.EntryProcessorException;
import javax.cache.processor.EntryProcessorResult;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import static com.hazelcast.cache.impl.CacheProxyUtil.validateNotNull;
/**
* ICache implementation for client
* <p/>
* This proxy is the implementation of ICache and javax.cache.Cache which is returned by
* HazelcastClientCacheManager. Represent a cache on client.
* <p/>
* This implementation is a thin proxy implementation using hazelcast client infrastructure
*
* @param <K> key type
* @param <V> value type
*/
public class ClientCacheProxy<K, V>
extends AbstractClientCacheProxy<K, V> {
protected final ILogger logger;
private AbstractHazelcastCacheManager cacheManager;
public ClientCacheProxy(CacheConfig<K, V> cacheConfig, ClientContext clientContext,
HazelcastClientCacheManager cacheManager) {
super(cacheConfig, clientContext);
this.cacheManager = cacheManager;
logger = Logger.getLogger(getClass());
}
@Override
public V get(K key) {
return get(key, null);
}
@Override
public Map<K, V> getAll(Set<? extends K> keys) {
return getAll(keys, null);
}
@Override
public boolean containsKey(K key) {
ensureOpen();
validateNotNull(key);
final Data keyData = toData(key);
Object cached = nearCache != null ? nearCache.get(keyData) : null;
if (cached != null && !ClientNearCache.NULL_OBJECT.equals(cached)) {
return true;
}
CacheContainsKeyRequest request = new CacheContainsKeyRequest(nameWithPrefix, keyData, cacheConfig.getInMemoryFormat());
ICompletableFuture future;
try {
future = invoke(request, keyData, false);
return (Boolean) toObject(future.get());
} catch (Exception e) {
throw ExceptionUtil.rethrow(e);
}
}
@Override
public void loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener) {
ensureOpen();
validateNotNull(keys);
for (K key : keys) {
CacheProxyUtil.validateConfiguredTypes(cacheConfig, key);
}
validateCacheLoader(completionListener);
HashSet<Data> keysData = new HashSet<Data>();
for (K key : keys) {
keysData.add(toData(key));
}
CacheLoadAllRequest request = new CacheLoadAllRequest(nameWithPrefix, keysData, replaceExistingValues);
try {
submitLoadAllTask(request, completionListener);
} catch (Exception e) {
if (completionListener != null) {
completionListener.onException(e);
}
throw new CacheException(e);
}
}
@Override
public void put(K key, V value) {
put(key, value, null);
}
@Override
public V getAndPut(K key, V value) {
return getAndPut(key, value, null);
}
@Override
public void putAll(Map<? extends K, ? extends V> map) {
putAll(map, null);
}
@Override
public boolean putIfAbsent(K key, V value) {
return putIfAbsent(key, value, null);
}
@Override
public boolean remove(K key) {
final ICompletableFuture<Boolean> f = removeAsyncInternal(key, null, false, false, true);
try {
return f.get();
} catch (Throwable e) {
throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
}
}
@Override
public boolean remove(K key, V oldValue) {
final ICompletableFuture<Boolean> f = removeAsyncInternal(key, oldValue, true, false, true);
try {
return f.get();
} catch (Throwable e) {
throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
}
}
@Override
public V getAndRemove(K key) {
final ICompletableFuture<V> f = removeAsyncInternal(key, null, false, true, true);
try {
return f.get();
} catch (Throwable e) {
throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
}
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
return replace(key, oldValue, newValue, null);
}
@Override
public boolean replace(K key, V value) {
final ExpiryPolicy expiryPolicy = null;
return replace(key, value, expiryPolicy);
}
@Override
public V getAndReplace(K key, V value) {
return getAndReplace(key, value, null);
}
@Override
public void removeAll(Set<? extends K> keys) {
ensureOpen();
validateNotNull(keys);
removeAllInternal(keys, true);
}
@Override
public void removeAll() {
ensureOpen();
removeAllInternal(null, true);
}
@Override
public void clear() {
ensureOpen();
removeAllInternal(null, false);
}
@Override
public <C extends Configuration<K, V>> C getConfiguration(Class<C> clazz) {
if (clazz.isInstance(cacheConfig)) {
return clazz.cast(cacheConfig.getAsReadOnly());
}
throw new IllegalArgumentException("The configuration class " + clazz + " is not supported by this implementation");
}
@Override
public <T> T invoke(K key, EntryProcessor<K, V, T> entryProcessor, Object... arguments)
throws EntryProcessorException {
ensureOpen();
validateNotNull(key);
if (entryProcessor == null) {
throw new NullPointerException("Entry Processor is null");
}
final Data keyData = toData(key);
final CacheEntryProcessorRequest request = new CacheEntryProcessorRequest(nameWithPrefix, keyData, entryProcessor,
cacheConfig.getInMemoryFormat(), arguments);
try {
final ICompletableFuture<Data> f = invoke(request, keyData, true);
final Data data = getSafely(f);
return toObject(data);
} catch (CacheException ce) {
throw ce;
} catch (Exception e) {
throw new EntryProcessorException(e);
}
}
@Override
public <T> Map<K, EntryProcessorResult<T>> invokeAll(Set<? extends K> keys, EntryProcessor<K, V, T> entryProcessor,
Object... arguments) {
//TODO implement a Multiple invoke operation and its factory
ensureOpen();
validateNotNull(keys);
if (entryProcessor == null) {
throw new NullPointerException("Entry Processor is null");
}
Map<K, EntryProcessorResult<T>> allResult = new HashMap<K, EntryProcessorResult<T>>();
for (K key : keys) {
CacheEntryProcessorResult<T> ceResult;
try {
final T result = this.invoke(key, entryProcessor, arguments);
ceResult = result != null ? new CacheEntryProcessorResult<T>(result) : null;
} catch (Exception e) {
ceResult = new CacheEntryProcessorResult<T>(e);
}
if (ceResult != null) {
allResult.put(key, ceResult);
}
}
return allResult;
}
@Override
public String getName() {
return name;
}
@Override
public CacheManager getCacheManager() {
return cacheManager;
}
@Override
public <T> T unwrap(Class<T> clazz) {
if (clazz.isAssignableFrom(((Object) this).getClass())) {
return clazz.cast(this);
}
throw new IllegalArgumentException("Unwrapping to " + clazz + " is not supported by this implementation");
}
@Override
public void registerCacheEntryListener(CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) {
ensureOpen();
if (cacheEntryListenerConfiguration == null) {
throw new NullPointerException("CacheEntryListenerConfiguration can't be " + "null");
}
final CacheEventListenerAdaptor<K, V> adaptor = new CacheEventListenerAdaptor<K, V>(this, cacheEntryListenerConfiguration,
clientContext.getSerializationService());
final EventHandler<Object> handler = createHandler(adaptor);
final CacheAddEntryListenerRequest registrationRequest = new CacheAddEntryListenerRequest(nameWithPrefix);
final String regId = clientContext.getListenerService().listen(registrationRequest, null, handler);
if (regId != null) {
cacheConfig.addCacheEntryListenerConfiguration(cacheEntryListenerConfiguration);
addListenerLocally(regId, cacheEntryListenerConfiguration);
//CREATE ON OTHERS TOO
updateCacheListenerConfigOnOtherNodes(cacheEntryListenerConfiguration, true);
}
}
@Override
public void deregisterCacheEntryListener(CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) {
if (cacheEntryListenerConfiguration == null) {
throw new NullPointerException("CacheEntryListenerConfiguration can't be " + "null");
}
final String regId = removeListenerLocally(cacheEntryListenerConfiguration);
if (regId != null) {
CacheRemoveEntryListenerRequest removeReq = new CacheRemoveEntryListenerRequest(nameWithPrefix, regId);
boolean isDeregistered = clientContext.getListenerService().stopListening(removeReq, regId);
if (isDeregistered) {
cacheConfig.removeCacheEntryListenerConfiguration(cacheEntryListenerConfiguration);
deregisterCompletionListener();
//REMOVE ON OTHERS TOO
updateCacheListenerConfigOnOtherNodes(cacheEntryListenerConfiguration, false);
} else {
addListenerLocally(regId, cacheEntryListenerConfiguration);
}
}
}
protected void updateCacheListenerConfigOnOtherNodes(CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration,
boolean isRegister) {
final ClientInvocationService invocationService = clientContext.getInvocationService();
final Collection<MemberImpl> members = clientContext.getClusterService().getMemberList();
final Collection<Future> futures = new ArrayList<Future>();
for (MemberImpl member : members) {
try {
final CacheListenerRegistrationRequest request = new CacheListenerRegistrationRequest(nameWithPrefix,
cacheEntryListenerConfiguration, isRegister, member.getAddress());
final Future future = invocationService.invokeOnTarget(request, member.getAddress());
futures.add(future);
} catch (Exception e) {
ExceptionUtil.sneakyThrow(e);
}
}
//make sure all configs are created
//TODO do we need this ???s
// try {
// FutureUtil.waitWithDeadline(futures, CacheProxyUtil.AWAIT_COMPLETION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
// } catch (TimeoutException e) {
// logger.warning(e);
// }
}
@Override
public Iterator<Entry<K, V>> iterator() {
ensureOpen();
return new ClientClusterWideIterator<K, V>(this, clientContext);
}
}