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

org.redisson.reactive.RedissonSetReactive Maven / Gradle / Ivy

There is a newer version: 3.9.1
Show newest version
/**
 * 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 java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import org.reactivestreams.Publisher;
import org.redisson.RedissonSet;
import org.redisson.api.RFuture;
import org.redisson.api.RSetReactive;
import org.redisson.client.RedisClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.ScanCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.decoder.ListScanResult;
import org.redisson.client.protocol.decoder.ScanObjectEntry;
import org.redisson.command.CommandReactiveExecutor;

import reactor.core.publisher.Flux;

/**
 * Distributed and concurrent implementation of {@link java.util.Set}
 *
 * @author Nikita Koksharov
 *
 * @param  value
 */
public class RedissonSetReactive extends RedissonExpirableReactive implements RSetReactive {

    private final RedissonSet instance;

    public RedissonSetReactive(CommandReactiveExecutor commandExecutor, String name) {
        super(commandExecutor, name);
        instance = new RedissonSet(commandExecutor.getConnectionManager().getCodec(), commandExecutor, name, null);
    }

    public RedissonSetReactive(Codec codec, CommandReactiveExecutor commandExecutor, String name) {
        super(codec, commandExecutor, name);
        instance = new RedissonSet(codec, commandExecutor, name, null);
    }

    @Override
    public Publisher addAll(Publisher c) {
        return new PublisherAdder(this).addAll(c);
    }

    @Override
    public Publisher size() {
        return commandExecutor.readReactive(getName(), codec, RedisCommands.SCARD_INT, getName());
    }

    @Override
    public Publisher contains(final Object o) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.containsAsync(o);
            }
        });
    }

    private Publisher> scanIteratorReactive(RedisClient client, long startPos) {
        return commandExecutor.readReactive(client, getName(), new ScanCodec(codec), RedisCommands.SSCAN, getName(), startPos);
    }

    @Override
    public Publisher add(V e) {
        return commandExecutor.writeReactive(getName(), codec, RedisCommands.SADD, getName(), encode(e));
    }

    @Override
    public Publisher removeRandom() {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.removeRandomAsync();
            }
        });
    }

    @Override
    public Publisher random() {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.randomAsync();
            }
        });
    }

    @Override
    public Publisher remove(final Object o) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.removeAsync(o);
            }
        });
    }

    @Override
    public Publisher move(final String destination, final V member) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.moveAsync(destination, member);
            }
        });
    }

    @Override
    public Publisher containsAll(final Collection c) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.containsAllAsync(c);
            }
        });
    }

    @Override
    public Publisher addAll(Collection c) {
        List args = new ArrayList(c.size() + 1);
        args.add(getName());
        encode(args, c);
        return commandExecutor.writeReactive(getName(), codec, RedisCommands.SADD, args.toArray());
    }

    @Override
    public Publisher retainAll(final Collection c) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.retainAllAsync(c);
            }
        });
    }

    @Override
    public Publisher removeAll(final Collection c) {
        return reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.removeAllAsync(c);
            }
        });
    }

    @Override
    public Publisher> readIntersection(final String... names) {
        return reactive(new Supplier>>() {
            @Override
            public RFuture> get() {
                return instance.readIntersectionAsync(names);
            }
        });
    }
    
    @Override
    public Publisher intersection(String... names) {
        List args = new ArrayList(names.length + 1);
        args.add(getName());
        args.addAll(Arrays.asList(names));
        return commandExecutor.writeReactive(getName(), codec, RedisCommands.SINTERSTORE, args.toArray());
    }
    
    @Override
    public Publisher diff(String... names) {
        List args = new ArrayList(names.length + 1);
        args.add(getName());
        args.addAll(Arrays.asList(names));
        return commandExecutor.writeReactive(getName(), codec, RedisCommands.SDIFFSTORE, args.toArray());
    }
    
    @Override
    public Publisher union(String... names) {
        List args = new ArrayList(names.length + 1);
        args.add(getName());
        args.addAll(Arrays.asList(names));
        return commandExecutor.writeReactive(getName(), codec, RedisCommands.SUNIONSTORE, args.toArray());
    }

    @Override
    public Publisher> readUnion(final String... names) {
        return reactive(new Supplier>>() {
            @Override
            public RFuture> get() {
                return instance.readUnionAsync(names);
            }
        });
    }

    @Override
    public Publisher iterator() {
        return Flux.create(new SetReactiveIterator() {
            @Override
            protected Publisher> scanIteratorReactive(RedisClient client, long nextIterPos) {
                return RedissonSetReactive.this.scanIteratorReactive(client, nextIterPos);
            }
        });
    }

}