-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update commandmanager to remove optional<route> argument Signed-off-by: Andrew Carbonetto <[email protected]> * Java: Add GET & SET commands Signed-off-by: Andrew Carbonetto <[email protected]> * Move IT tests to SharedCommandTests Signed-off-by: Andrew Carbonetto <[email protected]> * Spotless Signed-off-by: Andrew Carbonetto <[email protected]> * Add nonnull check to API Signed-off-by: Andrew Carbonetto <[email protected]> * Fix from merge Signed-off-by: Andrew Carbonetto <[email protected]> * Update handleRedisResponse Signed-off-by: Andrew Carbonetto <[email protected]> * Use @timeout annotation Signed-off-by: Andrew Carbonetto <[email protected]> * Remove extra @nonnull annotations Signed-off-by: Andrew Carbonetto <[email protected]> * Merge main Signed-off-by: Andrew Carbonetto <[email protected]> * Add NonNull Signed-off-by: Andrew Carbonetto <[email protected]> * Add back IT tests Signed-off-by: Andrew Carbonetto <[email protected]> * Remove extra tests Signed-off-by: Andrew Carbonetto <[email protected]> * Add DELME tests Signed-off-by: Andrew Carbonetto <[email protected]> * Minor: clean tests; throw exception Signed-off-by: Andrew Carbonetto <[email protected]> --------- Signed-off-by: Andrew Carbonetto <[email protected]>
- Loading branch information
1 parent
ceaa793
commit 8f35f5a
Showing
8 changed files
with
571 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
java/client/src/main/java/glide/api/commands/StringCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api.commands; | ||
|
||
import glide.api.models.commands.SetOptions; | ||
import glide.api.models.commands.SetOptions.ConditionalSet; | ||
import glide.api.models.commands.SetOptions.SetOptionsBuilder; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* String Commands interface to handle single commands. | ||
* | ||
* @see <a href="https://redis.io/commands/?group=string">String Commands</a> | ||
*/ | ||
public interface StringCommands { | ||
|
||
/** | ||
* Get the value associated with the given <code>key</code>, or <code>null</code> if no such value | ||
* exists. | ||
* | ||
* @see <a href="https://redis.io/commands/get/">redis.io</a> for details. | ||
* @param key The <code>key</code> to retrieve from the database. | ||
* @return Response from Redis. If <code>key</code> exists, returns the <code>value</code> of | ||
* <code>key</code> as a <code>String</code>. Otherwise, return <code>null</code>. | ||
*/ | ||
CompletableFuture<String> get(String key); | ||
|
||
/** | ||
* Set the given <code>key</code> with the given value. | ||
* | ||
* @see <a href="https://redis.io/commands/set/">redis.io</a> for details. | ||
* @param key The <code>key</code> to store. | ||
* @param value The value to store with the given <code>key</code>. | ||
* @return Response from Redis containing <code>"OK"</code>. | ||
*/ | ||
CompletableFuture<String> set(String key, String value); | ||
|
||
/** | ||
* Set the given key with the given value. Return value is dependent on the passed options. | ||
* | ||
* @see <a href="https://redis.io/commands/set/">redis.io</a> for details. | ||
* @param key The key to store. | ||
* @param value The value to store with the given key. | ||
* @param options The Set options. | ||
* @return Response from Redis containing a <code>String</code> or <code>null</code> response. If | ||
* the value is successfully set, return <code>"OK"</code>. If value isn't set because of | ||
* {@link ConditionalSet#ONLY_IF_EXISTS} or {@link ConditionalSet#ONLY_IF_DOES_NOT_EXIST} | ||
* conditions, return <code>null</code>. If {@link SetOptionsBuilder#returnOldValue(boolean)} | ||
* is set, return the old value as a <code>String</code>. | ||
*/ | ||
CompletableFuture<String> set(String key, String value, SetOptions options); | ||
} |
171 changes: 171 additions & 0 deletions
171
java/client/src/main/java/glide/api/models/commands/SetOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api.models.commands; | ||
|
||
import static glide.api.models.commands.SetOptions.ExpiryType.KEEP_EXISTING; | ||
import static glide.api.models.commands.SetOptions.ExpiryType.MILLISECONDS; | ||
import static glide.api.models.commands.SetOptions.ExpiryType.SECONDS; | ||
import static glide.api.models.commands.SetOptions.ExpiryType.UNIX_MILLISECONDS; | ||
import static glide.api.models.commands.SetOptions.ExpiryType.UNIX_SECONDS; | ||
|
||
import glide.api.commands.StringCommands; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import redis_request.RedisRequestOuterClass.Command; | ||
|
||
/** | ||
* Optional arguments for {@link StringCommands#set(String, String, SetOptions)} command. | ||
* | ||
* @see <a href="https://redis.io/commands/set/">redis.io</a> | ||
*/ | ||
@Builder | ||
public final class SetOptions { | ||
|
||
/** | ||
* If <code>conditionalSet</code> is not set the value will be set regardless of prior value | ||
* existence. If value isn't set because of the condition, command will return <code>null</code>. | ||
*/ | ||
private final ConditionalSet conditionalSet; | ||
|
||
/** | ||
* Set command to return the old string stored at <code>key</code>, or <code>null</code> if <code> | ||
* key</code> did not exist. An error is returned and <code>SET</code> aborted if the value stored | ||
* at <code>key</code> is not a string. Equivalent to <code>GET</code> in the Redis API. | ||
*/ | ||
private final boolean returnOldValue; | ||
|
||
/** If not set, no expiry time will be set for the value. */ | ||
private final Expiry expiry; | ||
|
||
/** Conditions which define whether new value should be set or not. */ | ||
@RequiredArgsConstructor | ||
@Getter | ||
public enum ConditionalSet { | ||
/** | ||
* Only set the key if it does not already exist. Equivalent to <code>XX</code> in the Redis | ||
* API. | ||
*/ | ||
ONLY_IF_EXISTS("XX"), | ||
/** Only set the key if it already exists. Equivalent to <code>NX</code> in the Redis API. */ | ||
ONLY_IF_DOES_NOT_EXIST("NX"); | ||
|
||
private final String redisApi; | ||
} | ||
|
||
/** Configuration of value lifetime. */ | ||
public static final class Expiry { | ||
|
||
/** Expiry type for the time to live */ | ||
private final ExpiryType type; | ||
|
||
/** | ||
* The amount of time to live before the key expires. Ignored when {@link | ||
* ExpiryType#KEEP_EXISTING} type is set. | ||
*/ | ||
private Long count; | ||
|
||
private Expiry(ExpiryType type) { | ||
this.type = type; | ||
} | ||
|
||
private Expiry(ExpiryType type, Long count) { | ||
this.type = type; | ||
this.count = count; | ||
} | ||
|
||
/** | ||
* Retain the time to live associated with the key. Equivalent to <code>KEEPTTL</code> in the | ||
* Redis API. | ||
*/ | ||
public static Expiry KeepExisting() { | ||
return new Expiry(KEEP_EXISTING); | ||
} | ||
|
||
/** | ||
* Set the specified expire time, in seconds. Equivalent to <code>EX</code> in the Redis API. | ||
* | ||
* @param seconds time to expire, in seconds | ||
* @return Expiry | ||
*/ | ||
public static Expiry Seconds(Long seconds) { | ||
return new Expiry(SECONDS, seconds); | ||
} | ||
|
||
/** | ||
* Set the specified expire time, in milliseconds. Equivalent to <code>PX</code> in the Redis | ||
* API. | ||
* | ||
* @param milliseconds time to expire, in milliseconds | ||
* @return Expiry | ||
*/ | ||
public static Expiry Milliseconds(Long milliseconds) { | ||
return new Expiry(MILLISECONDS, milliseconds); | ||
} | ||
|
||
/** | ||
* Set the specified Unix time at which the key will expire, in seconds. Equivalent to <code> | ||
* EXAT</code> in the Redis API. | ||
* | ||
* @param unixSeconds unix time to expire, in seconds | ||
* @return Expiry | ||
*/ | ||
public static Expiry UnixSeconds(Long unixSeconds) { | ||
return new Expiry(UNIX_SECONDS, unixSeconds); | ||
} | ||
|
||
/** | ||
* Set the specified Unix time at which the key will expire, in milliseconds. Equivalent to | ||
* <code>PXAT</code> in the Redis API. | ||
* | ||
* @param unixMilliseconds unix time to expire, in milliseconds | ||
* @return Expiry | ||
*/ | ||
public static Expiry UnixMilliseconds(Long unixMilliseconds) { | ||
return new Expiry(UNIX_MILLISECONDS, unixMilliseconds); | ||
} | ||
} | ||
|
||
/** Types of value expiration configuration. */ | ||
@RequiredArgsConstructor | ||
protected enum ExpiryType { | ||
KEEP_EXISTING("KEEPTTL"), | ||
SECONDS("EX"), | ||
MILLISECONDS("PX"), | ||
UNIX_SECONDS("EXAT"), | ||
UNIX_MILLISECONDS("PXAT"); | ||
|
||
private final String redisApi; | ||
} | ||
|
||
/** String representation of {@link #returnOldValue} when set. */ | ||
public static final String RETURN_OLD_VALUE = "GET"; | ||
|
||
/** | ||
* Converts SetOptions into a String[] to add to a {@link Command} arguments. | ||
* | ||
* @return String[] | ||
*/ | ||
public String[] toArgs() { | ||
List<String> optionArgs = new ArrayList<>(); | ||
if (conditionalSet != null) { | ||
optionArgs.add(conditionalSet.redisApi); | ||
} | ||
|
||
if (returnOldValue) { | ||
optionArgs.add(RETURN_OLD_VALUE); | ||
} | ||
|
||
if (expiry != null) { | ||
optionArgs.add(expiry.type.redisApi); | ||
if (expiry.type != KEEP_EXISTING) { | ||
assert expiry.count != null | ||
: "Set command received expiry type " + expiry.type + ", but count was not set."; | ||
optionArgs.add(expiry.count.toString()); | ||
} | ||
} | ||
|
||
return optionArgs.toArray(new String[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.