37
37
import io .lettuce .core .protocol .Command ;
38
38
import io .lettuce .core .protocol .CommandArgs ;
39
39
import io .lettuce .core .protocol .CommandType ;
40
+ import io .lettuce .core .protocol .ProtocolKeyword ;
40
41
import io .lettuce .core .pubsub .StatefulRedisPubSubConnection ;
41
42
import io .lettuce .core .sentinel .api .StatefulRedisSentinelConnection ;
42
43
import lombok .RequiredArgsConstructor ;
43
44
44
45
import java .lang .reflect .Constructor ;
46
+ import java .nio .charset .StandardCharsets ;
45
47
import java .util .ArrayList ;
46
48
import java .util .Collections ;
47
49
import java .util .HashMap ;
@@ -405,7 +407,7 @@ public Object execute(String command, @Nullable CommandOutput commandOutputTypeH
405
407
try {
406
408
407
409
String name = command .trim ().toUpperCase ();
408
- CommandType commandType = CommandType . valueOf (name );
410
+ ProtocolKeyword commandType = getCommandType (name );
409
411
410
412
validateCommandIfRunningInTransactionMode (commandType , args );
411
413
@@ -1046,14 +1048,14 @@ io.lettuce.core.ScanCursor getScanCursor(long cursorId) {
1046
1048
return io .lettuce .core .ScanCursor .of (Long .toString (cursorId ));
1047
1049
}
1048
1050
1049
- private void validateCommandIfRunningInTransactionMode (CommandType cmd , byte []... args ) {
1051
+ private void validateCommandIfRunningInTransactionMode (ProtocolKeyword cmd , byte []... args ) {
1050
1052
1051
1053
if (this .isQueueing ()) {
1052
1054
validateCommand (cmd , args );
1053
1055
}
1054
1056
}
1055
1057
1056
- private void validateCommand (CommandType cmd , @ Nullable byte []... args ) {
1058
+ private void validateCommand (ProtocolKeyword cmd , @ Nullable byte []... args ) {
1057
1059
1058
1060
RedisCommand redisCommand = RedisCommand .failsafeCommandLookup (cmd .name ());
1059
1061
if (!RedisCommand .UNKNOWN .equals (redisCommand ) && redisCommand .requiresArguments ()) {
@@ -1106,6 +1108,15 @@ LettuceConnectionProvider getConnectionProvider() {
1106
1108
return connectionProvider ;
1107
1109
}
1108
1110
1111
+ private static ProtocolKeyword getCommandType (String name ) {
1112
+
1113
+ try {
1114
+ return CommandType .valueOf (name );
1115
+ } catch (IllegalArgumentException e ) {
1116
+ return new CustomCommandType (name );
1117
+ }
1118
+ }
1119
+
1109
1120
/**
1110
1121
* {@link TypeHints} provide {@link CommandOutput} information for a given {@link CommandType}.
1111
1122
*
@@ -1114,7 +1125,7 @@ LettuceConnectionProvider getConnectionProvider() {
1114
1125
static class TypeHints {
1115
1126
1116
1127
@ SuppressWarnings ("rawtypes" ) //
1117
- private static final Map <CommandType , Class <? extends CommandOutput >> COMMAND_OUTPUT_TYPE_MAPPING = new HashMap <>();
1128
+ private static final Map <ProtocolKeyword , Class <? extends CommandOutput >> COMMAND_OUTPUT_TYPE_MAPPING = new HashMap <>();
1118
1129
1119
1130
@ SuppressWarnings ("rawtypes" ) //
1120
1131
private static final Map <Class <?>, Constructor <CommandOutput >> CONSTRUCTORS = new ConcurrentHashMap <>();
@@ -1275,7 +1286,7 @@ static class TypeHints {
1275
1286
* @return {@link ByteArrayOutput} as default when no matching {@link CommandOutput} available.
1276
1287
*/
1277
1288
@ SuppressWarnings ("rawtypes" )
1278
- public CommandOutput getTypeHint (CommandType type ) {
1289
+ public CommandOutput getTypeHint (ProtocolKeyword type ) {
1279
1290
return getTypeHint (type , new ByteArrayOutput <>(CODEC ));
1280
1291
}
1281
1292
@@ -1286,7 +1297,7 @@ public CommandOutput getTypeHint(CommandType type) {
1286
1297
* @return
1287
1298
*/
1288
1299
@ SuppressWarnings ("rawtypes" )
1289
- public CommandOutput getTypeHint (CommandType type , CommandOutput defaultType ) {
1300
+ public CommandOutput getTypeHint (ProtocolKeyword type , CommandOutput defaultType ) {
1290
1301
1291
1302
if (type == null || !COMMAND_OUTPUT_TYPE_MAPPING .containsKey (type )) {
1292
1303
return defaultType ;
@@ -1523,4 +1534,49 @@ public void onClose(StatefulConnection<?, ?> connection) {
1523
1534
connection .setAutoFlushCommands (true );
1524
1535
}
1525
1536
}
1537
+
1538
+ /**
1539
+ * @since 2.3.8
1540
+ */
1541
+ static class CustomCommandType implements ProtocolKeyword {
1542
+
1543
+ private final String name ;
1544
+
1545
+ CustomCommandType (String name ) {
1546
+ this .name = name ;
1547
+ }
1548
+
1549
+ @ Override
1550
+ public byte [] getBytes () {
1551
+ return name .getBytes (StandardCharsets .US_ASCII );
1552
+ }
1553
+
1554
+ @ Override
1555
+ public String name () {
1556
+ return name ;
1557
+ }
1558
+
1559
+ @ Override
1560
+ public boolean equals (Object o ) {
1561
+
1562
+ if (this == o ) {
1563
+ return true ;
1564
+ }
1565
+ if (!(o instanceof CustomCommandType )) {
1566
+ return false ;
1567
+ }
1568
+ CustomCommandType that = (CustomCommandType ) o ;
1569
+ return ObjectUtils .nullSafeEquals (name , that .name );
1570
+ }
1571
+
1572
+ @ Override
1573
+ public int hashCode () {
1574
+ return ObjectUtils .nullSafeHashCode (name );
1575
+ }
1576
+
1577
+ @ Override
1578
+ public String toString () {
1579
+ return name ;
1580
+ }
1581
+ }
1526
1582
}
0 commit comments