forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java: Add DUMP and RESTORE commands (#371)
* Cherry-pick Java: Add DUMP and RESTORE commands * Fixed codestyle issues * Addressed review comments * Added more test to SharedCommandtests * Change ByteArrayArgumentMatcher to use Arrays.equals() comparison instead * Addressed review comments - added custom setter methods for long values - changed seconds to idletime - minor comments updated * Updated few minor comments * Removed Optional in RestoreOptions and added setter methods for replace and absttl
- Loading branch information
1 parent
0b30286
commit 38e82a0
Showing
8 changed files
with
472 additions
and
0 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
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
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
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
87 changes: 87 additions & 0 deletions
87
java/client/src/main/java/glide/api/models/commands/RestoreOptions.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,87 @@ | ||
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api.models.commands; | ||
|
||
import glide.api.commands.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.*; | ||
|
||
/** | ||
* Optional arguments to {@link GenericBaseCommands#restore(byte[], long, byte[], RestoreOptions)} | ||
* | ||
* @see <a href="https://valkey.io/commands/restore/">valkey.io</a> | ||
*/ | ||
@Getter | ||
@Builder | ||
public final class RestoreOptions { | ||
/** <code>REPLACE</code> subcommand string to replace existing key */ | ||
public static final String REPLACE_REDIS_API = "REPLACE"; | ||
|
||
/** | ||
* <code>ABSTTL</code> subcommand string to represent absolute timestamp (in milliseconds) for TTL | ||
*/ | ||
public static final String ABSTTL_REDIS_API = "ABSTTL"; | ||
|
||
/** <code>IDELTIME</code> subcommand string to set Object Idletime */ | ||
public static final String IDLETIME_REDIS_API = "IDLETIME"; | ||
|
||
/** <code>FREQ</code> subcommand string to set Object Frequency */ | ||
public static final String FREQ_REDIS_API = "FREQ"; | ||
|
||
/** When `true`, it represents <code>REPLACE</code> keyword has been used */ | ||
@Builder.Default private boolean hasReplace = false; | ||
|
||
/** When `true`, it represents <code>ABSTTL</code> keyword has been used */ | ||
@Builder.Default private boolean hasAbsttl = false; | ||
|
||
/** It represents the idletime of object */ | ||
@Builder.Default private Long idletime = null; | ||
|
||
/** It represents the frequency of object */ | ||
@Builder.Default private Long frequency = null; | ||
|
||
/** | ||
* Creates the argument to be used in {@link GenericBaseCommands#restore(byte[], long, byte[], | ||
* RestoreOptions)} | ||
* | ||
* @return a byte array that holds the sub commands and their arguments. | ||
*/ | ||
public List<byte[]> toArgs(byte[] key, long ttl, byte[] value) { | ||
List<byte[]> resultList = new ArrayList<>(); | ||
|
||
resultList.add(key); | ||
resultList.add(Long.toString(ttl).getBytes()); | ||
resultList.add(value); | ||
|
||
if (hasReplace) { | ||
resultList.add(REPLACE_REDIS_API.getBytes()); | ||
} | ||
|
||
if (hasAbsttl) { | ||
resultList.add(ABSTTL_REDIS_API.getBytes()); | ||
} | ||
|
||
if (idletime != null) { | ||
resultList.add(IDLETIME_REDIS_API.getBytes()); | ||
resultList.add(Long.toString(idletime).getBytes()); | ||
} | ||
|
||
if (frequency != null) { | ||
resultList.add(FREQ_REDIS_API.getBytes()); | ||
resultList.add(Long.toString(frequency).getBytes()); | ||
} | ||
|
||
return resultList; | ||
} | ||
|
||
/** Custom setter methods for replace and absttl */ | ||
public static class RestoreOptionsBuilder { | ||
public RestoreOptionsBuilder replace() { | ||
return hasReplace(true); | ||
} | ||
|
||
public RestoreOptionsBuilder absttl() { | ||
return hasAbsttl(true); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
java/client/src/test/java/glide/api/ByteArrayArgumentMatcher.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,47 @@ | ||
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.mockito.ArgumentMatcher; | ||
|
||
/** | ||
* Argument matcher for comparing lists of byte arrays. | ||
* | ||
* <p>It is used in Mockito verifications to assert that a method call was made with a specific list | ||
* of byte arrays as arguments. | ||
*/ | ||
public class ByteArrayArgumentMatcher implements ArgumentMatcher<List<byte[]>> { | ||
List<byte[]> arguments; | ||
|
||
/** | ||
* Constructs a new ByteArrayArgumentMatcher with the provided list of byte arrays. | ||
* | ||
* @param arguments The list of byte arrays to match against. | ||
*/ | ||
public ByteArrayArgumentMatcher(List<byte[]> arguments) { | ||
this.arguments = arguments; | ||
} | ||
|
||
/** | ||
* Matches the provided list of byte arrays against the stored arguments. | ||
* | ||
* @param t The list of byte arrays to match | ||
* @return boolean - true if the provided list of byte arrays matches the stored arguments, false | ||
* Sotherwise. | ||
*/ | ||
@Override | ||
public boolean matches(List<byte[]> t) { | ||
// Check if the sizes of both lists are equal | ||
if (t.size() != arguments.size()) { | ||
return false; | ||
} | ||
|
||
for (int index = 0; index < t.size(); index++) { | ||
if (!Arrays.equals(arguments.get(index), t.get(index))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.