All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.transaction.BaseTransactionalMap Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
/**
 * Copyright (c) 2013-2021 Nikita Koksharov
 *
 * 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 org.redisson.transaction;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.redisson.RedissonMap;
import org.redisson.RedissonMultiLock;
import org.redisson.RedissonObject;
import org.redisson.ScanResult;
import org.redisson.api.RFuture;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.redisson.client.RedisClient;
import org.redisson.client.protocol.convertor.NumberConvertor;
import org.redisson.client.protocol.decoder.MapScanResult;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.misc.Hash;
import org.redisson.misc.HashValue;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;
import org.redisson.transaction.operation.DeleteOperation;
import org.redisson.transaction.operation.TouchOperation;
import org.redisson.transaction.operation.TransactionalOperation;
import org.redisson.transaction.operation.UnlinkOperation;
import org.redisson.transaction.operation.map.*;

import io.netty.buffer.ByteBuf;

/**
 * 
 * @author Nikita Koksharov
 *
 * @param  key type
 * @param  value type
 */
public class BaseTransactionalMap {

    public static class MapEntry {
        
        public static final MapEntry NULL = new MapEntry(null, null);
        
        private final Object key;
        private final Object value;
        
        public MapEntry(Object key, Object value) {
            super();
            this.key = key;
            this.value = value;
        }
        
        public Object getKey() {
            return key;
        }
        
        public Object getValue() {
            return value;
        }
        
    }
    
    private final long timeout;
    final List operations;
    final Map state = new HashMap();
    final RMap map;
    final CommandAsyncExecutor commandExecutor;
    final String transactionId;
    Boolean deleted;
    
    public BaseTransactionalMap(CommandAsyncExecutor commandExecutor, long timeout, List operations, RMap map, String transactionId) {
        super();
        this.timeout = timeout;
        this.operations = operations;
        this.map = map;
        this.commandExecutor = commandExecutor;
        this.transactionId = transactionId;
    }

    HashValue toKeyHash(Object key) {
        ByteBuf keyState = ((RedissonObject) map).encodeMapKey(key);
        try {
            return new HashValue(Hash.hash128(keyState));
        } finally {
            keyState.release();
        }
    }
    
    public RFuture isExistsAsync() {
        if (deleted != null) {
            return RedissonPromise.newSucceededFuture(!deleted);
        }
        
        return map.isExistsAsync();
    }
    
    public RFuture unlinkAsync(CommandAsyncExecutor commandExecutor) {
        return deleteAsync(commandExecutor, new UnlinkOperation(map.getName()));
    }
    
    public RFuture touchAsync(CommandAsyncExecutor commandExecutor) {
        RPromise result = new RedissonPromise();
        if (deleted != null && deleted) {
            operations.add(new TouchOperation(map.getName()));
            result.trySuccess(false);
            return result;
        }
        
        map.isExistsAsync().onComplete((exists, e) -> {
            if (e != null) {
                result.tryFailure(e);
                return;
            }
            
            operations.add(new TouchOperation(map.getName()));
            if (!exists) {
                for (MapEntry entry : state.values()) {
                    if (entry != MapEntry.NULL) {
                        exists = true;
                        break;
                    }
                }
            }
            result.trySuccess(exists);
        });
        return result;
    }

    public RFuture deleteAsync(CommandAsyncExecutor commandExecutor) {
        return deleteAsync(commandExecutor, new DeleteOperation(map.getName()));
    }

    protected RFuture deleteAsync(CommandAsyncExecutor commandExecutor, TransactionalOperation operation) {
        RPromise result = new RedissonPromise();
        if (deleted != null) {
            operations.add(operation);
            result.trySuccess(!deleted);
            deleted = true;
            return result;
        }
        
        map.isExistsAsync().onComplete((res, e) -> {
            if (e != null) {
                result.tryFailure(e);
                return;
            }
            
            operations.add(operation);
            state.replaceAll((k, v) -> MapEntry.NULL);
            deleted = true;
            result.trySuccess(res);
        });
        
        return result;
    }
    
    protected ScanResult> scanIterator(String name, RedisClient client,
            long startPos, String pattern, int count) {
        ScanResult> res = ((RedissonMap) map).scanIterator(name, client, startPos, pattern, count);
        Map newstate = new HashMap(state);
        Map newres = null;
        for (Iterator> iterator = res.getValues().iterator(); iterator.hasNext();) {
            Map.Entry entry = iterator.next();
            MapEntry mapEntry = newstate.remove(toKeyHash(entry.getKey()));
            if (mapEntry != null) {
                if (mapEntry == MapEntry.NULL) {
                    iterator.remove();
                    continue;
                }

                if (newres == null) {
                    newres = new HashMap<>();
                    for (Entry e : res.getValues()) {
                        newres.put(e.getKey(), e.getValue());
                    }
                }

                newres.put(entry.getKey(), mapEntry.getValue());
            }
        }
        
        if (startPos == 0) {
            for (Entry entry : newstate.entrySet()) {
                if (entry.getValue() == MapEntry.NULL) {
                    continue;
                }

                if (newres == null) {
                    newres = new HashMap<>();
                    for (Entry e : res.getValues()) {
                        newres.put(e.getKey(), e.getValue());
                    }
                }

                newres.put(entry.getValue().getKey(), entry.getValue().getValue());
            }
        }

        if (newres != null) {
            return new MapScanResult<>(res.getPos(), newres);
        }

        return res;
    }
    
    public RFuture containsKeyAsync(Object key) {
        HashValue keyHash = toKeyHash(key);
        MapEntry currentValue = state.get(keyHash);
        if (currentValue != null) {
            if (currentValue == MapEntry.NULL) {
                return RedissonPromise.newSucceededFuture(false);
            } else {
                return RedissonPromise.newSucceededFuture(true);
            }
        }
        
        return map.containsKeyAsync(key);
    }
    
    public RFuture containsValueAsync(Object value) {
        for (MapEntry entry : state.values()) {
            if (entry != MapEntry.NULL && isEqual(entry.getValue(), value)) {
                return RedissonPromise.newSucceededFuture(true);
            }
        }

        return map.containsValueAsync(value);
    }
    
    protected RFuture addAndGetOperationAsync(K key, Number value) {
        RPromise result = new RedissonPromise();
        long threadId = Thread.currentThread().getId();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    BigDecimal currentValue = BigDecimal.ZERO;
                    if (entry != MapEntry.NULL) {
                        currentValue = (BigDecimal) entry.getValue();
                    }
                    BigDecimal res = currentValue.add(new BigDecimal(value.toString()));

                    operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
                    state.put(keyHash, new MapEntry(key, res));
                    if (deleted != null) {
                        deleted = false;
                    }

                    NumberConvertor convertor = new NumberConvertor(value.getClass());
                    result.trySuccess((V) convertor.convert(res.toPlainString()));
                    return;
                }
                
                map.getAsync(key).onComplete((r, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    BigDecimal currentValue = new BigDecimal(r.toString());
                    BigDecimal res = currentValue.add(new BigDecimal(value.toString()));
                    
                    operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
                    state.put(keyHash, new MapEntry(key, res));
                    if (deleted != null) {
                        deleted = false;
                    }
                    
                    NumberConvertor convertor = new NumberConvertor(value.getClass());
                    result.trySuccess((V) convertor.convert(res.toPlainString()));
                });
            }
        });
        return result;
    }

    protected RFuture putIfExistsOperationAsync(K key, V value) {
        long threadId = Thread.currentThread().getId();
        return putIfExistsOperationAsync(key, value, new MapPutIfExistsOperation(map, key, value, transactionId, threadId));
    }

    protected RFuture putIfExistsOperationAsync(K key, V value, MapOperation mapOperation) {
        RPromise result = new RedissonPromise();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    operations.add(mapOperation);
                    if (entry != MapEntry.NULL) {
                        state.put(keyHash, new MapEntry(key, value));
                        if (deleted != null) {
                            deleted = false;
                        }

                        result.trySuccess((V) entry.getValue());
                    } else {
                        result.trySuccess(null);
                    }
                    return;
                }

                map.getAsync(key).onComplete((res, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }

                    operations.add(mapOperation);
                    if (res != null) {
                        state.put(keyHash, new MapEntry(key, value));
                        if (deleted != null) {
                            deleted = false;
                        }
                    }
                    result.trySuccess(res);
                });
            }
        });
        return result;
    }

    protected RFuture putIfAbsentOperationAsync(K key, V value) {
        long threadId = Thread.currentThread().getId();
        return putIfAbsentOperationAsync(key, value, new MapPutIfAbsentOperation(map, key, value, transactionId, threadId));
    }

    protected RFuture putIfAbsentOperationAsync(K key, V value, MapOperation mapOperation) {
        RPromise result = new RedissonPromise();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    operations.add(mapOperation);
                    if (entry == MapEntry.NULL) {
                        state.put(keyHash, new MapEntry(key, value));
                        if (deleted != null) {
                            deleted = false;
                        }

                        result.trySuccess(null);
                    } else {
                        result.trySuccess((V) entry.getValue());
                    }
                    return;
                }

                map.getAsync(key).onComplete((res, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    
                    operations.add(mapOperation);
                    if (res == null) {
                        state.put(keyHash, new MapEntry(key, value));
                        if (deleted != null) {
                            deleted = false;
                        }
                    }
                    result.trySuccess(res);
                });
            }
        });
        return result;
    }
    
    protected final RFuture putOperationAsync(K key, V value) {
        long threadId = Thread.currentThread().getId();
        return putOperationAsync(key, value, new MapPutOperation(map, key, value, transactionId, threadId));
    }

    protected RFuture putOperationAsync(K key, V value, MapOperation operation) {
        RPromise result = new RedissonPromise();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    operations.add(operation);
                    state.put(keyHash, new MapEntry(key, value));
                    if (deleted != null) {
                        deleted = false;
                    }
                    
                    if (entry == MapEntry.NULL) {
                        result.trySuccess(null);
                    } else {
                        result.trySuccess((V) entry.getValue());
                    }
                    return;
                }
                
                map.getAsync(key).onComplete((res, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    
                    operations.add(operation);
                    state.put(keyHash, new MapEntry(key, value));
                    if (deleted != null) {
                        deleted = false;
                    }
                    result.trySuccess(res);
                });
            }
        });
        return result;
    }

    protected RFuture fastPutIfExistsOperationAsync(K key, V value) {
        long threadId = Thread.currentThread().getId();
        return fastPutIfExistsOperationAsync(key, value, new MapFastPutIfExistsOperation(map, key, value, transactionId, threadId));
    }

    protected RFuture fastPutIfExistsOperationAsync(K key, V value, MapOperation mapOperation) {
        RPromise result = new RedissonPromise<>();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    operations.add(mapOperation);
                    if (entry != MapEntry.NULL) {
                        state.put(keyHash, new MapEntry(key, value));
                        if (deleted != null) {
                            deleted = false;
                        }

                        result.trySuccess(true);
                    } else {
                        result.trySuccess(false);
                    }
                    return;
                }

                map.getAsync(key).onComplete((res, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }

                    operations.add(mapOperation);
                    if (res != null) {
                        state.put(keyHash, new MapEntry(key, value));
                        if (deleted != null) {
                            deleted = false;
                        }
                    }
                    result.trySuccess(true);
                });
            }
        });
        return result;
    }

    protected RFuture fastPutIfAbsentOperationAsync(K key, V value) {
        long threadId = Thread.currentThread().getId();
        return fastPutIfAbsentOperationAsync(key, value, new MapFastPutIfAbsentOperation(map, key, value, transactionId, threadId));
    }

    protected RFuture fastPutIfAbsentOperationAsync(K key, V value, MapOperation mapOperation) {
        RPromise result = new RedissonPromise();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    operations.add(mapOperation);
                    if (entry == MapEntry.NULL) {
                        state.put(keyHash, new MapEntry(key, value));
                        if (deleted != null) {
                            deleted = false;
                        }
                        result.trySuccess(true);
                    } else {
                        result.trySuccess(false);
                    }
                    return;
                }

                map.getAsync(key).onComplete((res, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    
                    operations.add(mapOperation);
                    boolean isUpdated = res == null;
                    if (isUpdated) {
                        state.put(keyHash, new MapEntry(key, value));
                        if (deleted != null) {
                            deleted = false;
                        }
                    }
                    result.trySuccess(isUpdated);
                });
            }
        });
        return result;
    }
    
    protected RFuture fastPutOperationAsync(K key, V value) {
        long threadId = Thread.currentThread().getId();
        return fastPutOperationAsync(key, value, new MapFastPutOperation(map, key, value, transactionId, threadId));
    }

    protected RFuture fastPutOperationAsync(K key, V value, MapOperation operation) {
        RPromise result = new RedissonPromise();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    operations.add(operation);
                    state.put(keyHash, new MapEntry(key, value));
                    if (deleted != null) {
                        deleted = false;
                    }
                    
                    if (entry == MapEntry.NULL) {
                        result.trySuccess(true);
                    } else {
                        result.trySuccess(false);
                    }
                    return;
                }
                
                map.getAsync(key).onComplete((res, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    
                    operations.add(operation);
                    state.put(keyHash, new MapEntry(key, value));
                    if (deleted != null) {
                        deleted = false;
                    }

                    boolean isNew = res == null;
                    result.trySuccess(isNew);
                });
            }
        });
        return result;
    }
    
    @SuppressWarnings("unchecked")
    protected RFuture fastRemoveOperationAsync(K... keys) {
        RPromise result = new RedissonPromise();
        long threadId = Thread.currentThread().getId();
        executeLocked(result, new Runnable() {
            @Override
            public void run() {
                AtomicLong counter = new AtomicLong();
                List keyList = Arrays.asList(keys);
                for (Iterator iterator = keyList.iterator(); iterator.hasNext();) {
                    K key = iterator.next();
                    HashValue keyHash = toKeyHash(key);
                    MapEntry currentValue = state.get(keyHash);
                    if (currentValue != null && currentValue != MapEntry.NULL) {
                        operations.add(new MapFastRemoveOperation(map, key, transactionId, threadId));
                        state.put(keyHash, MapEntry.NULL);

                        counter.incrementAndGet();
                        iterator.remove();
                    }
                }
                
                // TODO optimize
                map.getAllAsync(new HashSet(keyList)).onComplete((res, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    
                    for (K key : res.keySet()) {
                        HashValue keyHash = toKeyHash(key);
                        operations.add(new MapFastRemoveOperation(map, key, transactionId, threadId));
                        counter.incrementAndGet();
                        state.put(keyHash, MapEntry.NULL);
                    }

                    result.trySuccess(counter.get());
                });
            }
        }, Arrays.asList(keys));
        return result;
    }
    
    public RFuture valueSizeAsync(K key) {
        HashValue keyHash = toKeyHash(key);
        MapEntry entry = state.get(keyHash);
        if (entry != null) {
            if (entry == MapEntry.NULL) {
                return RedissonPromise.newSucceededFuture(null);
            } else {
                ByteBuf valueState = ((RedissonObject) map).encodeMapValue(entry.getValue());
                try {
                    return RedissonPromise.newSucceededFuture(valueState.readableBytes());
                } finally {
                    valueState.release();
                }
            }
        }
        
        return map.valueSizeAsync(key);
    }
    
    protected RFuture getOperationAsync(K key) {
        HashValue keyHash = toKeyHash(key);
        MapEntry entry = state.get(keyHash);
        if (entry != null) {
            if (entry == MapEntry.NULL) {
                return RedissonPromise.newSucceededFuture(null);
            } else {
                return RedissonPromise.newSucceededFuture((V) entry.getValue());
            }
        }
        return ((RedissonMap) map).getOperationAsync(key);
    }

    public RFuture> readAllKeySetAsync() {
        RPromise> result = new RedissonPromise>();
        RFuture> future = map.readAllKeySetAsync();
        future.onComplete((res, e) -> {
            if (e != null) {
                result.tryFailure(e);
                return;
            }
            
            Map newstate = new HashMap(state);
            for (Iterator iterator = res.iterator(); iterator.hasNext();) {
                K key = iterator.next();
                MapEntry value = newstate.remove(toKeyHash(key));
                if (value == MapEntry.NULL) {
                    iterator.remove();
                }
            }
            
            for (MapEntry entry : newstate.values()) {
                if (entry == MapEntry.NULL) {
                    continue;
                }
                res.add((K) entry.getKey());
            }
            
            result.trySuccess(res);
        });
        
        return result;
    }
    
    public RFuture>> readAllEntrySetAsync() {
        RPromise>> result = new RedissonPromise>>();
        RFuture> future = readAllMapAsync();
        future.onComplete((res, e) -> {
            if (e != null) {
                result.tryFailure(e);
                return;
            }
            
            result.trySuccess(res.entrySet());
        });
        
        return result;
    }
    
    public RFuture> readAllValuesAsync() {
        RPromise> result = new RedissonPromise>();
        RFuture> future = readAllMapAsync();
        future.onComplete((res, e) -> {
            if (e != null) {
                result.tryFailure(e);
                return;
            }
            
            result.trySuccess(res.values());
        });
        
        return result;
    }
    
    public RFuture> readAllMapAsync() {
        RPromise> result = new RedissonPromise<>();
        RFuture> future = map.readAllMapAsync();
        future.onComplete((map, e) -> {
            if (e != null) {
                result.tryFailure(e);
                return;
            }
            
            Map newstate = new HashMap<>(state);
            for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
                K key = iterator.next();
                MapEntry entry = newstate.remove(toKeyHash(key));
                if (entry == MapEntry.NULL) {
                    iterator.remove();
                } else if (entry != null) {
                    map.put(key, (V) entry.getValue());
                }
            }
            
            for (MapEntry entry : newstate.values()) {
                if (entry == MapEntry.NULL) {
                    continue;
                }
                map.put((K) entry.getKey(), (V) entry.getValue());
            }
            
            result.trySuccess(map);
        });

        return result;
    }
    
    protected RFuture> getAllOperationAsync(Set keys) {
        RPromise> result = new RedissonPromise<>();
        Set keysToLoad = new HashSet();
        Map map = new HashMap();
        for (K key : keys) {
            HashValue keyHash = toKeyHash(key);
            
            MapEntry entry = state.get(keyHash);
            if (entry != null) {
                if (entry != MapEntry.NULL) {
                    map.put(key, (V) entry.getValue());
                }
            } else {
                keysToLoad.add(key);
            }
        }

        if (keysToLoad.isEmpty()) {
            return RedissonPromise.newSucceededFuture(map);
        }
        
        RFuture> future = ((RedissonMap) this.map).getAllOperationAsync(keysToLoad);
        future.onComplete((res, e) -> {
            if (e != null) {
                result.tryFailure(e);
                return;
            }
            
            map.putAll(res);
            result.trySuccess(map);
        });
        
        return result;
    }
    
    protected RFuture removeOperationAsync(K key) {
        RPromise result = new RedissonPromise<>();
        long threadId = Thread.currentThread().getId();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    operations.add(new MapRemoveOperation(map, key, transactionId, threadId));
                    if (entry == MapEntry.NULL) {
                        result.trySuccess(null);
                    } else {
                        state.put(keyHash, MapEntry.NULL);
                        result.trySuccess((V) entry.getValue());
                    }
                    return;
                }

                map.getAsync(key).onComplete((res, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    operations.add(new MapRemoveOperation(map, key, transactionId, threadId));
                    if (res != null) {
                        state.put(keyHash, MapEntry.NULL);
                    }

                    result.trySuccess(res);
                });
            }
        });
        return result;
    }
    
    protected RFuture removeOperationAsync(Object key, Object value) {
        RPromise result = new RedissonPromise<>();
        long threadId = Thread.currentThread().getId();
        executeLocked(result, (K) key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    if (entry == MapEntry.NULL) {
                        result.trySuccess(false);
                        return;
                    }
                    
                    operations.add(new MapRemoveOperation(map, key, value, transactionId, threadId));
                    if (isEqual(entry.getValue(), value)) {
                        state.put(keyHash, MapEntry.NULL);
                        result.trySuccess(true);
                        return;
                    }
                    
                    result.trySuccess(false);
                    return;
                }
                
                map.getAsync((K) key).onComplete((r, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    operations.add(new MapRemoveOperation(map, key, value, transactionId, threadId));
                    boolean res = isEqual(r, value);
                    if (res) {
                        state.put(keyHash, MapEntry.NULL);
                    }
                    result.trySuccess(res);
                });
            }
        });
        return result;
    }
    
    private boolean isEqual(Object value, Object oldValue) {
        ByteBuf valueBuf = ((RedissonObject) map).encodeMapValue(value);
        ByteBuf oldValueBuf = ((RedissonObject) map).encodeMapValue(oldValue);
        
        try {
            return valueBuf.equals(oldValueBuf);
        } finally {
            valueBuf.readableBytes();
            oldValueBuf.readableBytes();
        }
    }

    protected RFuture putAllOperationAsync(Map entries) {
        RPromise result = new RedissonPromise<>();
        long threadId = Thread.currentThread().getId();
        executeLocked(result, new Runnable() {
            @Override
            public void run() {
                for (Entry entry : entries.entrySet()) {
                    operations.add(new MapPutOperation(map, entry.getKey(), entry.getValue(), transactionId, threadId));
                    HashValue keyHash = toKeyHash(entry.getKey());
                    state.put(keyHash, new MapEntry(entry.getKey(), entry.getValue()));
                }
                
                if (deleted != null) {
                    deleted = false;
                }

                result.trySuccess(null);
            }
        }, (Collection) entries.keySet());
        return result;
    }
    
    protected RFuture replaceOperationAsync(K key, V oldValue, V newValue) {
        RPromise result = new RedissonPromise<>();
        long threadId = Thread.currentThread().getId();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                if (entry != null) {
                    if (entry == MapEntry.NULL) {
                        result.trySuccess(false);
                        return;
                    }
                    
                    operations.add(new MapReplaceOperation(map, key, newValue, oldValue, transactionId, threadId));
                    if (isEqual(entry.getValue(), oldValue)) {
                        state.put(keyHash, new MapEntry(key, newValue));
                        result.trySuccess(true);
                        return;
                    }
                    
                    result.trySuccess(false);
                    return;
                }

                map.getAsync(key).onComplete((r, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    
                    operations.add(new MapReplaceOperation(map, key, newValue, oldValue, transactionId, threadId));
                    boolean res = isEqual(r, oldValue);
                    if (res) {
                        state.put(keyHash, new MapEntry(key, newValue));
                    }
                    result.trySuccess(res);
                });
            }
        });
        return result;
    }

    protected RFuture replaceOperationAsync(K key, V value) {
        RPromise result = new RedissonPromise<>();
        long threadId = Thread.currentThread().getId();
        executeLocked(result, key, new Runnable() {
            @Override
            public void run() {
                HashValue keyHash = toKeyHash(key);
                MapEntry entry = state.get(keyHash);
                operations.add(new MapReplaceOperation(map, key, value, transactionId, threadId));
                if (entry != null) {
                    if (entry == MapEntry.NULL) {
                        result.trySuccess(null);
                        return;
                    }
                    
                    state.put(keyHash, new MapEntry(key, value));
                    result.trySuccess((V) entry.getValue());
                    return;
                }

                map.getAsync(key).onComplete((res, e) -> {
                    if (e != null) {
                        result.tryFailure(e);
                        return;
                    }
                    
                    operations.add(new MapReplaceOperation(map, key, value, transactionId, threadId));
                    if (res != null) {
                        state.put(keyHash, new MapEntry(key, value));
                    }
                    result.trySuccess(res);
                });
            }
        });
        return result;
    }
    
    protected  void executeLocked(RPromise promise, K key, Runnable runnable) {
        RLock lock = getLock(key);
        executeLocked(promise, runnable, lock);
    }

    protected RLock getLock(K key) {
        String lockName = ((RedissonMap) map).getLockByMapKey(key, "lock");
        return new RedissonTransactionalLock(commandExecutor, lockName, transactionId);
    }

    protected  void executeLocked(RPromise promise, Runnable runnable, RLock lock) {
        lock.lockAsync(timeout, TimeUnit.MILLISECONDS).onComplete((res, e) -> {
            if (e == null) {
                runnable.run();
            } else {
                promise.tryFailure(e);
            }
        });
    }
    
    protected  void executeLocked(RPromise promise, Runnable runnable, Collection keys) {
        List locks = new ArrayList<>(keys.size());
        for (K key : keys) {
            RLock lock = getLock(key);
            locks.add(lock);
        }
        RedissonMultiLock multiLock = new RedissonMultiLock(locks.toArray(new RLock[locks.size()]));
        long threadId = Thread.currentThread().getId();
        multiLock.lockAsync(timeout, TimeUnit.MILLISECONDS).onComplete((res, e) -> {
            if (e == null) {
                runnable.run();
            } else {
                multiLock.unlockAsync(threadId);
                promise.tryFailure(e);
            }
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy