MehulBatra commented on code in PR #1404: URL: https://github.com/apache/fluss/pull/1404#discussion_r2365817888
########## fluss-client/src/main/java/com/alibaba/fluss/client/utils/ConverterUtils.java: ########## @@ -0,0 +1,610 @@ +/* + * 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 com.alibaba.fluss.client.utils; + +import com.alibaba.fluss.row.BinaryString; +import com.alibaba.fluss.row.Decimal; +import com.alibaba.fluss.row.GenericRow; +import com.alibaba.fluss.row.InternalRow; +import com.alibaba.fluss.row.TimestampLtz; +import com.alibaba.fluss.row.TimestampNtz; +import com.alibaba.fluss.types.DataType; +import com.alibaba.fluss.types.DataTypeRoot; +import com.alibaba.fluss.types.DecimalType; +import com.alibaba.fluss.types.LocalZonedTimestampType; +import com.alibaba.fluss.types.RowType; +import com.alibaba.fluss.types.TimestampType; +import com.alibaba.fluss.utils.MapUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.math.BigDecimal; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Helper class for converting Java objects to Fluss's {@link InternalRow} format and vice versa. + * + * <p>This utility uses reflection to map fields from POJOs to InternalRow and back based on a given + * schema. It includes caching mechanisms to avoid repeated reflection operations for the same POJO + * types. + * + * <p>Example usage: + * + * <pre>{@code + * // Create a converter using the static factory method + * ConverterUtils<Order> converter = ConverterUtils.getConverter(Order.class, rowType); + * + * // Convert a POJO to GenericRow + * Order order = new Order(1001L, 5001L, 10, "123 Athens"); + * GenericRow row = converter.toRow(order); + * + * // Convert a GenericRow back to POJO + * Order convertedOrder = converter.fromRow(row); + * }</pre> + * + * <p>Note: Nested POJO fields are not supported in the current implementation. + * + * @param <T> The POJO type to convert + */ +public class ConverterUtils<T> { + private static final Logger LOG = LoggerFactory.getLogger(ConverterUtils.class); + + /** Cache for converters to avoid repeated reflection operations. */ + private static final ConcurrentHashMap<CacheKey, ConverterUtils<?>> CONVERTER_CACHE = + MapUtils.newConcurrentHashMap(); Review Comment: This cache grows indefinitely. In production systems with dynamic POJO types or frequent class loading/unloading, this will cause memory leaks in future, what do you think? ``` if (CONVERTER_CACHE.size() >= MAX_CACHE_SIZE) { CONVERTER_CACHE.clear(); ``` -- 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]
