ringles commented on a change in pull request #7261:
URL: https://github.com/apache/geode/pull/7261#discussion_r794943416



##########
File path: 
geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableByteArrayList.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.data.collections;
+
+import static org.apache.geode.internal.JvmSizeUtils.getObjectHeaderSize;
+import static org.apache.geode.internal.JvmSizeUtils.getReferenceSize;
+import static org.apache.geode.internal.JvmSizeUtils.memoryOverhead;
+import static org.apache.geode.internal.JvmSizeUtils.roundUpSize;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.ListIterator;
+
+import org.apache.geode.internal.size.Sizeable;
+
+public class SizeableByteArrayList extends LinkedList<byte[]> implements 
Sizeable {
+  private static final int BYTE_ARRAY_LIST_OVERHEAD = 
memoryOverhead(SizeableByteArrayList.class);
+  private static final int NODE_OVERHEAD =
+      roundUpSize(getObjectHeaderSize() + 3 * getReferenceSize());
+  private static final int BYTE_ARRAY_BASE_OVERHEAD = 16;
+  private int memberOverhead;
+
+  @Override
+  public int indexOf(Object o) {
+    ListIterator<byte[]> iterator = this.listIterator();
+    while (iterator.hasNext()) {
+      int index = iterator.nextIndex();
+      byte[] element = iterator.next();
+      if (Arrays.equals(element, (byte[]) o)) {
+        return index;
+      }
+    }
+    return -1;
+  }
+
+  @Override
+  public int lastIndexOf(Object o) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public boolean remove(Object o) {
+    int index = indexOf(o);
+    if (index == -1) {
+      return false;
+    }
+    memberOverhead -= calculateByteArrayOverhead((byte[]) o);
+    remove(index);
+    return true;

Review comment:
       I think it's slightly clearer if we pass 'element' to 
'calculateByteArrayOverhead()' but otherwise good catch!

##########
File path: 
geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableByteArrayList.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.data.collections;
+
+import static org.apache.geode.internal.JvmSizeUtils.getObjectHeaderSize;
+import static org.apache.geode.internal.JvmSizeUtils.getReferenceSize;
+import static org.apache.geode.internal.JvmSizeUtils.memoryOverhead;
+import static org.apache.geode.internal.JvmSizeUtils.roundUpSize;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.ListIterator;
+
+import org.apache.geode.internal.size.Sizeable;
+
+public class SizeableByteArrayList extends LinkedList<byte[]> implements 
Sizeable {
+  private static final int BYTE_ARRAY_LIST_OVERHEAD = 
memoryOverhead(SizeableByteArrayList.class);
+  private static final int NODE_OVERHEAD =
+      roundUpSize(getObjectHeaderSize() + 3 * getReferenceSize());
+  private static final int BYTE_ARRAY_BASE_OVERHEAD = 16;
+  private int memberOverhead;
+
+  @Override
+  public int indexOf(Object o) {
+    ListIterator<byte[]> iterator = this.listIterator();
+    while (iterator.hasNext()) {
+      int index = iterator.nextIndex();
+      byte[] element = iterator.next();
+      if (Arrays.equals(element, (byte[]) o)) {
+        return index;
+      }
+    }
+    return -1;
+  }
+
+  @Override
+  public int lastIndexOf(Object o) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public boolean remove(Object o) {
+    int index = indexOf(o);
+    if (index == -1) {
+      return false;
+    }
+    memberOverhead -= calculateByteArrayOverhead((byte[]) o);
+    remove(index);
+    return true;
+  }
+
+  @Override
+  public byte[] remove(int index) {
+    byte[] element = super.remove(index);
+    memberOverhead -= calculateByteArrayOverhead(element);
+    return element;
+  }
+
+  @Override
+  public void addFirst(byte[] element) {
+    memberOverhead += calculateByteArrayOverhead(element);
+    super.addFirst(element);
+  }
+
+  @Override
+  public void addLast(byte[] element) {
+    memberOverhead += calculateByteArrayOverhead(element);
+    super.addLast(element);
+  }
+
+  public boolean removeLastOccurrence(Object o) {
+    throw new UnsupportedOperationException();
+  }
+
+  private int calculateByteArrayOverhead(byte[] element) {
+    return BYTE_ARRAY_BASE_OVERHEAD + (element.length % 8 == 0 ? 0 : 8) +
+        NODE_OVERHEAD + (element.length / 8) * 8;
+  }
+
+  @Override
+  public int getSizeInBytes() {
+    return BYTE_ARRAY_LIST_OVERHEAD + memberOverhead;
+  }
+
+  @Override
+  public int hashCode() {
+    final int PRIME_NUMBER = 31;

Review comment:
       camelCased.

##########
File path: 
geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableByteArrayList.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.data.collections;
+
+import static org.apache.geode.internal.JvmSizeUtils.getObjectHeaderSize;
+import static org.apache.geode.internal.JvmSizeUtils.getReferenceSize;
+import static org.apache.geode.internal.JvmSizeUtils.memoryOverhead;
+import static org.apache.geode.internal.JvmSizeUtils.roundUpSize;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.ListIterator;
+
+import org.apache.geode.internal.size.Sizeable;
+
+public class SizeableByteArrayList extends LinkedList<byte[]> implements 
Sizeable {
+  private static final int BYTE_ARRAY_LIST_OVERHEAD = 
memoryOverhead(SizeableByteArrayList.class);
+  private static final int NODE_OVERHEAD =
+      roundUpSize(getObjectHeaderSize() + 3 * getReferenceSize());
+  private static final int BYTE_ARRAY_BASE_OVERHEAD = 16;
+  private int memberOverhead;
+
+  @Override
+  public int indexOf(Object o) {
+    ListIterator<byte[]> iterator = this.listIterator();
+    while (iterator.hasNext()) {
+      int index = iterator.nextIndex();
+      byte[] element = iterator.next();
+      if (Arrays.equals(element, (byte[]) o)) {
+        return index;
+      }
+    }
+    return -1;
+  }
+
+  @Override
+  public int lastIndexOf(Object o) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public boolean remove(Object o) {
+    int index = indexOf(o);
+    if (index == -1) {
+      return false;
+    }
+    memberOverhead -= calculateByteArrayOverhead((byte[]) o);
+    remove(index);
+    return true;
+  }
+
+  @Override
+  public byte[] remove(int index) {
+    byte[] element = super.remove(index);
+    memberOverhead -= calculateByteArrayOverhead(element);
+    return element;
+  }
+
+  @Override
+  public void addFirst(byte[] element) {
+    memberOverhead += calculateByteArrayOverhead(element);
+    super.addFirst(element);
+  }
+
+  @Override
+  public void addLast(byte[] element) {
+    memberOverhead += calculateByteArrayOverhead(element);
+    super.addLast(element);
+  }
+
+  public boolean removeLastOccurrence(Object o) {
+    throw new UnsupportedOperationException();
+  }
+
+  private int calculateByteArrayOverhead(byte[] element) {
+    return BYTE_ARRAY_BASE_OVERHEAD + (element.length % 8 == 0 ? 0 : 8) +
+        NODE_OVERHEAD + (element.length / 8) * 8;
+  }
+
+  @Override
+  public int getSizeInBytes() {
+    return BYTE_ARRAY_LIST_OVERHEAD + memberOverhead;
+  }
+
+  @Override
+  public int hashCode() {
+    final int PRIME_NUMBER = 31;
+    int hashCode = 1;
+    ListIterator<byte[]> iterator = this.listIterator();
+    while (iterator.hasNext()) {
+      int index = iterator.nextIndex() + 1;
+      hashCode = hashCode * (PRIME_NUMBER % index) + 
Arrays.hashCode(iterator.next());

Review comment:
       Consequence of incorrect test below, simplified.

##########
File path: 
geode-for-redis/src/test/java/org/apache/geode/redis/internal/data/RedisListTest.java
##########
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache 
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the 
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ * or implied. See the License for the specific language governing permissions 
and limitations under
+ * the License.
+ *
+ */
+
+package org.apache.geode.redis.internal.data;
+
+import static 
org.apache.geode.redis.internal.data.NullRedisDataStructures.NULL_REDIS_LIST;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.DataOutput;
+import java.io.IOException;
+import java.lang.reflect.Modifier;
+
+import org.junit.Test;
+
+import org.apache.geode.DataSerializer;
+import org.apache.geode.internal.HeapDataOutputStream;
+import org.apache.geode.internal.serialization.ByteArrayDataInput;
+import org.apache.geode.internal.serialization.SerializationContext;
+
+public class RedisListTest {
+
+  @Test
+  public void confirmSerializationIsStable() throws IOException, 
ClassNotFoundException {
+    RedisList list1 = createRedisList(1, 2);
+    int expirationTimestamp = 1000;
+    list1.setExpirationTimestampNoDelta(expirationTimestamp);
+    HeapDataOutputStream out = new HeapDataOutputStream(100);
+    DataSerializer.writeObject(list1, out);
+    ByteArrayDataInput in = new ByteArrayDataInput(out.toByteArray());
+    RedisList list2 = DataSerializer.readObject(in);
+    assertThat(list2.getExpirationTimestamp())
+        .isEqualTo(list1.getExpirationTimestamp())
+        .isEqualTo(expirationTimestamp);
+    assertThat(list2).isEqualTo(list1);
+  }
+
+  @Test
+  public void confirmToDataIsSynchronized() throws NoSuchMethodException {
+    assertThat(Modifier
+        .isSynchronized(RedisList.class
+            .getMethod("toData", DataOutput.class, 
SerializationContext.class).getModifiers()))
+                .isTrue();
+  }
+
+  @Test
+  public void hashcode_returnsSameValue_forEqualLists() {
+    RedisList list1 = createRedisList(1, 2);
+    RedisList list2 = createRedisList(1, 2);
+    assertThat(list1).isEqualTo(list2);
+    assertThat(list1.hashCode()).isEqualTo(list2.hashCode());
+  }
+
+  @Test
+  public void hashcode_returnsDifferentValue_forDifferentLists() {
+    RedisList list1 = createRedisList(1, 2);
+    RedisList list2 = createRedisList(2, 1);
+    assertThat(list1).isNotEqualTo(list2);
+    assertThat(list1.hashCode()).isEqualTo(list2.hashCode());

Review comment:
       Fixed.

##########
File path: 
geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLLenIntegrationTest.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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_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.AtomicLong;
+
+import org.assertj.core.api.AssertionsForClassTypes;
+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 AbstractLLenIntegrationTest 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 llen_withStringFails() {
+    jedis.set("string", PREEXISTING_VALUE);
+    assertThatThrownBy(() -> 
jedis.llen("string")).hasMessageContaining(ERROR_WRONG_TYPE);
+  }
+
+  @Test
+  public void llen_givenWrongNumOfArgs_returnsError() {
+    assertExactNumberOfArgs(jedis, Protocol.Command.LLEN, 1);
+  }
+
+  @Test
+  public void llen_givenNonexistentList_returnsZero() {
+    assertThat(jedis.llen("nonexistent")).isEqualTo(0L);
+  }
+
+  @Test
+  public void llen_returnsListLength() {
+    jedis.lpush(KEY, "e1", "e2", "e3");
+    assertThat(jedis.llen(KEY)).isEqualTo(3L);
+
+    String result = jedis.lpop(KEY);
+    assertThat(result).isEqualTo("e3");
+    assertThat(jedis.llen(KEY)).isEqualTo(2L);
+
+    result = jedis.lpop(KEY);
+    assertThat(result).isEqualTo("e2");
+    assertThat(jedis.llen(KEY)).isEqualTo(1L);
+
+    result = jedis.lpop(KEY);
+    assertThat(result).isEqualTo("e1");
+    assertThat(jedis.llen(KEY)).isEqualTo(0L);
+  }
+
+  @Test
+  public void llen_withConcurrentLPush_returnsCorrectValue() {
+    String[] valuesInitial = new String[] {"one", "two", "three"};
+    String[] valuesToAdd = new String[] {"pear", "apple", "plum", "orange", 
"peach"};
+    jedis.lpush(KEY, valuesInitial);
+
+    final AtomicLong llenReference = new AtomicLong();
+    new ConcurrentLoopingThreads(1000,
+        i -> jedis.lpush(KEY, valuesToAdd),
+        i -> llenReference.set(jedis.llen(KEY)))
+            .runWithAction(() -> {
+              AssertionsForClassTypes.assertThat(llenReference).satisfiesAnyOf(
+                  llenResult -> 
AssertionsForClassTypes.assertThat(llenResult.get())
+                      .isEqualTo(valuesInitial.length),
+                  llenResult -> 
AssertionsForClassTypes.assertThat(llenResult.get())

Review comment:
       Fixed.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to