jdeppe-pivotal commented on a change in pull request #6794: URL: https://github.com/apache/geode/pull/6794#discussion_r698814889
########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/sortedset/AbstractZUnionStoreIntegrationTest.java ########## @@ -0,0 +1,447 @@ +/* + * 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.RedisCommandArgumentsTestHelper.assertAtLeastNArgs; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.function.BiFunction; +import java.util.function.Function; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.Tuple; +import redis.clients.jedis.ZParams; + +import org.apache.geode.redis.ConcurrentLoopingThreads; +import org.apache.geode.redis.RedisIntegrationTest; +import org.apache.geode.redis.internal.RedisConstants; + +public abstract class AbstractZUnionStoreIntegrationTest implements RedisIntegrationTest { + + private static final String NEW_SET = "{user1}new"; + private static final String SORTED_SET_KEY1 = "{user1}sset1"; + private static final String SORTED_SET_KEY2 = "{user1}sset2"; + private static final String SORTED_SET_KEY3 = "{user1}sset3"; + + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void shouldError_givenWrongKeyType() { + final String NEW_KEY = "{user1}new"; + final String STRING_KEY = "{user1}stringKey"; + jedis.set(STRING_KEY, "value"); + assertThatThrownBy( + () -> jedis.sendCommand(NEW_KEY, Protocol.Command.ZUNIONSTORE, NEW_KEY, "2", STRING_KEY, + SORTED_SET_KEY1)) + .hasMessage("WRONGTYPE " + RedisConstants.ERROR_WRONG_TYPE); + } + + @Test + public void shouldError_givenSetsCrossSlots() { + final String NEW_KEY = "{user1}new"; + final String WRONG_KEY = "{user2}another"; + assertThatThrownBy( + () -> jedis.sendCommand(NEW_KEY, Protocol.Command.ZUNIONSTORE, NEW_KEY, "2", WRONG_KEY, + SORTED_SET_KEY1)) + .hasMessage("CROSSSLOT " + RedisConstants.ERROR_WRONG_SLOT); + } + + @Test + public void shouldError_givenTooFewArguments() { + assertAtLeastNArgs(jedis, Protocol.Command.ZUNIONSTORE, 3); + } + + @Test + public void shouldError_givenNumkeysTooLarge() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "2", + SORTED_SET_KEY1)) + .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX); + } + + @Test + public void shouldError_givenNumkeysTooSmall() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "1", + SORTED_SET_KEY1, SORTED_SET_KEY2)) + .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX); + } + + @Test + public void shouldError_givenTooManyWeights() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "1", + SORTED_SET_KEY1, "WEIGHTS", "2", "3")) + .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX); + } + + @Test + public void shouldError_givenTooFewWeights() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "2", + SORTED_SET_KEY1, SORTED_SET_KEY2, "WEIGHTS", "1")) + .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX); + } + + @Test + public void shouldError_givenWeightNotANumber() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "1", + SORTED_SET_KEY1, "WEIGHTS", "not-a-number")) + .hasMessage("ERR " + RedisConstants.ERROR_WEIGHT_NOT_A_FLOAT); + } + + @Test + public void shouldError_givenWeightsWithoutAnyValues() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "1", + SORTED_SET_KEY1, "WEIGHTS")) + .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX); + } + + @Test + public void shouldError_givenMultipleWeightKeywords() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "1", + SORTED_SET_KEY1, "WEIGHT", "1.0", "WEIGHT", "2.0")) + .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX); + } + + @Test + public void shouldError_givenUnknownAggregate() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "1", + SORTED_SET_KEY1, "AGGREGATE", "UNKNOWN", "WEIGHTS", "1")) + .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX); + } + + @Test + public void shouldError_givenAggregateKeywordWithoutValue() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "1", + SORTED_SET_KEY1, "AGGREGATE")) + .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX); + } + + @Test + public void shouldError_givenMultipleAggregates() { + assertThatThrownBy( + () -> jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "1", + SORTED_SET_KEY1, "WEIGHTS", "1", "AGGREGATE", "SUM", "MIN")) + .hasMessage("ERR " + RedisConstants.ERROR_SYNTAX); + } + + @Test + public void shouldUnionize_givenASingleSet() { + Map<String, Double> scores = makeScoreMap(10, x -> (double) x); + Set<Tuple> expectedResults = convertToTuples(scores, (i, x) -> x); + jedis.zadd(SORTED_SET_KEY1, scores); + + assertThat(jedis.zunionstore(NEW_SET, SORTED_SET_KEY1)).isEqualTo(10); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionize_givenOneSetDoesNotExist() { + Map<String, Double> scores = makeScoreMap(10, x -> (double) x); + Set<Tuple> expectedResults = convertToTuples(scores, (i, x) -> x); + jedis.zadd(SORTED_SET_KEY1, scores); + + jedis.zunionstore(NEW_SET, SORTED_SET_KEY1, SORTED_SET_KEY2); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionize_givenWeight() { + Map<String, Double> scores = makeScoreMap(10, x -> (double) x); + Set<Tuple> expectedResults = convertToTuples(scores, (i, x) -> x * 1.5); + jedis.zadd(SORTED_SET_KEY1, scores); + + jedis.zunionstore(SORTED_SET_KEY1, new ZParams().weights(1.5), SORTED_SET_KEY1); + + Set<Tuple> results = jedis.zrangeWithScores(SORTED_SET_KEY1, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionizeWithWeightAndDefaultAggregate_givenMultipleSetsWithWeights() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Map<String, Double> scores2 = makeScoreMap(10, x -> (double) (9 - x)); + jedis.zadd(SORTED_SET_KEY2, scores2); + + Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> (x * 2.0) + ((9 - x) * 1.5)); + + jedis.zunionstore(NEW_SET, new ZParams().weights(2.0, 1.5), SORTED_SET_KEY1, SORTED_SET_KEY2); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionize_givenMinAggregate() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Map<String, Double> scores2 = makeScoreMap(10, x -> 0D); + jedis.zadd(SORTED_SET_KEY2, scores2); + + Set<Tuple> expectedResults = convertToTuples(scores2, (i, x) -> x); + + jedis.zunionstore(NEW_SET, new ZParams().aggregate(ZParams.Aggregate.MIN), + SORTED_SET_KEY1, SORTED_SET_KEY2); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionize_givenMaxAggregate() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) ((x % 2 == 0) ? 0 : x)); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Map<String, Double> scores2 = makeScoreMap(10, x -> (double) ((x % 2 == 0) ? x : 0)); + jedis.zadd(SORTED_SET_KEY2, scores2); + + Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> (double) i); + + jedis.zunionstore(NEW_SET, new ZParams().aggregate(ZParams.Aggregate.MAX), + SORTED_SET_KEY1, SORTED_SET_KEY2); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionizeUsingLastAggregate_givenMultipleAggregateKeywords() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) 0); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Map<String, Double> scores2 = makeScoreMap(10, x -> (double) 1); + jedis.zadd(SORTED_SET_KEY2, scores2); + + Set<Tuple> expectedResults = convertToTuples(scores2, (i, x) -> x); + + jedis.sendCommand(NEW_SET, Protocol.Command.ZUNIONSTORE, NEW_SET, "2", + SORTED_SET_KEY1, SORTED_SET_KEY2, "AGGREGATE", "MIN", "AGGREGATE", "MAX"); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionize_givenMaxAggregateAndMultipleWeights() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) ((x % 2 == 0) ? 0 : x)); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Map<String, Double> scores2 = makeScoreMap(10, x -> (double) ((x % 2 == 0) ? x : 0)); + jedis.zadd(SORTED_SET_KEY2, scores2); + + Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> (double) (i * 2)); + + jedis.zunionstore(NEW_SET, new ZParams().aggregate(ZParams.Aggregate.MAX).weights(2, 2), + SORTED_SET_KEY1, SORTED_SET_KEY2); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionize_givenSumAggregateAndMultipleSets() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Map<String, Double> scores2 = makeScoreMap(10, x -> (double) (x * 2)); + jedis.zadd(SORTED_SET_KEY2, scores2); + + Map<String, Double> scores3 = makeScoreMap(10, x -> (double) (x * 3)); + jedis.zadd(SORTED_SET_KEY3, scores3); + + Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> x * 6); + + jedis.zunionstore(NEW_SET, new ZParams().aggregate(ZParams.Aggregate.SUM), + SORTED_SET_KEY1, SORTED_SET_KEY2, SORTED_SET_KEY3); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionize_givenSetsDoNotOverlap() { + Map<String, Double> scores1 = makeScoreMap(0, 2, 10, x -> (double) x); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Map<String, Double> scores2 = makeScoreMap(1, 2, 10, x -> (double) x); + jedis.zadd(SORTED_SET_KEY2, scores2); + + Set<Tuple> expectedResults = + convertToTuples(makeScoreMap(0, 1, 20, x -> (double) x), (i, x) -> x); + + jedis.zunionstore(NEW_SET, SORTED_SET_KEY1, SORTED_SET_KEY2); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 20); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionize_givenSetsPartiallyOverlap() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Map<String, Double> scores2 = makeScoreMap(5, 1, 10, x -> (double) (x < 10 ? x : x * 2)); + jedis.zadd(SORTED_SET_KEY2, scores2); + + Set<Tuple> expectedResults = convertToTuples(makeScoreMap(0, 1, 15, x -> (double) x), + (i, x) -> (double) (i < 5 ? i : i * 2)); + + jedis.zunionstore(NEW_SET, SORTED_SET_KEY1, SORTED_SET_KEY2); + + Set<Tuple> results = jedis.zrangeWithScores(NEW_SET, 0, 20); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void ensureWeightsAreAppliedBeforeAggregation() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x * 5); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Map<String, Double> scores2 = makeScoreMap(10, x -> (double) x); + jedis.zadd(SORTED_SET_KEY2, scores2); + + Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> (double) (i * 10)); + + jedis.zunionstore(SORTED_SET_KEY1, + new ZParams().weights(1, 10).aggregate(ZParams.Aggregate.MAX), SORTED_SET_KEY1, + SORTED_SET_KEY2); + + Set<Tuple> results = jedis.zrangeWithScores(SORTED_SET_KEY1, 0, 20); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldUnionize_whenTargetExistsAndSetsAreDuplicated() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> x * 2); + + // Default aggregation is SUM + jedis.zunionstore(SORTED_SET_KEY1, SORTED_SET_KEY1, SORTED_SET_KEY1); + + Set<Tuple> results = jedis.zrangeWithScores(SORTED_SET_KEY1, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void shouldPreserveSet_givenDestinationAndSourceAreTheSame() { + Map<String, Double> scores1 = makeScoreMap(10, x -> (double) x); + jedis.zadd(SORTED_SET_KEY1, scores1); + + Set<Tuple> expectedResults = convertToTuples(scores1, (i, x) -> x); + + jedis.zunionstore(SORTED_SET_KEY1, SORTED_SET_KEY1); + + Set<Tuple> results = jedis.zrangeWithScores(SORTED_SET_KEY1, 0, 10); + + assertThat(results).containsExactlyElementsOf(expectedResults); + } + + @Test + public void ensureSetConsistency_andNoExceptions_whenRunningConcurrently() { + int scoreCount = 1000; + jedis.zadd("{A}ones", makeScoreMap(scoreCount, x -> 1D)); Review comment: Honestly, I'd prefer not to. Since it's constrained to this one test method it feels like unnecessary clutter. ########## File path: geode-apis-compatible-with-redis/src/integrationTest/java/org/apache/geode/redis/internal/executor/set/AbstractSUnionIntegrationTest.java ########## @@ -198,4 +199,25 @@ public void testConcurrentSUnionStore() throws InterruptedException { assertThat(jedis.smembers("{user1}master").toArray()) .containsExactlyInAnyOrder(masterSet.toArray()); } + + @Test + public void doesNotThrowExceptions_whenConcurrentSaddAndSunionExecute() { Review comment: Yes ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZUnionStoreExecutor.java ########## @@ -0,0 +1,120 @@ +/* + * 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 ZUnionStoreExecutor 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 nex) { + return RedisResponse.error(ERROR_SYNTAX); + } + + List<RedisKey> sourceKeys = new ArrayList<>(); + List<Double> weights = new ArrayList<>(); + 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.size() > 0) { Review comment: Done ########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/executor/sortedset/ZUnionStoreExecutor.java ########## @@ -0,0 +1,120 @@ +/* + * 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 ZUnionStoreExecutor 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 nex) { + return RedisResponse.error(ERROR_SYNTAX); + } + + List<RedisKey> sourceKeys = new ArrayList<>(); + List<Double> weights = new ArrayList<>(); + 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.size() > 0) { + 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())); + } catch (IllegalArgumentException | NoSuchElementException e) { + return RedisResponse.error(ERROR_SYNTAX); + } + continue; + } + + return RedisResponse.error(ERROR_SYNTAX); + } + + if (sourceKeys.size() != numKeys) { + return RedisResponse.error(ERROR_SYNTAX); + } + + if (weights.size() == 0) { Review comment: Done -- 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]
