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

Adding command LMove #319

Merged
merged 6 commits into from
May 29, 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 @@ -196,6 +196,7 @@ enum RequestType {
BLMPop = 158;
XLen = 159;
LSet = 165;
LMove = 168;
}

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 @@ -166,6 +166,7 @@ pub enum RequestType {
BLMPop = 158,
XLen = 159,
LSet = 165,
LMove = 168,
}

fn get_two_word_command(first: &str, second: &str) -> Cmd {
Expand Down Expand Up @@ -335,6 +336,7 @@ impl From<::protobuf::EnumOrUnknown<ProtobufRequestType>> for RequestType {
ProtobufRequestType::PExpireTime => RequestType::PExpireTime,
ProtobufRequestType::XLen => RequestType::XLen,
ProtobufRequestType::LSet => RequestType::LSet,
ProtobufRequestType::LMove => RequestType::LMove,
}
}
}
Expand Down Expand Up @@ -500,6 +502,7 @@ impl RequestType {
RequestType::PExpireTime => Some(cmd("PEXPIRETIME")),
RequestType::XLen => Some(cmd("XLEN")),
RequestType::LSet => Some(cmd("LSET")),
RequestType::LMove => Some(cmd("LMOVE")),
}
}
}
36 changes: 27 additions & 9 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import static redis_request.RedisRequestOuterClass.RequestType.LInsert;
import static redis_request.RedisRequestOuterClass.RequestType.LLen;
import static redis_request.RedisRequestOuterClass.RequestType.LMPop;
import static redis_request.RedisRequestOuterClass.RequestType.LMove;
import static redis_request.RedisRequestOuterClass.RequestType.LPop;
import static redis_request.RedisRequestOuterClass.RequestType.LPush;
import static redis_request.RedisRequestOuterClass.RequestType.LPushX;
Expand Down Expand Up @@ -143,7 +144,7 @@
import glide.api.models.Script;
import glide.api.models.commands.ExpireOptions;
import glide.api.models.commands.LInsertOptions.InsertPosition;
import glide.api.models.commands.PopDirection;
import glide.api.models.commands.ListDirection;
import glide.api.models.commands.RangeOptions;
import glide.api.models.commands.RangeOptions.LexRange;
import glide.api.models.commands.RangeOptions.RangeQuery;
Expand Down Expand Up @@ -1506,12 +1507,14 @@ public CompletableFuture<Long> bitop(

@Override
public CompletableFuture<Map<String, String[]>> lmpop(
@NonNull String[] keys, @NonNull PopDirection direction, long count) {
@NonNull String[] keys, @NonNull ListDirection listDirection, long count) {
String[] arguments =
concatenateArrays(
new String[] {Long.toString(keys.length)},
keys,
new String[] {direction.toString(), COUNT_FOR_LIST_REDIS_API, Long.toString(count)});
new String[] {
listDirection.toString(), COUNT_FOR_LIST_REDIS_API, Long.toString(count)
});
return commandManager.submitNewCommand(
LMPop,
arguments,
Expand All @@ -1520,10 +1523,12 @@ public CompletableFuture<Map<String, String[]>> lmpop(

@Override
public CompletableFuture<Map<String, String[]>> lmpop(
@NonNull String[] keys, @NonNull PopDirection direction) {
@NonNull String[] keys, @NonNull ListDirection listDirection) {
String[] arguments =
concatenateArrays(
new String[] {Long.toString(keys.length)}, keys, new String[] {direction.toString()});
new String[] {Long.toString(keys.length)},
keys,
new String[] {listDirection.toString()});
return commandManager.submitNewCommand(
LMPop,
arguments,
Expand All @@ -1532,12 +1537,14 @@ public CompletableFuture<Map<String, String[]>> lmpop(

@Override
public CompletableFuture<Map<String, String[]>> blmpop(
@NonNull String[] keys, @NonNull PopDirection direction, long count, double timeout) {
@NonNull String[] keys, @NonNull ListDirection listDirection, long count, double timeout) {
String[] arguments =
concatenateArrays(
new String[] {Double.toString(timeout), Long.toString(keys.length)},
keys,
new String[] {direction.toString(), COUNT_FOR_LIST_REDIS_API, Long.toString(count)});
new String[] {
listDirection.toString(), COUNT_FOR_LIST_REDIS_API, Long.toString(count)
});
return commandManager.submitNewCommand(
BLMPop,
arguments,
Expand All @@ -1546,12 +1553,12 @@ public CompletableFuture<Map<String, String[]>> blmpop(

@Override
public CompletableFuture<Map<String, String[]>> blmpop(
@NonNull String[] keys, @NonNull PopDirection direction, double timeout) {
@NonNull String[] keys, @NonNull ListDirection listDirection, double timeout) {
String[] arguments =
concatenateArrays(
new String[] {Double.toString(timeout), Long.toString(keys.length)},
keys,
new String[] {direction.toString()});
new String[] {listDirection.toString()});
return commandManager.submitNewCommand(
BLMPop,
arguments,
Expand All @@ -1563,4 +1570,15 @@ public CompletableFuture<String> lset(@NonNull String key, long index, @NonNull
String[] arguments = new String[] {key, Long.toString(index), element};
return commandManager.submitNewCommand(LSet, arguments, this::handleStringResponse);
}

@Override
public CompletableFuture<String> lmove(
@NonNull String source,
@NonNull String destination,
@NonNull ListDirection wherefrom,
@NonNull ListDirection whereto) {
String[] arguments =
new String[] {source, destination, wherefrom.toString(), whereto.toString()};
return commandManager.submitNewCommand(LMove, arguments, this::handleStringOrNullResponse);
}
}
60 changes: 45 additions & 15 deletions java/client/src/main/java/glide/api/commands/ListBaseCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package glide.api.commands;

import glide.api.models.commands.LInsertOptions.InsertPosition;
import glide.api.models.commands.PopDirection;
import glide.api.models.commands.ListDirection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -382,8 +382,8 @@ CompletableFuture<Long> linsert(
* @apiNote When in cluster mode, all <code>keys</code> must map to the same hash slot.
* @see <a href="https://valkey.io/commands/lmpop/">valkey.io</a> for details.
* @param keys An array of keys to lists.
* @param direction The direction based on which elements are popped from - see {@link
* PopDirection}.
* @param listDirection The direction based on which elements are popped from - see {@link
* ListDirection}.
* @param count The maximum number of popped elements.
* @return A <code>Map</code> of <code>key</code> name mapped array of popped elements.
* @example
Expand All @@ -394,7 +394,8 @@ CompletableFuture<Long> linsert(
* assertArrayEquals(new String[] {"three"}, resultValue);
* }</pre>
*/
CompletableFuture<Map<String, String[]>> lmpop(String[] keys, PopDirection direction, long count);
CompletableFuture<Map<String, String[]>> lmpop(
String[] keys, ListDirection listDirection, long count);

/**
* Pops one element from the first non-empty list from the provided <code>keys</code>.
Expand All @@ -403,8 +404,8 @@ CompletableFuture<Long> linsert(
* @apiNote When in cluster mode, all <code>keys</code> must map to the same hash slot.
* @see <a href="https://valkey.io/commands/lmpop/">valkey.io</a> for details.
* @param keys An array of keys to lists.
* @param direction The direction based on which elements are popped from - see {@link
* PopDirection}.
* @param listDirection The direction based on which elements are popped from - see {@link
* ListDirection}.
* @return A <code>Map</code> of <code>key</code> name mapped array of the popped element.
* @example
* <pre>{@code
Expand All @@ -414,12 +415,12 @@ CompletableFuture<Long> linsert(
* assertArrayEquals(new String[] {"three"}, resultValue);
* }</pre>
*/
CompletableFuture<Map<String, String[]>> lmpop(String[] keys, PopDirection direction);
CompletableFuture<Map<String, String[]>> lmpop(String[] keys, ListDirection listDirection);

/**
* Blocks the connection until it pops one or more elements from the first non-empty list from the
* provided <code>keys</code> <code>BLMPOP</code> is the blocking variant of {@link
* #lmpop(String[], PopDirection, long)}.
* #lmpop(String[], ListDirection, long)}.
*
* @apiNote
* <ol>
Expand All @@ -432,8 +433,8 @@ CompletableFuture<Long> linsert(
* @since Redis 7.0 and above.
* @see <a href="https://valkey.io/commands/blmpop/">valkey.io</a> for details.
* @param keys An array of keys to lists.
* @param direction The direction based on which elements are popped from - see {@link
* PopDirection}.
* @param listDirection The direction based on which elements are popped from - see {@link
* ListDirection}.
* @param count The maximum number of popped elements.
* @param timeout The number of seconds to wait for a blocking operation to complete. A value of
* <code>0</code> will block indefinitely.
Expand All @@ -448,12 +449,12 @@ CompletableFuture<Long> linsert(
* }</pre>
*/
CompletableFuture<Map<String, String[]>> blmpop(
String[] keys, PopDirection direction, long count, double timeout);
String[] keys, ListDirection listDirection, long count, double timeout);

/**
* Blocks the connection until it pops one element from the first non-empty list from the provided
* <code>keys</code> <code>BLMPOP</code> is the blocking variant of {@link #lmpop(String[],
* PopDirection)}.
* ListDirection)}.
*
* @apiNote
* <ol>
Expand All @@ -466,8 +467,8 @@ CompletableFuture<Map<String, String[]>> blmpop(
* @since Redis 7.0 and above.
* @see <a href="https://valkey.io/commands/lmpop/">valkey.io</a> for details.
* @param keys An array of keys to lists.
* @param direction The direction based on which elements are popped from - see {@link
* PopDirection}.
* @param listDirection The direction based on which elements are popped from - see {@link
* ListDirection}.
* @param timeout The number of seconds to wait for a blocking operation to complete. A value of
* <code>0</code> will block indefinitely.
* @return A <code>Map</code> of <code>key</code> name mapped array of the popped element.<br>
Expand All @@ -481,7 +482,7 @@ CompletableFuture<Map<String, String[]>> blmpop(
* }</pre>
*/
CompletableFuture<Map<String, String[]>> blmpop(
String[] keys, PopDirection direction, double timeout);
String[] keys, ListDirection listDirection, double timeout);

/**
* Sets the list element at <code>index</code> to <code>element</code>.<br>
Expand All @@ -501,4 +502,33 @@ CompletableFuture<Map<String, String[]>> blmpop(
* }</pre>
*/
CompletableFuture<String> lset(String key, long index, String element);

/**
* Atomically pops and removes the first/last element of the list stored at <code>source</code>
* depending on <code>wherefrom</code>, and pushes the element at the first/last element of the
* list stored at <code>destination</code> depending on <code>wherefrom</code>.
*
* @since Redis 6.2.0 and above.
* @apiNote When in cluster mode, <code>source</code> and <code>destination</code> must map to the
* same hash slot.
* @see <a href="https://valkey.io/commands/lmove/">valkey.io</a> for details.
* @param source The key to the source list.
* @param destination The key to the destination list.
* @param wherefrom The direction the element should be removed from.
* @param whereto The direction the element should be added to.
* @return The popped element or <code>null</code> if <code>source</code> does not exist.
* @example
* <pre>{@code
* client.lpush("testKey1", new String[] {"two", "one"}).get();
* client.lpush("testKey2", new String[] {"four", "three"}).get();
* var result = client.lmove("testKey1", "testKey2", ListDirection.LEFT, ListDirection.LEFT).get();
* assertEquals(result, "one");
* String[] upratedArray1 = client.lrange("testKey1", 0, -1).get();
* String[] upratedArray2 = client.lrange("testKey2", 0, -1).get();
* assertArrayEquals(new String[] {"two"}, updatedArray1);
* assertArrayEquals(new String[] {"one", "three", "four"}, updatedArray2);
* }</pre>
*/
CompletableFuture<String> lmove(
String source, String destination, ListDirection wherefrom, ListDirection whereto);
}
Loading
Loading