dschneider-pivotal commented on a change in pull request #7261: URL: https://github.com/apache/geode/pull/7261#discussion_r797916793
########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisList.java ########## @@ -0,0 +1,180 @@ +/* + * 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.internal.JvmSizeUtils.memoryOverhead; +import static org.apache.geode.redis.internal.data.RedisDataType.REDIS_LIST; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.List; +import java.util.Objects; + +import org.apache.geode.DataSerializer; +import org.apache.geode.cache.Region; +import org.apache.geode.internal.serialization.DeserializationContext; +import org.apache.geode.internal.serialization.KnownVersion; +import org.apache.geode.internal.serialization.SerializationContext; +import org.apache.geode.redis.internal.data.collections.SizeableByteArrayList; +import org.apache.geode.redis.internal.data.delta.AddByteArrays; +import org.apache.geode.redis.internal.data.delta.RemoveElementsByIndex; + +public class RedisList extends AbstractRedisData { + protected static final int REDIS_LIST_OVERHEAD = memoryOverhead(RedisList.class); + private SizeableByteArrayList elementList; + + public RedisList() { + this.elementList = new SizeableByteArrayList(); + } + + /** + * @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 + * @return the number of elements actually added + */ + public long lpush(List<byte[]> elementsToAdd, Region<RedisKey, RedisData> region, RedisKey key) { + for (byte[] element : elementsToAdd) { + elementPush(element); + } + storeChanges(region, key, new AddByteArrays(elementsToAdd)); + return elementList.size(); + } + + /** + * @param region the region this instance is stored in + * @param key the name of the set 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); + storeChanges(region, key, removed); + return popped; + } + + /** + * @return the number of elements in the list + */ + public int llen() { + return elementList.size(); + } + + @Override + public void applyAddByteArrayDelta(byte[] bytes) { + elementPush(bytes); + } + + @Override + public void applyRemoveElementsByIndex(List<Integer> indexes) { + for (int index : indexes) { + elementRemove(index); + } + } + + /** + * Since GII (getInitialImage) can come in and call toData while other threads are modifying this + * object, the striped executor will not protect toData. So any methods that modify "elements" + * needs to be thread safe with toData. + */ + + @Override + public synchronized void toData(DataOutput out, SerializationContext context) throws IOException { + super.toData(out, context); + DataSerializer.writePrimitiveInt(elementList.size(), out); + for (byte[] element : elementList) { + DataSerializer.writeByteArray(element, out); + } + } + + @Override + public void fromData(DataInput in, DeserializationContext context) + throws IOException, ClassNotFoundException { + super.fromData(in, context); + int size = DataSerializer.readPrimitiveInt(in); + SizeableByteArrayList tempElementList = new SizeableByteArrayList(); + for (int i = 0; i < size; ++i) { + byte[] element = DataSerializer.readByteArray(in); + tempElementList.addLast(element); + } + elementList = tempElementList; Review comment: I might be missing something but it looks to me like if you changed this method to not assign "elementList" then the "elementList" field could be final. Making it final is worth doing. fromData is called an an instance that was just created with the default constructor which sets "elementList" to a new empty list. So couldn't this method just add to "elementList" instead of a temp list? And then change "elementList" to be "final". ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableByteArrayList.java ########## @@ -0,0 +1,130 @@ +/* + * 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) { + ListIterator<byte[]> iterator = this.listIterator(); + while (iterator.hasNext()) { + byte[] element = iterator.next(); + if (Arrays.equals(element, (byte[]) o)) { + iterator.remove(); + memberOverhead -= calculateByteArrayOverhead(element); + return true; + } + } + return false; + } + + @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 primeNumber = 31; + int hashCode = 1; + ListIterator<byte[]> iterator = this.listIterator(); Review comment: I'm not sure why an iterator is used here. Could you instead just do `for (byte[] element: this)`? ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableByteArrayList.java ########## @@ -0,0 +1,130 @@ +/* + * 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) { + ListIterator<byte[]> iterator = this.listIterator(); + while (iterator.hasNext()) { + byte[] element = iterator.next(); + if (Arrays.equals(element, (byte[]) o)) { + iterator.remove(); + memberOverhead -= calculateByteArrayOverhead(element); + return true; + } + } + return false; + } + + @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 primeNumber = 31; + int hashCode = 1; + ListIterator<byte[]> iterator = this.listIterator(); + while (iterator.hasNext()) { + hashCode = hashCode * primeNumber + Arrays.hashCode(iterator.next()); + } + return hashCode; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof SizeableByteArrayList)) { + return false; + } + SizeableByteArrayList sizeableByteArrayList = (SizeableByteArrayList) o; + if (sizeableByteArrayList.size() != this.size()) { + return false; + } + for (int i = 0; i < sizeableByteArrayList.size(); i++) { Review comment: given that these are linked lists it seems wrong keep calling get(i) which is going to need to scan the list each time because random access is not supported. I think it would be better to create two iterators, one for each list, and then loop around advancing each iterator on each iteration. ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/collections/SizeableByteArrayList.java ########## @@ -0,0 +1,130 @@ +/* + * 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) { + ListIterator<byte[]> iterator = this.listIterator(); + while (iterator.hasNext()) { + byte[] element = iterator.next(); + if (Arrays.equals(element, (byte[]) o)) { + iterator.remove(); + memberOverhead -= calculateByteArrayOverhead(element); + return true; + } + } + return false; + } + + @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 primeNumber = 31; + int hashCode = 1; + ListIterator<byte[]> iterator = this.listIterator(); + while (iterator.hasNext()) { + hashCode = hashCode * primeNumber + Arrays.hashCode(iterator.next()); Review comment: could this be `hashCode *= primeNumber + ...`? -- 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]
