jhutchison commented on a change in pull request #5216:
URL: https://github.com/apache/geode/pull/5216#discussion_r439053138



##########
File path: 
geode-redis/src/main/java/org/apache/geode/redis/internal/executor/string/SetExecutor.java
##########
@@ -35,105 +37,182 @@
   public RedisResponse executeCommandWithResponse(Command command,
       ExecutionHandlerContext context) {
 
-    List<byte[]> commandElems = command.getProcessedCommand();
     ByteArrayWrapper keyToSet = command.getKey();
-    ByteArrayWrapper valueToSet = getValueToSet(commandElems);
-
+    List<byte[]> commandElementsBytes = command.getProcessedCommand();
+    List<byte[]> optionalParameterBytes = 
getOptionalParameters(commandElementsBytes);
+    ByteArrayWrapper valueToSet = getValueToSet(commandElementsBytes);
     RedisStringCommands redisStringCommands = getRedisStringCommands(context);
     SetOptions setOptions;
+
     try {
-      setOptions = parseCommandElems(commandElems);
+      setOptions = parseOptionalParameters(optionalParameterBytes);
     } catch (IllegalArgumentException ex) {
       return RedisResponse.error(ex.getMessage());
     }
 
-    return doSet(command, context, keyToSet, valueToSet, redisStringCommands, 
setOptions);
+    return doSet(keyToSet, valueToSet, redisStringCommands, setOptions);
+  }
+
+  private List<byte[]> getOptionalParameters(List<byte[]> 
commandElementsBytes) {
+    return commandElementsBytes.subList(3, commandElementsBytes.size());
   }
 
-  private RedisResponse doSet(Command command, ExecutionHandlerContext context,
-      ByteArrayWrapper key,
-      ByteArrayWrapper value, RedisStringCommands redisStringCommands, 
SetOptions setOptions) {
+  private RedisResponse doSet(ByteArrayWrapper key,
+      ByteArrayWrapper value,
+      RedisStringCommands redisStringCommands,
+      SetOptions setOptions) {
 
-    boolean result = redisStringCommands.set(key, value, setOptions);
+    boolean setCompletedSuccessfully = redisStringCommands.set(key, value, 
setOptions);
 
-    if (result) {
+    if (setCompletedSuccessfully) {
       return RedisResponse.string(SUCCESS);
+    } else {
+      return RedisResponse.nil();
     }
-
-    return RedisResponse.nil();
   }
 
   private ByteArrayWrapper getValueToSet(List<byte[]> commandElems) {
     byte[] value = commandElems.get(2);
     return new ByteArrayWrapper(value);
   }
 
+  private SetOptions parseOptionalParameters(List<byte[]> 
optionalParameterBytes)
+      throws IllegalArgumentException {
 
-  private SetOptions parseCommandElems(List<byte[]> commandElems) throws 
IllegalArgumentException {
     boolean keepTTL = false;
     SetOptions.Exists existsOption = SetOptions.Exists.NONE;
     long expiration = 0L;
 
-    for (int i = 3; i < commandElems.size(); i++) {
-      String current_arg = 
Coder.bytesToString(commandElems.get(i)).toUpperCase();
-      switch (current_arg) {
-        case "KEEPTTL":
-          keepTTL = true;
-          break;
-        case "EX":
-          if (expiration != 0) {
-            throw new IllegalArgumentException(ERROR_SYNTAX);
-          }
-          i++;
-          expiration = parseExpirationTime(i, commandElems);
-          expiration = SECONDS.toMillis(expiration);
-          break;
-        case "PX":
-          if (expiration != 0) {
-            throw new IllegalArgumentException(ERROR_SYNTAX);
-          }
-          i++;
-          expiration = parseExpirationTime(i, commandElems);
-          break;
-        case "NX":
-          if (existsOption != SetOptions.Exists.NONE) {
-            throw new IllegalArgumentException(ERROR_SYNTAX);
-          }
-          existsOption = SetOptions.Exists.NX;
-          break;
-        case "XX":
-          if (existsOption != SetOptions.Exists.NONE) {
-            throw new IllegalArgumentException(ERROR_SYNTAX);
-          }
-          existsOption = SetOptions.Exists.XX;
-          break;
-        default:
-          throw new IllegalArgumentException(ERROR_SYNTAX);
+    List<String> optionalParametersStrings =
+        optionalParameterBytes.stream()
+            .map(item -> Coder.bytesToString(item).toUpperCase())
+            .collect(Collectors.toList());
+
+    throwExceptionIfUnknownParameter(optionalParametersStrings);
+    throwExceptionIfIncompatableParamaterOptions(optionalParametersStrings);
+    keepTTL = optionalParametersStrings.contains("KEEPTL");

Review comment:
       thanks




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to