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

org.redisson.rx.RedissonSetRx Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
/**
 * 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.rx;

import org.reactivestreams.Publisher;
import org.redisson.RedissonSet;
import org.redisson.api.RFuture;
import org.redisson.api.RLockRx;
import org.redisson.api.RPermitExpirableSemaphoreRx;
import org.redisson.api.RReadWriteLockRx;
import org.redisson.api.RSemaphoreRx;
import org.redisson.api.RSet;
import org.redisson.api.RedissonRxClient;
import org.redisson.client.RedisClient;
import org.redisson.client.protocol.decoder.ListScanResult;

import io.reactivex.Flowable;

/**
 * Distributed and concurrent implementation of {@link java.util.Set}
 *
 * @author Nikita Koksharov
 *
 * @param  value
 */
public class RedissonSetRx {

    private final RSet instance;
    private final RedissonRxClient redisson;

    public RedissonSetRx(RSet instance, RedissonRxClient redisson) {
        this.instance = instance;
        this.redisson = redisson;
    }
    
    public Flowable addAll(Publisher c) {
        return new PublisherAdder() {
            @Override
            public RFuture add(Object e) {
                return instance.addAsync((V)e);
            }
        }.addAll(c);
    }

    public Flowable iterator(int count) {
        return iterator(null, count);
    }
    
    public Flowable iterator(String pattern) {
        return iterator(pattern, 10);
    }

    public Flowable iterator(final String pattern, final int count) {
        return new SetRxIterator() {
            @Override
            protected RFuture> scanIterator(RedisClient client, long nextIterPos) {
                return ((RedissonSet)instance).scanIteratorAsync(instance.getName(), client, nextIterPos, pattern, count);
            }
        }.create();
    }

    public Publisher iterator() {
        return iterator(null, 10);
    }
    
    public RPermitExpirableSemaphoreRx getPermitExpirableSemaphore(V value) {
        String name = ((RedissonSet)instance).getLockName(value, "permitexpirablesemaphore");
        return redisson.getPermitExpirableSemaphore(name);
    }

    public RSemaphoreRx getSemaphore(V value) {
        String name = ((RedissonSet)instance).getLockName(value, "semaphore");
        return redisson.getSemaphore(name);
    }
    
    public RLockRx getFairLock(V value) {
        String name = ((RedissonSet)instance).getLockName(value, "fairlock");
        return redisson.getFairLock(name);
    }
    
    public RReadWriteLockRx getReadWriteLock(V value) {
        String name = ((RedissonSet)instance).getLockName(value, "rw_lock");
        return redisson.getReadWriteLock(name);
    }
    
    public RLockRx getLock(V value) {
        String name = ((RedissonSet)instance).getLockName(value, "lock");
        return redisson.getLock(name);
    }
    
}