sabbey37 commented on a change in pull request #6489: URL: https://github.com/apache/geode/pull/6489#discussion_r636170733
########## 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: That's correct, the used memory should stay the same if given the XX option for a new sorted set. The key itself should never be created and it should not be added to the region at all. In `NullRedisSortedSet` you've already added code to check if the option is XX, which I think is good: ``` if (options.isXX()) { return 0; } region.create(key, new RedisSortedSet(membersToAdd)); ``` With that code in place, we don't actually need to pass the options to the `RedisSortedSet` constructor... and we can get rid of that constructor altogether. Try creating a new sorted set with the XX option in native Redis, then check if it exists... it should not exist at all. -- 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