Skip to content

Commit

Permalink
support move, ltrim, sadd, srem, scard and smove with GlideString (#1638
Browse files Browse the repository at this point in the history
)

* support move, ltrim, sadd, srem and smove with GlideString

* add to integration tests the use of the API with GlideString parameters

* remove calls to getBytes()

* nit: apply spotlessApply

* add binary and not binary integration test for smove

* add integration tests with GlideString version

* nit: spotlessApply

* remove the use of gs() in ltrim_existing_non_existing_key_and_type_error

* add binary version integration tests
  • Loading branch information
alon-arenberg authored Jun 26, 2024
1 parent 5b3f7ca commit 5a0b405
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 8 deletions.
33 changes: 33 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,14 @@ public CompletableFuture<String> ltrim(@NonNull String key, long start, long end
this::handleStringResponse);
}

@Override
public CompletableFuture<String> ltrim(@NonNull GlideString key, long start, long end) {
return commandManager.submitNewCommand(
LTrim,
new GlideString[] {key, gs(Long.toString(start)), gs(Long.toString(end))},
this::handleStringResponse);
}

@Override
public CompletableFuture<Long> llen(@NonNull String key) {
return commandManager.submitNewCommand(LLen, new String[] {key}, this::handleLongResponse);
Expand Down Expand Up @@ -1032,6 +1040,12 @@ public CompletableFuture<Long> sadd(@NonNull String key, @NonNull String[] membe
return commandManager.submitNewCommand(SAdd, arguments, this::handleLongResponse);
}

@Override
public CompletableFuture<Long> sadd(@NonNull GlideString key, @NonNull GlideString[] members) {
GlideString[] arguments = ArrayUtils.addFirst(members, key);
return commandManager.submitNewCommand(SAdd, arguments, this::handleLongResponse);
}

@Override
public CompletableFuture<Boolean> sismember(@NonNull String key, @NonNull String member) {
return commandManager.submitNewCommand(
Expand All @@ -1044,6 +1058,12 @@ public CompletableFuture<Long> srem(@NonNull String key, @NonNull String[] membe
return commandManager.submitNewCommand(SRem, arguments, this::handleLongResponse);
}

@Override
public CompletableFuture<Long> srem(@NonNull GlideString key, @NonNull GlideString[] members) {
GlideString[] arguments = ArrayUtils.addFirst(members, key);
return commandManager.submitNewCommand(SRem, arguments, this::handleLongResponse);
}

@Override
public CompletableFuture<Set<String>> smembers(@NonNull String key) {
return commandManager.submitNewCommand(SMembers, new String[] {key}, this::handleSetResponse);
Expand All @@ -1060,6 +1080,12 @@ public CompletableFuture<Long> scard(@NonNull String key) {
return commandManager.submitNewCommand(SCard, new String[] {key}, this::handleLongResponse);
}

@Override
public CompletableFuture<Long> scard(@NonNull GlideString key) {
return commandManager.submitNewCommand(
SCard, new GlideString[] {key}, this::handleLongResponse);
}

@Override
public CompletableFuture<Set<String>> sdiff(@NonNull String[] keys) {
return commandManager.submitNewCommand(SDiff, keys, this::handleSetResponse);
Expand All @@ -1085,6 +1111,13 @@ public CompletableFuture<Boolean> smove(
SMove, new String[] {source, destination, member}, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> smove(
@NonNull GlideString source, @NonNull GlideString destination, @NonNull GlideString member) {
return commandManager.submitNewCommand(
SMove, new GlideString[] {source, destination, member}, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Long> sinterstore(@NonNull String destination, @NonNull String[] keys) {
String[] arguments = ArrayUtils.addFirst(keys, destination);
Expand Down
6 changes: 6 additions & 0 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ public CompletableFuture<Boolean> move(@NonNull String key, long dbIndex) {
Move, new String[] {key, Long.toString(dbIndex)}, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> move(@NonNull GlideString key, long dbIndex) {
return commandManager.submitNewCommand(
Move, new GlideString[] {key, gs(Long.toString(dbIndex))}, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Map<String, Object>[]> functionList(boolean withCode) {
return commandManager.submitNewCommand(
Expand Down
19 changes: 19 additions & 0 deletions java/client/src/main/java/glide/api/commands/GenericCommands.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */
package glide.api.commands;

import glide.api.models.GlideString;
import glide.api.models.Transaction;
import glide.api.models.commands.SortOptions;
import glide.api.models.configuration.ReadFrom;
Expand Down Expand Up @@ -77,6 +78,24 @@ public interface GenericCommands {
*/
CompletableFuture<Boolean> move(String key, long dbIndex);

/**
* Move <code>key</code> from the currently selected database to the database specified by <code>
* dbIndex</code>.
*
* @see <a href="https://redis.io/commands/move/">redis.io</a> for more details.
* @param key The key to move.
* @param dbIndex The index of the database to move <code>key</code> to.
* @return <code>true</code> if <code>key</code> was moved, or <code>false</code> if the <code>key
* </code> already exists in the destination database or does not exist in the source
* database.
* @example
* <pre>{@code
* Boolean moved = client.move(gs("some_key"), 1L).get();
* assert moved;
* }</pre>
*/
CompletableFuture<Boolean> move(GlideString key, long dbIndex);

/**
* Copies the value stored at the <code>source</code> to the <code>destination</code> key on
* <code>destinationDB</code>. When <code>replace</code> is true, removes the <code>destination
Expand Down
26 changes: 26 additions & 0 deletions java/client/src/main/java/glide/api/commands/ListBaseCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ CompletableFuture<Long[]> lposCount(
*/
CompletableFuture<String> ltrim(String key, long start, long end);

/**
* Trims an existing list so that it will contain only the specified range of elements specified.
* <br>
* The offsets <code>start</code> and <code>end</code> are zero-based indexes, with 0 being the
* first element of the list, 1 being the next element and so on.<br>
* These offsets can also be negative numbers indicating offsets starting at the end of the list,
* with -1 being the last element of the list, -2 being the penultimate, and so on.
*
* @see <a href="https://redis.io/commands/ltrim/">redis.io</a> for details.
* @param key The key of the list.
* @param start The starting point of the range.
* @param end The end of the range.
* @return Always <code>OK</code>.<br>
* If <code>start</code> exceeds the end of the list, or if <code>start</code> is greater than
* <code>end</code>, the result will be an empty list (which causes key to be removed).<br>
* If <code>end</code> exceeds the actual end of the list, it will be treated like the last
* element of the list.<br>
* If <code>key</code> does not exist, OK will be returned without changes to the database.
* @example
* <pre>{@code
* String payload = client.ltrim(gs("my_list"), 0, 1).get();
* assert payload.equals("OK");
* }</pre>
*/
CompletableFuture<String> ltrim(GlideString key, long start, long end);

/**
* Returns the length of the list stored at <code>key</code>.
*
Expand Down
71 changes: 71 additions & 0 deletions java/client/src/main/java/glide/api/commands/SetBaseCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public interface SetBaseCommands {
*/
CompletableFuture<Long> sadd(String key, String[] members);

/**
* Adds specified members to the set stored at <code>key</code>. Specified members that are
* already a member of this set are ignored.
*
* @see <a href="https://redis.io/commands/sadd/">redis.io</a> for details.
* @param key The <code>key</code> where members will be added to its set.
* @param members A list of members to add to the set stored at <code>key</code>.
* @return The number of members that were added to the set, excluding members already present.
* @remarks If <code>key</code> does not exist, a new set is created before adding <code>members
* </code>.
* @example
* <pre>{@code
* Long result = client.sadd(gs("my_set"), new GlideString[]{gs("member1"), gs("member2")}).get();
* assert result == 2L;
* }</pre>
*/
CompletableFuture<Long> sadd(GlideString key, GlideString[] members);

/**
* Removes specified members from the set stored at <code>key</code>. Specified members that are
* not a member of this set are ignored.
Expand All @@ -51,6 +69,24 @@ public interface SetBaseCommands {
*/
CompletableFuture<Long> srem(String key, String[] members);

/**
* Removes specified members from the set stored at <code>key</code>. Specified members that are
* not a member of this set are ignored.
*
* @see <a href="https://redis.io/commands/srem/">redis.io</a> for details.
* @param key The <code>key</code> from which members will be removed.
* @param members A list of members to remove from the set stored at <code>key</code>.
* @return The number of members that were removed from the set, excluding non-existing members.
* @remarks If <code>key</code> does not exist, it is treated as an empty set and this command
* returns <code>0</code>.
* @example
* <pre>{@code
* Long result = client.srem(gs("my_set"), new GlideString[]{gs("member1"), gs("member2")}).get();
* assert result == 2L;
* }</pre>
*/
CompletableFuture<Long> srem(GlideString key, GlideString[] members);

/**
* Retrieves all the members of the set value stored at <code>key</code>.
*
Expand Down Expand Up @@ -95,6 +131,20 @@ public interface SetBaseCommands {
*/
CompletableFuture<Long> scard(String key);

/**
* Retrieves the set cardinality (number of elements) of the set stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/scard/">redis.io</a> for details.
* @param key The key from which to retrieve the number of set members.
* @return The cardinality (number of elements) of the set, or 0 if the key does not exist.
* @example
* <pre>{@code
* Long result = client.scard("my_set").get();
* assert result == 3L;
* }</pre>
*/
CompletableFuture<Long> scard(GlideString key);

/**
* Checks whether each member is contained in the members of the set stored at <code>key</code>.
*
Expand Down Expand Up @@ -132,6 +182,27 @@ public interface SetBaseCommands {
*/
CompletableFuture<Boolean> smove(String source, String destination, String member);

/**
* Moves <code>member</code> from the set at <code>source</code> to the set at <code>destination
* </code>, removing it from the source set. Creates a new destination set if needed. The
* operation is atomic.
*
* @apiNote When in cluster mode, both <code>source</code> and <code>destination</code> must map
* to the same hash slot.
* @see <a href="https://redis.io/commands/smove/">redis.io</a> for details.
* @param source The key of the set to remove the element from.
* @param destination The key of the set to add the element to.
* @param member The set element to move.
* @return <code>true</code> on success, or <code>false</code> if the <code>source</code> set does
* not exist or the element is not a member of the source set.
* @example
* <pre>{@code
* Boolean moved = client.smove(gs("set1"), gs("set2"), gs("element")).get();
* assert moved;
* }</pre>
*/
CompletableFuture<Boolean> smove(GlideString source, GlideString destination, GlideString member);

/**
* Returns if <code>member</code> is a member of the set stored at <code>key</code>.
*
Expand Down
Loading

0 comments on commit 5a0b405

Please sign in to comment.