Feature - RRingBuffer.setCapacity() method added #3056

pull/3060/head
Nikita Koksharov 5 years ago
parent eb663c6f2f
commit 84e28d0b3f

@ -62,7 +62,21 @@ public class RedissonRingBuffer<V> extends RedissonQueue<V> implements RRingBuff
public boolean trySetCapacity(int capacity) {
return get(trySetCapacityAsync(capacity));
}
@Override
public RFuture<Void> setCapacityAsync(int capacity) {
return commandExecutor.evalWriteAsync(getName(), 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(getName(), settingsName), capacity);
}
@Override
public void setCapacity(int capacity) {
get(setCapacityAsync(capacity));
}
@Override
public RFuture<Boolean> addAsync(V e) {
return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,

@ -29,14 +29,22 @@ package org.redisson.api;
public interface RRingBuffer<V> extends RQueue<V>, RRingBufferAsync<V> {
/**
* Sets queue capacity only if it is not set before.
* Sets capacity of the queue only if it wasn't set before.
*
* @param capacity - queue capacity
* @return <code>true</code> if capacity set successfully
* <code>false</code> if capacity already set
*/
boolean trySetCapacity(int capacity);
/**
* Sets capacity of the queue and overrides current value.
* Trims queue if previous capacity value was greater than new.
*
* @param capacity - queue capacity
*/
void setCapacity(int capacity);
/**
* Returns remaining capacity of this queue
*

@ -29,14 +29,22 @@ package org.redisson.api;
public interface RRingBufferAsync<V> extends RQueueAsync<V> {
/**
* Sets queue capacity only if it is not set before.
* Sets capacity of the queue only if it wasn't set before.
*
* @param capacity - queue capacity
* @return <code>true</code> if capacity set successfully
* <code>false</code> if capacity already set
*/
RFuture<Boolean> trySetCapacityAsync(int capacity);
/**
* Sets capacity of the queue and overrides current value.
* Trims queue if previous capacity value was greater than new.
*
* @param capacity - queue capacity
*/
RFuture<Void> setCapacityAsync(int capacity);
/**
* Returns remaining capacity of this queue
*

@ -38,7 +38,15 @@ public interface RRingBufferReactive<V> extends RQueueReactive<V> {
* <code>false</code> if capacity already set
*/
Mono<Boolean> trySetCapacity(int capacity);
/**
* Sets capacity of the queue and overrides current value.
* Trims queue if previous capacity value was greater than new.
*
* @param capacity - queue capacity
*/
Mono<Void> setCapacity(int capacity);
/**
* Returns remaining capacity of this queue
*

@ -15,6 +15,7 @@
*/
package org.redisson.api;
import io.reactivex.Completable;
import io.reactivex.Single;
/**
@ -38,7 +39,15 @@ public interface RRingBufferRx<V> extends RQueueRx<V> {
* <code>false</code> if capacity already set
*/
Single<Boolean> trySetCapacity(int capacity);
/**
* Sets capacity of the queue and overrides current value.
* Trims queue if previous capacity value was greater than new.
*
* @param capacity - queue capacity
*/
Completable setCapacity(int capacity);
/**
* Returns remaining capacity of this queue
*

@ -10,6 +10,27 @@ import org.redisson.api.RRingBuffer;
public class RedissonRingBufferTest extends BaseTest {
@Test
public void testSetCapacity() {
RRingBuffer<Integer> buffer = redisson.getRingBuffer("test");
buffer.trySetCapacity(5);
for (int i = 0; i < 10; i++) {
buffer.add(i);
}
assertThat(buffer).containsExactly(5, 6, 7, 8, 9);
buffer.setCapacity(3);
assertThat(buffer).containsExactly(7, 8, 9);
RRingBuffer<Integer> buffer2 = redisson.getRingBuffer("test2");
buffer2.setCapacity(3);
for (int i = 0; i < 10; i++) {
buffer2.add(i);
}
assertThat(buffer2).containsExactly(7, 8, 9);
}
@Test
public void testAdd() {
RRingBuffer<Integer> buffer = redisson.getRingBuffer("test");

Loading…
Cancel
Save