Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: Add command Zrangestore. (Sorted Set Group) (#202) #1276

Merged
merged 7 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions glide-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ enum RequestType {
ZLexCount = 109;
Append = 110;
SInterStore = 114;
ZRangeStore = 115;
}

message Command {
Expand Down
3 changes: 3 additions & 0 deletions glide-core/src/request_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub enum RequestType {
ZLexCount = 109,
Append = 110,
SInterStore = 114,
ZRangeStore = 115,
}

fn get_two_word_command(first: &str, second: &str) -> Cmd {
Expand Down Expand Up @@ -243,6 +244,7 @@ impl From<::protobuf::EnumOrUnknown<ProtobufRequestType>> for RequestType {
ProtobufRequestType::ZLexCount => RequestType::ZLexCount,
ProtobufRequestType::Append => RequestType::Append,
ProtobufRequestType::SInterStore => RequestType::SInterStore,
ProtobufRequestType::ZRangeStore => RequestType::ZRangeStore,
}
}
}
Expand Down Expand Up @@ -362,6 +364,7 @@ impl RequestType {
RequestType::ZLexCount => Some(cmd("ZLEXCOUNT")),
RequestType::Append => Some(cmd("APPEND")),
RequestType::SInterStore => Some(cmd("SINTERSTORE")),
RequestType::ZRangeStore => Some(cmd("ZRANGESTORE")),
}
}
}
23 changes: 21 additions & 2 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.ZMScore;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMax;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMin;
import static redis_request.RedisRequestOuterClass.RequestType.ZRangeStore;
import static redis_request.RedisRequestOuterClass.RequestType.ZRemRangeByLex;
import static redis_request.RedisRequestOuterClass.RequestType.ZRemRangeByRank;
import static redis_request.RedisRequestOuterClass.RequestType.ZRemRangeByScore;
Expand Down Expand Up @@ -810,6 +811,24 @@ public CompletableFuture<Long> zlexcount(
ZLexCount, new String[] {key, minLex.toArgs(), maxLex.toArgs()}, this::handleLongResponse);
}

@Override
public CompletableFuture<Long> zrangestore(
@NonNull String destination,
@NonNull String source,
@NonNull RangeQuery rangeQuery,
boolean reverse) {
String[] arguments =
RangeOptions.createZRangeStoreArgs(destination, source, rangeQuery, reverse);

return commandManager.submitNewCommand(ZRangeStore, arguments, this::handleLongResponse);
}

@Override
public CompletableFuture<Long> zrangestore(
@NonNull String destination, @NonNull String source, @NonNull RangeQuery rangeQuery) {
return this.zrangestore(destination, source, rangeQuery, false);
}

@Override
public CompletableFuture<String> xadd(@NonNull String key, @NonNull Map<String, String> values) {
return xadd(key, values, StreamAddOptions.builder().build());
Expand Down Expand Up @@ -879,7 +898,7 @@ public CompletableFuture<Long> lpushx(@NonNull String key, @NonNull String[] ele
@Override
public CompletableFuture<String[]> zrange(
@NonNull String key, @NonNull RangeQuery rangeQuery, boolean reverse) {
String[] arguments = RangeOptions.createZrangeArgs(key, rangeQuery, reverse, false);
String[] arguments = RangeOptions.createZRangeArgs(key, rangeQuery, reverse, false);

return commandManager.submitNewCommand(
Zrange,
Expand All @@ -895,7 +914,7 @@ public CompletableFuture<String[]> zrange(@NonNull String key, @NonNull RangeQue
@Override
public CompletableFuture<Map<String, Double>> zrangeWithScores(
@NonNull String key, @NonNull ScoredRangeQuery rangeQuery, boolean reverse) {
String[] arguments = RangeOptions.createZrangeArgs(key, rangeQuery, reverse, true);
String[] arguments = RangeOptions.createZRangeArgs(key, rangeQuery, reverse, true);

return commandManager.submitNewCommand(Zrange, arguments, this::handleMapResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,67 @@ CompletableFuture<Map<String, Double>> zrangeWithScores(
*/
CompletableFuture<Map<String, Double>> zrangeWithScores(String key, ScoredRangeQuery rangeQuery);

/**
* Stores a specified range of elements from the sorted set at <code>source</code>, into a new
* sorted set at <code>destination</code>. If <code>destination</code> doesn't exist, a new sorted
* set is created; if it exists, it's overwritten.<br>
*
* @see <a href="https://redis.io/commands/zrangestore/">redis.io</a> for more details.
* @param destination The key for the destination sorted set.
* @param source The key of the source sorted set.
* @param rangeQuery The range query object representing the type of range query to perform.<br>
* <ul>
* <li>For range queries by index (rank), use {@link RangeByIndex}.
* <li>For range queries by lexicographical order, use {@link RangeByLex}.
* <li>For range queries by score, use {@link RangeByScore}.
* </ul>
*
* @param reverse If <code>true</code>, reverses the sorted set, with index <code>0</code> as the
* element with the highest score.
* @return The number of elements in the resulting sorted set.
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
* @example
* <pre>{@code
* RangeByIndex query1 = new RangeByIndex(0, -1); // Query for all members.
* Long payload1 = client.zrangestore("destinationKey", "mySortedSet", query1, true).get();
* assert payload1 == 7L;
*
* RangeByScore query2 = new RangeByScore(InfScoreBound.NEGATIVE_INFINITY, new ScoreBoundary(3)); // Query for members with scores within the range of negative infinity to 3.
* Long payload2 = client.zrangestore("destinationKey", "mySortedSet", query2, false).get();
* assert payload2 == 5L;
* }</pre>
*/
CompletableFuture<Long> zrangestore(
String destination, String source, RangeQuery rangeQuery, boolean reverse);

/**
* Stores a specified range of elements from the sorted set at <code>source</code>, into a new
* sorted set at <code>destination</code>. If <code>destination</code> doesn't exist, a new sorted
* set is created; if it exists, it's overwritten.<br>
*
* @see <a href="https://redis.io/commands/zrangestore/">redis.io</a> for more details.
* @param destination The key for the destination sorted set.
* @param source The key of the source sorted set.
* @param rangeQuery The range query object representing the type of range query to perform.<br>
* <ul>
* <li>For range queries by index (rank), use {@link RangeByIndex}.
* <li>For range queries by lexicographical order, use {@link RangeByLex}.
* <li>For range queries by score, use {@link RangeByScore}.
* </ul>
*
* @return The number of elements in the resulting sorted set.
* @example
* <pre>{@code
* RangeByIndex query1 = new RangeByIndex(0, -1); // Query for all members.
* Long payload1 = client.zrangestore("destinationKey", "mySortedSet", query1).get();
* assert payload1 == 7L;
*
* RangeByScore query2 = new RangeByScore(InfScoreBound.NEGATIVE_INFINITY, new ScoreBoundary(3)); // Query for members with scores within the range of negative infinity to 3.
* Long payload2 = client.zrangestore("destinationKey", "mySortedSet", query2).get();
* assert payload2 == 5L;
* }</pre>
*/
CompletableFuture<Long> zrangestore(String destination, String source, RangeQuery rangeQuery);

/**
* Returns the rank of <code>member</code> in the sorted set stored at <code>key</code>, with
* scores ordered from low to high.<br>
Expand Down
60 changes: 57 additions & 3 deletions java/client/src/main/java/glide/api/models/BaseTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import static glide.api.commands.SortedSetBaseCommands.WITH_SCORES_REDIS_API;
import static glide.api.commands.SortedSetBaseCommands.WITH_SCORE_REDIS_API;
import static glide.api.models.commands.RangeOptions.createZrangeArgs;
import static glide.api.models.commands.RangeOptions.createZRangeArgs;
import static glide.utils.ArrayTransformUtils.concatenateArrays;
import static glide.utils.ArrayTransformUtils.convertMapToKeyValueStringArray;
import static glide.utils.ArrayTransformUtils.convertMapToValueKeyStringArray;
Expand Down Expand Up @@ -81,6 +81,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.ZMScore;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMax;
import static redis_request.RedisRequestOuterClass.RequestType.ZPopMin;
import static redis_request.RedisRequestOuterClass.RequestType.ZRangeStore;
import static redis_request.RedisRequestOuterClass.RequestType.ZRemRangeByLex;
import static redis_request.RedisRequestOuterClass.RequestType.ZRemRangeByRank;
import static redis_request.RedisRequestOuterClass.RequestType.ZRemRangeByScore;
Expand All @@ -96,6 +97,7 @@
import glide.api.models.commands.InfoOptions;
import glide.api.models.commands.InfoOptions.Section;
import glide.api.models.commands.LInsertOptions.InsertPosition;
import glide.api.models.commands.RangeOptions;
import glide.api.models.commands.RangeOptions.InfLexBound;
import glide.api.models.commands.RangeOptions.InfScoreBound;
import glide.api.models.commands.RangeOptions.LexBoundary;
Expand Down Expand Up @@ -1642,6 +1644,58 @@ public T zremrangebyrank(@NonNull String key, long start, long end) {
return getThis();
}

/**
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
* Stores a specified range of elements from the sorted set at <code>source</code>, into a new
* sorted set at <code>destination</code>. If <code>destination</code> doesn't exist, a new sorted
* set is created; if it exists, it's overwritten.<br>
*
* @see <a href="https://redis.io/commands/zrangestore/">redis.io</a> for more details.
* @param destination The key for the destination sorted set.
* @param source The key of the source sorted set.
* @param rangeQuery The range query object representing the type of range query to perform.<br>
* <ul>
* <li>For range queries by index (rank), use {@link RangeByIndex}.
* <li>For range queries by lexicographical order, use {@link RangeByLex}.
* <li>For range queries by score, use {@link RangeByScore}.
* </ul>
*
* @param reverse If <code>true</code>, reverses the sorted set, with index <code>0</code> as the
* element with the highest score.
* @return Command Response - The number of elements in the resulting sorted set.
*/
public T zrangestore(
@NonNull String destination,
@NonNull String source,
@NonNull RangeQuery rangeQuery,
boolean reverse) {
ArgsArray commandArgs =
buildArgs(RangeOptions.createZRangeStoreArgs(destination, source, rangeQuery, reverse));
protobufTransaction.addCommands(buildCommand(ZRangeStore, commandArgs));
return getThis();
}

/**
* Stores a specified range of elements from the sorted set at <code>source</code>, into a new
* sorted set at <code>destination</code>. If <code>destination</code> doesn't exist, a new sorted
* set is created; if it exists, it's overwritten.<br>
*
* @see <a href="https://redis.io/commands/zrangestore/">redis.io</a> for more details.
* @param destination The key for the destination sorted set.
* @param source The key of the source sorted set.
* @param rangeQuery The range query object representing the type of range query to perform.<br>
* <ul>
* <li>For range queries by index (rank), use {@link RangeByIndex}.
* <li>For range queries by lexicographical order, use {@link RangeByLex}.
* <li>For range queries by score, use {@link RangeByScore}.
* </ul>
*
* @return Command Response - The number of elements in the resulting sorted set.
*/
public T zrangestore(
@NonNull String destination, @NonNull String source, @NonNull RangeQuery rangeQuery) {
return getThis().zrangestore(destination, source, rangeQuery, false);
}

/**
* Removes all elements in the sorted set stored at <code>key</code> with a lexicographical order
* between <code>minLex</code> and <code>maxLex</code>.
Expand Down Expand Up @@ -1927,7 +1981,7 @@ public T blpop(@NonNull String[] keys, double timeout) {
* array.
*/
public T zrange(@NonNull String key, @NonNull RangeQuery rangeQuery, boolean reverse) {
ArgsArray commandArgs = buildArgs(createZrangeArgs(key, rangeQuery, reverse, false));
ArgsArray commandArgs = buildArgs(createZRangeArgs(key, rangeQuery, reverse, false));
protobufTransaction.addCommands(buildCommand(Zrange, commandArgs));
return getThis();
}
Expand Down Expand Up @@ -1975,7 +2029,7 @@ public T zrange(@NonNull String key, @NonNull RangeQuery rangeQuery) {
*/
public T zrangeWithScores(
@NonNull String key, @NonNull ScoredRangeQuery rangeQuery, boolean reverse) {
ArgsArray commandArgs = buildArgs(createZrangeArgs(key, rangeQuery, reverse, true));
ArgsArray commandArgs = buildArgs(createZRangeArgs(key, rangeQuery, reverse, true));
protobufTransaction.addCommands(buildCommand(Zrange, commandArgs));
return getThis();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
* Arguments for {@link SortedSetBaseCommands#zcount}, {@link
* SortedSetBaseCommands#zremrangebyrank}, {@link SortedSetBaseCommands#zremrangebylex(String,
* LexRange, LexRange)}, {@link SortedSetBaseCommands#zremrangebyscore}, {@link
* SortedSetBaseCommands#zrange}, {@link SortedSetBaseCommands#zrangeWithScores}, and {@link
* SortedSetBaseCommands#zlexcount}
* SortedSetBaseCommands#zrange}, {@link SortedSetBaseCommands#zrangestore}, {@link
* SortedSetBaseCommands#zrangeWithScores}, and {@link SortedSetBaseCommands#zlexcount}
*
* @see <a href="https://redis.io/commands/zcount/">redis.io</a>
* @see <a href="https://redis.io/commands/zremrangebyrank/">redis.io</a>
* @see <a href="https://redis.io/commands/zremrangebylex/">redis.io</a>
* @see <a href="https://redis.io/commands/zremrangebyscore/">redis.io</a>
* @see <a href="https://redis.io/commands/zrange/">redis.io</a>
* @see <a href="https://redis.io/commands/zrangestore/">redis.io</a>
* @see <a href="https://redis.io/commands/zlexcount/">redis.io</a>
*/
public class RangeOptions {
Expand Down Expand Up @@ -298,9 +299,25 @@ public RangeByScore(
}
}

public static String[] createZrangeArgs(
public static String[] createZRangeArgs(
String key, RangeQuery rangeQuery, boolean reverse, boolean withScores) {
String[] arguments = new String[] {key, rangeQuery.getStart(), rangeQuery.getEnd()};
String[] arguments =
concatenateArrays(new String[] {key}, createZRangeBaseArgs(rangeQuery, reverse));
if (withScores) {
arguments = concatenateArrays(arguments, new String[] {WITH_SCORES_REDIS_API});
}

return arguments;
}

public static String[] createZRangeStoreArgs(
String destination, String source, RangeQuery rangeQuery, boolean reverse) {
return concatenateArrays(
new String[] {destination, source}, createZRangeBaseArgs(rangeQuery, reverse));
}

private static String[] createZRangeBaseArgs(RangeQuery rangeQuery, boolean reverse) {
String[] arguments = new String[] {rangeQuery.getStart(), rangeQuery.getEnd()};

if (rangeQuery instanceof RangeByScore) {
arguments = concatenateArrays(arguments, new String[] {"BYSCORE"});
Expand All @@ -323,10 +340,6 @@ public static String[] createZrangeArgs(
});
}

if (withScores) {
arguments = concatenateArrays(arguments, new String[] {WITH_SCORES_REDIS_API});
}

return arguments;
}
}
Loading
Loading