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

io.quarkus.redis.runtime.datasource.ReactiveCountMinCommandsImpl Maven / Gradle / Ivy

There is a newer version: 3.17.5
Show newest version
package io.quarkus.redis.runtime.datasource;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.quarkus.redis.datasource.ReactiveRedisCommands;
import io.quarkus.redis.datasource.ReactiveRedisDataSource;
import io.quarkus.redis.datasource.countmin.ReactiveCountMinCommands;
import io.smallrye.mutiny.Uni;
import io.vertx.mutiny.redis.client.Response;

public class ReactiveCountMinCommandsImpl extends AbstractCountMinCommands
        implements ReactiveCountMinCommands, ReactiveRedisCommands {

    private final ReactiveRedisDataSource reactive;

    public ReactiveCountMinCommandsImpl(ReactiveRedisDataSourceImpl redis, Type k, Type v) {
        super(redis, k, v);
        this.reactive = redis;
    }

    @Override
    public ReactiveRedisDataSource getDataSource() {
        return reactive;
    }

    @Override
    public Uni cmsIncrBy(K key, V value, long increment) {
        return super._cmsIncrBy(key, value, increment)
                .map(r -> r.get(0).toLong());
    }

    @Override
    public Uni> cmsIncrBy(K key, Map couples) {
        return super._cmsIncrBy(key, couples)
                .map(r -> decodeAsMapVL(couples, r));
    }

    Map decodeAsMapVL(Map couples, Response r) {
        Map result = new LinkedHashMap<>();
        var iterator = couples.keySet().iterator();
        for (Response response : r) {
            result.put(iterator.next(), response.toLong());
        }
        return result;
    }

    @Override
    public Uni cmsInitByDim(K key, long width, long depth) {
        return super._cmsInitByDim(key, width, depth)
                .replaceWithVoid();
    }

    @Override
    public Uni cmsInitByProb(K key, double error, double probability) {
        return super._cmsInitByProb(key, error, probability)
                .replaceWithVoid();
    }

    @Override
    public Uni cmsQuery(K key, V item) {
        return super._cmsQuery(key, item)
                .map(r -> decodeAListOfLongs(r).get(0));
    }

    @Override
    public Uni> cmsQuery(K key, V... items) {
        return super._cmsQuery(key, items)
                .map(this::decodeAListOfLongs);
    }

    List decodeAListOfLongs(Response r) {
        List results = new ArrayList<>();
        for (Response response : r) {
            results.add(response.toLong());
        }
        return results;
    }

    @Override
    public Uni cmsMerge(K dest, List src, List weight) {
        return super._cmsMerge(dest, src, weight)
                .replaceWithVoid();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy