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

support lset, hexists, sismember and copy with GlideString #1651

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 37 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,12 @@ public CompletableFuture<Boolean> hexists(@NonNull String key, @NonNull String f
HExists, new String[] {key, field}, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> hexists(@NonNull GlideString key, @NonNull GlideString field) {
return commandManager.submitNewCommand(
HExists, new GlideString[] {key, field}, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Map<String, String>> hgetall(@NonNull String key) {
return commandManager.submitNewCommand(HGetAll, new String[] {key}, this::handleMapResponse);
Expand Down Expand Up @@ -1014,6 +1020,13 @@ public CompletableFuture<Boolean> sismember(@NonNull String key, @NonNull String
SIsMember, new String[] {key, member}, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> sismember(
@NonNull GlideString key, @NonNull GlideString member) {
return commandManager.submitNewCommand(
SIsMember, new GlideString[] {key, member}, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Long> srem(@NonNull String key, @NonNull String[] members) {
String[] arguments = ArrayUtils.addFirst(members, key);
Expand Down Expand Up @@ -2195,6 +2208,13 @@ public CompletableFuture<String> lset(@NonNull String key, long index, @NonNull
return commandManager.submitNewCommand(LSet, arguments, this::handleStringResponse);
}

@Override
public CompletableFuture<String> lset(
@NonNull GlideString key, long index, @NonNull GlideString element) {
GlideString[] arguments = new GlideString[] {key, gs(Long.toString(index)), element};
return commandManager.submitNewCommand(LSet, arguments, this::handleStringResponse);
}

@Override
public CompletableFuture<String> lmove(
@NonNull String source,
Expand Down Expand Up @@ -2306,12 +2326,29 @@ public CompletableFuture<Boolean> copy(
return commandManager.submitNewCommand(Copy, arguments, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> copy(
@NonNull GlideString source, @NonNull GlideString destination, boolean replace) {
GlideString[] arguments = new GlideString[] {source, destination};
if (replace) {
arguments = ArrayUtils.add(arguments, gs(REPLACE_REDIS_API));
}
return commandManager.submitNewCommand(Copy, arguments, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> copy(@NonNull String source, @NonNull String destination) {
String[] arguments = new String[] {source, destination};
return commandManager.submitNewCommand(Copy, arguments, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> copy(
@NonNull GlideString source, @NonNull GlideString destination) {
GlideString[] arguments = new GlideString[] {source, destination};
return commandManager.submitNewCommand(Copy, arguments, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> msetnx(@NonNull Map<String, String> keyValueMap) {
String[] args = convertMapToKeyValueStringArray(keyValueMap);
Expand Down
22 changes: 22 additions & 0 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ public CompletableFuture<Boolean> copy(
return commandManager.submitNewCommand(Copy, arguments, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> copy(
@NonNull GlideString source, @NonNull GlideString destination, long destinationDB) {
GlideString[] arguments =
new GlideString[] {source, destination, gs(DB_REDIS_API), gs(Long.toString(destinationDB))};
return commandManager.submitNewCommand(Copy, arguments, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> copy(
@NonNull String source, @NonNull String destination, long destinationDB, boolean replace) {
Expand All @@ -332,6 +340,20 @@ public CompletableFuture<Boolean> copy(
return commandManager.submitNewCommand(Copy, arguments, this::handleBooleanResponse);
}

@Override
public CompletableFuture<Boolean> copy(
@NonNull GlideString source,
@NonNull GlideString destination,
long destinationDB,
boolean replace) {
GlideString[] arguments =
new GlideString[] {source, destination, gs(DB_REDIS_API), gs(Long.toString(destinationDB))};
if (replace) {
arguments = ArrayUtils.add(arguments, gs(REPLACE_REDIS_API));
}
return commandManager.submitNewCommand(Copy, arguments, this::handleBooleanResponse);
}

@Override
public CompletableFuture<String> functionKill() {
return commandManager.submitNewCommand(FunctionKill, new String[0], this::handleStringResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,28 @@ CompletableFuture<Boolean> pexpireAt(
*/
CompletableFuture<Boolean> copy(String source, String destination);

/**
* Copies the value stored at the <code>source</code> to the <code>destination</code> key if the
* <code>destination</code> key does not yet exist.
*
* @apiNote When in cluster mode, both <code>source</code> and <code>destination</code> must map
* to the same hash slot.
* @since Redis 6.2.0 and above.
* @see <a href="https://redis.io/commands/copy/">redis.io</a> for details.
* @param source The key to the source value.
* @param destination The key where the value should be copied to.
* @return <code>true</code> if <code>source</code> was copied, <code>false</code> if <code>source
* </code> was not copied.
* @example
* <pre>{@code
* client.set(gs("test1"), gs("one")).get();
* client.set(gs("test2", gs("two")).get();
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
* assert !client.copy(gs("test1", gs("test2")).get();
* assert client.copy(gs("test1"), gs("test2")).get();
* }</pre>
*/
CompletableFuture<Boolean> copy(GlideString source, GlideString destination);

/**
* Copies the value stored at the <code>source</code> to the <code>destination</code> key. When
* <code>replace</code> is true, removes the <code>destination</code> key first if it already
Expand All @@ -649,6 +671,30 @@ CompletableFuture<Boolean> pexpireAt(
*/
CompletableFuture<Boolean> copy(String source, String destination, boolean replace);

/**
* Copies the value stored at the <code>source</code> to the <code>destination</code> key. When
* <code>replace</code> is true, removes the <code>destination</code> key first if it already
* exists, otherwise performs no action.
*
* @apiNote When in cluster mode, both <code>source</code> and <code>destination</code> must map
* to the same hash slot.
* @since Redis 6.2.0 and above.
* @see <a href="https://redis.io/commands/copy/">redis.io</a> for details.
* @param source The key to the source value.
* @param destination The key where the value should be copied to.
* @param replace If the destination key should be removed before copying the value to it.
* @return <code>true</code> if <code>source</code> was copied, <code>false</code> if <code>source
* </code> was not copied.
* @example
* <pre>{@code
* client.set(gs("test1"), gs("one")).get();
* client.set(gs("test2"), gs("two")).get();
* assert !client.copy(gs("test1", gs("test2"), false).get();
* assert client.copy(gs("test1", gs("test2"), true).get();
* }</pre>
*/
CompletableFuture<Boolean> copy(GlideString source, GlideString destination, boolean replace);

/**
* Serialize the value stored at <code>key</code> in a Valkey-specific format and return it to the
* user.
Expand Down
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 @@ -99,6 +100,28 @@ public interface GenericCommands {
CompletableFuture<Boolean> copy(
String source, String destination, long destinationDB, boolean replace);

/**
* 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
* </code> key first if it already exists, otherwise performs no action.
*
* @since Redis 6.2.0 and above.
* @see <a href="https://redis.io/commands/copy/">redis.io</a> for details.
* @param source The key to the source value.
* @param destination The key where the value should be copied to.
* @param destinationDB The alternative logical database index for the destination key.
* @param replace If the destination key should be removed before copying the value to it.
* @return <code>true</code> if <code>source</code> was copied, <code>false</code> if <code>source
* </code> was not copied.
* @example
* <pre>{@code
* client.set(gs("test1"), gs("one")).get();
* assert client.copy(gs("test1"), gs("test2"), 1, false).get();
* }</pre>
*/
CompletableFuture<Boolean> copy(
GlideString source, GlideString destination, long destinationDB, boolean replace);

/**
* 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 All @@ -119,6 +142,26 @@ CompletableFuture<Boolean> copy(
*/
CompletableFuture<Boolean> copy(String source, String destination, long destinationDB);

/**
* 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
* </code> key first if it already exists, otherwise performs no action.
*
* @since Redis 6.2.0 and above.
* @see <a href="https://redis.io/commands/copy/">redis.io</a> for details.
* @param source The key to the source value.
* @param destination The key where the value should be copied to.
* @param destinationDB The alternative logical database index for the destination key.
* @return <code>true</code> if <code>source</code> was copied, <code>false</code> if <code>source
* </code> was not copied.
* @example
* <pre>{@code
* client.set(gs("test1"), gs("one")).get();
* assert client.copy(gs("test1"), gs("test2"), 1).get();
* }</pre>
*/
CompletableFuture<Boolean> copy(GlideString source, GlideString destination, long destinationDB);

/**
* Returns a random key from currently selected database.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ public interface HashBaseCommands {
*/
CompletableFuture<Boolean> hexists(String key, String field);

/**
* Returns if <code>field</code> is an existing field in the hash stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/hexists/">redis.io</a> for details.
* @param key The key of the hash.
* @param field The field to check in the hash stored at <code>key</code>.
* @return <code>True</code> if the hash contains the specified field. If the hash does not
* contain the field, or if the key does not exist, it returns <code>False</code>.
* @example
* <pre>{@code
* Boolean exists = client.hexists(gs("my_hash"), gs("field1")).get();
* assert exists;
*
* Boolean exists = client.hexists(gs("my_hash"), gs("non_existent_field")).get();
* assert !exists;
* }</pre>
*/
CompletableFuture<Boolean> hexists(GlideString key, GlideString field);

/**
* Returns all fields and values of the hash stored at <code>key</code>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,25 @@ CompletableFuture<Map<String, String[]>> blmpop(
*/
CompletableFuture<String> lset(String key, long index, String element);

/**
* Sets the list element at <code>index</code> to <code>element</code>.<br>
* The index is zero-based, so <code>0</code> means the first element, <code>1</code> the second
* element and so on. Negative indices can be used to designate elements starting at the tail of
* the list. Here, <code>-1</code> means the last element, <code>-2</code> means the penultimate
* and so forth.
*
* @see <a href="https://valkey.io/commands/lset/">valkey.io</a> for details.
* @param key The key of the list.
* @param index The index of the element in the list to be set.
* @return <code>OK</code>.
* @example
* <pre>{@code
* String response = client.lset(gs("testKey"), 1, gs("two")).get();
* assertEquals(response, "OK");
* }</pre>
*/
CompletableFuture<String> lset(GlideString key, long index, GlideString element);

/**
* Atomically pops and removes the left/right-most element to the list stored at <code>source
* </code> depending on <code>wherefrom</code>, and pushes the element at the first/last element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ public interface SetBaseCommands {
*/
CompletableFuture<Boolean> sismember(String key, String member);

/**
* Returns if <code>member</code> is a member of the set stored at <code>key</code>.
*
* @see <a href="https://redis.io/commands/sismember/">redis.io</a> for details.
* @param key The key of the set.
* @param member The member to check for existence in the set.
* @return <code>true</code> if the member exists in the set, <code>false</code> otherwise. If
* <code>key</code> doesn't exist, it is treated as an <code>empty set</code> and the command
* returns <code>false</code>.
* @example
* <pre>{@code
* Boolean payload1 = client.sismember(gs("mySet")), gs("member1")).get();
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
* assert payload1; // Indicates that "member1" exists in the set "mySet".
*
* Boolean payload2 = client.sismember(gs("mySet"), gs("nonExistingMember")).get();
* assert !payload2; // Indicates that "nonExistingMember" does not exist in the set "mySet".
* }</pre>
*/
CompletableFuture<Boolean> sismember(GlideString key, GlideString member);

/**
* Computes the difference between the first set and all the successive sets in <code>keys</code>.
*
Expand Down
Loading
Loading