|
105 | 105 | import static command_request.CommandRequestOuterClass.RequestType.PExpire; |
106 | 106 | import static command_request.CommandRequestOuterClass.RequestType.PExpireAt; |
107 | 107 | import static command_request.CommandRequestOuterClass.RequestType.PExpireTime; |
| 108 | +import static command_request.CommandRequestOuterClass.RequestType.PSubscribe; |
| 109 | +import static command_request.CommandRequestOuterClass.RequestType.PSubscribeBlocking; |
108 | 110 | import static command_request.CommandRequestOuterClass.RequestType.PTTL; |
| 111 | +import static command_request.CommandRequestOuterClass.RequestType.PUnsubscribe; |
| 112 | +import static command_request.CommandRequestOuterClass.RequestType.PUnsubscribeBlocking; |
109 | 113 | import static command_request.CommandRequestOuterClass.RequestType.Persist; |
110 | 114 | import static command_request.CommandRequestOuterClass.RequestType.PfAdd; |
111 | 115 | import static command_request.CommandRequestOuterClass.RequestType.PfCount; |
|
148 | 152 | import static command_request.CommandRequestOuterClass.RequestType.Sort; |
149 | 153 | import static command_request.CommandRequestOuterClass.RequestType.SortReadOnly; |
150 | 154 | import static command_request.CommandRequestOuterClass.RequestType.Strlen; |
| 155 | +import static command_request.CommandRequestOuterClass.RequestType.Subscribe; |
| 156 | +import static command_request.CommandRequestOuterClass.RequestType.SubscribeBlocking; |
151 | 157 | import static command_request.CommandRequestOuterClass.RequestType.TTL; |
152 | 158 | import static command_request.CommandRequestOuterClass.RequestType.Touch; |
153 | 159 | import static command_request.CommandRequestOuterClass.RequestType.Type; |
154 | 160 | import static command_request.CommandRequestOuterClass.RequestType.Unlink; |
| 161 | +import static command_request.CommandRequestOuterClass.RequestType.Unsubscribe; |
| 162 | +import static command_request.CommandRequestOuterClass.RequestType.UnsubscribeBlocking; |
155 | 163 | import static command_request.CommandRequestOuterClass.RequestType.Wait; |
156 | 164 | import static command_request.CommandRequestOuterClass.RequestType.WaitAof; |
157 | 165 | import static command_request.CommandRequestOuterClass.RequestType.Watch; |
@@ -378,7 +386,7 @@ public enum ResponseFlags { |
378 | 386 | public static final String LCS_MATCHES_RESULT_KEY = "matches"; |
379 | 387 |
|
380 | 388 | // Constant empty arrays to reduce allocations |
381 | | - private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 389 | + protected static final String[] EMPTY_STRING_ARRAY = new String[0]; |
382 | 390 | protected static final GlideString[] EMPTY_GLIDE_STRING_ARRAY = new GlideString[0]; |
383 | 391 |
|
384 | 392 | // Client components |
@@ -6243,6 +6251,119 @@ public CompletableFuture<String> aclWhoami() { |
6243 | 6251 | AclWhoami, EMPTY_STRING_ARRAY, this::handleStringResponse); |
6244 | 6252 | } |
6245 | 6253 |
|
| 6254 | + public CompletableFuture<Void> subscribe(Set<String> channels) { |
| 6255 | + return commandManager.submitNewCommand( |
| 6256 | + Subscribe, channels.toArray(EMPTY_STRING_ARRAY), response -> null); |
| 6257 | + } |
| 6258 | + |
| 6259 | + public CompletableFuture<Void> subscribe(Set<String> channels, int timeoutMs) { |
| 6260 | + if (timeoutMs < 0) { |
| 6261 | + throw new IllegalArgumentException("Timeout must be non-negative, got: " + timeoutMs); |
| 6262 | + } |
| 6263 | + String[] args = new String[channels.size() + 1]; |
| 6264 | + int i = 0; |
| 6265 | + for (String channel : channels) { |
| 6266 | + args[i++] = channel; |
| 6267 | + } |
| 6268 | + args[i] = String.valueOf(timeoutMs); |
| 6269 | + return commandManager.submitNewCommand(SubscribeBlocking, args, response -> null); |
| 6270 | + } |
| 6271 | + |
| 6272 | + public CompletableFuture<Void> psubscribe(Set<String> patterns) { |
| 6273 | + return commandManager.submitNewCommand( |
| 6274 | + PSubscribe, patterns.toArray(EMPTY_STRING_ARRAY), response -> null); |
| 6275 | + } |
| 6276 | + |
| 6277 | + public CompletableFuture<Void> psubscribe(Set<String> patterns, int timeoutMs) { |
| 6278 | + if (timeoutMs < 0) { |
| 6279 | + throw new IllegalArgumentException("Timeout must be non-negative, got: " + timeoutMs); |
| 6280 | + } |
| 6281 | + String[] args = new String[patterns.size() + 1]; |
| 6282 | + int i = 0; |
| 6283 | + for (String pattern : patterns) { |
| 6284 | + args[i++] = pattern; |
| 6285 | + } |
| 6286 | + args[i] = String.valueOf(timeoutMs); |
| 6287 | + return commandManager.submitNewCommand(PSubscribeBlocking, args, response -> null); |
| 6288 | + } |
| 6289 | + |
| 6290 | + public CompletableFuture<Void> unsubscribe() { |
| 6291 | + return commandManager.submitNewCommand(Unsubscribe, EMPTY_STRING_ARRAY, response -> null); |
| 6292 | + } |
| 6293 | + |
| 6294 | + public CompletableFuture<Void> unsubscribe(Set<String> channels) { |
| 6295 | + return commandManager.submitNewCommand( |
| 6296 | + Unsubscribe, channels.toArray(EMPTY_STRING_ARRAY), response -> null); |
| 6297 | + } |
| 6298 | + |
| 6299 | + public CompletableFuture<Void> unsubscribe(Set<String> channels, int timeoutMs) { |
| 6300 | + if (timeoutMs < 0) { |
| 6301 | + throw new IllegalArgumentException("Timeout must be non-negative, got: " + timeoutMs); |
| 6302 | + } |
| 6303 | + String[] args = new String[channels.size() + 1]; |
| 6304 | + int i = 0; |
| 6305 | + for (String channel : channels) { |
| 6306 | + args[i++] = channel; |
| 6307 | + } |
| 6308 | + args[i] = String.valueOf(timeoutMs); |
| 6309 | + return commandManager.submitNewCommand(UnsubscribeBlocking, args, response -> null); |
| 6310 | + } |
| 6311 | + |
| 6312 | + public CompletableFuture<Void> unsubscribe(int timeoutMs) { |
| 6313 | + if (timeoutMs < 0) { |
| 6314 | + throw new IllegalArgumentException("Timeout must be non-negative, got: " + timeoutMs); |
| 6315 | + } |
| 6316 | + return commandManager.submitNewCommand( |
| 6317 | + UnsubscribeBlocking, new String[] {String.valueOf(timeoutMs)}, response -> null); |
| 6318 | + } |
| 6319 | + |
| 6320 | + public CompletableFuture<Void> punsubscribe() { |
| 6321 | + return commandManager.submitNewCommand(PUnsubscribe, EMPTY_STRING_ARRAY, response -> null); |
| 6322 | + } |
| 6323 | + |
| 6324 | + public CompletableFuture<Void> punsubscribe(Set<String> patterns) { |
| 6325 | + return commandManager.submitNewCommand( |
| 6326 | + PUnsubscribe, patterns.toArray(EMPTY_STRING_ARRAY), response -> null); |
| 6327 | + } |
| 6328 | + |
| 6329 | + public CompletableFuture<Void> punsubscribe(Set<String> patterns, int timeoutMs) { |
| 6330 | + if (timeoutMs < 0) { |
| 6331 | + throw new IllegalArgumentException("Timeout must be non-negative, got: " + timeoutMs); |
| 6332 | + } |
| 6333 | + String[] args = new String[patterns.size() + 1]; |
| 6334 | + int i = 0; |
| 6335 | + for (String pattern : patterns) { |
| 6336 | + args[i++] = pattern; |
| 6337 | + } |
| 6338 | + args[i] = String.valueOf(timeoutMs); |
| 6339 | + return commandManager.submitNewCommand(PUnsubscribeBlocking, args, response -> null); |
| 6340 | + } |
| 6341 | + |
| 6342 | + public CompletableFuture<Void> punsubscribe(int timeoutMs) { |
| 6343 | + if (timeoutMs < 0) { |
| 6344 | + throw new IllegalArgumentException("Timeout must be non-negative, got: " + timeoutMs); |
| 6345 | + } |
| 6346 | + return commandManager.submitNewCommand( |
| 6347 | + PUnsubscribeBlocking, new String[] {String.valueOf(timeoutMs)}, response -> null); |
| 6348 | + } |
| 6349 | + |
| 6350 | + protected Object parseSubscriptionState(Object response) { |
| 6351 | + if (!(response instanceof Object[])) { |
| 6352 | + throw new RuntimeException("Invalid response format from GetSubscriptions"); |
| 6353 | + } |
| 6354 | + Object[] arr = (Object[]) response; |
| 6355 | + if (arr.length != 4) { |
| 6356 | + throw new RuntimeException("Invalid response format from GetSubscriptions"); |
| 6357 | + } |
| 6358 | + |
| 6359 | + @SuppressWarnings("unchecked") |
| 6360 | + Map<String, Object[]> desiredMap = (Map<String, Object[]>) arr[1]; |
| 6361 | + @SuppressWarnings("unchecked") |
| 6362 | + Map<String, Object[]> actualMap = (Map<String, Object[]>) arr[3]; |
| 6363 | + |
| 6364 | + return new Object[] {desiredMap, actualMap}; |
| 6365 | + } |
| 6366 | + |
6246 | 6367 | /** |
6247 | 6368 | * Internal method for enqueueing PubSub messages from native callback. This is called by the |
6248 | 6369 | * native layer when PubSub messages are received. |
|
0 commit comments