-
Notifications
You must be signed in to change notification settings - Fork 53
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
LCS + mset + msetnx #1698
LCS + mset + msetnx #1698
Changes from 3 commits
59ef80e
78b03f6
1cfa3c8
eeebcc0
04d3eb8
6f6377b
5123052
4f10f79
bb29ba9
3d12fb6
0fe9721
8167b60
b1073ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import static glide.utils.ArrayTransformUtils.castMapOfArrays; | ||
import static glide.utils.ArrayTransformUtils.concatenateArrays; | ||
import static glide.utils.ArrayTransformUtils.convertMapToKeyValueStringArray; | ||
import static glide.utils.ArrayTransformUtils.convertMapToKeyValueGlideStringArray; | ||
import static glide.utils.ArrayTransformUtils.convertMapToValueKeyStringArray; | ||
import static glide.utils.ArrayTransformUtils.mapGeoDataToArray; | ||
import static redis_request.RedisRequestOuterClass.RequestType.Append; | ||
|
@@ -632,9 +633,24 @@ public CompletableFuture<GlideString[]> mget(@NonNull GlideString[] keys) { | |
} | ||
|
||
@Override | ||
public CompletableFuture<String> mset(@NonNull Map<String, String> keyValueMap) { | ||
String[] args = convertMapToKeyValueStringArray(keyValueMap); | ||
return commandManager.submitNewCommand(MSet, args, this::handleStringResponse); | ||
public <ArgType> CompletableFuture<String> mset(@NonNull Map<ArgType, ArgType> keyValueMap) { | ||
if (keyValueMap.isEmpty()) { | ||
throw new IllegalArgumentException("empty map"); | ||
} | ||
|
||
Comment on lines
+752
to
+755
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can omit this. We have tests where intentionally send to redis an empty map There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I follow. This isn't for testing purpose, if we remove this and get an empty map with mset we'll recieve There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if map is empty - submit command with 0 args. But track arg type to avoid issues. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yury-Fridlyand so basically you want the below code to be enclosed within: if (!keyValueMap.isEmpty()) {
ArgType firstValue = keyValueMap.keySet().iterator().next();
..
} else {
// submit empty array
return commandManager.submitNewCommand(MSet, new String[] {}, this::handleStringResponse);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
ArgType firstValue = keyValueMap.keySet().iterator().next(); | ||
|
||
Object[] args; | ||
if (firstValue instanceof String) { | ||
args = convertMapToKeyValueStringArray((Map<String, ?>) keyValueMap); | ||
return commandManager.submitNewCommand(MSet, (String[]) args, this::handleStringResponse); | ||
} else if (firstValue instanceof GlideString) { | ||
args = convertMapToKeyValueGlideStringArray((Map<GlideString, GlideString>) keyValueMap); | ||
return commandManager.submitNewCommand( | ||
MSet, (GlideString[]) args, this::handleStringResponse); | ||
} else { | ||
throw new IllegalArgumentException("Expected String or GlideString"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you decide to not duplicate - add a test for this case |
||
} | ||
} | ||
|
||
@Override | ||
|
@@ -2648,6 +2664,12 @@ public CompletableFuture<String> lcs(@NonNull String key1, @NonNull String key2) | |
return commandManager.submitNewCommand(LCS, arguments, this::handleStringResponse); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<GlideString> lcs(@NonNull GlideString key1, @NonNull GlideString key2) { | ||
GlideString[] arguments = new GlideString[] {key1, key2}; | ||
return commandManager.submitNewCommand(LCS, arguments, this::handleGlideStringResponse); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Long> lcsLen(@NonNull String key1, @NonNull String key2) { | ||
String[] arguments = new String[] {key1, key2, LEN_REDIS_API}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,7 +263,7 @@ public interface StringBaseCommands { | |
* assert result.equals("OK")); | ||
* }</pre> | ||
*/ | ||
CompletableFuture<String> mset(Map<String, String> keyValueMap); | ||
<ArgType> CompletableFuture<String> mset(Map<ArgType , ArgType> keyValueMap); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you decide to not duplicate - describe what is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, we can't really duplicate these functions. Java's type erasure does not differentiate between: void foo(Map<GlideString, GlideString> m) and void foo(Map<String, String> m) Both have the same signature of
With the second approach, the user gets the "feel" of overloading: he can still call the method with either |
||
|
||
/** | ||
* Sets multiple keys to values if the key does not exist. The operation is atomic, and if one or | ||
|
@@ -536,6 +536,28 @@ public interface StringBaseCommands { | |
*/ | ||
CompletableFuture<String> lcs(String key1, String key2); | ||
|
||
/** | ||
* Returns the longest common subsequence between strings stored at <code>key1</code> and <code> | ||
* key2</code>. | ||
* | ||
* @since Redis 7.0 and above. | ||
* @apiNote When in cluster mode, <code>key1</code> and <code>key2</code> must map to the same | ||
* hash slot. | ||
* @see <a href="https://valkey.io/commands/lcs/">valkey.io</a> for details. | ||
* @param key1 The key that stores the first string. | ||
* @param key2 The key that stores the second string. | ||
* @return A <code>String</code> containing the longest common subsequence between the 2 strings. | ||
* An empty <code>String</code> is returned if the keys do not exist or have no common | ||
* subsequences. | ||
* @example | ||
* <pre>{@code | ||
* // testKey1 = abcd, testKey2 = axcd | ||
* GlideString result = client.lcs(gs("testKey1"), gs("testKey2")).get(); | ||
* assert result.equals("acd"); | ||
Yury-Fridlyand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* }</pre> | ||
*/ | ||
CompletableFuture<GlideString> lcs(GlideString key1, GlideString key2); | ||
|
||
/** | ||
* Returns the length of the longest common subsequence between strings stored at <code>key1 | ||
* </code> and <code>key2</code>. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to duplicate code - as I understand, all APIs with
String
would be removed laterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the functions signature differ only in template input Java doesn't allow such overload. I.E this kind of overloading isn't permitted:
someType foo(Map<String,String> ...);
someType foo(Map<GlideString,GlideString> ...);
That's why i had to resort to templating.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Java's "type erasure" thingi