7
7
8
8
import java .io .Closeable ;
9
9
import java .net .URI ;
10
- import java .util .ArrayList ;
11
10
import java .util .Collections ;
12
11
import java .util .Iterator ;
13
12
import java .util .LinkedHashSet ;
@@ -40,6 +39,8 @@ public class Jedis implements ServerCommands, DatabaseCommands, JedisCommands, J
40
39
private final RedisCommandObjects commandObjects = new RedisCommandObjects ();
41
40
private int db = 0 ;
42
41
private Transaction transaction = null ;
42
+ private boolean isInMulti = false ;
43
+ private boolean isInWatch = false ;
43
44
private Pipeline pipeline = null ;
44
45
protected static final byte [][] DUMMY_ARRAY = new byte [0 ][];
45
46
@@ -69,7 +70,7 @@ public Jedis(final String uriString) {
69
70
}
70
71
71
72
public Jedis (final HostAndPort hp ) {
72
- this ( hp , DefaultJedisClientConfig . builder (). build () );
73
+ connection = new Connection ( hp );
73
74
}
74
75
75
76
public Jedis (final String host , final int port ) {
@@ -302,7 +303,26 @@ public Jedis(final Connection connection) {
302
303
303
304
@ Override
304
305
public String toString () {
305
- return "BinaryJedis{" + connection + '}' ;
306
+ return "Jedis{" + connection + '}' ;
307
+ }
308
+
309
+ // Legacy
310
+ public Connection getClient () {
311
+ return getConnection ();
312
+ }
313
+
314
+ public Connection getConnection () {
315
+ return connection ;
316
+ }
317
+
318
+ // Legacy
319
+ public void connect () {
320
+ connection .connect ();
321
+ }
322
+
323
+ // Legacy
324
+ public void disconnect () {
325
+ connection .disconnect ();
306
326
}
307
327
308
328
public boolean isConnected () {
@@ -324,6 +344,11 @@ public void resetState() {
324
344
}
325
345
//
326
346
// connection.resetState();
347
+ if (isInWatch ) {
348
+ connection .sendCommand (UNWATCH );
349
+ connection .getStatusCodeReply ();
350
+ isInWatch = false ;
351
+ }
327
352
}
328
353
329
354
transaction = null ;
@@ -349,12 +374,33 @@ public void close() {
349
374
}
350
375
}
351
376
352
- public Connection getConnection () {
353
- return connection ;
377
+ // Legacy
378
+ public Transaction multi () {
379
+ // client.multi();
380
+ // client.getOne(); // expected OK
381
+ // transaction = new Transaction(client);
382
+ transaction = new Transaction (this );
383
+ return transaction ;
354
384
}
355
385
356
- public Connection getClient () {
357
- return getConnection ();
386
+ // Legacy
387
+ public Pipeline pipelined () {
388
+ // pipeline = new Pipeline();
389
+ // pipeline.setClient(connection);
390
+ pipeline = new Pipeline (this );
391
+ return pipeline ;
392
+ }
393
+
394
+ // Legacy
395
+ protected void checkIsInMultiOrPipeline () {
396
+ // if (connection.isInMulti()) {
397
+ if (transaction != null ) {
398
+ throw new IllegalStateException (
399
+ "Cannot use Jedis when in Multi. Please use Transaction or reset jedis state." );
400
+ } else if (pipeline != null && pipeline .hasPipelinedResponse ()) {
401
+ throw new IllegalStateException (
402
+ "Cannot use Jedis when in Pipeline. Please use Pipeline or reset jedis state." );
403
+ }
358
404
}
359
405
360
406
public int getDB () {
@@ -483,6 +529,7 @@ public String quit() {
483
529
connection .sendCommand (QUIT );
484
530
String quitReturn = connection .getStatusCodeReply ();
485
531
connection .disconnect ();
532
+ connection .setBroken ();
486
533
return quitReturn ;
487
534
}
488
535
@@ -2189,35 +2236,13 @@ public Set<Tuple> zpopmin(final byte[] key, final int count) {
2189
2236
return connection .executeCommand (commandObjects .zpopmin (key , count ));
2190
2237
}
2191
2238
2192
- public Pipeline pipelined () {
2193
- // pipeline = new Pipeline();
2194
- // pipeline.setClient(connection);
2195
- pipeline = new Pipeline (connection );
2196
- return pipeline ;
2197
- }
2198
-
2199
- public Transaction multi () {
2200
- connection .sendCommand (MULTI );
2201
- connection .getOne (); // expected OK
2202
- transaction = new Transaction (connection );
2203
- return transaction ;
2204
- }
2205
-
2206
- protected void checkIsInMultiOrPipeline () {
2207
- // if (connection.isInMulti()) {
2208
- if (transaction != null ) {
2209
- throw new IllegalStateException (
2210
- "Cannot use Jedis when in Multi. Please use Transaction or reset jedis state." );
2211
- } else if (pipeline != null && pipeline .hasPipelinedResponse ()) {
2212
- throw new IllegalStateException (
2213
- "Cannot use Jedis when in Pipeline. Please use Pipeline or reset jedis state." );
2214
- }
2215
- }
2216
-
2217
2239
public String watch (final byte []... keys ) {
2218
2240
checkIsInMultiOrPipeline ();
2219
2241
connection .sendCommand (WATCH , keys );
2220
- return connection .getStatusCodeReply ();
2242
+ // return connection.getStatusCodeReply();
2243
+ String status = connection .getStatusCodeReply ();
2244
+ isInWatch = true ;
2245
+ return status ;
2221
2246
}
2222
2247
2223
2248
public String unwatch () {
@@ -3260,6 +3285,7 @@ public String shutdown() throws JedisException {
3260
3285
} catch (JedisConnectionException jce ) {
3261
3286
// expected
3262
3287
status = null ;
3288
+ connection .setBroken ();
3263
3289
}
3264
3290
return status ;
3265
3291
}
@@ -3271,6 +3297,7 @@ public void shutdown(final SaveMode saveMode) throws JedisException {
3271
3297
throw new JedisException (connection .getStatusCodeReply ());
3272
3298
} catch (JedisConnectionException jce ) {
3273
3299
// expected
3300
+ connection .setBroken ();
3274
3301
}
3275
3302
}
3276
3303
@@ -3751,19 +3778,19 @@ public List<Object> slowlogGetBinary(final long entries) {
3751
3778
3752
3779
@ Override
3753
3780
public Long objectRefcount (final byte [] key ) {
3754
- connection .sendCommand (OBJECT , REFCOUNT );
3781
+ connection .sendCommand (OBJECT , REFCOUNT . getRaw (), key );
3755
3782
return connection .getIntegerReply ();
3756
3783
}
3757
3784
3758
3785
@ Override
3759
3786
public byte [] objectEncoding (final byte [] key ) {
3760
- connection .sendCommand (OBJECT , ENCODING );
3787
+ connection .sendCommand (OBJECT , ENCODING . getRaw (), key );
3761
3788
return connection .getBinaryBulkReply ();
3762
3789
}
3763
3790
3764
3791
@ Override
3765
3792
public Long objectIdletime (final byte [] key ) {
3766
- connection .sendCommand (OBJECT , IDLETIME );
3793
+ connection .sendCommand (OBJECT , IDLETIME . getRaw (), key );
3767
3794
return connection .getIntegerReply ();
3768
3795
}
3769
3796
@@ -3775,7 +3802,7 @@ public List<byte[]> objectHelpBinary() {
3775
3802
3776
3803
@ Override
3777
3804
public Long objectFreq (final byte [] key ) {
3778
- connection .sendCommand (OBJECT , FREQ );
3805
+ connection .sendCommand (OBJECT , FREQ . getRaw (), key );
3779
3806
return connection .getIntegerReply ();
3780
3807
}
3781
3808
@@ -3886,13 +3913,20 @@ public Long memoryUsage(final byte[] key) {
3886
3913
@ Override
3887
3914
public Long memoryUsage (final byte [] key , final int samples ) {
3888
3915
checkIsInMultiOrPipeline ();
3889
- connection .sendCommand (MEMORY , USAGE .getRaw (), key , toByteArray (samples ));
3916
+ connection .sendCommand (MEMORY , USAGE .getRaw (), key , SAMPLES . getRaw (), toByteArray (samples ));
3890
3917
return connection .getIntegerReply ();
3891
3918
}
3892
3919
3893
3920
@ Override
3894
3921
public String failover () {
3895
- return failover (null );
3922
+ checkIsInMultiOrPipeline ();
3923
+ connection .sendCommand (FAILOVER );
3924
+ connection .setTimeoutInfinite ();
3925
+ try {
3926
+ return connection .getStatusCodeReply ();
3927
+ } finally {
3928
+ connection .rollbackTimeout ();
3929
+ }
3896
3930
}
3897
3931
3898
3932
@ Override
@@ -4142,7 +4176,7 @@ public String migrate(final String host, final int port, final int destinationDB
4142
4176
final int timeout , final MigrateParams params , final byte []... keys ) {
4143
4177
checkIsInMultiOrPipeline ();
4144
4178
CommandArguments args = new CommandArguments (MIGRATE ).add (host ).add (port ).add (new byte [0 ]).add (destinationDB )
4145
- .add (timeout ).addParams (params ).keys ((Object []) keys );
4179
+ .add (timeout ).addParams (params ).add ( Keyword . KEYS ). keys ((Object []) keys );
4146
4180
connection .sendCommand (args );
4147
4181
return connection .getStatusCodeReply ();
4148
4182
}
@@ -6250,7 +6284,10 @@ public Set<Tuple> zpopmin(final String key, final int count) {
6250
6284
public String watch (final String ... keys ) {
6251
6285
checkIsInMultiOrPipeline ();
6252
6286
connection .sendCommand (WATCH , keys );
6253
- return connection .getStatusCodeReply ();
6287
+ // return connection.getStatusCodeReply();
6288
+ String status = connection .getStatusCodeReply ();
6289
+ isInWatch = true ;
6290
+ return status ;
6254
6291
}
6255
6292
6256
6293
/**
@@ -7490,19 +7527,19 @@ public List<Slowlog> slowlogGet(final long entries) {
7490
7527
7491
7528
@ Override
7492
7529
public Long objectRefcount (final String key ) {
7493
- connection .sendCommand (OBJECT , REFCOUNT );
7530
+ connection .sendCommand (OBJECT , REFCOUNT . name (), key );
7494
7531
return connection .getIntegerReply ();
7495
7532
}
7496
7533
7497
7534
@ Override
7498
7535
public String objectEncoding (final String key ) {
7499
- connection .sendCommand (OBJECT , ENCODING );
7536
+ connection .sendCommand (OBJECT , ENCODING . name (), key );
7500
7537
return connection .getBulkReply ();
7501
7538
}
7502
7539
7503
7540
@ Override
7504
7541
public Long objectIdletime (final String key ) {
7505
- connection .sendCommand (OBJECT , IDLETIME );
7542
+ connection .sendCommand (OBJECT , IDLETIME . name (), key );
7506
7543
return connection .getIntegerReply ();
7507
7544
}
7508
7545
@@ -7514,7 +7551,7 @@ public List<String> objectHelp() {
7514
7551
7515
7552
@ Override
7516
7553
public Long objectFreq (final String key ) {
7517
- connection .sendCommand (OBJECT , FREQ );
7554
+ connection .sendCommand (OBJECT , FREQ . name (), key );
7518
7555
return connection .getIntegerReply ();
7519
7556
}
7520
7557
@@ -7900,7 +7937,7 @@ public String clientList() {
7900
7937
@ Override
7901
7938
public String clientList (ClientType type ) {
7902
7939
checkIsInMultiOrPipeline ();
7903
- connection .sendCommand (CLIENT , LIST .getRaw (), type .getRaw ());
7940
+ connection .sendCommand (CLIENT , LIST .getRaw (), Keyword . TYPE . getRaw (), type .getRaw ());
7904
7941
return connection .getBulkReply ();
7905
7942
}
7906
7943
@@ -7939,7 +7976,7 @@ public String migrate(final String host, final int port, final int destinationDB
7939
7976
final int timeout , final MigrateParams params , final String ... keys ) {
7940
7977
checkIsInMultiOrPipeline ();
7941
7978
CommandArguments args = new CommandArguments (MIGRATE ).add (host ).add (port ).add (new byte [0 ]).add (destinationDB )
7942
- .add (timeout ).addParams (params ).keys ((Object []) keys );
7979
+ .add (timeout ).addParams (params ).add ( Keyword . KEYS ). keys ((Object []) keys );
7943
7980
connection .sendCommand (args );
7944
7981
return connection .getStatusCodeReply ();
7945
7982
}
0 commit comments