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);
}
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();
}
}
}
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;
}
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);
}
}
}
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;
}
Aggregations