Search in sources :

Example 6 with Data

use of com.hazelcast.nio.serialization.Data in project hazelcast by hazelcast.

the class ClientCacheHelper method createCacheConfig.

/**
     * Sends the cache configuration to server.
     *
     * @param client             the client instance which will send the operation to server
     * @param configs            {@link ConcurrentMap} based store that holds cache configurations
     * @param cacheName          full cache name with prefixes
     * @param newCacheConfig     the cache configuration to be sent to server
     * @param createAlsoOnOthers flag which represents whether cache config
     *                           will be sent to other nodes by the target node
     * @param syncCreate         flag which represents response will be waited from the server
     * @param <K>                type of the key of the cache
     * @param <V>                type of the value of the cache
     * @return the created cache configuration
     */
static <K, V> CacheConfig<K, V> createCacheConfig(HazelcastClientInstanceImpl client, ConcurrentMap<String, CacheConfig> configs, String cacheName, CacheConfig<K, V> newCacheConfig, boolean createAlsoOnOthers, boolean syncCreate) {
    try {
        CacheConfig<K, V> currentCacheConfig = configs.get(cacheName);
        int partitionId = client.getClientPartitionService().getPartitionId(newCacheConfig.getNameWithPrefix());
        Object resolvedConfig = resolveCacheConfig(client, newCacheConfig, partitionId);
        Data configData = client.getSerializationService().toData(resolvedConfig);
        ClientMessage request = CacheCreateConfigCodec.encodeRequest(configData, createAlsoOnOthers);
        ClientInvocation clientInvocation = new ClientInvocation(client, request, partitionId);
        Future<ClientMessage> future = clientInvocation.invoke();
        if (syncCreate) {
            final ClientMessage response = future.get();
            final Data data = CacheCreateConfigCodec.decodeResponse(response).response;
            return resolveCacheConfig(client, clientInvocation, data);
        } else {
            return currentCacheConfig;
        }
    } catch (Exception e) {
        throw rethrow(e);
    }
}
Also used : Data(com.hazelcast.nio.serialization.Data) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) IOException(java.io.IOException)

Example 7 with Data

use of com.hazelcast.nio.serialization.Data in project hazelcast by hazelcast.

the class ClientCacheProxy method updateCacheListenerConfigOnOtherNodes.

protected void updateCacheListenerConfigOnOtherNodes(CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration, boolean isRegister) {
    final Collection<Member> members = clientContext.getClusterService().getMemberList();
    final HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance();
    for (Member member : members) {
        try {
            final Address address = member.getAddress();
            Data configData = toData(cacheEntryListenerConfiguration);
            final ClientMessage request = CacheListenerRegistrationCodec.encodeRequest(nameWithPrefix, configData, isRegister, address);
            final ClientInvocation invocation = new ClientInvocation(client, request, address);
            invocation.invoke();
        } catch (Exception e) {
            ExceptionUtil.sneakyThrow(e);
        }
    }
}
Also used : Address(com.hazelcast.nio.Address) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) Data(com.hazelcast.nio.serialization.Data) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) Member(com.hazelcast.core.Member) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException)

Example 8 with Data

use of com.hazelcast.nio.serialization.Data in project hazelcast by hazelcast.

the class ClientCacheProxy method loadAll.

@Override
public void loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener) {
    ensureOpen();
    validateNotNull(keys);
    for (K key : keys) {
        CacheProxyUtil.validateConfiguredTypes(cacheConfig, key);
    }
    HashSet<Data> keysData = new HashSet<Data>();
    for (K key : keys) {
        keysData.add(toData(key));
    }
    ClientMessage request = CacheLoadAllCodec.encodeRequest(nameWithPrefix, keysData, replaceExistingValues);
    try {
        submitLoadAllTask(request, completionListener, keysData);
    } catch (Exception e) {
        if (completionListener != null) {
            completionListener.onException(e);
        }
        throw new CacheException(e);
    }
}
Also used : CacheException(javax.cache.CacheException) Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) HashSet(java.util.HashSet)

Example 9 with Data

use of com.hazelcast.nio.serialization.Data in project hazelcast by hazelcast.

the class ClientCacheProxy method containsKey.

@Override
public boolean containsKey(K key) {
    ensureOpen();
    validateNotNull(key);
    final Data keyData = toData(key);
    Object cached = getCachedValue(keyData, false);
    if (cached != NOT_CACHED) {
        return true;
    }
    ClientMessage request = CacheContainsKeyCodec.encodeRequest(nameWithPrefix, keyData);
    ClientMessage result = invoke(request, keyData);
    return CacheContainsKeyCodec.decodeResponse(result).response;
}
Also used : Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 10 with Data

use of com.hazelcast.nio.serialization.Data in project hazelcast by hazelcast.

the class ClientCacheProxy method invoke.

@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);
    Data epData = toData(entryProcessor);
    List<Data> argumentsData = null;
    if (arguments != null) {
        argumentsData = new ArrayList<Data>(arguments.length);
        for (int i = 0; i < arguments.length; i++) {
            argumentsData.add(toData(arguments[i]));
        }
    }
    final int completionId = nextCompletionId();
    ClientMessage request = CacheEntryProcessorCodec.encodeRequest(nameWithPrefix, keyData, epData, argumentsData, completionId);
    try {
        final ICompletableFuture<ClientMessage> f = invoke(request, keyData, completionId);
        final ClientMessage response = getSafely(f);
        final Data data = CacheEntryProcessorCodec.decodeResponse(response).response;
        // At client side, we don't know what entry processor does so we ignore it from statistics perspective
        return toObject(data);
    } catch (CacheException ce) {
        throw ce;
    } catch (Exception e) {
        throw new EntryProcessorException(e);
    }
}
Also used : EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException)

Aggregations

Data (com.hazelcast.nio.serialization.Data)773 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)140 Test (org.junit.Test)121 QuickTest (com.hazelcast.test.annotation.QuickTest)118 ParallelTest (com.hazelcast.test.annotation.ParallelTest)108 ArrayList (java.util.ArrayList)81 Map (java.util.Map)64 SerializationService (com.hazelcast.spi.serialization.SerializationService)54 HashMap (java.util.HashMap)54 NodeEngine (com.hazelcast.spi.NodeEngine)50 HashSet (java.util.HashSet)39 Address (com.hazelcast.nio.Address)28 AbstractMap (java.util.AbstractMap)28 Record (com.hazelcast.map.impl.record.Record)27 HazelcastInstance (com.hazelcast.core.HazelcastInstance)26 HeapData (com.hazelcast.internal.serialization.impl.HeapData)26 List (java.util.List)20 Future (java.util.concurrent.Future)20 CacheEventData (com.hazelcast.cache.impl.CacheEventData)19 Operation (com.hazelcast.spi.Operation)18