Feature - takeFirstElements and takeLastElements stream methods added to RScoredSortedSetReactive object

pull/1821/head
Nikita Koksharov 6 years ago
parent 4774e48c4e
commit cb59a7b2cf

@ -113,16 +113,18 @@ public interface RBlockingDequeReactive<V> extends RDequeReactive<V>, RBlockingQ
Mono<V> takeFirst(); Mono<V> takeFirst();
/** /**
* Retrieves and removes stream of elements from the head of this queue. Waits for an element become available. * Retrieves and removes continues stream of elements from the head of this queue.
* Waits for next element become available.
* *
* @return the head element of this queue * @return stream of head elements
*/ */
Flux<V> takeFirstElements(); Flux<V> takeFirstElements();
/** /**
* Retrieves and removes stream of elements from the tail of this queue. Waits for an element become available. * Retrieves and removes continues stream of elements from the tail of this queue.
* Waits for next element become available.
* *
* @return the head element of this queue * @return stream of tail elements
*/ */
Flux<V> takeLastElements(); Flux<V> takeLastElements();

@ -156,9 +156,10 @@ public interface RBlockingQueueReactive<V> extends RQueueReactive<V> {
Mono<Void> put(V e); Mono<Void> put(V e);
/** /**
* Retrieves and removes stream of elements from the head of this queue. * Retrieves and removes continues stream of elements from the head of this queue.
* Waits for next element become available.
* *
* @return stream of messages * @return stream of elements
*/ */
Flux<V> takeElements(); Flux<V> takeElements();

@ -430,5 +430,21 @@ public interface RScoredSortedSetReactive<V> extends RExpirableReactive, RSortab
* @return the tail element * @return the tail element
*/ */
Mono<V> takeLast(); Mono<V> takeLast();
/**
* Retrieves and removes continues stream of elements from the head of this queue.
* Waits for next element become available.
*
* @return stream of head elements
*/
Flux<V> takeFirstElements();
/**
* Retrieves and removes continues stream of elements from the tail of this queue.
* Waits for next element become available.
*
* @return stream of tail elements
*/
Flux<V> takeLastElements();
} }

@ -0,0 +1,77 @@
/**
* Copyright 2018 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.reactive;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.redisson.api.RFuture;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
/**
*
* @author Nikita Koksharov
*
*/
public class ElementsStream {
private static <V> void take(final Callable<RFuture<V>> factory, final FluxSink<V> emitter, final AtomicLong counter, final AtomicReference<RFuture<V>> futureRef) {
RFuture<V> future;
try {
future = factory.call();
} catch (Exception e) {
emitter.error(e);
return;
}
futureRef.set(future);
future.addListener(new FutureListener<V>() {
@Override
public void operationComplete(Future<V> future) throws Exception {
if (!future.isSuccess()) {
emitter.error(future.cause());
return;
}
emitter.next(future.getNow());
if (counter.decrementAndGet() == 0) {
emitter.complete();
}
take(factory, emitter, counter, futureRef);
}
});
}
public static <V> Flux<V> takeElements(Callable<RFuture<V>> callable) {
return Flux.<V>create(emitter -> {
emitter.onRequest(n -> {
AtomicLong counter = new AtomicLong(n);
AtomicReference<RFuture<V>> futureRef = new AtomicReference<RFuture<V>>();
take(callable, emitter, counter, futureRef);
emitter.onDispose(() -> {
futureRef.get().cancel(true);
});
});
});
}
}

@ -38,7 +38,7 @@ public class RedissonBlockingDequeReactive<V> extends RedissonBlockingQueueReact
} }
public Flux<V> takeFirstElements() { public Flux<V> takeFirstElements() {
return takeElements(new Callable<RFuture<V>>() { return ElementsStream.takeElements(new Callable<RFuture<V>>() {
@Override @Override
public RFuture<V> call() throws Exception { public RFuture<V> call() throws Exception {
return queue.takeFirstAsync(); return queue.takeFirstAsync();
@ -47,7 +47,7 @@ public class RedissonBlockingDequeReactive<V> extends RedissonBlockingQueueReact
} }
public Flux<V> takeLastElements() { public Flux<V> takeLastElements() {
return takeElements(new Callable<RFuture<V>>() { return ElementsStream.takeElements(new Callable<RFuture<V>>() {
@Override @Override
public RFuture<V> call() throws Exception { public RFuture<V> call() throws Exception {
return queue.takeLastAsync(); return queue.takeLastAsync();

@ -16,17 +16,12 @@
package org.redisson.reactive; package org.redisson.reactive;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.redisson.api.RBlockingQueue; import org.redisson.api.RBlockingQueue;
import org.redisson.api.RFuture; import org.redisson.api.RFuture;
import org.redisson.api.RListAsync; import org.redisson.api.RListAsync;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
/** /**
* *
@ -43,35 +38,8 @@ public class RedissonBlockingQueueReactive<V> extends RedissonListReactive<V> {
this.queue = queue; this.queue = queue;
} }
private void take(final Callable<RFuture<V>> factory, final FluxSink<V> emitter, final AtomicLong counter, final AtomicReference<RFuture<V>> futureRef) {
RFuture<V> future;
try {
future = factory.call();
} catch (Exception e) {
emitter.error(e);
return;
}
futureRef.set(future);
future.addListener(new FutureListener<V>() {
@Override
public void operationComplete(Future<V> future) throws Exception {
if (!future.isSuccess()) {
emitter.error(future.cause());
return;
}
emitter.next(future.getNow());
if (counter.decrementAndGet() == 0) {
emitter.complete();
}
take(factory, emitter, counter, futureRef);
}
});
}
public Flux<V> takeElements() { public Flux<V> takeElements() {
return takeElements(new Callable<RFuture<V>>() { return ElementsStream.takeElements(new Callable<RFuture<V>>() {
@Override @Override
public RFuture<V> call() throws Exception { public RFuture<V> call() throws Exception {
return queue.takeAsync(); return queue.takeAsync();
@ -79,17 +47,4 @@ public class RedissonBlockingQueueReactive<V> extends RedissonListReactive<V> {
}); });
} }
protected final Flux<V> takeElements(Callable<RFuture<V>> callable) {
return Flux.<V>create(emitter -> {
emitter.onRequest(n -> {
AtomicLong counter = new AtomicLong(n);
AtomicReference<RFuture<V>> futureRef = new AtomicReference<RFuture<V>>();
take(callable, emitter, counter, futureRef);
emitter.onDispose(() -> {
futureRef.get().cancel(true);
});
});
});
}
} }

@ -15,7 +15,8 @@
*/ */
package org.redisson.reactive; package org.redisson.reactive;
import org.reactivestreams.Publisher; import java.util.concurrent.Callable;
import org.redisson.RedissonScoredSortedSet; import org.redisson.RedissonScoredSortedSet;
import org.redisson.api.RFuture; import org.redisson.api.RFuture;
import org.redisson.api.RScoredSortedSetAsync; import org.redisson.api.RScoredSortedSetAsync;
@ -50,6 +51,24 @@ public class RedissonScoredSortedSetReactive<V> {
private RedissonScoredSortedSetReactive(Codec codec, CommandReactiveExecutor commandExecutor, String name, RScoredSortedSetAsync<V> instance) { private RedissonScoredSortedSetReactive(Codec codec, CommandReactiveExecutor commandExecutor, String name, RScoredSortedSetAsync<V> instance) {
this.instance = instance; this.instance = instance;
} }
public Flux<V> takeFirstElements() {
return ElementsStream.takeElements(new Callable<RFuture<V>>() {
@Override
public RFuture<V> call() throws Exception {
return instance.takeFirstAsync();
}
});
}
public Flux<V> takeLastElements() {
return ElementsStream.takeElements(new Callable<RFuture<V>>() {
@Override
public RFuture<V> call() throws Exception {
return instance.takeLastAsync();
}
});
}
private Flux<V> scanIteratorReactive(final String pattern, final int count) { private Flux<V> scanIteratorReactive(final String pattern, final int count) {
return Flux.create(new SetReactiveIterator<V>() { return Flux.create(new SetReactiveIterator<V>() {

Loading…
Cancel
Save