dschneider-pivotal commented on a change in pull request #6715:
URL: https://github.com/apache/geode/pull/6715#discussion_r676789061
##########
File path:
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -310,18 +311,62 @@ long zcount(SortedSetRangeOptions rangeOptions) {
return getRange(min, max, withScores, false);
}
+ List<byte[]> zrevrangebyscore(SortedSetRangeOptions rangeOptions, boolean
withScores) {
+ List<byte[]> result = new ArrayList<>();
+ AbstractOrderedSetEntry maxEntry =
+ new DummyOrderedSetEntry(rangeOptions.getStartDouble(),
rangeOptions.isStartExclusive(),
+ false);
+ int maxIndex = scoreSet.indexOf(maxEntry);
+
+ AbstractOrderedSetEntry minEntry =
+ new DummyOrderedSetEntry(rangeOptions.getEndDouble(),
rangeOptions.isEndExclusive(), true);
+ int minIndex = scoreSet.indexOf(minEntry);
+ if (minIndex > getSortedSetSize()) {
+ return Collections.emptyList();
+ }
+
+ if (minIndex == maxIndex) {
+ return Collections.emptyList();
+ }
+
+ // Okay, if we make it this far there's a potential range of things to
return.
+ int count = Integer.MAX_VALUE;
+ if (rangeOptions.hasLimit()) {
+ count = rangeOptions.getCount();
+ maxIndex -= rangeOptions.getOffset();
+ if (maxIndex < 0) {
+ return Collections.emptyList();
+ }
+ }
+ Iterator<AbstractOrderedSetEntry> entryIterator =
+ scoreSet.getIndexRange(maxIndex - 1, Math.min(count, maxIndex -
minIndex), true);
Review comment:
It seems like the values passed to getIndexRange here could also be used
to compute a good initialize size of the ArrayList result. You could allocate
the result here and do it with this better size which will prevent extra
copying of the result ArrayList as it grows as we add to it in the loop.
Remember to make its initial size *2 if withScores. I think Donal has this same
logic in the bylex method.
##########
File path:
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/AbstractZRangeByScoreExecutor.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.RedisConstants.ERROR_SYNTAX;
+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.isNaN;
+import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
+import static org.apache.geode.redis.internal.netty.StringBytesGlossary.bLIMIT;
+import static
org.apache.geode.redis.internal.netty.StringBytesGlossary.bWITHSCORES;
+
+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 abstract class AbstractZRangeByScoreExecutor 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[] startBytes = commandElements.get(2);
+ byte[] endBytes = commandElements.get(3);
+ if (isNaN(startBytes) || isNaN(endBytes)) {
+ return RedisResponse.error(ERROR_MIN_MAX_NOT_A_FLOAT);
+ }
+ rangeOptions = new SortedSetRangeOptions(startBytes, endBytes);
+ } catch (NumberFormatException ex) {
+ return RedisResponse.error(ERROR_MIN_MAX_NOT_A_FLOAT);
+ }
+
+ // Native redis allows multiple "withscores" and "limit ? ?" clauses; the
last "limit"
+ // clause overrides any previous ones
+ if (commandElements.size() >= 5) {
+ int currentCommandElement = 4;
+
+ while (currentCommandElement < commandElements.size()) {
+ try {
+ if (equalsIgnoreCaseBytes(commandElements.get(currentCommandElement),
+ bWITHSCORES)) {
+ withScores = true;
+ currentCommandElement++;
+ } else {
+ parseLimitArguments(rangeOptions, commandElements,
currentCommandElement);
+ currentCommandElement += 3;
+ }
+ } catch (NumberFormatException nfex) {
+ return RedisResponse.error(ERROR_NOT_INTEGER);
+ } catch (IllegalArgumentException iex) {
+ return RedisResponse.error(ERROR_SYNTAX);
+ }
+ }
+ }
+
+ // If the range is empty (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.getStartDouble() == rangeOptions.getEndDouble())
+ && rangeOptions.isStartExclusive() &&
rangeOptions.isEndExclusive()) {
+ return RedisResponse.emptyArray();
+ }
+ // For ZRANGEBYSCORE, min and max are reversed in order; check if limits
are impossible
+ if (isRev() ? (rangeOptions.getStartDouble() < rangeOptions.getEndDouble())
+ : (rangeOptions.getStartDouble() > rangeOptions.getEndDouble())) {
+ return RedisResponse.emptyArray();
+ }
+
+ List<byte[]> result;
+ if (isRev()) {
+ result = redisSortedSetCommands.zrevrangebyscore(command.getKey(),
rangeOptions, withScores);
+ } else {
+ result = redisSortedSetCommands.zrangebyscore(command.getKey(),
rangeOptions, withScores);
+ }
+
+ return RedisResponse.array(result);
+ }
+
+ void parseLimitArguments(SortedSetRangeOptions rangeOptions, List<byte[]>
commandElements,
+ int commandIndex) {
+ int offset;
+ int count;
+ if (equalsIgnoreCaseBytes(commandElements.get(commandIndex), bLIMIT)
+ && commandElements.size() > commandIndex + 2) {
+ offset = narrowLongToInt(bytesToLong(commandElements.get(commandIndex +
1)));
+ count = narrowLongToInt(bytesToLong(commandElements.get(commandIndex +
2)));
+ if (count < 0) {
+ count = Integer.MAX_VALUE;
+ }
+ } else {
+ throw new IllegalArgumentException();
+ }
+ rangeOptions.setLimitValues(offset, count);
Review comment:
shouldn't we only call setLimitValues if we found the bLIMIT keyword? I
guess it works that way (since if we don't find the keyword we throw an
exception). I think the code would be a little cleaner if you declared offset
and count inside the if block as you initialize them and made this call at the
end of the if block
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]