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



##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -607,6 +669,87 @@ private void addIfMatching(GlobPattern matchPattern, 
List<byte[]> resultList, by
     }
   }
 
+  private void computeIntersection(List<RedisSortedSet> sets, ZAggregator 
aggregator) {
+    RedisSortedSet retVal = new RedisSortedSet(Collections.emptyList(), new 
double[] {});
+    RedisSortedSet smallestSet = sets.get(0);
+
+    for (RedisSortedSet set : sets) {
+      if (set.getSortedSetSize() < smallestSet.getSortedSetSize()) {
+        smallestSet = set;
+      }
+    }
+
+    for (byte[] member : smallestSet.members.keySet()) {
+      Double newScore;
+      if (aggregator.equals(ZAggregator.SUM)) {
+        newScore = getSumOfScoresForMember(sets, member);
+      } else if (aggregator.equals(ZAggregator.MAX)) {
+        newScore = getMaxScoreForMember(sets, member);
+      } else {
+        newScore = getMinScoreForMember(sets, member);
+      }
+
+      if (newScore != null) {
+        if (newScore.isNaN()) {
+          throw new ArithmeticException(ERROR_OPERATION_PRODUCED_NAN);
+        }
+        retVal.memberAdd(member, newScore);
+      }
+    }
+  }
+
+  private Double getSumOfScoresForMember(List<RedisSortedSet> sets, byte[] 
memberName) {
+    OrderedSetEntry member;
+    double runningTotal = 0;
+
+    for (RedisSortedSet set : sets) {
+      if ((member = set.members.get(memberName)) != null) {
+        if (Double.isInfinite(runningTotal) && member.getScore() == 
-runningTotal) {
+          runningTotal = 0;
+        } else {
+          runningTotal += member.getScore();
+        }
+      } else {
+        return null;
+      }
+    }
+
+    this.memberAdd(memberName, runningTotal);
+    return runningTotal;
+  }
+
+  private Double getMaxScoreForMember(List<RedisSortedSet> sets, byte[] 
member) {
+    double runningMax = Double.MIN_VALUE;
+    for (RedisSortedSet set : sets) {
+      if (set.members.containsKey(member)) {

Review comment:
       You had a question about an old comment I made about containsKey. In 
this code instead of calling members.containsKey on line 724 followed by 
members.get on line 725 you could just call members.get and test for null. That 
way you do one map lookup instead of two.

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZInterStoreExecutor.java
##########
@@ -0,0 +1,126 @@
+/*
+ * 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_SYNTAX;
+import static 
org.apache.geode.redis.internal.RedisConstants.ERROR_WEIGHT_NOT_A_FLOAT;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_SLOT;
+import static org.apache.geode.redis.internal.netty.Coder.toUpperCaseBytes;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bAGGREGATE;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bWEIGHTS;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.executor.AbstractExecutor;
+import org.apache.geode.redis.internal.executor.RedisResponse;
+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 ZInterStoreExecutor extends AbstractExecutor {
+
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext 
context) {
+    List<byte[]> commandElements = command.getProcessedCommand();
+
+    Iterator<byte[]> argIterator = commandElements.iterator();
+    // Skip command and destination key
+    argIterator.next();
+    argIterator.next();
+
+    long numKeys;
+    try {
+      numKeys = Coder.bytesToLong(argIterator.next());
+    } catch (NumberFormatException ex) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    // Rough validation so that we can use numKeys to initialize the array 
sizes below.
+    if (numKeys > commandElements.size()) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    List<RedisKey> sourceKeys = new ArrayList<>((int) numKeys);
+    List<Double> weights = new ArrayList<>((int) numKeys);
+    ZAggregator aggregator = ZAggregator.SUM;
+
+    while (argIterator.hasNext()) {
+      byte[] arg = argIterator.next();
+
+      if (sourceKeys.size() < numKeys) {
+        sourceKeys.add(new RedisKey(arg));
+        continue;
+      }
+
+      arg = toUpperCaseBytes(arg);
+      if (Arrays.equals(arg, bWEIGHTS)) {
+        if (!weights.isEmpty()) {
+          return RedisResponse.error(ERROR_SYNTAX);
+        }
+        for (int i = 0; i < numKeys; i++) {
+          if (!argIterator.hasNext()) {
+            return RedisResponse.error(ERROR_SYNTAX);
+          }
+          try {
+            weights.add(Coder.bytesToDouble(argIterator.next()));
+          } catch (NumberFormatException nex) {
+            return RedisResponse.error(ERROR_WEIGHT_NOT_A_FLOAT);
+          }
+        }
+        continue;
+      }
+
+      if (Arrays.equals(arg, bAGGREGATE)) {
+        try {
+          aggregator = 
ZAggregator.valueOf(Coder.bytesToString(argIterator.next()));

Review comment:
       sounds good

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZStoreExecutor.java
##########
@@ -0,0 +1,129 @@
+/*
+ * 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_SYNTAX;
+import static 
org.apache.geode.redis.internal.RedisConstants.ERROR_WEIGHT_NOT_A_FLOAT;
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_SLOT;
+import static org.apache.geode.redis.internal.netty.Coder.toUpperCaseBytes;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bAGGREGATE;
+import static 
org.apache.geode.redis.internal.netty.StringBytesGlossary.bWEIGHTS;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.executor.AbstractExecutor;
+import org.apache.geode.redis.internal.executor.RedisResponse;
+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 abstract class ZStoreExecutor extends AbstractExecutor {
+
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext 
context) {
+    List<byte[]> commandElements = command.getProcessedCommand();
+
+    Iterator<byte[]> argIterator = commandElements.iterator();
+    // Skip command and destination key
+    argIterator.next();
+    argIterator.next();
+
+    long numKeys;
+    try {
+      numKeys = Coder.bytesToLong(argIterator.next());
+    } catch (NumberFormatException ex) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    // Rough validation so that we can use numKeys to initialize the array 
sizes below.
+    if (numKeys > commandElements.size()) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    List<ZKeyWeight> keyWeights = new ArrayList<>((int) numKeys);
+    ZAggregator aggregator = ZAggregator.SUM;
+
+    while (argIterator.hasNext()) {
+      byte[] arg = argIterator.next();
+
+      if (keyWeights.size() < numKeys) {
+        keyWeights.add(new ZKeyWeight(new RedisKey(arg), 1D));
+        continue;
+      }
+
+      arg = toUpperCaseBytes(arg);
+      if (Arrays.equals(arg, bWEIGHTS)) {
+        if (!allWeightsAreOne(keyWeights)) {
+          return RedisResponse.error(ERROR_SYNTAX);
+        }
+        for (int i = 0; i < numKeys; i++) {
+          if (!argIterator.hasNext()) {
+            return RedisResponse.error(ERROR_SYNTAX);
+          }
+          try {
+            
keyWeights.get(i).setWeight(Coder.bytesToDouble(argIterator.next()));
+          } catch (NumberFormatException nex) {
+            return RedisResponse.error(ERROR_WEIGHT_NOT_A_FLOAT);
+          }
+        }
+        continue;
+      }
+
+      if (Arrays.equals(arg, bAGGREGATE)) {
+        try {
+          aggregator = 
ZAggregator.valueOf(Coder.bytesToString(argIterator.next()));
+        } catch (IllegalArgumentException | NoSuchElementException e) {
+          return RedisResponse.error(ERROR_SYNTAX);
+        }
+        continue;
+      }
+
+      // End up here if we have more keys than weights
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    if (keyWeights.size() != numKeys) {
+      return RedisResponse.error(ERROR_SYNTAX);
+    }
+
+    int bucket = command.getKey().getBucketId();
+    for (ZKeyWeight keyWeight : keyWeights) {
+      if (keyWeight.getKey().getBucketId() != bucket) {
+        return RedisResponse.crossSlot(ERROR_WRONG_SLOT);

Review comment:
       I'm not sure but I also saw a comment from Jens that seemed related to 
this. He would be a good one to ask about this too

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -607,6 +669,87 @@ private void addIfMatching(GlobPattern matchPattern, 
List<byte[]> resultList, by
     }
   }
 
+  private void computeIntersection(List<RedisSortedSet> sets, ZAggregator 
aggregator) {
+    RedisSortedSet retVal = new RedisSortedSet(Collections.emptyList(), new 
double[] {});
+    RedisSortedSet smallestSet = sets.get(0);
+
+    for (RedisSortedSet set : sets) {
+      if (set.getSortedSetSize() < smallestSet.getSortedSetSize()) {
+        smallestSet = set;
+      }
+    }
+
+    for (byte[] member : smallestSet.members.keySet()) {
+      Double newScore;
+      if (aggregator.equals(ZAggregator.SUM)) {
+        newScore = getSumOfScoresForMember(sets, member);
+      } else if (aggregator.equals(ZAggregator.MAX)) {
+        newScore = getMaxScoreForMember(sets, member);
+      } else {
+        newScore = getMinScoreForMember(sets, member);
+      }
+
+      if (newScore != null) {
+        if (newScore.isNaN()) {
+          throw new ArithmeticException(ERROR_OPERATION_PRODUCED_NAN);
+        }
+        retVal.memberAdd(member, newScore);
+      }
+    }
+  }
+
+  private Double getSumOfScoresForMember(List<RedisSortedSet> sets, byte[] 
memberName) {

Review comment:
       are these three methods also dead code now?

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -607,6 +669,87 @@ private void addIfMatching(GlobPattern matchPattern, 
List<byte[]> resultList, by
     }
   }
 
+  private void computeIntersection(List<RedisSortedSet> sets, ZAggregator 
aggregator) {
+    RedisSortedSet retVal = new RedisSortedSet(Collections.emptyList(), new 
double[] {});
+    RedisSortedSet smallestSet = sets.get(0);
+
+    for (RedisSortedSet set : sets) {
+      if (set.getSortedSetSize() < smallestSet.getSortedSetSize()) {
+        smallestSet = set;
+      }
+    }
+
+    for (byte[] member : smallestSet.members.keySet()) {
+      Double newScore;
+      if (aggregator.equals(ZAggregator.SUM)) {
+        newScore = getSumOfScoresForMember(sets, member);
+      } else if (aggregator.equals(ZAggregator.MAX)) {
+        newScore = getMaxScoreForMember(sets, member);
+      } else {
+        newScore = getMinScoreForMember(sets, member);
+      }
+
+      if (newScore != null) {
+        if (newScore.isNaN()) {

Review comment:
       the new "zinterstore" impl does not do this NaN check. Did you decide it 
was not needed?

##########
File path: 
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java
##########
@@ -607,6 +669,87 @@ private void addIfMatching(GlobPattern matchPattern, 
List<byte[]> resultList, by
     }
   }
 
+  private void computeIntersection(List<RedisSortedSet> sets, ZAggregator 
aggregator) {

Review comment:
       should this method be removed? I don't see any callers




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


Reply via email to