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

io.quarkus.redis.runtime.datasource.ReactiveTopKCommandsImpl 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.Iterator;
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.topk.ReactiveTopKCommands;
import io.smallrye.mutiny.Uni;
import io.vertx.mutiny.redis.client.Response;

public class ReactiveTopKCommandsImpl extends AbstractTopKCommands
        implements ReactiveTopKCommands, ReactiveRedisCommands {

    private final ReactiveRedisDataSource reactive;
    final Type typeOfValue;

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

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

    @Override
    public Uni topkAdd(K key, V item) {
        return super._topkAdd(key, item)
                .map(r -> marshaller. decodeAsList(r, typeOfValue).get(0));
    }

    @Override
    public Uni> topkAdd(K key, V... items) {
        return super._topkAdd(key, items)
                .map(r -> marshaller.decodeAsList(r, typeOfValue));
    }

    @Override
    public Uni topkIncrBy(K key, V item, int increment) {
        return super._topkIncrBy(key, item, increment)
                .map(r -> marshaller. decodeAsList(r, typeOfValue).get(0));
    }

    @Override
    public Uni> topkIncrBy(K key, Map couples) {
        return super._topkIncrBy(key, couples)
                .map(r -> decodeAsMapVV(couples, r));
    }

    Map decodeAsMapVV(Map couples, Response r) {
        Map map = new LinkedHashMap<>();
        Iterator iterator = couples.keySet().iterator();
        for (Response response : r) {
            map.put(iterator.next(), marshaller.decode(typeOfValue, response));
        }
        return map;
    }

    @Override
    public Uni> topkList(K key) {
        return super._topkList(key)
                .map(r -> marshaller.decodeAsList(r, typeOfValue));
    }

    @Override
    public Uni> topkListWithCount(K key) {
        return super._topkListWithCount(key)
                .map(this::decodeAsMapVInt);
    }

    Map decodeAsMapVInt(Response r) {
        Map map = new LinkedHashMap<>();
        V current = null;
        for (Response response : r) {
            if (current == null) {
                current = decodeV(response);
            } else {
                map.put(current, response.toInteger());
                current = null;
            }
        }
        return map;
    }

    @Override
    public Uni topkQuery(K key, V item) {
        return super._topkQuery(key, item)
                .map(r -> marshaller.decodeAsList(r, Response::toBoolean).get(0));
    }

    @Override
    public Uni> topkQuery(K key, V... items) {
        return super._topkQuery(key, items)
                .map(r -> marshaller.decodeAsList(r, Response::toBoolean));
    }

    @Override
    public Uni topkReserve(K key, int topk) {
        return super._topkReserve(key, topk)
                .replaceWithVoid();
    }

    @Override
    public Uni topkReserve(K key, int topk, int width, int depth, double decay) {
        return super._topkReserve(key, topk, width, depth, decay)
                .replaceWithVoid();
    }

    V decodeV(Response r) {
        return marshaller.decode(typeOfValue, r);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy