|
1 | 1 | package redis.clients.jedis; |
2 | 2 |
|
3 | | -import java.util.Arrays; |
4 | | -import java.util.HashSet; |
5 | 3 | import redis.clients.jedis.exceptions.JedisValidationException; |
6 | 4 |
|
| 5 | +/** |
| 6 | + * Configuration for CLIENT SETINFO command behaviour. |
| 7 | + * <p> |
| 8 | + * This class supports two modes of operation: |
| 9 | + * <ul> |
| 10 | + * <li>Advanced mode: Using {@link #withLibNameSuffix(String)} for advanced suffix customization, |
| 11 | + * where the provided string is already preformatted according to the rules of the `CLIENT SETINFO` |
| 12 | + * command</li> |
| 13 | + * <li>Simple mode: Using {@link #ClientSetInfoConfig(DriverInfo)} used when the command parameter |
| 14 | + * will be built by the driver based on the {@link DriverInfo} provided</li> |
| 15 | + * </ul> |
| 16 | + * <p> |
| 17 | + * For backward compatibility, {@link #getUpstreamDrivers()} returns the upstream drivers string |
| 18 | + * when using driver info mode. |
| 19 | + * @see DriverInfo |
| 20 | + * @see <a href="https://redis.io/docs/latest/commands/client-setinfo/">CLIENT SETINFO</a> |
| 21 | + */ |
7 | 22 | public final class ClientSetInfoConfig { |
8 | 23 |
|
9 | 24 | private final boolean disabled; |
10 | 25 |
|
11 | | - private final String libNameSuffix; |
| 26 | + private final DriverInfo driverInfo; |
12 | 27 |
|
| 28 | + /** |
| 29 | + * Creates a new ClientSetInfoConfig with default settings. |
| 30 | + * <p> |
| 31 | + * The default configuration uses the "jedis" library name without any upstream drivers. |
| 32 | + */ |
13 | 33 | public ClientSetInfoConfig() { |
14 | | - this(false, null); |
| 34 | + this(false); |
15 | 35 | } |
16 | 36 |
|
| 37 | + /** |
| 38 | + * Creates a new ClientSetInfoConfig with the specified disabled state. |
| 39 | + * <p> |
| 40 | + * When disabled, the CLIENT SETINFO command will not be sent to Redis. |
| 41 | + * @param disabled {@code true} to disable CLIENT SETINFO, {@code false} otherwise |
| 42 | + */ |
17 | 43 | public ClientSetInfoConfig(boolean disabled) { |
18 | | - this(disabled, null); |
| 44 | + this.disabled = disabled; |
| 45 | + this.driverInfo = DriverInfo.builder().build(); |
19 | 46 | } |
20 | 47 |
|
21 | 48 | /** |
22 | | - * @param libNameSuffix must not have braces ({@code ()[]{}}) and spaces will be replaced with hyphens |
| 49 | + * Creates a new ClientSetInfoConfig with a library name suffix. |
| 50 | + * <p> |
| 51 | + * This constructor is for legacy compatibility. The suffix will be appended to "jedis" in |
| 52 | + * parentheses, resulting in a format like: {@code jedis(suffix)}. |
| 53 | + * <p> |
| 54 | + * For adding upstream driver information, use {@link #ClientSetInfoConfig(DriverInfo)} with a |
| 55 | + * {@link DriverInfo} that has upstream drivers. |
| 56 | + * @param libNameSuffix the suffix to append to "jedis" (will be placed in parentheses) |
| 57 | + * @throws JedisValidationException if libNameSuffix contains braces |
23 | 58 | */ |
24 | 59 | public ClientSetInfoConfig(String libNameSuffix) { |
25 | | - this(false, libNameSuffix); |
| 60 | + this.disabled = false; |
| 61 | + this.driverInfo = DriverInfo.builder().addUpstreamDriver(libNameSuffix).build(); |
26 | 62 | } |
27 | 63 |
|
28 | | - private ClientSetInfoConfig(boolean disabled, String libNameSuffix) { |
29 | | - this.disabled = disabled; |
30 | | - this.libNameSuffix = validateLibNameSuffix(libNameSuffix); |
31 | | - } |
32 | | - |
33 | | - private static final HashSet<Character> BRACES = new HashSet<>(Arrays.asList('(', ')', '[', ']', '{', '}')); |
34 | | - |
35 | | - private static String validateLibNameSuffix(String suffix) { |
36 | | - if (suffix == null || suffix.trim().isEmpty()) { |
37 | | - return null; |
38 | | - } |
39 | | - |
40 | | - for (int i = 0; i < suffix.length(); i++) { |
41 | | - char c = suffix.charAt(i); |
42 | | - if (c < ' ' || c > '~' || BRACES.contains(c)) { |
43 | | - throw new JedisValidationException("lib-name suffix cannot contain braces, newlines or " |
44 | | - + "special characters."); |
45 | | - } |
| 64 | + /** |
| 65 | + * Creates a new ClientSetInfoConfig with the specified driver information. |
| 66 | + * <p> |
| 67 | + * This is the recommended constructor for setting up driver information with upstream drivers. |
| 68 | + * The driver information can optionally override the library name completely. |
| 69 | + * @param driverInfo the driver information, must not be {@code null} |
| 70 | + * @throws JedisValidationException if driverInfo is {@code null} |
| 71 | + */ |
| 72 | + public ClientSetInfoConfig(DriverInfo driverInfo) { |
| 73 | + if (driverInfo == null) { |
| 74 | + throw new JedisValidationException("DriverInfo must not be null"); |
46 | 75 | } |
47 | | - |
48 | | - return suffix.replaceAll("\\s", "-"); |
| 76 | + this.disabled = false; |
| 77 | + this.driverInfo = driverInfo; |
49 | 78 | } |
50 | 79 |
|
51 | | - public final boolean isDisabled() { |
| 80 | + /** |
| 81 | + * @return {@code true} if CLIENT SETINFO is disabled, {@code false} otherwise |
| 82 | + */ |
| 83 | + public boolean isDisabled() { |
52 | 84 | return disabled; |
53 | 85 | } |
54 | 86 |
|
55 | | - public final String getLibNameSuffix() { |
56 | | - return libNameSuffix; |
| 87 | + /** |
| 88 | + * @return the driver information |
| 89 | + */ |
| 90 | + public DriverInfo getDriverInfo() { |
| 91 | + return driverInfo; |
57 | 92 | } |
58 | 93 |
|
| 94 | + /** |
| 95 | + * Returns the formatted upstream drivers string. |
| 96 | + * <p> |
| 97 | + * Multiple drivers are separated by semicolons, with the most recently added driver appearing |
| 98 | + * first. |
| 99 | + * <p> |
| 100 | + * Examples: |
| 101 | + * <ul> |
| 102 | + * <li>{@code "spring-data-redis_v3.2.0"} - single upstream driver</li> |
| 103 | + * <li>{@code "lettuce-core_v6.4.1;spring-data-redis_v3.2.0"} - multiple upstream drivers</li> |
| 104 | + * </ul> |
| 105 | + * @return the formatted upstream drivers string, or {@code null} if no upstream drivers are set |
| 106 | + */ |
| 107 | + public String getUpstreamDrivers() { |
| 108 | + return driverInfo.getUpstreamDrivers(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Default configuration that uses the Jedis library name without any upstream drivers. |
| 113 | + */ |
59 | 114 | public static final ClientSetInfoConfig DEFAULT = new ClientSetInfoConfig(); |
60 | 115 |
|
| 116 | + /** |
| 117 | + * Configuration that disables CLIENT SETINFO command. |
| 118 | + */ |
61 | 119 | public static final ClientSetInfoConfig DISABLED = new ClientSetInfoConfig(true); |
62 | 120 |
|
63 | 121 | /** |
64 | | - * @param suffix must not have braces ({@code ()[]{}}) and spaces will be replaced with hyphens |
65 | | - * @return config |
| 122 | + * Creates a new ClientSetInfoConfig with a library name suffix. |
| 123 | + * <p> |
| 124 | + * This is the legacy method for simple name customization. The provided suffix will be appended |
| 125 | + * to "jedis" in parentheses, resulting in a format like: {@code jedis(suffix)}. For adding |
| 126 | + * upstream driver information, use {@link #ClientSetInfoConfig(DriverInfo)} with a * |
| 127 | + * {@link DriverInfo} that has upstream drivers. |
| 128 | + * @param suffix the suffix to append to "jedis" (will be placed in parentheses) |
| 129 | + * @return a new ClientSetInfoConfig with the library name suffix |
66 | 130 | */ |
67 | 131 | public static ClientSetInfoConfig withLibNameSuffix(String suffix) { |
68 | 132 | return new ClientSetInfoConfig(suffix); |
|
0 commit comments