Guosmilesmile commented on code in PR #15996: URL: https://github.com/apache/iceberg/pull/15996#discussion_r3098184650
########## flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/EqualityFieldSerializer.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.iceberg.flink.maintenance.operator; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.ByteBuffer; +import java.util.List; +import org.apache.iceberg.StructLike; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; + +/** + * Serializes equality field values into an {@link SerializedEqualityValues} for use as a Flink + * keyed state key. Using the full serialized key (rather than a hash) eliminates + * hash-collision-induced data loss. + * + * <p>Supports multiple equality field sets by prefixing the serialized key with the sorted field + * IDs. + */ +class EqualityFieldSerializer { + + private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + private final DataOutputStream dos = new DataOutputStream(baos); + + /** + * Serializes a primary key to an {@link SerializedEqualityValues}. The field ID prefix keeps keys + * from different equality field sets separate. Reuses internal buffers, so this instance must not + * be shared across threads. + */ + public SerializedEqualityValues serializeKey(StructLike key, Types.StructType keyType) { + baos.reset(); + try { + List<Types.NestedField> fields = keyType.fields(); + dos.writeInt(fields.size()); + for (int i = 0; i < fields.size(); i++) { + dos.writeInt(fields.get(i).fieldId()); + } + + for (int i = 0; i < fields.size(); i++) { + serializeField(key, i, fields.get(i).type()); + } + + dos.flush(); + } catch (IOException e) { + throw new UncheckedIOException("Failed to serialize PK index key", e); + } + + return new SerializedEqualityValues(baos.toByteArray()); + } + + private void serializeField(StructLike key, int pos, Type fieldType) throws IOException { + Object value = key.get(pos, Object.class); + if (value == null) { + dos.writeBoolean(true); + return; + } + + dos.writeBoolean(false); + ByteBuffer buf = Conversions.toByteBuffer(fieldType, value); + dos.writeInt(buf.limit()); + dos.write(buf.array(), buf.arrayOffset() + buf.position(), buf.limit()); Review Comment: Should we use limit() here or use remaining() since position may be not always 0? -- 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]
