adarshsanjeev commented on code in PR #16269: URL: https://github.com/apache/druid/pull/16269#discussion_r1616790362
########## extensions-core/multi-stage-query/src/main/java/org/apache/druid/msq/statistics/serde/ClusterByStatisticsSnapshotSerde.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.druid.msq.statistics.serde; + +import org.apache.druid.frame.key.RowKey; +import org.apache.druid.msq.statistics.ClusterByStatisticsSnapshot; +import org.apache.druid.msq.statistics.KeyCollectorSnapshot; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Handles the serialization and deserialization of {@link ClusterByStatisticsSnapshot}, into a byte array. + */ +public class ClusterByStatisticsSnapshotSerde +{ + /** + * Deserializes the {@link ClusterByStatisticsSnapshot} and writes it to the {@link OutputStream}. + * <br> + * Format: + * - 1 byte : Header byte + * - 4 bytes: Number of buckets + * - 4 bytes: Number of entries in {@link ClusterByStatisticsSnapshot#getHasMultipleValues()} + * - 4 * number of multivalue bucket bytes: List of integers + * - number of buckets * buckets, serailized by {@link #serializeBucket(OutputStream, ClusterByStatisticsSnapshot.Bucket)} + */ + public static void serialize(OutputStream outputStream, ClusterByStatisticsSnapshot snapshot) throws IOException + { + final Map<Long, ClusterByStatisticsSnapshot.Bucket> buckets = snapshot.getBuckets(); + final Set<Integer> multipleValueBuckets = snapshot.getHasMultipleValues(); + + // Write a header byte, to be used to contain any metadata in the future. + outputStream.write(0x0); + + writeIntToStream(outputStream, buckets.size()); + ByteBuffer multivalueBuffer = ByteBuffer.allocate(Integer.BYTES + multipleValueBuckets.size()) + .putInt(multipleValueBuckets.size()); + multipleValueBuckets.forEach(multivalueBuffer::putInt); + outputStream.write(multivalueBuffer.array()); + + for (Map.Entry<Long, ClusterByStatisticsSnapshot.Bucket> entry : buckets.entrySet()) { + writeLongToStream(outputStream, entry.getKey()); + serializeBucket(outputStream, entry.getValue()); + } + } + + private static final int HEADER_OFFSET = 0; + private static final int BUCKET_COUNT_OFFSET = HEADER_OFFSET + Byte.BYTES; + private static final int MV_SET_SIZE_OFFSET = BUCKET_COUNT_OFFSET + Integer.BYTES; + private static final int MV_VALUES_OFFSET = MV_SET_SIZE_OFFSET + Integer.BYTES; + + private static final int TIMECHUNK_OFFSET = 0; + private static final int BUCKET_SIZE_OFFSET = TIMECHUNK_OFFSET + Long.BYTES; + private static final int BUCKET_OFFSET = BUCKET_SIZE_OFFSET + Integer.BYTES; + + public static ClusterByStatisticsSnapshot deserialize(ByteBuffer byteBuffer) + { + int position = byteBuffer.position(); + + final int bucketCount = byteBuffer.getInt(position + BUCKET_COUNT_OFFSET); + final int mvSetSize = byteBuffer.getInt(position + MV_SET_SIZE_OFFSET); + + final Set<Integer> hasMultiValues = new HashSet<>(); + for (int offset = position + MV_VALUES_OFFSET; offset < position + mvSetSize * Integer.BYTES; offset += Integer.BYTES) { Review Comment: Thanks for catching this! -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
