Skip to content

Commit

Permalink
Apply PR comments
Browse files Browse the repository at this point in the history
* Fix method ordering in BaseTransaction
* Fix broken line breaks within code tags in ScanOptions
* More thoroughly test results in SharedCommandTests
  • Loading branch information
jduo committed Jun 27, 2024
1 parent efc944b commit bf7a91a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 72 deletions.
134 changes: 67 additions & 67 deletions java/client/src/main/java/glide/api/models/BaseTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4878,73 +4878,6 @@ public T sunion(@NonNull String[] keys) {
return getThis();
}

/**
* Sorts the elements in the list, set, or sorted set at <code>key</code> and returns the result.
* <br>
* The <code>sort</code> command can be used to sort elements based on different criteria and
* apply transformations on sorted elements.<br>
* To store the result into a new key, see {@link #sortStore(String, String)}.<br>
*
* @param key The key of the list, set, or sorted set to be sorted.
* @return Command Response - An <code>Array</code> of sorted elements.
*/
public T sort(@NonNull String key) {
ArgsArray commandArgs = buildArgs(key);
protobufTransaction.addCommands(buildCommand(Sort, commandArgs));
return getThis();
}

/**
* Iterates incrementally over a set.
*
* @see <a href="https://valkey.io/commands/sscan">valkey.io</a> for details.
* @param key The key of the set.
* @param cursor The cursor that points to the next iteration of results.
* @return Command Response - An <code>Array</code> of <code>Objects</code>. The first element is
* always the <code>cursor</code> for the next iteration of results. <code>0</code> will be
* the <code>cursor</code> returned on the last iteration of the set. The second element is
* always an <code>Array</code> of the subset of the set held in <code>key</code>.
*/
public T sscan(@NonNull String key, long cursor) {
protobufTransaction.addCommands(buildCommand(SScan, buildArgs(key, Long.toString(cursor))));
return getThis();
}

/**
* Sorts the elements in the list, set, or sorted set at <code>key</code> and returns the result.
* <br>
* The <code>sortReadOnly</code> command can be used to sort elements based on different criteria
* and apply transformations on sorted elements.
*
* @since Redis 7.0 and above.
* @param key The key of the list, set, or sorted set to be sorted.
* @return Command Response - An <code>Array</code> of sorted elements.
*/
public T sortReadOnly(@NonNull String key) {
ArgsArray commandArgs = buildArgs(key);
protobufTransaction.addCommands(buildCommand(SortReadOnly, commandArgs));
return getThis();
}

/**
* Sorts the elements in the list, set, or sorted set at <code>key</code> and stores the result in
* <code>destination</code>. The <code>sort</code> command can be used to sort elements based on
* different criteria, apply transformations on sorted elements, and store the result in a new
* key.<br>
* To get the sort result without storing it into a key, see {@link #sort(String)} or {@link
* #sortReadOnly(String)}.
*
* @param key The key of the list, set, or sorted set to be sorted.
* @param destination The key where the sorted result will be stored.
* @return Command Response - The number of elements in the sorted key stored at <code>destination
* </code>.
*/
public T sortStore(@NonNull String key, @NonNull String destination) {
ArgsArray commandArgs = buildArgs(new String[] {key, STORE_COMMAND_STRING, destination});
protobufTransaction.addCommands(buildCommand(Sort, commandArgs));
return getThis();
}

/**
* Returns the indices and length of the longest common subsequence between strings stored at
* <code>key1</code> and <code>key2</code>.
Expand Down Expand Up @@ -5144,6 +5077,73 @@ public T lcsIdxWithMatchLen(@NonNull String key1, @NonNull String key2, long min
return getThis();
}

/**
* Sorts the elements in the list, set, or sorted set at <code>key</code> and returns the result.
* <br>
* The <code>sort</code> command can be used to sort elements based on different criteria and
* apply transformations on sorted elements.<br>
* To store the result into a new key, see {@link #sortStore(String, String)}.<br>
*
* @param key The key of the list, set, or sorted set to be sorted.
* @return Command Response - An <code>Array</code> of sorted elements.
*/
public T sort(@NonNull String key) {
ArgsArray commandArgs = buildArgs(key);
protobufTransaction.addCommands(buildCommand(Sort, commandArgs));
return getThis();
}

/**
* Sorts the elements in the list, set, or sorted set at <code>key</code> and returns the result.
* <br>
* The <code>sortReadOnly</code> command can be used to sort elements based on different criteria
* and apply transformations on sorted elements.
*
* @since Redis 7.0 and above.
* @param key The key of the list, set, or sorted set to be sorted.
* @return Command Response - An <code>Array</code> of sorted elements.
*/
public T sortReadOnly(@NonNull String key) {
ArgsArray commandArgs = buildArgs(key);
protobufTransaction.addCommands(buildCommand(SortReadOnly, commandArgs));
return getThis();
}

/**
* Sorts the elements in the list, set, or sorted set at <code>key</code> and stores the result in
* <code>destination</code>. The <code>sort</code> command can be used to sort elements based on
* different criteria, apply transformations on sorted elements, and store the result in a new
* key.<br>
* To get the sort result without storing it into a key, see {@link #sort(String)} or {@link
* #sortReadOnly(String)}.
*
* @param key The key of the list, set, or sorted set to be sorted.
* @param destination The key where the sorted result will be stored.
* @return Command Response - The number of elements in the sorted key stored at <code>destination
* </code>.
*/
public T sortStore(@NonNull String key, @NonNull String destination) {
ArgsArray commandArgs = buildArgs(new String[] {key, STORE_COMMAND_STRING, destination});
protobufTransaction.addCommands(buildCommand(Sort, commandArgs));
return getThis();
}

/**
* Iterates incrementally over a set.
*
* @see <a href="https://valkey.io/commands/sscan">valkey.io</a> for details.
* @param key The key of the set.
* @param cursor The cursor that points to the next iteration of results.
* @return Command Response - An <code>Array</code> of <code>Objects</code>. The first element is
* always the <code>cursor</code> for the next iteration of results. <code>0</code> will be
* the <code>cursor</code> returned on the last iteration of the set. The second element is
* always an <code>Array</code> of the subset of the set held in <code>key</code>.
*/
public T sscan(@NonNull String key, long cursor) {
protobufTransaction.addCommands(buildCommand(SScan, buildArgs(key, Long.toString(cursor))));
return getThis();
}

/**
* Iterates incrementally over a set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public abstract class ScanOptions {
* match the pattern specified. If the set, hash, or list is large enough for scan commands to
* return only a subset of the set, hash, or list, then there could be a case where the result is
* empty although there are items that match the pattern specified. This is due to the default
* <code>
* COUNT</code> being <code>10</code> which indicates that it will only fetch and match <code>10
* </code> items from the list.
* <code>COUNT</code> being <code>10</code> which indicates that it will only fetch and match
* <code>10</code> items from the list.
*/
private final String matchPattern;

Expand Down
12 changes: 10 additions & 2 deletions java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ArrayUtils;
Expand Down Expand Up @@ -7044,8 +7045,9 @@ public void sscan(BaseClient client) {
result = client.sscan(key1, initialCursor).get();
assertEquals(String.valueOf(initialCursor), result[resultCursorIndex]);
assertEquals(charMembers.length, ((Object[]) result[resultCollectionIndex]).length);
Arrays.stream((Object[]) result[resultCollectionIndex])
.forEach(member -> assertTrue(charMemberSet.contains(member)));
final Set<Object> resultMembers =
Arrays.stream((Object[]) result[resultCollectionIndex]).collect(Collectors.toSet());
assertTrue(resultMembers.containsAll(charMemberSet));

result =
client.sscan(key1, initialCursor, SScanOptions.builder().matchPattern("a").build()).get();
Expand All @@ -7055,6 +7057,7 @@ public void sscan(BaseClient client) {
// Result contains a subset of the key
assertEquals(numberMembers.length, client.sadd(key1, numberMembers).get());
long resultCursor = 0;
final Set<Object> secondResultValues = new HashSet<>();
do {
result = client.sscan(key1, resultCursor).get();
resultCursor = Long.valueOf(result[resultCursorIndex].toString());
Expand All @@ -7068,8 +7071,13 @@ public void sscan(BaseClient client) {
Arrays.deepEquals(
ArrayUtils.toArray(result[resultCollectionIndex]),
ArrayUtils.toArray(secondResult[resultCollectionIndex])));
secondResultValues.addAll(
Arrays.stream((Object[]) secondResult[resultCollectionIndex])
.collect(Collectors.toSet()));
} while (resultCursor != 0); // 0 is returned for the cursor of the last iteration.

assertTrue(secondResultValues.containsAll(numberMembersSet));

// Test match pattern
result =
client.sscan(key1, initialCursor, SScanOptions.builder().matchPattern("*").build()).get();
Expand Down

0 comments on commit bf7a91a

Please sign in to comment.