You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@geode.apache.org by GitBox <gi...@apache.org> on 2021/07/15 21:38:36 UTC

[GitHub] [geode] dschneider-pivotal commented on a change in pull request #6700: GEODE-9378: Implement ZRANGEBYSCORE

dschneider-pivotal commented on a change in pull request #6700:
URL: https://github.com/apache/geode/pull/6700#discussion_r670809077



##########
File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSetCommandsFunctionExecutor.java
##########
@@ -62,6 +62,13 @@ public long zcount(RedisKey key, SortedSetRangeOptions rangeOptions) {
         () -> getRedisSortedSet(key, true).zrange(min, max, withScores));
   }
 
+  @Override
+  public List<byte[]> zrangebyscore(RedisKey key, SortedSetRangeOptions rangeOptions,
+      boolean withScores) {
+    return stripedExecute(key,
+        () -> getRedisSortedSet(key, true).zrangebyscore(rangeOptions, withScores));

Review comment:
       I noticed you are calling getRedisSortedSet with updatesStats set to true. This seems reasonable. But I also noticed that a bunch of other methods on this class (zadd, zincrby, zrange, zrem) call it with false. Do you know when to set it to true vs false?

##########
File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -307,6 +308,57 @@ long zcount(SortedSetRangeOptions rangeOptions) {
     return getRange(min, max, withScores, false);
   }
 
+
+  List<byte[]> zrangebyscore(SortedSetRangeOptions rangeOptions, boolean withScores) {
+    List<byte[]> result = new ArrayList<>();
+    AbstractOrderedSetEntry minEntry =
+        new DummyOrderedSetEntry(rangeOptions.getMinDouble(), rangeOptions.isMinExclusive(), true);
+    long minIndex = scoreSet.indexOf(minEntry);

Review comment:
       why is minIndex and maxIndex typed as long? indexOf returns an int and later we cast it to an int. Seems like if you typed it as "int" you could get rid of the cast later.

##########
File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZRangeByScoreExecutor.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.executor.sortedset;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_MIN_MAX_NOT_A_FLOAT;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.netty.Coder.bytesToLong;
+import static org.apache.geode.redis.internal.netty.Coder.equalsIgnoreCaseBytes;
+import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
+import static org.apache.geode.redis.internal.netty.StringBytesGlossary.bRADISH_LIMIT;
+import static org.apache.geode.redis.internal.netty.StringBytesGlossary.bRADISH_WITHSCORES;
+
+import java.util.List;
+
+import org.apache.geode.redis.internal.executor.AbstractExecutor;
+import org.apache.geode.redis.internal.executor.RedisResponse;
+import org.apache.geode.redis.internal.netty.Command;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+
+public class ZRangeByScoreExecutor extends AbstractExecutor {
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) {
+    RedisSortedSetCommands redisSortedSetCommands = context.getSortedSetCommands();
+
+    List<byte[]> commandElements = command.getProcessedCommand();
+
+    SortedSetRangeOptions rangeOptions;
+    boolean withScores = false;
+
+    try {
+      byte[] minBytes = commandElements.get(2);
+      byte[] maxBytes = commandElements.get(3);
+      rangeOptions = new SortedSetRangeOptions(minBytes, maxBytes);
+    } catch (NumberFormatException ex) {
+      return RedisResponse.error(ERROR_MIN_MAX_NOT_A_FLOAT);
+    }
+
+    if (commandElements.size() >= 5) {
+      int currentCommandElement = 4;
+      while (currentCommandElement < commandElements.size()) {
+        try {
+          if (equalsIgnoreCaseBytes(commandElements.get(currentCommandElement),
+              bRADISH_WITHSCORES)) {
+            withScores = true;
+            currentCommandElement++;
+          } else {
+            parseLimitArguments(rangeOptions, commandElements, currentCommandElement);
+            currentCommandElement += 3;
+          }
+        } catch (NumberFormatException ex) {
+          return RedisResponse.error(ERROR_NOT_INTEGER);
+        } catch (Exception e) {
+          return RedisResponse.error(ERROR_MIN_MAX_NOT_A_FLOAT);
+        }
+      }
+    }
+
+    // If the range is empty (min > max or min == max and both are exclusive), or
+    // limit specified but count is zero, return early
+    if ((rangeOptions.hasLimit() && (rangeOptions.getCount() == 0 || rangeOptions.getOffset() < 0))
+        ||
+        rangeOptions.getMinDouble() > rangeOptions.getMaxDouble() ||
+        (rangeOptions.getMinDouble() == rangeOptions.getMaxDouble())
+            && rangeOptions.isMinExclusive() && rangeOptions.isMaxExclusive()) {
+      return RedisResponse.emptyArray();
+    }
+
+    List<byte[]> result =
+        redisSortedSetCommands.zrangebyscore(command.getKey(), rangeOptions, withScores);
+
+    return RedisResponse.array(result);
+  }
+
+  void parseLimitArguments(SortedSetRangeOptions rangeOptions, List<byte[]> commandElements,
+      int commandIndex)
+      throws Exception {
+    int offset;
+    int count;
+    if (equalsIgnoreCaseBytes(commandElements.get(commandIndex), bRADISH_LIMIT)) {
+      offset = narrowLongToInt(bytesToLong(commandElements.get(commandIndex + 1)));
+      count = narrowLongToInt(bytesToLong(commandElements.get(commandIndex + 2)));
+      if (count < 0) {
+        count = Integer.MAX_VALUE;
+      }
+    } else {
+      throw new Exception();

Review comment:
       can you make this a more specific exception? When the higher level catches Exception it could be catching all kinds of things (like NullPointerException) and reporting them as something else




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

To unsubscribe, e-mail: notifications-unsubscribe@geode.apache.org

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