sabbey37 commented on a change in pull request #6489:
URL: https://github.com/apache/geode/pull/6489#discussion_r635365879



##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/ParameterRequirements/ZAddParameterRequirements.java
##########
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache 
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the 
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ */
+
+package org.apache.geode.redis.internal.ParameterRequirements;
+
+import static 
org.apache.geode.redis.internal.RedisConstants.ERROR_INVALID_ZADD_OPTION_NX_XX;
+import static 
org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_A_VALID_FLOAT;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_SYNTAX;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.geode.redis.internal.netty.Coder;
+import org.apache.geode.redis.internal.netty.Command;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+
+public class ZAddParameterRequirements implements ParameterRequirements {
+  @Override
+  public void checkParameters(Command command, ExecutionHandlerContext 
context) {
+    int numberOfArguments = command.getProcessedCommand().size();
+
+    if (numberOfArguments < 4) {
+      throw new 
RedisParametersMismatchException(command.wrongNumberOfArgumentsErrorMessage());
+    }
+
+    int optionsFoundCount = confirmKnownSubcommands(command);
+
+    if ((numberOfArguments - optionsFoundCount - 2) % 2 != 0) {
+      throw new RedisParametersMismatchException(ERROR_SYNTAX);
+    }
+  }
+
+  private int confirmKnownSubcommands(Command command) {
+    int optionsFoundCount = 0;
+    boolean nxFound = false, xxFound = false, gtFound = false, ltFound = false;
+
+    List<byte[]> commandElements = command.getProcessedCommand();
+    Iterator<byte[]> commandIterator = commandElements.iterator();
+    commandIterator.next(); // Skip past command
+    commandIterator.next(); // and key
+
+    boolean scoreFound = false;
+    while (commandIterator.hasNext()) {
+      byte[] subcommand = commandIterator.next();
+      String subCommandString = Coder.bytesToString(subcommand).toLowerCase();
+      switch (subCommandString) {
+        case "ch":
+          break;
+        case "incr":
+          break;
+        case "nx":
+          nxFound = true;
+          break;
+        case "xx":
+          xxFound = true;
+          break;
+        case "gt":
+          gtFound = true;
+          if (nxFound) {
+            throw new 
RedisParametersMismatchException(String.format(ERROR_SYNTAX));
+          }
+          break;
+        case "lt":
+          ltFound = true;
+          if (nxFound) {
+            throw new 
RedisParametersMismatchException(String.format(ERROR_SYNTAX));
+          }

Review comment:
       The error cases for `lt` and `gt` could be eliminated altogether... we 
might need to fix the way we're parsig the arguments though ...the errors for 
`lt` and `gt` come from them not being available at all in Redis 5.x.  .. they 
are treated the same as any other non-viable option.  There are also some 
scenarios that would not work with our current implementation:
   
   one example is the following:
   `zadd key lt 1` returns `ERR value is not a valid float` in Redis (because 
it's trying to get the double value of `lt`), but we return `ERR syntax error`.




-- 
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