com.github.lontime.shaded.org.redisson.RedissonRingBuffer Maven / Gradle / Ivy
The 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 com.github.lontime.shaded.org.redisson;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import com.github.lontime.shaded.org.redisson.api.RFuture;
import com.github.lontime.shaded.org.redisson.api.RRingBuffer;
import com.github.lontime.shaded.org.redisson.api.RedissonClient;
import com.github.lontime.shaded.org.redisson.client.codec.Codec;
import com.github.lontime.shaded.org.redisson.client.codec.LongCodec;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisCommands;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisStrictCommand;
import com.github.lontime.shaded.org.redisson.client.protocol.convertor.IntegerReplayConvertor;
import com.github.lontime.shaded.org.redisson.command.CommandAsyncExecutor;
import com.github.lontime.shaded.org.redisson.misc.CompletableFutureWrapper;
/**
*
* @author Nikita Koksharov
*
* @param value type
*/
public class RedissonRingBuffer extends RedissonQueue implements RRingBuffer {
private static final RedisStrictCommand GET_INTEGER = new RedisStrictCommand("GET", new IntegerReplayConvertor(0));
private final String settingsName;
public RedissonRingBuffer(CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) {
super(commandExecutor, name, redisson);
settingsName = prefixName("redisson_rb", getRawName());
}
public RedissonRingBuffer(Codec codec, CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) {
super(codec, commandExecutor, name, redisson);
settingsName = prefixName("redisson_rb", getRawName());
}
@Override
public RFuture trySetCapacityAsync(int capacity) {
return commandExecutor.writeAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.SETNX, settingsName, capacity);
}
@Override
public boolean trySetCapacity(int capacity) {
return get(trySetCapacityAsync(capacity));
}
@Override
public RFuture setCapacityAsync(int capacity) {
return commandExecutor.evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_VOID,
"redis.call('set', KEYS[2], ARGV[1]); " +
"local len = redis.call('llen', KEYS[1]); " +
"redis.call('ltrim', KEYS[1], len - tonumber(ARGV[1]), len - 1); ",
Arrays.asList(getRawName(), settingsName), capacity);
}
@Override
public void setCapacity(int capacity) {
get(setCapacityAsync(capacity));
}
@Override
public RFuture addAsync(V e) {
return commandExecutor.evalWriteNoRetryAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local limit = redis.call('get', KEYS[2]); "
+ "assert(limit ~= false, 'RingBuffer capacity is not defined'); "
+ "local size = redis.call('rpush', KEYS[1], ARGV[1]); "
+ "if size > tonumber(limit) then "
+ "redis.call('lpop', KEYS[1]); "
+ "end; "
+ "return 1; ",
Arrays.asList(getRawName(), settingsName), encode(e));
}
@Override
public RFuture addAllAsync(Collection extends V> c) {
if (c.isEmpty()) {
return new CompletableFutureWrapper<>(false);
}
List