Search in sources :

Example 1 with Data

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

the class AbstractClientCacheProxy method putToAllPartitionsAndWaitForCompletion.

private void putToAllPartitionsAndWaitForCompletion(List<Map.Entry<Data, Data>>[] entriesPerPartition, ExpiryPolicy expiryPolicy, long start) throws ExecutionException, InterruptedException {
    Data expiryPolicyData = toData(expiryPolicy);
    List<FutureEntriesTuple> futureEntriesTuples = new ArrayList<FutureEntriesTuple>(entriesPerPartition.length);
    for (int partitionId = 0; partitionId < entriesPerPartition.length; partitionId++) {
        List<Map.Entry<Data, Data>> entries = entriesPerPartition[partitionId];
        if (entries != null) {
            int completionId = nextCompletionId();
            // TODO If there is a single entry, we could make use of a put operation since that is a bit cheaper
            ClientMessage request = CachePutAllCodec.encodeRequest(nameWithPrefix, entries, expiryPolicyData, completionId);
            Future f = invoke(request, partitionId, completionId);
            futureEntriesTuples.add(new FutureEntriesTuple(f, entries));
        }
    }
    waitResponseFromAllPartitionsForPutAll(futureEntriesTuples, start);
}
Also used : ArrayList(java.util.ArrayList) ClientDelegatingFuture(com.hazelcast.client.util.ClientDelegatingFuture) Future(java.util.concurrent.Future) CompletedFuture(com.hazelcast.util.executor.CompletedFuture) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture) ICompletableFuture(com.hazelcast.core.ICompletableFuture) Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 2 with Data

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

the class AbstractClientCacheProxy method populateResultFromNearCache.

private void populateResultFromNearCache(Set<Data> keySet, Map<K, V> result) {
    if (nearCache == null) {
        return;
    }
    Iterator<Data> iterator = keySet.iterator();
    while (iterator.hasNext()) {
        Data key = iterator.next();
        Object cached = getCachedValue(key, true);
        if (cached != NOT_CACHED) {
            result.put((K) toObject(key), (V) cached);
            iterator.remove();
        }
    }
}
Also used : Data(com.hazelcast.nio.serialization.Data)

Example 3 with Data

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

the class AbstractClientCacheProxy method getAll.

@Override
public Map<K, V> getAll(Set<? extends K> keys, ExpiryPolicy expiryPolicy) {
    final long start = System.nanoTime();
    ensureOpen();
    validateNotNull(keys);
    if (keys.isEmpty()) {
        return emptyMap();
    }
    final Set<Data> keySet = new HashSet<Data>(keys.size());
    for (K key : keys) {
        final Data k = toData(key);
        keySet.add(k);
    }
    Map<K, V> result = createHashMap(keys.size());
    populateResultFromNearCache(keySet, result);
    if (keySet.isEmpty()) {
        return result;
    }
    List<Map.Entry<Data, Data>> entries;
    Map<Data, Long> reservations = createHashMap(keySet.size());
    try {
        for (Data key : keySet) {
            long reservationId = tryReserveForUpdate(key);
            if (reservationId != NOT_RESERVED) {
                reservations.put(key, reservationId);
            }
        }
        Data expiryPolicyData = toData(expiryPolicy);
        ClientMessage request = CacheGetAllCodec.encodeRequest(nameWithPrefix, keySet, expiryPolicyData);
        ClientMessage responseMessage = invoke(request);
        entries = CacheGetAllCodec.decodeResponse(responseMessage).response;
        for (Map.Entry<Data, Data> dataEntry : entries) {
            Data keyData = dataEntry.getKey();
            Data valueData = dataEntry.getValue();
            K key = toObject(keyData);
            V value = toObject(valueData);
            result.put(key, value);
            Long reservationId = reservations.get(keyData);
            if (reservationId != null) {
                storeInNearCache(keyData, valueData, value, reservationId, false);
                reservations.remove(keyData);
            }
        }
    } finally {
        releaseRemainingReservedKeys(reservations);
    }
    if (statisticsEnabled) {
        statistics.increaseCacheHits(entries.size());
        statistics.addGetTimeNanos(System.nanoTime() - start);
    }
    return result;
}
Also used : Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) MapUtil.createHashMap(com.hazelcast.util.MapUtil.createHashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) AbstractMap(java.util.AbstractMap) HashSet(java.util.HashSet)

Example 4 with Data

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

the class AbstractClientCacheProxy method getInternal.

protected Object getInternal(final K key, ExpiryPolicy expiryPolicy, boolean async) {
    final long start = System.nanoTime();
    ensureOpen();
    validateNotNull(key);
    final Data keyData = toData(key);
    Object cached = getCachedValue(keyData, !async);
    if (cached != NOT_CACHED) {
        return asCompletedFutureOrValue(cached, async);
    }
    final long reservationId = tryReserveForUpdate(keyData);
    final Data expiryPolicyData = toData(expiryPolicy);
    ClientMessage request = CacheGetCodec.encodeRequest(nameWithPrefix, keyData, expiryPolicyData);
    ClientInvocationFuture future;
    try {
        final int partitionId = clientContext.getPartitionService().getPartitionId(keyData);
        final HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance();
        final ClientInvocation clientInvocation = new ClientInvocation(client, request, partitionId);
        future = clientInvocation.invoke();
    } catch (Throwable t) {
        invalidateNearCache(keyData);
        throw rethrow(t);
    }
    SerializationService serializationService = clientContext.getSerializationService();
    ClientDelegatingFuture<V> delegatingFuture = new ClientDelegatingFuture<V>(future, serializationService, cacheGetResponseDecoder);
    if (async) {
        if (nearCache != null) {
            delegatingFuture.andThenInternal(new ExecutionCallback<Data>() {

                public void onResponse(Data valueData) {
                    storeInNearCache(keyData, valueData, null, reservationId, false);
                    if (statisticsEnabled) {
                        handleStatisticsOnGet(start, valueData);
                    }
                }

                public void onFailure(Throwable t) {
                    invalidateNearCache(keyData);
                }
            }, false);
        }
        return delegatingFuture;
    } else {
        try {
            V value = toObject(delegatingFuture.get());
            if (nearCache != null) {
                storeInNearCache(keyData, (Data) delegatingFuture.getResponse(), value, reservationId, false);
            }
            if (statisticsEnabled) {
                handleStatisticsOnGet(start, value);
            }
            return value;
        } catch (Throwable e) {
            invalidateNearCache(keyData);
            throw rethrowAllowedTypeFirst(e, CacheException.class);
        }
    }
}
Also used : ClientDelegatingFuture(com.hazelcast.client.util.ClientDelegatingFuture) CacheException(javax.cache.CacheException) SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.serialization.Data) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl)

Example 5 with Data

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

the class AbstractClientCacheProxy method groupDataToPartitions.

private List<Map.Entry<Data, Data>>[] groupDataToPartitions(Map<? extends K, ? extends V> map, ClientPartitionService partitionService, int partitionCount) {
    List<Map.Entry<Data, Data>>[] entriesPerPartition = new List[partitionCount];
    SerializationService serializationService = clientContext.getSerializationService();
    for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
        K key = entry.getKey();
        V value = entry.getValue();
        validateNotNull(key, value);
        Data keyData = serializationService.toData(key);
        Data valueData = serializationService.toData(value);
        int partitionId = partitionService.getPartitionId(keyData);
        List<Map.Entry<Data, Data>> entries = entriesPerPartition[partitionId];
        if (entries == null) {
            entries = new ArrayList<Map.Entry<Data, Data>>();
            entriesPerPartition[partitionId] = entries;
        }
        entries.add(new AbstractMap.SimpleImmutableEntry<Data, Data>(keyData, valueData));
    }
    return entriesPerPartition;
}
Also used : SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.serialization.Data) AbstractMap(java.util.AbstractMap) ArrayList(java.util.ArrayList) List(java.util.List) MapUtil.createHashMap(com.hazelcast.util.MapUtil.createHashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) AbstractMap(java.util.AbstractMap)

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