sabbey37 commented on a change in pull request #6489: URL: https://github.com/apache/geode/pull/6489#discussion_r635390119
########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/data/RedisSortedSet.java ########## @@ -0,0 +1,286 @@ +/* + * 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.RedisDataType.REDIS_SORTED_SET; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import it.unimi.dsi.fastutil.bytes.ByteArrays; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap; + +import org.apache.geode.cache.Region; +import org.apache.geode.internal.InternalDataSerializer; +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.delta.AddsDeltaInfo; +import org.apache.geode.redis.internal.delta.DeltaInfo; +import org.apache.geode.redis.internal.delta.RemsDeltaInfo; +import org.apache.geode.redis.internal.executor.sortedset.SortedSetOptions; + +public class RedisSortedSet extends AbstractRedisData { + private Object2ObjectOpenCustomHashMap<byte[], byte[]> members; + + protected static final int BASE_REDIS_SORTED_SET_OVERHEAD = 184; + protected static final int PER_PAIR_OVERHEAD = 48; + + private int sizeInBytes; + + @Override + public int getSizeInBytes() { + return sizeInBytes; + } + + private enum AddOrChange { + ADDED, CHANGED, NOOP + } + + private int calculateSizeOfNewFieldValuePair(byte[] member, byte[] score) { + return PER_PAIR_OVERHEAD + member.length + score.length; + } + + RedisSortedSet(List<byte[]> members) { + sizeInBytes += BASE_REDIS_SORTED_SET_OVERHEAD; + this.members = new Object2ObjectOpenCustomHashMap<>(members.size(), ByteArrays.HASH_STRATEGY); + Iterator<byte[]> iterator = members.iterator(); + + while (iterator.hasNext()) { + byte[] score = iterator.next(); + byte[] member = iterator.next(); + sizeInBytes += calculateSizeOfNewFieldValuePair(member, score); + this.members.put(member, score); + } + } + + RedisSortedSet(List<byte[]> members, SortedSetOptions options) { + if (options.isXX()) { + sizeInBytes += BASE_REDIS_SORTED_SET_OVERHEAD; + this.members = new Object2ObjectOpenCustomHashMap<>(members.size(), ByteArrays.HASH_STRATEGY); + } else { + RedisSortedSet tempSet = new RedisSortedSet(members); + this.members = tempSet.members; + this.sizeInBytes = tempSet.sizeInBytes; + } + } Review comment: I think we could do something like this instead to clean this up a bit (removing the addition of the overhead to `sizeInBytes` once the variable is initialized with that): ``` RedisSortedSet(List<byte[]> members) { sizeInBytes += BASE_REDIS_SORTED_SET_OVERHEAD; this.members = new Object2ObjectOpenCustomHashMap<>(members.size(), ByteArrays.HASH_STRATEGY); Iterator<byte[]> iterator = members.iterator(); while (iterator.hasNext()) { byte[] score = iterator.next(); byte[] member = iterator.next(); sizeInBytes += calculateSizeOfNewFieldValuePair(member, score); this.members.put(member, score); } } ``` -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org