
org.redisson.reactive.RedissonListReactive Maven / Gradle / Ivy
/**
* Copyright 2016 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 static org.redisson.client.protocol.RedisCommands.LINDEX;
import static org.redisson.client.protocol.RedisCommands.LREM_SINGLE;
import static org.redisson.client.protocol.RedisCommands.RPUSH;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.LongConsumer;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.redisson.RedissonList;
import org.redisson.api.RFuture;
import org.redisson.api.RListReactive;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.convertor.LongReplayConvertor;
import org.redisson.command.CommandReactiveExecutor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Mono;
/**
* Distributed and concurrent implementation of {@link java.util.List}
*
* @author Nikita Koksharov
*
* @param the type of elements held in this collection
*/
public class RedissonListReactive extends RedissonExpirableReactive implements RListReactive {
private final RedissonList instance;
public RedissonListReactive(CommandReactiveExecutor commandExecutor, String name) {
super(commandExecutor, name);
instance = new RedissonList(commandExecutor, name, null);
}
public RedissonListReactive(Codec codec, CommandReactiveExecutor commandExecutor, String name) {
super(codec, commandExecutor, name);
instance = new RedissonList(codec, commandExecutor, name, null);
}
@Override
public Publisher size() {
return commandExecutor.readReactive(getName(), codec, RedisCommands.LLEN_INT, getName());
}
@Override
public Publisher descendingIterator() {
return iterator(-1, false);
}
@Override
public Publisher iterator() {
return iterator(0, true);
}
@Override
public Publisher descendingIterator(int startIndex) {
return iterator(startIndex, false);
}
@Override
public Publisher iterator(int startIndex) {
return iterator(startIndex, true);
}
private Publisher iterator(final int startIndex, final boolean forward) {
return Flux.create(new Consumer>() {
@Override
public void accept(FluxSink emitter) {
emitter.onRequest(new LongConsumer() {
int currentIndex = startIndex;
@Override
public void accept(long value) {
onRequest(forward, emitter, value);
}
protected void onRequest(final boolean forward, FluxSink emitter, long n) {
get(currentIndex).subscribe(new Subscriber() {
V currValue;
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(V value) {
currValue = value;
emitter.next(value);
if (forward) {
currentIndex++;
} else {
currentIndex--;
}
}
@Override
public void onError(Throwable error) {
emitter.error(error);
}
@Override
public void onComplete() {
if (currValue == null) {
emitter.complete();
return;
}
if (n-1 == 0) {
return;
}
onRequest(forward, emitter, n-1);
}
});
}
});
}
});
}
@Override
public Publisher add(V e) {
return commandExecutor.writeReactive(getName(), codec, RPUSH, getName(), encode(e));
}
@Override
public Publisher remove(final Object o) {
return reactive(new Supplier>() {
@Override
public RFuture get() {
return instance.removeAsync(o);
}
});
}
protected Publisher remove(Object o, int count) {
return commandExecutor.writeReactive(getName(), codec, LREM_SINGLE, getName(), count, encode(o));
}
@Override
public Publisher containsAll(final Collection> c) {
return reactive(new Supplier>() {
@Override
public RFuture get() {
return instance.containsAllAsync(c);
}
});
}
@Override
public Publisher addAll(Publisher extends V> c) {
return new PublisherAdder(this) {
@Override
public Integer sum(Integer first, Integer second) {
return second;
}
}.addAll(c);
}
@Override
public Publisher addAll(Collection extends V> c) {
if (c.isEmpty()) {
return size();
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy