io.quarkus.redis.runtime.datasource.ReactiveJsonCommandsImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-redis-client Show documentation
Show all versions of quarkus-redis-client Show documentation
Connect to Redis in either imperative or reactive style
package io.quarkus.redis.runtime.datasource;
import static io.smallrye.mutiny.helpers.ParameterValidation.nonNull;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import io.quarkus.redis.datasource.ReactiveRedisDataSource;
import io.quarkus.redis.datasource.json.JsonSetArgs;
import io.quarkus.redis.datasource.json.ReactiveJsonCommands;
import io.smallrye.mutiny.Uni;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.mutiny.core.buffer.Buffer;
import io.vertx.mutiny.redis.client.Response;
import io.vertx.redis.client.ResponseType;
public class ReactiveJsonCommandsImpl extends AbstractJsonCommands implements ReactiveJsonCommands {
private final ReactiveRedisDataSource reactive;
public ReactiveJsonCommandsImpl(ReactiveRedisDataSourceImpl redis, Type k) {
super(redis, k);
this.reactive = redis;
}
@Override
public ReactiveRedisDataSource getDataSource() {
return reactive;
}
@Override
public Uni jsonSet(K key, String path, T value) {
return _jsonSet(key, path, value)
.replaceWithVoid();
}
@Override
public Uni jsonSet(K key, String path, JsonObject json) {
return _jsonSet(key, path, json)
.replaceWithVoid();
}
@Override
public Uni jsonSet(K key, String path, JsonObject json, JsonSetArgs args) {
return _jsonSet(key, path, json, args)
.replaceWithVoid();
}
@Override
public Uni jsonSet(K key, String path, JsonArray json) {
return _jsonSet(key, path, json)
.replaceWithVoid();
}
@Override
public Uni jsonSet(K key, String path, JsonArray json, JsonSetArgs args) {
return _jsonSet(key, path, json, args)
.replaceWithVoid();
}
@Override
public Uni jsonSet(K key, String path, T value, JsonSetArgs args) {
return _jsonSet(key, path, value, args)
.replaceWithVoid();
}
@Override
public Uni jsonGet(K key, Class clazz) {
nonNull(clazz, "clazz");
return _jsonGet(key)
.map(r -> {
var m = getJsonObject(r);
if (m != null) {
return m.mapTo(clazz);
}
return null;
});
}
@Override
public Uni jsonGetObject(K key) {
return _jsonGet(key)
.map(ReactiveJsonCommandsImpl::getJsonObject);
}
@Override
public Uni jsonGetArray(K key) {
return _jsonGet(key)
.map(ReactiveJsonCommandsImpl::getJsonArray);
}
static JsonArray getJsonArray(Response r) {
if (r == null || r.toString().equalsIgnoreCase("null")) { // JSON null
return null;
}
// With Redis 7.2 the response is a BULK (String) but using a nested array.
Buffer buffer = r.toBuffer();
JsonArray array = buffer.toJsonArray();
if (array.size() == 1 && array.getString(0).startsWith("[")) {
return array.getJsonArray(0);
}
return array;
}
static JsonObject getJsonObject(Response r) {
if (r == null || r.toString().equalsIgnoreCase("null")) { // JSON null
return null;
}
// With Redis 7.2 the response is a BULK (String) but using a nested array.
Buffer buffer = r.toBuffer();
if (buffer.toJson() instanceof JsonArray) {
var array = buffer.toJsonArray();
if (array.size() == 0) {
return null;
}
return array.getJsonObject(0);
}
return r.toBuffer().toJsonObject();
}
static JsonArray getJsonArrayFromJsonGet(Response r) {
if (r == null || r.toString().equalsIgnoreCase("null")) { // JSON null
return null;
}
// With Redis 7.2 the response is a BULK (String) but using a nested array.
Buffer buffer = r.toBuffer();
if (buffer.toJson() instanceof JsonArray) {
var array = buffer.toJsonArray();
if (array.size() == 0) {
return new JsonArray();
}
return array;
}
return buffer.toJsonArray();
}
@Override
public Uni jsonGet(K key, String path) {
return _jsonGet(key, path)
.map(ReactiveJsonCommandsImpl::getJsonArrayFromJsonGet);
}
@Override
public Uni jsonGet(K key, String... paths) {
return _jsonGet(key, paths)
.map(ReactiveJsonCommandsImpl::getJsonObject);
}
@Override
public Uni> jsonArrAppend(K key, String path, T... values) {
return _jsonArrAppend(key, path, values)
.map(ReactiveJsonCommandsImpl::decodeAsListOfInteger);
}
@Override
public Uni> jsonArrIndex(K key, String path, T value, int start, int end) {
return _jsonArrIndex(key, path, value, start, end)
.map(ReactiveJsonCommandsImpl::decodeAsListOfInteger);
}
static List decodeAsListOfInteger(Response r) {
List list = new ArrayList<>();
if (r.type() == ResponseType.MULTI) {
for (Response response : r) {
list.add(response == null ? null : response.toInteger());
}
} else {
list.add(r.toInteger());
}
return list;
}
@Override
public Uni> jsonArrInsert(K key, String path, int index, T... values) {
return _jsonArrInsert(key, path, index, values)
.map(ReactiveJsonCommandsImpl::decodeAsListOfInteger);
}
@Override
public Uni> jsonArrLen(K key, String path) {
return _jsonArrLen(key, path)
.map(ReactiveJsonCommandsImpl::decodeAsListOfInteger);
}
@Override
public Uni> jsonArrPop(K key, Class clazz, String path, int index) {
nonNull(clazz, "clazz");
return _jsonArrPop(key, path, index)
.map(r -> decodeArrPopResponse(clazz, r));
}
static List decodeArrPopResponse(Class clazz, Response r) {
List list = new ArrayList<>();
if (r == null) {
list.add(null);
return list;
}
if (r.type() == ResponseType.MULTI) {
for (Response response : r) {
list.add(response == null ? null : Json.decodeValue(response.toString(), clazz));
}
} else {
list.add(Json.decodeValue(r.toString(), clazz));
}
return list;
}
@Override
public Uni> jsonArrTrim(K key, String path, int start, int stop) {
return _jsonArrTrim(key, path, start, stop)
.map(ReactiveJsonCommandsImpl::decodeAsListOfInteger);
}
@Override
public Uni jsonClear(K key, String path) {
return _jsonClear(key, path)
.map(Response::toInteger);
}
@Override
public Uni jsonDel(K key, String path) {
return _jsonDel(key, path)
.map(Response::toInteger);
}
@Override
public Uni> jsonMget(String path, K... keys) {
return _jsonMget(path, keys)
.map(ReactiveJsonCommandsImpl::decodeMGetResponse);
}
static List decodeMGetResponse(Response r) {
List list = new ArrayList<>();
if (r.type() == ResponseType.MULTI) {
for (Response response : r) {
list.add(response == null ? null : response.toBuffer().toJsonArray());
}
} else {
list.add(r.toBuffer().toJsonArray());
}
return list;
}
@Override
public Uni jsonNumincrby(K key, String path, double value) {
return _jsonNumincrby(key, path, value)
.replaceWithVoid();
}
@Override
public Uni>> jsonObjKeys(K key, String path) {
return _jsonObjKeys(key, path)
.map(ReactiveJsonCommandsImpl::decodeObjKeysResponse);
}
static List> decodeObjKeysResponse(Response r) {
List> list = new ArrayList<>();
if (r.type() == ResponseType.MULTI) {
List sub = new ArrayList<>();
for (Response item : r) {
if (item == null) {
list.add(null);
} else {
if (item.type() == ResponseType.MULTI) {
for (Response nested : item) {
sub.add(nested == null ? null : nested.toString());
}
} else { // BULK
sub.add(item.toString());
}
}
}
list.add(sub);
} else {
list.add(null);
}
return list;
}
@Override
public Uni> jsonObjLen(K key, String path) {
return _jsonObjLen(key, path)
.map(ReactiveJsonCommandsImpl::decodeAsListOfInteger);
}
@Override
public Uni> jsonStrAppend(K key, String path, String value) {
return _jsonStrAppend(key, path, value)
.map(ReactiveJsonCommandsImpl::decodeAsListOfInteger);
}
@Override
public Uni> jsonStrLen(K key, String path) {
return _jsonStrLen(key, path)
.map(ReactiveJsonCommandsImpl::decodeAsListOfInteger);
}
@Override
public Uni> jsonToggle(K key, String path) {
return _jsonToggle(key, path)
.map(ReactiveJsonCommandsImpl::decodeToggleResponse);
}
static List decodeToggleResponse(Response r) {
List list = new ArrayList<>();
if (r.type() == ResponseType.MULTI) {
for (Response response : r) {
list.add(response == null ? null : response.toBoolean());
}
} else {
list.add(r.toBoolean());
}
return list;
}
@Override
public Uni> jsonType(K key, String path) {
return _jsonType(key, path)
.map(ReactiveJsonCommandsImpl::decodeTypeResponse);
}
static List decodeTypeResponse(Response r) {
List list = new ArrayList<>();
if (r.type() == ResponseType.MULTI) {
for (Response response : r) {
if (response == null) {
list.add(null);
} else if (response.type() == ResponseType.MULTI) {
// Redis 7.2 behavior
for (Response nested : response) {
list.add(nested == null ? null : nested.toString());
}
} else {
list.add(response.toString());
}
}
} else {
list.add(r.toString());
}
return list;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy