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 2022/01/03 14:17:45 UTC

[GitHub] [geode] Kris-10-0 opened a new pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Kris-10-0 opened a new pull request #7228:
URL: https://github.com/apache/geode/pull/7228


   <!-- Thank you for submitting a contribution to Apache Geode. -->
   
   <!-- In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken: 
   -->
   
   ### For all changes:
   - [x] Is there a JIRA ticket associated with this PR? Is it referenced in the commit message?
   
   - [x] Has your PR been rebased against the latest commit within the target branch (typically `develop`)?
   
   - [x] Is your initial contribution a single, squashed commit?
   
   - [x] Does `gradlew build` run cleanly?
   
   - [x] Have you written or updated unit tests to verify your changes?
   
   - [NA] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)?
   
   <!-- Note:
   Please ensure that once the PR is submitted, check Concourse for build issues and
   submit an update to your PR as soon as possible. If you need help, please send an
   email to dev@geode.apache.org.
   -->
   


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



[GitHub] [geode] nonbinaryprogrammer commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
nonbinaryprogrammer commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r780458016



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -202,48 +202,50 @@ static int setOpStoreResult(RegionProvider regionProvider, RedisKey destinationK
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
-    boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
+  public List<byte[]> srandmember(int count) {
+    boolean uniqueNumberList = count > 0;
+    count = uniqueNumberList ? count : -count;
+    int memberMapSize = members.getMemberMapSize();
 
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
+    // TODO: Optomize algorithm
+    List<byte[]> result = new ArrayList<>(count);
+    if (uniqueNumberList) {
+      if (count >= members.size()) {
+        result.addAll(members);
+      } else {
+        srandomUniqueList(count, memberMapSize, result);
+      }
+    } else {
+      srandomDuplicateList(count, memberMapSize, result);
     }
+    return result;
+  }
 
-    Random rand = new Random();
+  private void srandomDuplicateList(int count, int memberMapSize, List<byte[]> result) {
+    while (result.size() != count) {
+      int randIndex = rand.nextInt(memberMapSize);
+      byte[] member = members.getKeyAtIndex(randIndex);
 
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
+      if (member != null) {
+        result.add(member);
       }
-      return result;
-    } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+    }
+  }
+
+  private void srandomUniqueList(int count, int memberMapSize, List<byte[]> result) {
+    List<Integer> allIndexes = new ArrayList<>(memberMapSize);
+    for (int i = 0; i < memberMapSize; i++) {
+      allIndexes.add(i);
+    }
+    Collections.shuffle(allIndexes);
+    int i = 0;
+    while (result.size() != count) {
+      byte[] member = members.getKeyAtIndex(i);

Review comment:
       why not just call `getKey(pos)` directly?

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSetsIntegrationTest.java
##########
@@ -98,67 +97,6 @@ public void testSAdd_canStoreBinaryData() {
     assertThat(result).containsExactly(blob);
   }
 
-  @Test
-  public void srandmember_withStringFails() {
-    jedis.set("string", "value");
-    assertThatThrownBy(() -> jedis.srandmember("string")).hasMessageContaining("WRONGTYPE");
-  }
-
-  @Test
-  public void srandmember_withNonExistentKeyReturnsNull() {
-    assertThat(jedis.srandmember("non existent")).isNull();
-  }
-
-  @Test
-  public void srandmemberCount_withNonExistentKeyReturnsEmptyArray() {
-    assertThat(jedis.srandmember("non existent", 3)).isEmpty();
-  }
-
-  @Test
-  public void srandmember_returnsOneMember() {
-    jedis.sadd("key", "m1", "m2");
-    String result = jedis.srandmember("key");
-    assertThat(result).isIn("m1", "m2");
-  }
-
-  @Test
-  public void srandmemberCount_returnsTwoUniqueMembers() {
-    jedis.sadd("key", "m1", "m2", "m3");
-    List<String> results = jedis.srandmember("key", 2);
-    assertThat(results).hasSize(2);
-    assertThat(results).containsAnyOf("m1", "m2", "m3");
-    assertThat(results.get(0)).isNotEqualTo(results.get(1));
-  }
-
-  @Test
-  public void srandmemberNegativeCount_returnsThreeMembers() {
-    jedis.sadd("key", "m1", "m2", "m3");
-    List<String> results = jedis.srandmember("key", -3);
-    assertThat(results).hasSize(3);
-    assertThat(results).containsAnyOf("m1", "m2", "m3");
-  }
-
-  @Test
-  public void srandmemberNegativeCount_givenSmallSet_returnsThreeMembers() {
-    jedis.sadd("key", "m1");
-    List<String> results = jedis.srandmember("key", -3);
-    assertThat(results).hasSize(3);
-    assertThat(results).containsAnyOf("m1");
-  }
-
-  @Test
-  public void smembers_givenKeyNotProvided_returnsWrongNumberOfArgumentsError() {
-    assertThatThrownBy(() -> jedis.sendCommand("key", Protocol.Command.SMEMBERS))
-        .hasMessageContaining("ERR wrong number of arguments for 'smembers' command");
-  }
-
-  @Test
-  public void smembers_givenMoreThanTwoArguments_returnsWrongNumberOfArgumentsError() {
-    assertThatThrownBy(() -> jedis
-        .sendCommand("key", Protocol.Command.SMEMBERS, "key", "extraArg"))
-            .hasMessageContaining("ERR wrong number of arguments for 'smembers' command");
-  }

Review comment:
       did you mean to delete these smembers ones too?




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



[GitHub] [geode] DonalEvans commented on pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
DonalEvans commented on pull request #7228:
URL: https://github.com/apache/geode/pull/7228#issuecomment-1004456329


   In addition to the comments I left, `AbstractSetsIntegrationTest` should be modified to remove tests related to srandmember, since those tests now belong in `AbstractSRandMemberIntegrationTest`. `AbstractSetsIntegrationTest` should probably be renamed to `AbstractSAddIntegrationTest` and all non-sadd tests moved out of it, really, but maybe that's better left to another PR.


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



[GitHub] [geode] Kris-10-0 commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
Kris-10-0 commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r780556209



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,38 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();

Review comment:
       @DonalEvans Creating a final field for the class messes with the size of the bytes causing tests in RedisSet to fail.




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



[GitHub] [geode] nonbinaryprogrammer commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
nonbinaryprogrammer commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r780458016



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -202,48 +202,50 @@ static int setOpStoreResult(RegionProvider regionProvider, RedisKey destinationK
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
-    boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
+  public List<byte[]> srandmember(int count) {
+    boolean uniqueNumberList = count > 0;
+    count = uniqueNumberList ? count : -count;
+    int memberMapSize = members.getMemberMapSize();
 
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
+    // TODO: Optomize algorithm
+    List<byte[]> result = new ArrayList<>(count);
+    if (uniqueNumberList) {
+      if (count >= members.size()) {
+        result.addAll(members);
+      } else {
+        srandomUniqueList(count, memberMapSize, result);
+      }
+    } else {
+      srandomDuplicateList(count, memberMapSize, result);
     }
+    return result;
+  }
 
-    Random rand = new Random();
+  private void srandomDuplicateList(int count, int memberMapSize, List<byte[]> result) {
+    while (result.size() != count) {
+      int randIndex = rand.nextInt(memberMapSize);
+      byte[] member = members.getKeyAtIndex(randIndex);
 
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
+      if (member != null) {
+        result.add(member);
       }
-      return result;
-    } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+    }
+  }
+
+  private void srandomUniqueList(int count, int memberMapSize, List<byte[]> result) {
+    List<Integer> allIndexes = new ArrayList<>(memberMapSize);
+    for (int i = 0; i < memberMapSize; i++) {
+      allIndexes.add(i);
+    }
+    Collections.shuffle(allIndexes);
+    int i = 0;
+    while (result.size() != count) {
+      byte[] member = members.getKeyAtIndex(i);

Review comment:
       why not just call `getKey(pos)` directly?

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSetsIntegrationTest.java
##########
@@ -98,67 +97,6 @@ public void testSAdd_canStoreBinaryData() {
     assertThat(result).containsExactly(blob);
   }
 
-  @Test
-  public void srandmember_withStringFails() {
-    jedis.set("string", "value");
-    assertThatThrownBy(() -> jedis.srandmember("string")).hasMessageContaining("WRONGTYPE");
-  }
-
-  @Test
-  public void srandmember_withNonExistentKeyReturnsNull() {
-    assertThat(jedis.srandmember("non existent")).isNull();
-  }
-
-  @Test
-  public void srandmemberCount_withNonExistentKeyReturnsEmptyArray() {
-    assertThat(jedis.srandmember("non existent", 3)).isEmpty();
-  }
-
-  @Test
-  public void srandmember_returnsOneMember() {
-    jedis.sadd("key", "m1", "m2");
-    String result = jedis.srandmember("key");
-    assertThat(result).isIn("m1", "m2");
-  }
-
-  @Test
-  public void srandmemberCount_returnsTwoUniqueMembers() {
-    jedis.sadd("key", "m1", "m2", "m3");
-    List<String> results = jedis.srandmember("key", 2);
-    assertThat(results).hasSize(2);
-    assertThat(results).containsAnyOf("m1", "m2", "m3");
-    assertThat(results.get(0)).isNotEqualTo(results.get(1));
-  }
-
-  @Test
-  public void srandmemberNegativeCount_returnsThreeMembers() {
-    jedis.sadd("key", "m1", "m2", "m3");
-    List<String> results = jedis.srandmember("key", -3);
-    assertThat(results).hasSize(3);
-    assertThat(results).containsAnyOf("m1", "m2", "m3");
-  }
-
-  @Test
-  public void srandmemberNegativeCount_givenSmallSet_returnsThreeMembers() {
-    jedis.sadd("key", "m1");
-    List<String> results = jedis.srandmember("key", -3);
-    assertThat(results).hasSize(3);
-    assertThat(results).containsAnyOf("m1");
-  }
-
-  @Test
-  public void smembers_givenKeyNotProvided_returnsWrongNumberOfArgumentsError() {
-    assertThatThrownBy(() -> jedis.sendCommand("key", Protocol.Command.SMEMBERS))
-        .hasMessageContaining("ERR wrong number of arguments for 'smembers' command");
-  }
-
-  @Test
-  public void smembers_givenMoreThanTwoArguments_returnsWrongNumberOfArgumentsError() {
-    assertThatThrownBy(() -> jedis
-        .sendCommand("key", Protocol.Command.SMEMBERS, "key", "extraArg"))
-            .hasMessageContaining("ERR wrong number of arguments for 'smembers' command");
-  }

Review comment:
       did you mean to delete these smembers ones too?




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



[GitHub] [geode] DonalEvans commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
DonalEvans commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r777743456



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableObjectOpenCustomHashSet.java
##########
@@ -62,6 +62,15 @@ public boolean remove(Object k) {
     return removed;
   }
 
+  public K getKey(final int pos) {
+    K member = key[pos];
+    return member;

Review comment:
       This can be simplified to just `return key[pos];`

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+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.RedisConstants.ERROR_WRONG_TYPE;
+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.Arrays;
+import java.util.List;
+
+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 org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String nonExistentSetKey = "{user1}nonExistentSet";
+  private static final String setKey = "{user1}setKey";
+  private static final String[] setMembers = {"one", "two", "three", "four", "five"};
+
+  @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 srandmemberTooFewArgs_returnsError() {
+    assertAtLeastNArgs(jedis, Protocol.Command.SRANDMEMBER, 1);
+  }
+
+  @Test
+  public void srandmemberTooManyArgs_returnsError() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "5", "5"))
+            .hasMessageContaining(ERROR_SYNTAX);
+  }
+
+  @Test
+  public void srandmemberWithInvalidCount_returnsError() {
+    assertThatThrownBy(() -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "b"))
+        .hasMessageContaining(ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+    assertThat(jedis.srandmember(nonExistentSetKey)).isNull();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
+    assertThat(jedis.srandmember(nonExistentSetKey, 1)).isEmpty();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(setKey, setMembers);
+
+    String result = jedis.srandmember(setKey);
+    assertThat(setMembers).contains(result);
+  }
+
+  @Test
+  public void srandmemberWithCount_withExistentSet_returnsEmptySet() {

Review comment:
       This test name is incorrect. It should probably be "srandmemberWithCount_withExistentSet_returnsCorrectNumberOfMembers".
   
   It would also be good to have a test showing that when count is positive and larger than the size of the set, no duplicate entries are returned.

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,38 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();
-
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
-      }
-      return result;
+    List<byte[]> result = new ArrayList<>(count);
+    if (!duplicatesAllowed && membersSize <= count) {
+      result.addAll(members);
     } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+      Set<Integer> usedIndexes = null;
+      if (!duplicatesAllowed) {
+        usedIndexes = new HashSet<>(count);
+      }
+
+      for (int i = 0; i < count; i++) {
+        int randIndex = rand.nextInt(memberMapSize);
+        byte[] member = members.getKeyAtIndex(randIndex);
+
+        while (member == null || (!duplicatesAllowed && usedIndexes.contains(randIndex))) {

Review comment:
       This loop could end up taking a long time to execute if `members` is large, duplicates are not allowed and `count` is close to `members.size()`, as it requires generating a random number that isn't the same as any of the previously generated numbers. Including the outer for loop, this ends up making the srandmember command execute in O(N^2) (or thereabouts) which is not in line with native Redis' quoted complexity of O(N).
   
   For the no duplicates case, native Redis uses a different implementation where, if count is greater than 1/3 of the size of the member set, they instead create a duplicate of the entire set and then remove members from it at random until it has the correct size.  From t_set.c (SRANDMEMBER_SUB_STRATEGY_MUL is defined as 3):
   >    /* CASE 3:
        * The number of elements inside the set is not greater than
        * SRANDMEMBER_SUB_STRATEGY_MUL times the number of requested elements.
        * In this case we create a set from scratch with all the elements, and
        * subtract random elements to reach the requested number of elements.
        *
        * This is done because if the number of requested elements is just
        * a bit less than the number of elements in the set, the natural approach
        * used into CASE 4 is highly inefficient. */
   
   It's possible that there's a better approach than this, but it's an improvement over the current one, at least.

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+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.RedisConstants.ERROR_WRONG_TYPE;
+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.Arrays;
+import java.util.List;
+
+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 org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String nonExistentSetKey = "{user1}nonExistentSet";
+  private static final String setKey = "{user1}setKey";
+  private static final String[] setMembers = {"one", "two", "three", "four", "five"};
+
+  @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 srandmemberTooFewArgs_returnsError() {
+    assertAtLeastNArgs(jedis, Protocol.Command.SRANDMEMBER, 1);
+  }
+
+  @Test
+  public void srandmemberTooManyArgs_returnsError() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "5", "5"))
+            .hasMessageContaining(ERROR_SYNTAX);
+  }
+
+  @Test
+  public void srandmemberWithInvalidCount_returnsError() {
+    assertThatThrownBy(() -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "b"))
+        .hasMessageContaining(ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+    assertThat(jedis.srandmember(nonExistentSetKey)).isNull();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
+    assertThat(jedis.srandmember(nonExistentSetKey, 1)).isEmpty();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(setKey, setMembers);
+
+    String result = jedis.srandmember(setKey);
+    assertThat(setMembers).contains(result);
+  }
+
+  @Test
+  public void srandmemberWithCount_withExistentSet_returnsEmptySet() {
+    jedis.sadd(setKey, setMembers);
+    int count = 2;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(2);
+    assertThat(result).containsAnyElementsOf(Arrays.asList(setMembers));

Review comment:
       This assertion is only testing that `result` contains at least one element found in `setMembers`. The correct assertion to use here would be `isSubsetOf(setMembers)`, along with `assertThat(result).doesNotHaveDuplicates()` to ensure that only unique members are returned.

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/set/SetRandomExecutor.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.data.RedisDataType.REDIS_SET;
+import static org.apache.geode.redis.internal.netty.Coder.bytesToLong;
+import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.geode.redis.internal.commands.Command;
+import org.apache.geode.redis.internal.commands.executor.CommandExecutor;
+import org.apache.geode.redis.internal.commands.executor.RedisResponse;
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.data.RedisSet;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+import org.apache.geode.redis.internal.services.RegionProvider;
+
+public abstract class SetRandomExecutor implements CommandExecutor {

Review comment:
       Does this class need to exist? It's only used by the `SRandMemberExecutor`, and as far as I can tell, there aren't any other currently unsupported Redis set commands that would also use this class but implement a different `performCommand()` method. Could these two classes just be combined into `SRandMemberExecutor`?

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+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.RedisConstants.ERROR_WRONG_TYPE;
+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.Arrays;
+import java.util.List;
+
+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 org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String nonExistentSetKey = "{user1}nonExistentSet";
+  private static final String setKey = "{user1}setKey";
+  private static final String[] setMembers = {"one", "two", "three", "four", "five"};
+
+  @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 srandmemberTooFewArgs_returnsError() {
+    assertAtLeastNArgs(jedis, Protocol.Command.SRANDMEMBER, 1);
+  }
+
+  @Test
+  public void srandmemberTooManyArgs_returnsError() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "5", "5"))
+            .hasMessageContaining(ERROR_SYNTAX);
+  }
+
+  @Test
+  public void srandmemberWithInvalidCount_returnsError() {
+    assertThatThrownBy(() -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "b"))
+        .hasMessageContaining(ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+    assertThat(jedis.srandmember(nonExistentSetKey)).isNull();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
+    assertThat(jedis.srandmember(nonExistentSetKey, 1)).isEmpty();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(setKey, setMembers);
+
+    String result = jedis.srandmember(setKey);
+    assertThat(setMembers).contains(result);
+  }
+
+  @Test
+  public void srandmemberWithCount_withExistentSet_returnsEmptySet() {
+    jedis.sadd(setKey, setMembers);
+    int count = 2;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(2);
+    assertThat(result).containsAnyElementsOf(Arrays.asList(setMembers));
+  }
+
+  @Test
+  public void srandmemberWithCountAsSetSize_withExistentSet_returnsAllMembers() {
+    jedis.sadd(setKey, setMembers);
+    int count = setMembers.length;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(count);
+    assertThat(result).containsExactlyInAnyOrder(setMembers);
+  }
+
+  @Test
+  public void srandmemberWithCountAsGreaterThanSetSize_withExistentSet_returnsAllMembersWithDuplicates() {
+    jedis.sadd(setKey, setMembers);
+    int count = -20;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(-count);
+    for (String s : result) {
+      assertThat(s).isNotNull();
+      assertThat(setMembers).contains(s);
+    }

Review comment:
       This can be simplified to:
   ```
   assertThat(result).isSubsetOf(setMembers);
   ```

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,38 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();

Review comment:
       Rather than creating a new `Random` instance each time this command is executed, it might be better to create a final field on the class so that it only needs to be instantiated once.

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+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.RedisConstants.ERROR_WRONG_TYPE;
+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.Arrays;
+import java.util.List;
+
+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 org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String nonExistentSetKey = "{user1}nonExistentSet";
+  private static final String setKey = "{user1}setKey";
+  private static final String[] setMembers = {"one", "two", "three", "four", "five"};
+
+  @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 srandmemberTooFewArgs_returnsError() {
+    assertAtLeastNArgs(jedis, Protocol.Command.SRANDMEMBER, 1);
+  }
+
+  @Test
+  public void srandmemberTooManyArgs_returnsError() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "5", "5"))
+            .hasMessageContaining(ERROR_SYNTAX);
+  }
+
+  @Test
+  public void srandmemberWithInvalidCount_returnsError() {
+    assertThatThrownBy(() -> jedis.sendCommand(setKey, Protocol.Command.SRANDMEMBER, setKey, "b"))
+        .hasMessageContaining(ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+    assertThat(jedis.srandmember(nonExistentSetKey)).isNull();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
+    assertThat(jedis.srandmember(nonExistentSetKey, 1)).isEmpty();
+    assertThat(jedis.exists(nonExistentSetKey)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(setKey, setMembers);
+
+    String result = jedis.srandmember(setKey);
+    assertThat(setMembers).contains(result);
+  }
+
+  @Test
+  public void srandmemberWithCount_withExistentSet_returnsEmptySet() {
+    jedis.sadd(setKey, setMembers);
+    int count = 2;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(2);
+    assertThat(result).containsAnyElementsOf(Arrays.asList(setMembers));
+  }
+
+  @Test
+  public void srandmemberWithCountAsSetSize_withExistentSet_returnsAllMembers() {
+    jedis.sadd(setKey, setMembers);
+    int count = setMembers.length;
+
+    List<String> result = jedis.srandmember(setKey, count);
+    assertThat(result.size()).isEqualTo(count);
+    assertThat(result).containsExactlyInAnyOrder(setMembers);
+  }
+
+  @Test
+  public void srandmemberWithCountAsGreaterThanSetSize_withExistentSet_returnsAllMembersWithDuplicates() {

Review comment:
       This test name is a little misleading, as the test is actually testing a negative count value, not a count that is greater than the set size.




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



[GitHub] [geode] DonalEvans commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
DonalEvans commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r778392896



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/set/SetRandomExecutor.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.data.RedisDataType.REDIS_SET;
+import static org.apache.geode.redis.internal.netty.Coder.bytesToLong;
+import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.geode.redis.internal.commands.Command;
+import org.apache.geode.redis.internal.commands.executor.CommandExecutor;
+import org.apache.geode.redis.internal.commands.executor.RedisResponse;
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.data.RedisSet;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+import org.apache.geode.redis.internal.services.RegionProvider;
+
+public abstract class SetRandomExecutor implements CommandExecutor {

Review comment:
       Ah, good call. I hadn't thought about SPOP being random.




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



[GitHub] [geode] dschneider-pivotal commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
dschneider-pivotal commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r777630052



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,42 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();
-
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
-      }
-      return result;
+    List<byte[]> randMembers = new ArrayList<>(count);
+    if (!duplicatesAllowed && membersSize <= count) {
+      randMembers.addAll(members);
     } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+      Set<Integer> usedPos = null;
+      if (!duplicatesAllowed) {
+        usedPos = new HashSet<>(count);
+      }
+
+      for (int i = 0; i < count; i++) {
+        int randNum = rand.nextInt(memberMapSize);

Review comment:
       would "randomIndex" be a better name?

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/set/SRandMemberExecutor.java
##########
@@ -14,55 +14,13 @@
  */
 package org.apache.geode.redis.internal.commands.executor.set;
 
-import static org.apache.geode.redis.internal.netty.Coder.bytesToLong;
-import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
-
-import java.util.Collection;
 import java.util.List;
 
-import org.apache.geode.redis.internal.commands.Command;
-import org.apache.geode.redis.internal.commands.executor.CommandExecutor;
-import org.apache.geode.redis.internal.commands.executor.RedisResponse;
-import org.apache.geode.redis.internal.data.RedisKey;
-import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
-
-public class SRandMemberExecutor implements CommandExecutor {
-
-  private static final String ERROR_NOT_NUMERIC = "The count provided must be numeric";
+import org.apache.geode.redis.internal.data.RedisSet;
 
+public class SRandMemberExecutor extends SetRandomExecutor {
   @Override
-  public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) {
-    List<byte[]> commandElems = command.getProcessedCommand();
-
-    RedisKey key = command.getKey();
-
-    boolean countSpecified = false;
-    int count;
-
-    if (commandElems.size() > 2) {
-      try {
-        count = narrowLongToInt(bytesToLong(commandElems.get(2)));
-        countSpecified = true;
-      } catch (NumberFormatException e) {
-        return RedisResponse.error(ERROR_NOT_NUMERIC);
-      }
-    } else {
-      count = 1;
-    }
-
-    if (count == 0) {
-      return RedisResponse.emptyArray();
-    }
-
-    Collection<byte[]> results = context.setLockedExecute(key, true,
-        set -> set.srandmember(count));
-
-    if (countSpecified) {
-      return RedisResponse.array(results, true);
-    } else if (results.isEmpty()) {
-      return RedisResponse.nil();
-    } else {
-      return RedisResponse.bulkString(results.iterator().next());
-    }
+  protected List<byte[]> preformCommand(RedisSet set, int count) {

Review comment:
       is the name a typo? I think it should be "perform" instead of "preform"

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,42 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();
-
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
-      }
-      return result;
+    List<byte[]> randMembers = new ArrayList<>(count);
+    if (!duplicatesAllowed && membersSize <= count) {
+      randMembers.addAll(members);
     } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+      Set<Integer> usedPos = null;

Review comment:
       would "usedIndexes" be a better name?

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,42 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();
-
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
-      }
-      return result;
+    List<byte[]> randMembers = new ArrayList<>(count);
+    if (!duplicatesAllowed && membersSize <= count) {
+      randMembers.addAll(members);
     } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+      Set<Integer> usedPos = null;
+      if (!duplicatesAllowed) {
+        usedPos = new HashSet<>(count);
+      }
+
+      for (int i = 0; i < count; i++) {
+        int randNum = rand.nextInt(memberMapSize);
+        byte[] member = members.getKeyAtIndex(randNum);
+
+        while (member == null || (!duplicatesAllowed && usedPos.contains(randNum))) {
+          // TODO: Can a member be null?
+          if (!duplicatesAllowed && member == null) {
+            usedPos.add(randNum);
+          }
+          randNum = rand.nextInt(memberMapSize);
+          member = members.getKeyAtIndex(randNum);
+        }
+
+        randMembers.add(members.getKeyAtIndex(randNum));

Review comment:
       I think this can be simplified to "randMembers.add(member);"




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



[GitHub] [geode] Kris-10-0 commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
Kris-10-0 commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r780464930



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -202,48 +202,50 @@ static int setOpStoreResult(RegionProvider regionProvider, RedisKey destinationK
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
-    boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
+  public List<byte[]> srandmember(int count) {
+    boolean uniqueNumberList = count > 0;
+    count = uniqueNumberList ? count : -count;
+    int memberMapSize = members.getMemberMapSize();
 
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
+    // TODO: Optomize algorithm
+    List<byte[]> result = new ArrayList<>(count);
+    if (uniqueNumberList) {
+      if (count >= members.size()) {
+        result.addAll(members);
+      } else {
+        srandomUniqueList(count, memberMapSize, result);
+      }
+    } else {
+      srandomDuplicateList(count, memberMapSize, result);
     }
+    return result;
+  }
 
-    Random rand = new Random();
+  private void srandomDuplicateList(int count, int memberMapSize, List<byte[]> result) {
+    while (result.size() != count) {
+      int randIndex = rand.nextInt(memberMapSize);
+      byte[] member = members.getKeyAtIndex(randIndex);
 
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
+      if (member != null) {
+        result.add(member);
       }
-      return result;
-    } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+    }
+  }
+
+  private void srandomUniqueList(int count, int memberMapSize, List<byte[]> result) {
+    List<Integer> allIndexes = new ArrayList<>(memberMapSize);
+    for (int i = 0; i < memberMapSize; i++) {
+      allIndexes.add(i);
+    }
+    Collections.shuffle(allIndexes);
+    int i = 0;
+    while (result.size() != count) {
+      byte[] member = members.getKeyAtIndex(i);

Review comment:
       I think I was worried since getKey(pos) returned an object K it wouldn't be a byte[] when I called it directly. I tried the direct call and it worked. So I'll make the fix.

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSetsIntegrationTest.java
##########
@@ -98,67 +97,6 @@ public void testSAdd_canStoreBinaryData() {
     assertThat(result).containsExactly(blob);
   }
 
-  @Test
-  public void srandmember_withStringFails() {
-    jedis.set("string", "value");
-    assertThatThrownBy(() -> jedis.srandmember("string")).hasMessageContaining("WRONGTYPE");
-  }
-
-  @Test
-  public void srandmember_withNonExistentKeyReturnsNull() {
-    assertThat(jedis.srandmember("non existent")).isNull();
-  }
-
-  @Test
-  public void srandmemberCount_withNonExistentKeyReturnsEmptyArray() {
-    assertThat(jedis.srandmember("non existent", 3)).isEmpty();
-  }
-
-  @Test
-  public void srandmember_returnsOneMember() {
-    jedis.sadd("key", "m1", "m2");
-    String result = jedis.srandmember("key");
-    assertThat(result).isIn("m1", "m2");
-  }
-
-  @Test
-  public void srandmemberCount_returnsTwoUniqueMembers() {
-    jedis.sadd("key", "m1", "m2", "m3");
-    List<String> results = jedis.srandmember("key", 2);
-    assertThat(results).hasSize(2);
-    assertThat(results).containsAnyOf("m1", "m2", "m3");
-    assertThat(results.get(0)).isNotEqualTo(results.get(1));
-  }
-
-  @Test
-  public void srandmemberNegativeCount_returnsThreeMembers() {
-    jedis.sadd("key", "m1", "m2", "m3");
-    List<String> results = jedis.srandmember("key", -3);
-    assertThat(results).hasSize(3);
-    assertThat(results).containsAnyOf("m1", "m2", "m3");
-  }
-
-  @Test
-  public void srandmemberNegativeCount_givenSmallSet_returnsThreeMembers() {
-    jedis.sadd("key", "m1");
-    List<String> results = jedis.srandmember("key", -3);
-    assertThat(results).hasSize(3);
-    assertThat(results).containsAnyOf("m1");
-  }
-
-  @Test
-  public void smembers_givenKeyNotProvided_returnsWrongNumberOfArgumentsError() {
-    assertThatThrownBy(() -> jedis.sendCommand("key", Protocol.Command.SMEMBERS))
-        .hasMessageContaining("ERR wrong number of arguments for 'smembers' command");
-  }
-
-  @Test
-  public void smembers_givenMoreThanTwoArguments_returnsWrongNumberOfArgumentsError() {
-    assertThatThrownBy(() -> jedis
-        .sendCommand("key", Protocol.Command.SMEMBERS, "key", "extraArg"))
-            .hasMessageContaining("ERR wrong number of arguments for 'smembers' command");
-  }

Review comment:
       Nope accident. 

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,38 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();

Review comment:
       @DonalEvans Creating a final field for the class messes with the size of the bytes causing tests in RedisSet to fail.




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



[GitHub] [geode] Kris-10-0 commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
Kris-10-0 commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r780554562



##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSetsIntegrationTest.java
##########
@@ -98,67 +97,6 @@ public void testSAdd_canStoreBinaryData() {
     assertThat(result).containsExactly(blob);
   }
 
-  @Test
-  public void srandmember_withStringFails() {
-    jedis.set("string", "value");
-    assertThatThrownBy(() -> jedis.srandmember("string")).hasMessageContaining("WRONGTYPE");
-  }
-
-  @Test
-  public void srandmember_withNonExistentKeyReturnsNull() {
-    assertThat(jedis.srandmember("non existent")).isNull();
-  }
-
-  @Test
-  public void srandmemberCount_withNonExistentKeyReturnsEmptyArray() {
-    assertThat(jedis.srandmember("non existent", 3)).isEmpty();
-  }
-
-  @Test
-  public void srandmember_returnsOneMember() {
-    jedis.sadd("key", "m1", "m2");
-    String result = jedis.srandmember("key");
-    assertThat(result).isIn("m1", "m2");
-  }
-
-  @Test
-  public void srandmemberCount_returnsTwoUniqueMembers() {
-    jedis.sadd("key", "m1", "m2", "m3");
-    List<String> results = jedis.srandmember("key", 2);
-    assertThat(results).hasSize(2);
-    assertThat(results).containsAnyOf("m1", "m2", "m3");
-    assertThat(results.get(0)).isNotEqualTo(results.get(1));
-  }
-
-  @Test
-  public void srandmemberNegativeCount_returnsThreeMembers() {
-    jedis.sadd("key", "m1", "m2", "m3");
-    List<String> results = jedis.srandmember("key", -3);
-    assertThat(results).hasSize(3);
-    assertThat(results).containsAnyOf("m1", "m2", "m3");
-  }
-
-  @Test
-  public void srandmemberNegativeCount_givenSmallSet_returnsThreeMembers() {
-    jedis.sadd("key", "m1");
-    List<String> results = jedis.srandmember("key", -3);
-    assertThat(results).hasSize(3);
-    assertThat(results).containsAnyOf("m1");
-  }
-
-  @Test
-  public void smembers_givenKeyNotProvided_returnsWrongNumberOfArgumentsError() {
-    assertThatThrownBy(() -> jedis.sendCommand("key", Protocol.Command.SMEMBERS))
-        .hasMessageContaining("ERR wrong number of arguments for 'smembers' command");
-  }
-
-  @Test
-  public void smembers_givenMoreThanTwoArguments_returnsWrongNumberOfArgumentsError() {
-    assertThatThrownBy(() -> jedis
-        .sendCommand("key", Protocol.Command.SMEMBERS, "key", "extraArg"))
-            .hasMessageContaining("ERR wrong number of arguments for 'smembers' command");
-  }

Review comment:
       Nope accident. 




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



[GitHub] [geode] DonalEvans commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
DonalEvans commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r780557151



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,38 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();

Review comment:
       That's fine, those tests (and probably the value defined in `expectedPerEntryOverhead()` in MemoryOverheadIntegrationTest) can be adjusted to account for the new size.




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



[GitHub] [geode] Kris-10-0 commented on pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
Kris-10-0 commented on pull request #7228:
URL: https://github.com/apache/geode/pull/7228#issuecomment-1007602259


   Forced push to rebase


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



[GitHub] [geode] Kris-10-0 commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
Kris-10-0 commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r780464930



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -202,48 +202,50 @@ static int setOpStoreResult(RegionProvider regionProvider, RedisKey destinationK
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
-    boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
+  public List<byte[]> srandmember(int count) {
+    boolean uniqueNumberList = count > 0;
+    count = uniqueNumberList ? count : -count;
+    int memberMapSize = members.getMemberMapSize();
 
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
+    // TODO: Optomize algorithm
+    List<byte[]> result = new ArrayList<>(count);
+    if (uniqueNumberList) {
+      if (count >= members.size()) {
+        result.addAll(members);
+      } else {
+        srandomUniqueList(count, memberMapSize, result);
+      }
+    } else {
+      srandomDuplicateList(count, memberMapSize, result);
     }
+    return result;
+  }
 
-    Random rand = new Random();
+  private void srandomDuplicateList(int count, int memberMapSize, List<byte[]> result) {
+    while (result.size() != count) {
+      int randIndex = rand.nextInt(memberMapSize);
+      byte[] member = members.getKeyAtIndex(randIndex);
 
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
+      if (member != null) {
+        result.add(member);
       }
-      return result;
-    } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+    }
+  }
+
+  private void srandomUniqueList(int count, int memberMapSize, List<byte[]> result) {
+    List<Integer> allIndexes = new ArrayList<>(memberMapSize);
+    for (int i = 0; i < memberMapSize; i++) {
+      allIndexes.add(i);
+    }
+    Collections.shuffle(allIndexes);
+    int i = 0;
+    while (result.size() != count) {
+      byte[] member = members.getKeyAtIndex(i);

Review comment:
       I think I was worried since getKey(pos) returned an object K it wouldn't be a byte[] when I called it directly. I tried the direct call and it worked. So I'll make the fix.




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



[GitHub] [geode] DonalEvans commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
DonalEvans commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r780557151



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,38 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();

Review comment:
       That's fine, those tests (and probably the value defined in `expectedPerEntryOverhead()` in MemoryOverheadIntegrationTest) can be adjusted to account for the new size.




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



[GitHub] [geode] Kris-10-0 commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
Kris-10-0 commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r778316479



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/set/SetRandomExecutor.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.data.RedisDataType.REDIS_SET;
+import static org.apache.geode.redis.internal.netty.Coder.bytesToLong;
+import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.geode.redis.internal.commands.Command;
+import org.apache.geode.redis.internal.commands.executor.CommandExecutor;
+import org.apache.geode.redis.internal.commands.executor.RedisResponse;
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.data.RedisSet;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+import org.apache.geode.redis.internal.services.RegionProvider;
+
+public abstract class SetRandomExecutor implements CommandExecutor {

Review comment:
       I was going to use it for SPOP, which is basically SRANDMEMBER, but it removes the members. 




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



[GitHub] [geode] Kris-10-0 commented on pull request #7228: GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
Kris-10-0 commented on pull request #7228:
URL: https://github.com/apache/geode/pull/7228#issuecomment-1015984184


   Forced push to rebase


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



[GitHub] [geode] DonalEvans commented on a change in pull request #7228: GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
DonalEvans commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r787945659



##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -64,84 +66,100 @@ public void srandmemberTooManyArgs_returnsError() {
   }
 
   @Test
-  public void srandmemberWithInvalidCount_returnsError() {
+  public void srandmember_withInvalidCount_returnsError() {
     assertThatThrownBy(() -> jedis.sendCommand(SET_KEY, Protocol.Command.SRANDMEMBER, SET_KEY, "b"))
         .hasMessageContaining(ERROR_NOT_INTEGER);
   }
 
   @Test
-  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+  public void srandmember_withoutCount_withNonExistentSet_returnsNull() {
     assertThat(jedis.srandmember(NON_EXISTENT_SET_KEY)).isNull();
     assertThat(jedis.exists(NON_EXISTENT_SET_KEY)).isFalse();
   }
 
   @Test
-  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
-    assertThat(jedis.srandmember(NON_EXISTENT_SET_KEY, 1)).isEmpty();
-    assertThat(jedis.exists(NON_EXISTENT_SET_KEY)).isFalse();
+  public void srandmember_withoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(SET_KEY, SET_MEMBERS);
+
+    String result = jedis.srandmember(SET_KEY);
+    assertThat(result).isIn(Arrays.asList(SET_MEMBERS));
   }
 
   @Test
-  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+  public void srandmember_withCountAsZero_withExistentSet_returnsSubsetOfSet() {

Review comment:
       This test name is inaccurate. It seems like it should be "srandmember_withCountAsZero_withExistentSet_returnsEmptyList"

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,167 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+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.RedisConstants.ERROR_WRONG_TYPE;
+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.Arrays;
+import java.util.List;
+
+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 org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String NON_EXISTENT_SET_KEY = "{user1}nonExistentSet";
+  private static final String SET_KEY = "{user1}setKey";

Review comment:
       Sorry I didn't catch this earlier, but could these tags be `{tag1}` to follow the new convention?




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



[GitHub] [geode] jdeppe-pivotal merged pull request #7228: GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
jdeppe-pivotal merged pull request #7228:
URL: https://github.com/apache/geode/pull/7228


   


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



[GitHub] [geode] DonalEvans commented on a change in pull request #7228: GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
DonalEvans commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r785224467



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableObjectOpenCustomHashSet.java
##########
@@ -62,6 +62,14 @@ public boolean remove(Object k) {
     return removed;
   }
 
+  public K getKey(final int pos) {
+    return key[pos];
+  }
+
+  public int getMemberMapSize() {

Review comment:
       This method name would be a bit clearer as "getBackingArrayLength()" since it's explicitly not returning the size of the map.

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableObjectOpenCustomHashSet.java
##########
@@ -62,6 +62,14 @@ public boolean remove(Object k) {
     return removed;
   }
 
+  public K getKey(final int pos) {

Review comment:
       This method name would be more accurately "getElementForIndex()" or "getFromBackingArray()" or something similar, since a Set doesn't really have a concept of keys.

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/set/SetRandomExecutor.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.data.RedisDataType.REDIS_SET;
+import static org.apache.geode.redis.internal.netty.Coder.bytesToLong;
+import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.geode.redis.internal.commands.Command;
+import org.apache.geode.redis.internal.commands.executor.CommandExecutor;
+import org.apache.geode.redis.internal.commands.executor.RedisResponse;
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.data.RedisSet;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+import org.apache.geode.redis.internal.services.RegionProvider;
+
+public abstract class SetRandomExecutor implements CommandExecutor {
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) {
+    List<byte[]> commandElems = command.getProcessedCommand();
+    RedisKey key = command.getKey();
+    int argsCount = commandElems.size();

Review comment:
       This method can be simplified/clarified a little bit by introducing a boolean here:
   ```
   boolean hasCount = commandElems.size() == 3;
   ```
   and using it in the if statements on lines 41 and 53.

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/set/SetRandomExecutor.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.data.RedisDataType.REDIS_SET;
+import static org.apache.geode.redis.internal.netty.Coder.bytesToLong;
+import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.geode.redis.internal.commands.Command;
+import org.apache.geode.redis.internal.commands.executor.CommandExecutor;
+import org.apache.geode.redis.internal.commands.executor.RedisResponse;
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.data.RedisSet;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+import org.apache.geode.redis.internal.services.RegionProvider;
+
+public abstract class SetRandomExecutor implements CommandExecutor {
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) {
+    List<byte[]> commandElems = command.getProcessedCommand();
+    RedisKey key = command.getKey();
+    int argsCount = commandElems.size();
+    int count;
+
+    if (argsCount == 3) {
+      try {
+        count = narrowLongToInt(bytesToLong(commandElems.get(2)));
+      } catch (NumberFormatException e) {
+        return RedisResponse.error(ERROR_NOT_INTEGER);
+      }
+    } else {
+      count = 1;
+    }
+
+    List<byte[]> results =
+        context.lockedExecute(key, () -> getResult(count, context.getRegionProvider(), key));
+    if (argsCount == 2) {
+      byte[] byteResult = null;
+      if (!results.isEmpty()) {
+        byteResult = results.get(0);
+      }
+      return RedisResponse.bulkString(byteResult);
+    }
+    return RedisResponse.array(results, true);
+  }
+
+  private List<byte[]> getResult(int count, RegionProvider regionProvider, RedisKey key) {
+    RedisSet set =
+        regionProvider.getTypedRedisData(REDIS_SET, key, true);
+    if (count == 0 || set.scard() == 0) {

Review comment:
       For consistency with other code that's been added recently, this `set.scard() == 0` check might be better as `set == NULL_REDIS_SET`

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/set/SetRandomExecutor.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
+import static org.apache.geode.redis.internal.data.RedisDataType.REDIS_SET;
+import static org.apache.geode.redis.internal.netty.Coder.bytesToLong;
+import static org.apache.geode.redis.internal.netty.Coder.narrowLongToInt;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.geode.redis.internal.commands.Command;
+import org.apache.geode.redis.internal.commands.executor.CommandExecutor;
+import org.apache.geode.redis.internal.commands.executor.RedisResponse;
+import org.apache.geode.redis.internal.data.RedisKey;
+import org.apache.geode.redis.internal.data.RedisSet;
+import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;
+import org.apache.geode.redis.internal.services.RegionProvider;
+
+public abstract class SetRandomExecutor implements CommandExecutor {
+  @Override
+  public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) {
+    List<byte[]> commandElems = command.getProcessedCommand();
+    RedisKey key = command.getKey();
+    int argsCount = commandElems.size();
+    int count;
+
+    if (argsCount == 3) {
+      try {
+        count = narrowLongToInt(bytesToLong(commandElems.get(2)));
+      } catch (NumberFormatException e) {
+        return RedisResponse.error(ERROR_NOT_INTEGER);
+      }
+    } else {
+      count = 1;
+    }
+
+    List<byte[]> results =
+        context.lockedExecute(key, () -> getResult(count, context.getRegionProvider(), key));
+    if (argsCount == 2) {
+      byte[] byteResult = null;
+      if (!results.isEmpty()) {
+        byteResult = results.get(0);
+      }
+      return RedisResponse.bulkString(byteResult);
+    }
+    return RedisResponse.array(results, true);

Review comment:
       If the suggestion to use the `hasCount` boolean above is taken, this can be refactored to be a little more easily readable:
   ```
       if (hasCount) {
         return RedisResponse.array(results, true);
       } else {
         if (results.isEmpty()) {
           return RedisResponse.nil();
         } else {
           return RedisResponse.bulkString(results.get(0));
         }
       }
   ```

##########
File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/set/AbstractSRandMemberIntegrationTest.java
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.commands.executor.set;
+
+import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertAtLeastNArgs;
+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.RedisConstants.ERROR_WRONG_TYPE;
+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.List;
+
+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 org.apache.geode.redis.RedisIntegrationTest;
+
+public abstract class AbstractSRandMemberIntegrationTest implements RedisIntegrationTest {
+  private JedisCluster jedis;
+  private static final String NON_EXISTENT_SET_KEY = "{user1}nonExistentSet";
+  private static final String SET_KEY = "{user1}setKey";
+  private static final String[] SET_MEMBERS = {"one", "two", "three", "four", "five"};
+
+  @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 srandmemberTooFewArgs_returnsError() {
+    assertAtLeastNArgs(jedis, Protocol.Command.SRANDMEMBER, 1);
+  }
+
+  @Test
+  public void srandmemberTooManyArgs_returnsError() {
+    assertThatThrownBy(
+        () -> jedis.sendCommand(SET_KEY, Protocol.Command.SRANDMEMBER, SET_KEY, "5", "5"))
+            .hasMessageContaining(ERROR_SYNTAX);
+  }
+
+  @Test
+  public void srandmemberWithInvalidCount_returnsError() {
+    assertThatThrownBy(() -> jedis.sendCommand(SET_KEY, Protocol.Command.SRANDMEMBER, SET_KEY, "b"))
+        .hasMessageContaining(ERROR_NOT_INTEGER);
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withNonExistentSet_returnsNull() {
+    assertThat(jedis.srandmember(NON_EXISTENT_SET_KEY)).isNull();
+    assertThat(jedis.exists(NON_EXISTENT_SET_KEY)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithCount_withNonExistentSet_returnsEmptySet() {
+    assertThat(jedis.srandmember(NON_EXISTENT_SET_KEY, 1)).isEmpty();
+    assertThat(jedis.exists(NON_EXISTENT_SET_KEY)).isFalse();
+  }
+
+  @Test
+  public void srandmemberWithoutCount_withExistentSet_returnsOneMember() {
+    jedis.sadd(SET_KEY, SET_MEMBERS);
+
+    String result = jedis.srandmember(SET_KEY);
+    assertThat(SET_MEMBERS).contains(result);

Review comment:
       Strictly speaking, this assertion should be:
   ```
   assertThat(result).isIn(Arrays.asList(SET_MEMBERS));
   ```
   since the thing we're asserting on should be the unknown/variable and the thing we're comparing to should be the expected/fixed thing. In this case it's functionally equivalent, but just for following best practices, changing the assertion would be good.

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -255,48 +255,61 @@ static int setOpStoreResult(RegionProvider regionProvider, RedisKey destinationK
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
-    boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
+  public List<byte[]> srandmember(int count) {
+    if (count == 0) {
+      return Collections.emptyList();
     }
 
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
+    List<byte[]> result = new ArrayList<>();
+    int randMethodRatio = 3;
+    if (count < 0) {
+      srandomDuplicateList(-count, result);
+    } else if (count * randMethodRatio < members.size()) {
+      // Count is small enough to add random elements to result
+      srandomUniqueListWithSmallCount(count, result);
+    } else {
+      // Count either equal or greater to member size or close to the member size
+      result.addAll(members);
+
+      // Removes elemnts if count is less than member size
+      if (count < members.size()) {
+        srandomUniqueListWithLargeCount(count, result);
+      }

Review comment:
       It would be better to move the `result.addAll(members);` line and the early return logic of the if statement inside the `srandomUniqueListWithLargeCount()` method, so that method doing the same as `srandomUniqueListWithSmallCount()` in terms of what gets passed in as arguments. Having one of them expect an empty list and one expect a full list is a little counterintuitive.
   
   With this change, srandomUniqueListWithLargeCount() becomes:
   ```
     private void srandomUniqueListWithLargeCount(int count, List<byte[]> result) {
       Random rand = new Random();
       result.addAll(members);
       if (count >= members.size()) {
         return;
       }
       int resultSize;
       while ((resultSize = result.size()) != count) {
         int randIndex = rand.nextInt(resultSize);
         result.remove(randIndex);
       }
     }
   ```
   
   However, it's quite expensive to do repeated array removals, as each one is O(n), so this approach should be tweaked to use a `MemberSet` as the intermediate collection rather than an `ArrayList`, since removing an element from a set is O(1). A more efficient implementation would then be something like:
   ```
     private void srandomUniqueListWithLargeCount(int count, List<byte[]> result) {
       if (count >= members.size()) {
         result.addAll(members);
         return;
       }
       MemberSet duplicateSet = new MemberSet(members);
       Random rand = new Random();
       
       while (duplicateSet.size() != count) {
         int randIndex = rand.nextInt(duplicateSet.getMemberMapSize());
         byte[] member = duplicateSet.getKey(randIndex);
         if (member != null) {
           duplicateSet.remove(member);
         }
       }
       result.addAll(duplicateSet);
     }
   ```
   
   A small side benefit is that the code in this method can then be refactored and used in SPOP, since that's effectively what's being done on `duplicateSet`.




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



[GitHub] [geode] dschneider-pivotal commented on a change in pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
dschneider-pivotal commented on a change in pull request #7228:
URL: https://github.com/apache/geode/pull/7228#discussion_r777676361



##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,42 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();
-
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
-      }
-      return result;
+    List<byte[]> randMembers = new ArrayList<>(count);

Review comment:
       would "randomMembers" be a better name? Or maybe even better "result"?

##########
File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java
##########
@@ -161,49 +162,42 @@ public RedisSet() {}
     return popped;
   }
 
-  public Collection<byte[]> srandmember(int count) {
-    int membersSize = members.size();
+  public List<byte[]> srandmember(int count) {
     boolean duplicatesAllowed = count < 0;
-    if (duplicatesAllowed) {
-      count = -count;
-    }
-
-    if (!duplicatesAllowed && membersSize <= count && count != 1) {
-      return new ArrayList<>(members);
-    }
+    count = duplicatesAllowed ? -count : count;
+    int membersSize = members.size();
+    int memberMapSize = members.getMemberMapSize();
 
     Random rand = new Random();
-
-    // TODO: this could be optimized to take advantage of MemberSet
-    // storing its data in an array. We probably don't need to copy it
-    // into another array here.
-    byte[][] entries = members.toArray(new byte[membersSize][]);
-
-    if (count == 1) {
-      byte[] randEntry = entries[rand.nextInt(entries.length)];
-      // TODO: Now that the result is no longer serialized this could use singleton.
-      // Note using ArrayList because Collections.singleton has serialization issues.
-      List<byte[]> result = new ArrayList<>(1);
-      result.add(randEntry);
-      return result;
-    }
-    if (duplicatesAllowed) {
-      List<byte[]> result = new ArrayList<>(count);
-      while (count > 0) {
-        result.add(entries[rand.nextInt(entries.length)]);
-        count--;
-      }
-      return result;
+    List<byte[]> randMembers = new ArrayList<>(count);
+    if (!duplicatesAllowed && membersSize <= count) {
+      randMembers.addAll(members);
     } else {
-      Set<byte[]> result = new MemberSet(count);
-      // Note that rand.nextInt can return duplicates when "count" is high
-      // so we need to use a Set to collect the results.
-      while (result.size() < count) {
-        byte[] s = entries[rand.nextInt(entries.length)];
-        result.add(s);
+      Set<Integer> usedPos = null;
+      if (!duplicatesAllowed) {
+        usedPos = new HashSet<>(count);
+      }
+
+      for (int i = 0; i < count; i++) {
+        int randNum = rand.nextInt(memberMapSize);
+        byte[] member = members.getKeyAtIndex(randNum);
+
+        while (member == null || (!duplicatesAllowed && usedPos.contains(randNum))) {
+          // TODO: Can a member be null?
+          if (!duplicatesAllowed && member == null) {

Review comment:
       I think you can remove this "if" block. If member is null we don't add it to the result. By adding the randNum that is an index to "null" you make the usedPos set bigger but if in the future we get the same value in "randNum" we still also call "getKeyAtIndex" and it will return "null". So in the while loop we see "member == null" is true so we don't even do the usedPos.contains check. So I think it would be better to get rid of this "if" block and only add to usedPos when we actually add to "randMembers" which we already do down on line 195.




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



[GitHub] [geode] Kris-10-0 commented on pull request #7228: feature/GEODE-9834: SRANDMEMBER Command Support

Posted by GitBox <gi...@apache.org>.
Kris-10-0 commented on pull request #7228:
URL: https://github.com/apache/geode/pull/7228#issuecomment-1007602259


   Forced push to rebase


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