ringles commented on a change in pull request #6489: URL: https://github.com/apache/geode/pull/6489#discussion_r636236362
########## 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; + } + } + + // for serialization + public RedisSortedSet() {} + + @Override + protected void applyDelta(DeltaInfo deltaInfo) { + if (deltaInfo instanceof AddsDeltaInfo) { + AddsDeltaInfo addsDeltaInfo = (AddsDeltaInfo) deltaInfo; + membersAddAll(addsDeltaInfo); + } else { + RemsDeltaInfo remsDeltaInfo = (RemsDeltaInfo) deltaInfo; + membersRemoveAll(remsDeltaInfo); + } + } + + /** + * 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 "members" + * needs to be thread safe with toData. + */ + + @Override + public synchronized void toData(DataOutput out, SerializationContext context) throws IOException { + super.toData(out, context); + InternalDataSerializer.writePrimitiveInt(members.size(), out); + for (Map.Entry<byte[], byte[]> entry : members.entrySet()) { + byte[] key = entry.getKey(); + byte[] value = entry.getValue(); + InternalDataSerializer.writeByteArray(key, out); + InternalDataSerializer.writeByteArray(value, out); + } + InternalDataSerializer.writePrimitiveInt(sizeInBytes, out); + } + + @Override + public void fromData(DataInput in, DeserializationContext context) + throws IOException, ClassNotFoundException { + super.fromData(in, context); + int size = InternalDataSerializer.readPrimitiveInt(in); + members = new Object2ObjectOpenCustomHashMap<>(size, ByteArrays.HASH_STRATEGY); + for (int i = 0; i < size; i++) { + members.put(InternalDataSerializer.readByteArray(in), + InternalDataSerializer.readByteArray(in)); + } + sizeInBytes = InternalDataSerializer.readPrimitiveInt(in); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof RedisSortedSet)) { + return false; + } + if (!super.equals(o)) { + return false; + } + RedisSortedSet redisSortedSet = (RedisSortedSet) o; + if (members.size() != redisSortedSet.members.size()) { + return false; + } + + for (Map.Entry<byte[], byte[]> entry : members.entrySet()) { + if (!Arrays.equals(redisSortedSet.members.get(entry.getKey()), (entry.getValue()))) { + return false; + } + } + return true; + } + + @Override + public int getDSFID() { + return REDIS_SORTED_SET_ID; + } + + protected synchronized AddOrChange memberAdd(byte[] memberToAdd, byte[] scoreToAdd) { + boolean added = (members.put(memberToAdd, scoreToAdd) == null); + if (added) { + sizeInBytes += calculateSizeOfNewFieldValuePair(memberToAdd, scoreToAdd); + } + return added ? AddOrChange.ADDED : AddOrChange.NOOP; + } + + private synchronized void membersAddAll(AddsDeltaInfo addsDeltaInfo) { + Iterator<byte[]> iterator = addsDeltaInfo.getAdds().iterator(); + while (iterator.hasNext()) { + byte[] member = iterator.next(); + sizeInBytes += PER_PAIR_OVERHEAD + member.length; + byte[] score = iterator.next(); + sizeInBytes += PER_PAIR_OVERHEAD + score.length; + members.put(member, score); + } + } + + + private synchronized void membersRemoveAll(RemsDeltaInfo remsDeltaInfo) { + for (byte[] member : remsDeltaInfo.getRemoves()) { + sizeInBytes -= (PER_PAIR_OVERHEAD + member.length); + members.remove(member); + } + } + + /** + * @param region the region this instance is stored in + * @param key the name of the set to add to + * @param membersToAdd members to add to this set; NOTE this list may by modified by this call + * @return the number of members actually added + */ + long zadd(Region<RedisKey, RedisData> region, RedisKey key, List<byte[]> membersToAdd, + SortedSetOptions options) { + int membersAdded = 0; + if (options == null) { + options = new SortedSetOptions(SortedSetOptions.Exists.NONE, SortedSetOptions.Update.NONE); + } + AddsDeltaInfo deltaInfo = null; + Iterator<byte[]> iterator = membersToAdd.iterator(); + while (iterator.hasNext()) { + boolean delta = true; + byte[] score = iterator.next(); + byte[] member = iterator.next(); + + if (options.isNX()) { + if (members.get(member) != null) { + continue; + } + } + if (options.isXX()) { + if (members.get(member) == null) { + continue; + } + } Review comment: Cleaner, 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org