Skip to content

Commit

Permalink
Implemented all JSON commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tishun committed Aug 9, 2024
1 parent f1d1a6a commit fc5d14b
Show file tree
Hide file tree
Showing 20 changed files with 432 additions and 147 deletions.
6 changes: 3 additions & 3 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ public RedisFuture<List<JsonValue<K, V>>> jsonMGet(JsonPath jsonPath, K... keys)
}

@Override
public RedisFuture<Boolean> jsonMSet(JsonMsetArgs... arguments) {
public RedisFuture<String> jsonMSet(JsonMsetArgs... arguments) {
return dispatch(jsonCommandBuilder.jsonMSet(arguments));
}

Expand All @@ -1528,7 +1528,7 @@ public RedisFuture<List<Number>> jsonNumincrby(K key, JsonPath jsonPath, Number
}

@Override
public RedisFuture<List<List<V>>> jsonObjkeys(K key, JsonPath jsonPath) {
public RedisFuture<List<K>> jsonObjkeys(K key, JsonPath jsonPath) {
return dispatch(jsonCommandBuilder.jsonObjkeys(key, jsonPath));
}

Expand All @@ -1553,7 +1553,7 @@ public RedisFuture<List<Long>> jsonStrlen(K key, JsonPath jsonPath) {
}

@Override
public RedisFuture<List<Boolean>> jsonToggle(K key, JsonPath jsonPath) {
public RedisFuture<List<Long>> jsonToggle(K key, JsonPath jsonPath) {
return dispatch(jsonCommandBuilder.jsonToggle(key, jsonPath));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ public Flux<JsonValue<K, V>> jsonMGet(JsonPath jsonPath, K... keys) {
}

@Override
public Mono<Boolean> jsonMSet(JsonMsetArgs... arguments) {
public Mono<String> jsonMSet(JsonMsetArgs... arguments) {
return createMono(() -> jsonCommandBuilder.jsonMSet(arguments));
}

Expand All @@ -1593,7 +1593,7 @@ public Flux<Number> jsonNumincrby(K key, JsonPath jsonPath, Number number) {
}

@Override
public Flux<List<V>> jsonObjkeys(K key, JsonPath jsonPath) {
public Flux<K> jsonObjkeys(K key, JsonPath jsonPath) {
return createDissolvingFlux(() -> jsonCommandBuilder.jsonObjkeys(key, jsonPath));
}

Expand All @@ -1618,7 +1618,7 @@ public Flux<Long> jsonStrlen(K key, JsonPath jsonPath) {
}

@Override
public Flux<Boolean> jsonToggle(K key, JsonPath jsonPath) {
public Flux<Long> jsonToggle(K key, JsonPath jsonPath) {
return createDissolvingFlux(() -> jsonCommandBuilder.jsonToggle(key, jsonPath));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/lettuce/core/RedisAsyncCommandsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class RedisAsyncCommandsImpl<K, V> extends AbstractRedisAsyncCommands<K, V>
implements RedisAsyncCommands<K, V>, RedisClusterAsyncCommands<K, V> {

private final RedisCodec<K,V> codec;
private final RedisCodec<K, V> codec;

/**
* Initialize a new instance.
Expand Down
186 changes: 132 additions & 54 deletions src/main/java/io/lettuce/core/RedisJsonCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@
* All rights reserved.
*
* Licensed under the MIT License.
*
* This file contains contributions from third-party contributors
* 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
*
* https://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 io.lettuce.core;
Expand All @@ -27,29 +14,21 @@
import io.lettuce.core.json.JsonPath;
import io.lettuce.core.json.arguments.JsonRangeArgs;
import io.lettuce.core.json.arguments.JsonSetArgs;
import io.lettuce.core.output.BooleanListOutput;
import io.lettuce.core.output.BooleanOutput;
import io.lettuce.core.output.IntegerListOutput;
import io.lettuce.core.output.IntegerOutput;
import io.lettuce.core.output.JsonValueListOutput;
import io.lettuce.core.output.KeyListOutput;
import io.lettuce.core.output.NumberListOutput;
import io.lettuce.core.output.StatusOutput;
import io.lettuce.core.output.ValueListOutput;
import io.lettuce.core.protocol.BaseRedisCommandBuilder;
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.RedisCommand;

import java.util.List;

import static io.lettuce.core.protocol.CommandType.JSON_ARRAPPEND;
import static io.lettuce.core.protocol.CommandType.JSON_ARRLEN;
import static io.lettuce.core.protocol.CommandType.JSON_ARRPOP;
import static io.lettuce.core.protocol.CommandType.JSON_DEL;
import static io.lettuce.core.protocol.CommandType.JSON_GET;
import static io.lettuce.core.protocol.CommandType.JSON_NUMINCRBY;
import static io.lettuce.core.protocol.CommandType.JSON_SET;
import static io.lettuce.core.protocol.CommandType.JSON_TYPE;
import static io.lettuce.core.protocol.CommandType.*;

/**
* Implementation of the {@link BaseRedisCommandBuilder} handling JSON commands.
Expand All @@ -63,7 +42,7 @@ class RedisJsonCommandBuilder<K, V> extends BaseRedisCommandBuilder<K, V> {
super(codec);
}

Command<K, V, List<Long>> jsonArrappend(K key, JsonPath jsonPath, JsonValue<K, V>[] jsonValues) {
Command<K, V, List<Long>> jsonArrappend(K key, JsonPath jsonPath, JsonValue<K, V>... jsonValues) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);
Expand All @@ -79,7 +58,7 @@ Command<K, V, List<Long>> jsonArrappend(K key, JsonPath jsonPath, JsonValue<K, V
return createCommand(JSON_ARRAPPEND, new IntegerListOutput<>(codec), args);
}

RedisCommand<K, V, List<Long>> jsonArrindex(K key, JsonPath jsonPath, JsonValue<K, V> value, JsonRangeArgs range) {
Command<K, V, List<Long>> jsonArrindex(K key, JsonPath jsonPath, JsonValue<K, V> value, JsonRangeArgs range) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);
Expand All @@ -95,14 +74,28 @@ RedisCommand<K, V, List<Long>> jsonArrindex(K key, JsonPath jsonPath, JsonValue<
range.build(args);
}

return createCommand(JSON_ARRLEN, new IntegerListOutput<>(codec), args);
return createCommand(JSON_ARRINDEX, new IntegerListOutput<>(codec), args);
}

RedisCommand<K, V, List<Long>> jsonArrinsert(K key, JsonPath jsonPath, int index, JsonValue<K, V>[] values) {
return null;
Command<K, V, List<Long>> jsonArrinsert(K key, JsonPath jsonPath, int index, JsonValue<K, V>... values) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
args.add(jsonPath.toString());
}

args.add(index);

for (JsonValue<K, V> value : values) {
args.add(value.asByteBuffer().array());
}

return createCommand(JSON_ARRINSERT, new IntegerListOutput<>(codec), args);
}

RedisCommand<K, V, List<Long>> jsonArrlen(K key, JsonPath jsonPath) {
Command<K, V, List<Long>> jsonArrlen(K key, JsonPath jsonPath) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);
Expand All @@ -113,7 +106,7 @@ RedisCommand<K, V, List<Long>> jsonArrlen(K key, JsonPath jsonPath) {
return createCommand(JSON_ARRLEN, new IntegerListOutput<>(codec), args);
}

RedisCommand<K, V, List<JsonValue<K, V>>> jsonArrpop(K key, JsonPath jsonPath, int index) {
Command<K, V, List<JsonValue<K, V>>> jsonArrpop(K key, JsonPath jsonPath, int index) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);
Expand All @@ -129,15 +122,36 @@ RedisCommand<K, V, List<JsonValue<K, V>>> jsonArrpop(K key, JsonPath jsonPath, i
return createCommand(JSON_ARRPOP, new JsonValueListOutput<>(codec), args);
}

RedisCommand<K, V, List<Long>> jsonArrtrim(K key, JsonPath jsonPath, JsonRangeArgs range) {
return null;
Command<K, V, List<Long>> jsonArrtrim(K key, JsonPath jsonPath, JsonRangeArgs range) {

notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
args.add(jsonPath.toString());
}

if (range != null) {
range.build(args);
}

return createCommand(JSON_ARRTRIM, new IntegerListOutput<>(codec), args);
}

RedisCommand<K, V, Long> jsonClear(K key, JsonPath jsonPath) {
return null;
Command<K, V, Long> jsonClear(K key, JsonPath jsonPath) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
args.add(jsonPath.toString());
}

return createCommand(JSON_CLEAR, new IntegerOutput<>(codec), args);
}

RedisCommand<K, V, List<JsonValue<K, V>>> jsonGet(K key, JsonGetArgs options, JsonPath[] jsonPaths) {
Command<K, V, List<JsonValue<K, V>>> jsonGet(K key, JsonGetArgs options, JsonPath... jsonPaths) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);
Expand All @@ -157,11 +171,22 @@ RedisCommand<K, V, List<JsonValue<K, V>>> jsonGet(K key, JsonGetArgs options, Js
return createCommand(JSON_GET, new JsonValueListOutput<>(codec), args);
}

RedisCommand<K, V, Boolean> jsonMerge(K key, JsonPath jsonPath, JsonValue<K, V> value) {
return null;
Command<K, V, Boolean> jsonMerge(K key, JsonPath jsonPath, JsonValue<K, V> value) {

notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
args.add(jsonPath.toString());
}

args.add(value.asByteBuffer().array());

return createCommand(JSON_MERGE, new BooleanOutput<>(codec), args);
}

RedisCommand<K, V, List<JsonValue<K, V>>> jsonMGet(JsonPath jsonPath, K[] keys) {
Command<K, V, List<JsonValue<K, V>>> jsonMGet(JsonPath jsonPath, K... keys) {
notEmpty(keys);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKeys(keys);
Expand All @@ -170,14 +195,23 @@ RedisCommand<K, V, List<JsonValue<K, V>>> jsonMGet(JsonPath jsonPath, K[] keys)
args.add(jsonPath.toString());
}

return createCommand(JSON_GET, new JsonValueListOutput<>(codec), args);
return createCommand(JSON_MGET, new JsonValueListOutput<>(codec), args);
}

RedisCommand<K, V, Boolean> jsonMSet(JsonMsetArgs[] arguments) {
return null;
Command<K, V, String> jsonMSet(JsonMsetArgs... arguments) {

notEmpty(arguments);

CommandArgs<K, V> args = new CommandArgs<>(codec);

for (JsonMsetArgs argument : arguments) {
argument.build(args);
}

return createCommand(JSON_MSET, new StatusOutput<>(codec), args);
}

RedisCommand<K, V, List<Number>> jsonNumincrby(K key, JsonPath jsonPath, Number number) {
Command<K, V, List<Number>> jsonNumincrby(K key, JsonPath jsonPath, Number number) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);
Expand All @@ -191,15 +225,32 @@ RedisCommand<K, V, List<Number>> jsonNumincrby(K key, JsonPath jsonPath, Number
return createCommand(JSON_NUMINCRBY, new NumberListOutput<>(codec), args);
}

RedisCommand<K, V, List<List<V>>> jsonObjkeys(K key, JsonPath jsonPath) {
return null;
Command<K, V, List<K>> jsonObjkeys(K key, JsonPath jsonPath) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
args.add(jsonPath.toString());
}

return createCommand(JSON_OBJKEYS, new KeyListOutput<>(codec), args);
}

RedisCommand<K, V, List<Long>> jsonObjlen(K key, JsonPath jsonPath) {
return null;
Command<K, V, List<Long>> jsonObjlen(K key, JsonPath jsonPath) {

notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
args.add(jsonPath.toString());
}

return createCommand(JSON_OBJLEN, new IntegerListOutput<>(codec), args);
}

RedisCommand<K, V, String> jsonSet(K key, JsonPath jsonPath, JsonValue<K, V> value, JsonSetArgs options) {
Command<K, V, String> jsonSet(K key, JsonPath jsonPath, JsonValue<K, V> value, JsonSetArgs options) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);
Expand All @@ -217,19 +268,46 @@ RedisCommand<K, V, String> jsonSet(K key, JsonPath jsonPath, JsonValue<K, V> val
return createCommand(JSON_SET, new StatusOutput<>(codec), args);
}

RedisCommand<K, V, List<Long>> jsonStrappend(K key, JsonPath jsonPath, JsonValue<K, V> value) {
return null;
Command<K, V, List<Long>> jsonStrappend(K key, JsonPath jsonPath, JsonValue<K, V> value) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
args.add(jsonPath.toString());
}

args.add(value.asByteBuffer().array());

return createCommand(JSON_STRAPPEND, new IntegerListOutput<>(codec), args);
}

RedisCommand<K, V, List<Long>> jsonStrlen(K key, JsonPath jsonPath) {
return null;
Command<K, V, List<Long>> jsonStrlen(K key, JsonPath jsonPath) {

notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
args.add(jsonPath.toString());
}

return createCommand(JSON_STRLEN, new IntegerListOutput<>(codec), args);
}

RedisCommand<K, V, List<Boolean>> jsonToggle(K key, JsonPath jsonPath) {
return null;
Command<K, V, List<Long>> jsonToggle(K key, JsonPath jsonPath) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);

if (jsonPath != null && !jsonPath.isRootPath()) {
args.add(jsonPath.toString());
}

return createCommand(JSON_TOGGLE, new IntegerListOutput<>(codec), args);
}

RedisCommand<K, V, Long> jsonDel(K key, JsonPath jsonPath) {
Command<K, V, Long> jsonDel(K key, JsonPath jsonPath) {
notNullKey(key);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class RedisReactiveCommandsImpl<K, V> extends AbstractRedisReactiveCommands<K, V>
implements RedisReactiveCommands<K, V>, RedisClusterReactiveCommands<K, V> {

private final RedisCodec<K,V> codec;
private final RedisCodec<K, V> codec;

/**
* Initialize a new instance.
Expand Down
Loading

0 comments on commit fc5d14b

Please sign in to comment.