ringles commented on a change in pull request #7403: URL: https://github.com/apache/geode/pull/7403#discussion_r832335002
########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java ########## @@ -359,8 +359,10 @@ public int scard() { } @Override - public void applyAddByteArrayDelta(byte[] bytes) { - membersAdd(bytes); + public void applyAddByteArrayDelta(List<byte[]> byteArrays) { Review comment: Mooted after rebase ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisList.java ########## @@ -127,50 +127,139 @@ private int getArrayIndex(int listIndex) { } /** - * @param elementsToAdd elements to add to this set; NOTE this list may by modified by this call - * @param region the region this instance is stored in - * @param key the name of the set to add to - * @param onlyIfExists if true then the elements should only be added if the key already exists - * and holds a list, otherwise no operation is performed. - * @return the length of the list after the operation - */ - public long lpush(List<byte[]> elementsToAdd, Region<RedisKey, RedisData> region, RedisKey key, - final boolean onlyIfExists) { - elementsPush(elementsToAdd); - storeChanges(region, key, new AddByteArrays(elementsToAdd)); + * @return the number of elements in the list + **/ + public int llen() { return elementList.size(); } /** * @param region the region this instance is stored in - * @param key the name of the set to add to + * @param key the name of the list to add to * @return the element actually popped */ public byte[] lpop(Region<RedisKey, RedisData> region, RedisKey key) { - byte[] popped = elementRemove(0); - RemoveElementsByIndex removed = new RemoveElementsByIndex(); - removed.add(0); + byte newVersion; + byte[] popped; + RemoveElementsByIndex removed; + synchronized (this) { + newVersion = incrementAndGetVersion(); + popped = removeFirstElement(); + removed = new RemoveElementsByIndex(newVersion); + removed.add(0); + } storeChanges(region, key, removed); return popped; } + public synchronized byte[] removeFirstElement() { + return elementList.removeFirst(); + } + + public synchronized byte[] removeLastElement() { + return elementList.removeLast(); + } + /** - * @return the number of elements in the list + * @param elementsToAdd elements to add to this list; NOTE this list may be modified by this call + * @param region the region this instance is stored in + * @param key the name of the list to add to + * @param onlyIfExists if true then the elements should only be added if the key already exists + * and holds a list, otherwise no operation is performed. + * @return the length of the list after the operation */ - public int llen() { + public long lpush(List<byte[]> elementsToAdd, Region<RedisKey, RedisData> region, RedisKey key, + final boolean onlyIfExists) { + byte newVersion; + AddByteArrays addByteArrays; + synchronized (this) { + newVersion = incrementAndGetVersion(); + elementsPush(elementsToAdd); + addByteArrays = new AddByteArrays(newVersion, elementsToAdd); + } + storeChanges(region, key, addByteArrays); return elementList.size(); } + /** + * @param elementsToAdd elements to add to this list; NOTE this list may be modified by this call + * @param region the region this instance is stored in + * @param key the name of the list to add to + * @return the number of elements actually added + */ + public long lpush(List<byte[]> elementsToAdd, Region<RedisKey, RedisData> region, RedisKey key) { + byte newVersion; + AddByteArrays addByteArrays; + synchronized (this) { + newVersion = incrementAndGetVersion(); + elementsPush(elementsToAdd); + addByteArrays = new AddByteArrays(newVersion, elementsToAdd); + } + storeChanges(region, key, addByteArrays); + return elementList.size(); + } + + public byte[] ltrim(long start, long end, Region<RedisKey, RedisData> region, Review comment: Fixed ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/list/LTrimExecutor.java ########## @@ -0,0 +1,57 @@ +/* + * 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.list; + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; + +import java.util.List; + +import org.apache.geode.cache.Region; +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.RedisData; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.netty.Coder; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class LTrimExecutor implements CommandExecutor { + private static final int startIndex = 2; + private static final int stopIndex = 3; + + @Override + public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) { + List<byte[]> commandElems = command.getProcessedCommand(); + Region<RedisKey, RedisData> region = context.getRegion(); + RedisKey key = command.getKey(); + + long start; + long end; + + try { + byte[] startI = commandElems.get(startIndex); + byte[] stopI = commandElems.get(stopIndex); + start = Coder.bytesToLong(startI); + end = Coder.bytesToLong(stopI); + } catch (NumberFormatException e) { + return RedisResponse.error(ERROR_NOT_INTEGER); + } + + byte[] retVal = + context.listLockedExecute(key, false, list -> list.ltrim(start, end, region, key)); + // return RedisResponse.error(ERROR_NOT_INTEGER); Review comment: Yanked ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/commands/executor/list/LTrimExecutor.java ########## @@ -0,0 +1,57 @@ +/* + * 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.list; + +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; + +import java.util.List; + +import org.apache.geode.cache.Region; +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.RedisData; +import org.apache.geode.redis.internal.data.RedisKey; +import org.apache.geode.redis.internal.netty.Coder; +import org.apache.geode.redis.internal.netty.ExecutionHandlerContext; + +public class LTrimExecutor implements CommandExecutor { + private static final int startIndex = 2; + private static final int stopIndex = 3; + + @Override + public RedisResponse executeCommand(Command command, ExecutionHandlerContext context) { + List<byte[]> commandElems = command.getProcessedCommand(); + Region<RedisKey, RedisData> region = context.getRegion(); + RedisKey key = command.getKey(); + + long start; + long end; + + try { + byte[] startI = commandElems.get(startIndex); + byte[] stopI = commandElems.get(stopIndex); + start = Coder.bytesToLong(startI); + end = Coder.bytesToLong(stopI); Review comment: Streamlined by inlining ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLTrimIntegrationTest.java ########## @@ -0,0 +1,228 @@ +/* + * 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.list; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; +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.concurrent.atomic.AtomicReference; + +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.ConcurrentLoopingThreads; +import org.apache.geode.redis.RedisIntegrationTest; + +public abstract class AbstractLTrimIntegrationTest implements RedisIntegrationTest { + public static final String KEY = "key"; + public static final String PREEXISTING_VALUE = "preexistingValue"; + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void givenWrongNumOfArgs_returnsError() { + assertExactNumberOfArgs(jedis, Protocol.Command.LTRIM, 3); + } + + @Test + public void withNonListKey_Fails() { + jedis.set("string", PREEXISTING_VALUE); + assertThatThrownBy(() -> jedis.ltrim("string", 0, -1)) + .hasMessage(ERROR_WRONG_TYPE); + } + + @Test + public void withNonExistentKey_returnsNull() { + assertThat(jedis.ltrim("nonexistent", 0, -1)).isEqualTo("OK"); + } + + @Test + public void withNonIntegerRangeSpecifier_Fails() { + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "0", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "-1")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + } + + @Test + public void trimsToSpecifiedRange_givenValidRange() { + initializeTestList(); + + jedis.ltrim(KEY, 0, 0); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 2); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, 2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, -2, -1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, -1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e1"); + } + + @Test + public void trimsToCorrectRange_givenSpecifiersOutsideListSize() { + initializeTestList(); + + jedis.ltrim(KEY, -4, -1); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, -10, 10); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 4); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 10); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + } + + private void initializeTestList() { + jedis.del(KEY); + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + } + + @Test + public void removesKey_whenLastElementRemoved() { + final String keyWithTagForKeysCommand = "{tag}" + KEY; + jedis.lpush(keyWithTagForKeysCommand, "e1", "e2", "e3"); + + jedis.ltrim(keyWithTagForKeysCommand, 0, -4); + assertThat(jedis.llen(keyWithTagForKeysCommand)).isEqualTo(0L); + assertThat(jedis.keys(keyWithTagForKeysCommand)).isEmpty(); + } + + @Test + public void removesKey_whenLastElementRemoved_multipleTimes() { + final String keyWithTagForKeysCommand = "{tag}" + KEY; + jedis.lpush(keyWithTagForKeysCommand, "e1", "e2", "e3"); + + jedis.ltrim(keyWithTagForKeysCommand, 0, -4); + jedis.ltrim(keyWithTagForKeysCommand, 0, -4); + + assertThat(jedis.keys(keyWithTagForKeysCommand)).isEmpty(); + } + + @Test + public void withConcurrentLPush_returnsCorrectValue() { + String[] valuesInitial = new String[] {"un", "deux", "troix"}; Review comment: Mais oui! ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLTrimIntegrationTest.java ########## @@ -0,0 +1,228 @@ +/* + * 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.list; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; +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.concurrent.atomic.AtomicReference; + +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.ConcurrentLoopingThreads; +import org.apache.geode.redis.RedisIntegrationTest; + +public abstract class AbstractLTrimIntegrationTest implements RedisIntegrationTest { + public static final String KEY = "key"; + public static final String PREEXISTING_VALUE = "preexistingValue"; + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void givenWrongNumOfArgs_returnsError() { + assertExactNumberOfArgs(jedis, Protocol.Command.LTRIM, 3); + } + + @Test + public void withNonListKey_Fails() { + jedis.set("string", PREEXISTING_VALUE); + assertThatThrownBy(() -> jedis.ltrim("string", 0, -1)) + .hasMessage(ERROR_WRONG_TYPE); + } + + @Test + public void withNonExistentKey_returnsNull() { + assertThat(jedis.ltrim("nonexistent", 0, -1)).isEqualTo("OK"); + } + + @Test + public void withNonIntegerRangeSpecifier_Fails() { + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "0", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "-1")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + } + + @Test + public void trimsToSpecifiedRange_givenValidRange() { + initializeTestList(); + + jedis.ltrim(KEY, 0, 0); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 2); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, 2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, -2, -1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, -1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e1"); + } + + @Test + public void trimsToCorrectRange_givenSpecifiersOutsideListSize() { + initializeTestList(); + + jedis.ltrim(KEY, -4, -1); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, -10, 10); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 4); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 10); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + } + + private void initializeTestList() { + jedis.del(KEY); + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + } + + @Test + public void removesKey_whenLastElementRemoved() { + final String keyWithTagForKeysCommand = "{tag}" + KEY; + jedis.lpush(keyWithTagForKeysCommand, "e1", "e2", "e3"); + + jedis.ltrim(keyWithTagForKeysCommand, 0, -4); + assertThat(jedis.llen(keyWithTagForKeysCommand)).isEqualTo(0L); + assertThat(jedis.keys(keyWithTagForKeysCommand)).isEmpty(); Review comment: Done for both ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLTrimIntegrationTest.java ########## @@ -0,0 +1,228 @@ +/* + * 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.list; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; +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.concurrent.atomic.AtomicReference; + +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.ConcurrentLoopingThreads; +import org.apache.geode.redis.RedisIntegrationTest; + +public abstract class AbstractLTrimIntegrationTest implements RedisIntegrationTest { + public static final String KEY = "key"; + public static final String PREEXISTING_VALUE = "preexistingValue"; + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void givenWrongNumOfArgs_returnsError() { + assertExactNumberOfArgs(jedis, Protocol.Command.LTRIM, 3); + } + + @Test + public void withNonListKey_Fails() { + jedis.set("string", PREEXISTING_VALUE); + assertThatThrownBy(() -> jedis.ltrim("string", 0, -1)) + .hasMessage(ERROR_WRONG_TYPE); + } + + @Test + public void withNonExistentKey_returnsNull() { + assertThat(jedis.ltrim("nonexistent", 0, -1)).isEqualTo("OK"); + } + + @Test + public void withNonIntegerRangeSpecifier_Fails() { + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "0", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "-1")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + } + + @Test + public void trimsToSpecifiedRange_givenValidRange() { + initializeTestList(); + + jedis.ltrim(KEY, 0, 0); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 2); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, 2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, -2, -1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, -1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e1"); + } + + @Test + public void trimsToCorrectRange_givenSpecifiersOutsideListSize() { + initializeTestList(); + + jedis.ltrim(KEY, -4, -1); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + initializeTestList(); Review comment: In this test case, you're right. ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java ########## @@ -422,16 +424,21 @@ synchronized boolean membersRemove(byte[] memberToRemove) { * @return the number of members actually added */ public long sadd(List<byte[]> membersToAdd, Region<RedisKey, RedisData> region, RedisKey key) { - AddByteArrays delta = new AddByteArrays(); + AddByteArrays delta; + byte newVersion; int membersAdded = 0; - for (byte[] member : membersToAdd) { - if (membersAdd(member)) { - delta.add(member); - membersAdded++; + synchronized (this) { + newVersion = incrementAndGetVersion(); + delta = new AddByteArrays(newVersion); + for (byte[] member : membersToAdd) { + if (membersAdd(member)) { + delta.add(member); + membersAdded++; + } + } + if (membersAdded == 0) { Review comment: No longer touching RedisSet after rebase ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSet.java ########## @@ -422,16 +424,21 @@ synchronized boolean membersRemove(byte[] memberToRemove) { * @return the number of members actually added */ public long sadd(List<byte[]> membersToAdd, Region<RedisKey, RedisData> region, RedisKey key) { - AddByteArrays delta = new AddByteArrays(); + AddByteArrays delta; + byte newVersion; int membersAdded = 0; - for (byte[] member : membersToAdd) { - if (membersAdd(member)) { - delta.add(member); - membersAdded++; + synchronized (this) { + newVersion = incrementAndGetVersion(); Review comment: Reworked -- 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-unsubscr...@geode.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org