Search in sources :

Example 51 with MapOperation

use of com.hazelcast.map.impl.operation.MapOperation in project hazelcast by hazelcast.

the class TransactionalMapProxySupport method lockAndGet.

private VersionedValue lockAndGet(Data key, long timeout, boolean shouldLoad) {
    VersionedValue versionedValue = valueMap.get(key);
    if (versionedValue != null) {
        return versionedValue;
    }
    boolean blockReads = tx.getTransactionType() == TransactionType.ONE_PHASE;
    MapOperation operation = operationProvider.createTxnLockAndGetOperation(name, key, timeout, timeout, tx.getOwnerUuid(), shouldLoad, blockReads);
    operation.setThreadId(ThreadUtil.getThreadId());
    try {
        int partitionId = partitionService.getPartitionId(key);
        Future<VersionedValue> future = operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId);
        versionedValue = future.get();
        if (versionedValue == null) {
            throw new TransactionTimedOutException("Transaction couldn't obtain lock for the key: " + toObjectIfNeeded(key));
        }
        valueMap.put(key, versionedValue);
        return versionedValue;
    } catch (Throwable t) {
        throw rethrow(t);
    }
}
Also used : TransactionTimedOutException(com.hazelcast.transaction.TransactionTimedOutException) MapOperation(com.hazelcast.map.impl.operation.MapOperation)

Example 52 with MapOperation

use of com.hazelcast.map.impl.operation.MapOperation in project hazelcast by hazelcast.

the class TransactionalMapProxySupport method putIfAbsentInternal.

Data putIfAbsentInternal(Data key, Data value, NearCachingHook hook) {
    boolean unlockImmediately = !valueMap.containsKey(key);
    VersionedValue versionedValue = lockAndGet(key, tx.getTimeoutMillis());
    if (versionedValue.value != null) {
        if (unlockImmediately) {
            unlock(key, versionedValue);
            return versionedValue.value;
        }
        addUnlockTransactionRecord(key, versionedValue.version);
        return versionedValue.value;
    }
    MapOperation operation = operationProvider.createTxnSetOperation(name, key, value, versionedValue.version, UNSET);
    tx.add(new MapTransactionLogRecord(name, key, getPartitionId(key), operation, tx.getOwnerUuid(), tx.getTxnId(), hook));
    return versionedValue.value;
}
Also used : MapOperation(com.hazelcast.map.impl.operation.MapOperation)

Example 53 with MapOperation

use of com.hazelcast.map.impl.operation.MapOperation in project hazelcast by hazelcast.

the class TransactionalMapProxySupport method getInternal.

Object getInternal(Object nearCacheKey, Data keyData, boolean skipNearCacheLookup) {
    if (!skipNearCacheLookup && nearCacheEnabled) {
        Object value = getCachedValue(nearCacheKey, true);
        if (value != NOT_CACHED) {
            return value;
        }
    }
    MapOperation operation = operationProvider.createGetOperation(name, keyData);
    operation.setThreadId(ThreadUtil.getThreadId());
    int partitionId = partitionService.getPartitionId(keyData);
    try {
        Future future = operationService.createInvocationBuilder(SERVICE_NAME, operation, partitionId).setResultDeserialized(false).invoke();
        return future.get();
    } catch (Throwable t) {
        throw rethrow(t);
    }
}
Also used : Future(java.util.concurrent.Future) TransactionalDistributedObject(com.hazelcast.spi.impl.TransactionalDistributedObject) MapOperation(com.hazelcast.map.impl.operation.MapOperation)

Example 54 with MapOperation

use of com.hazelcast.map.impl.operation.MapOperation in project hazelcast by hazelcast.

the class MapKeyLoader method sendBatch.

/**
 * Sends the key batches to the partition owners for value
 * loading. The returned futures represent pending offloading
 * of the value loading on the partition owner. This means
 * that once the partition owner receives the keys, it will
 * offload the value loading task and return immediately,
 * thus completing the future. The future does not mean
 * the value loading tasks have been completed or that the
 * entries have been loaded and put into the record store.
 *
 * @param batch                    a map from partition ID
 *                                 to a batch of keys for that partition
 * @param replaceExistingValues    if the existing
 *                                 entries for the loaded keys should be replaced
 * @param nodeWideLoadedKeyLimiter controls number of loaded keys
 * @return a list of futures representing pending
 * completion of the value offloading task
 */
private List<Future> sendBatch(Map<Integer, List<Data>> batch, boolean replaceExistingValues, Semaphore nodeWideLoadedKeyLimiter) {
    Set<Entry<Integer, List<Data>>> entries = batch.entrySet();
    List<Future> futures = new ArrayList<>(entries.size());
    Iterator<Entry<Integer, List<Data>>> iterator = entries.iterator();
    while (iterator.hasNext()) {
        Entry<Integer, List<Data>> e = iterator.next();
        int partitionId = e.getKey();
        List<Data> keys = e.getValue();
        int numberOfLoadedKeys = keys.size();
        try {
            MapOperation op = operationProvider.createLoadAllOperation(mapName, keys, replaceExistingValues);
            InternalCompletableFuture<Object> future = opService.invokeOnPartition(SERVICE_NAME, op, partitionId);
            futures.add(future);
        } finally {
            nodeWideLoadedKeyLimiter.release(numberOfLoadedKeys);
        }
        iterator.remove();
    }
    return futures;
}
Also used : ArrayList(java.util.ArrayList) Data(com.hazelcast.internal.serialization.Data) MapOperation(com.hazelcast.map.impl.operation.MapOperation) Entry(java.util.Map.Entry) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Future(java.util.concurrent.Future) List(java.util.List) ArrayList(java.util.ArrayList)

Example 55 with MapOperation

use of com.hazelcast.map.impl.operation.MapOperation in project hazelcast by hazelcast.

the class MapProxySupport method putInternal.

protected Data putInternal(Object key, Data valueData, long ttl, TimeUnit ttlUnit, long maxIdle, TimeUnit maxIdleUnit) {
    Data keyData = toDataWithStrategy(key);
    MapOperation operation = newPutOperation(keyData, valueData, ttl, ttlUnit, maxIdle, maxIdleUnit);
    return (Data) invokeOperation(keyData, operation);
}
Also used : Data(com.hazelcast.internal.serialization.Data) MapOperation(com.hazelcast.map.impl.operation.MapOperation)

Aggregations

MapOperation (com.hazelcast.map.impl.operation.MapOperation)80 MapOperationProvider (com.hazelcast.map.impl.operation.MapOperationProvider)25 Data (com.hazelcast.internal.serialization.Data)23 Future (java.util.concurrent.Future)16 Data (com.hazelcast.nio.serialization.Data)6 ArrayList (java.util.ArrayList)5 InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)4 SerializationService (com.hazelcast.internal.serialization.SerializationService)3 MapService (com.hazelcast.map.impl.MapService)3 MapServiceContext (com.hazelcast.map.impl.MapServiceContext)3 InternalCompletableFuture (com.hazelcast.spi.InternalCompletableFuture)3 AbstractDistributedObject (com.hazelcast.spi.impl.AbstractDistributedObject)3 InitializingObject (com.hazelcast.spi.impl.InitializingObject)3 InternalCompletableFuture.newCompletedFuture (com.hazelcast.spi.impl.InternalCompletableFuture.newCompletedFuture)3 InvocationFuture (com.hazelcast.spi.impl.operationservice.impl.InvocationFuture)3 OperationServiceImpl (com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 EntryView (com.hazelcast.core.EntryView)2 EntryProcessor (com.hazelcast.map.EntryProcessor)2