From a6590d485bf30dbbbd1d6c45054d1c4d9e38cb90 Mon Sep 17 00:00:00 2001 From: xgc Date: Tue, 14 May 2024 10:43:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E5=8F=8D=E5=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../milvus/plus/cache/MilvusCache.java | 2 +- .../milvus/plus/cache/PropertyCache.java | 4 +++- .../plus/converter/MilvusEntityConverter.java | 22 +++++++++++++++++- .../plus/converter/SearchRespConverter.java | 4 ++-- .../milvus/plus/core/FieldFunction.java | 23 +++++++++++++++---- .../milvus/plus/core/mapper/MilvusMapper.java | 2 +- 6 files changed, 47 insertions(+), 10 deletions(-) diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/MilvusCache.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/MilvusCache.java index 204b873..3a466e9 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/MilvusCache.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/MilvusCache.java @@ -9,6 +9,6 @@ import java.util.concurrent.ConcurrentHashMap; **/ @Data public class MilvusCache { - public static final Map,ConversionCache> milvusCache=new ConcurrentHashMap<>(); + public static final Map milvusCache=new ConcurrentHashMap<>(); //类名-->缓存 } diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/PropertyCache.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/PropertyCache.java index 7423a28..4de1aec 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/PropertyCache.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/cache/PropertyCache.java @@ -7,7 +7,9 @@ import java.util.Map; **/ public class PropertyCache { - public Map functionToPropertyMap = new HashMap<>(); //属性名称->表名称 + public Map functionToPropertyMap = new HashMap<>(); //属性名称->集合属性名称 + + public Map methodToPropertyMap = new HashMap<>(); //属性get方法名称->集合属性名称 // 根据值查找第一个匹配的键 diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/MilvusEntityConverter.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/MilvusEntityConverter.java index cb8ce5d..8e508a7 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/MilvusEntityConverter.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/MilvusEntityConverter.java @@ -59,6 +59,7 @@ public class MilvusEntityConverter { String fieldName = fieldAnnotation.name().isEmpty() ? field.getName() : fieldAnnotation.name(); // 缓存属性名与函数名的映射 propertyCache.functionToPropertyMap.put(field.getName(),fieldName); + propertyCache.methodToPropertyMap.put(getGetMethodName(field),fieldName); // 处理主键字段 if (fieldAnnotation.isPrimaryKey()) { CollectionToPrimaryCache.collectionToPrimary.put(collectionName,fieldName); @@ -104,7 +105,7 @@ public class MilvusEntityConverter { conversionCache.setMilvusEntity(milvus); conversionCache.setCollectionName(collectionName); conversionCache.setPropertyCache(propertyCache); - MilvusCache.milvusCache.put(entityClass,conversionCache); + MilvusCache.milvusCache.put(entityClass.getName(),conversionCache); return milvus; } @@ -140,6 +141,25 @@ public class MilvusEntityConverter { .extraParams(map) .build(); } + public static String getGetMethodName(Field field) { + // 检查字段是否为布尔类型 + String prefix = field.getType() == boolean.class || field.getType() == Boolean.class ? "is" : "get"; + + // 确保字段名不为空或null + if (field == null) { + throw new IllegalArgumentException("Field must not be null."); + } + // 获取字段名的首字母大写形式 + String fieldName = capitalizeFirstLetter(field.getName()); + // 构建并返回getter方法名 + return prefix + fieldName; + } + private static String capitalizeFirstLetter(String original) { + if (original == null || original.isEmpty()) { + return original; + } + return original.substring(0, 1).toUpperCase() + original.substring(1); + } } diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/SearchRespConverter.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/SearchRespConverter.java index 9cfbda4..3a59822 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/SearchRespConverter.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/converter/SearchRespConverter.java @@ -33,7 +33,7 @@ public class SearchRespConverter { */ public static MilvusResp>> convertSearchRespToMilvusResp(SearchResp searchResp, Class entityType) { // 从缓存中获取对应实体类型的转换缓存和属性缓存 - ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType); + ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType.getName()); PropertyCache propertyCache = conversionCache.getPropertyCache(); // 获取原始搜索结果列表,并将其转换为MilvusResult类型的列表 @@ -113,7 +113,7 @@ public class SearchRespConverter { */ private static MilvusResp>> convertQuery(List getResults, Class entityType){ // 初始化转换缓存和属性缓存,用于帮助将查询结果映射到Java实体 - ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType); + ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType.getName()); PropertyCache propertyCache = conversionCache.getPropertyCache(); List entities = new ArrayList<>(); diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/FieldFunction.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/FieldFunction.java index 8b8777f..5f38ad2 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/FieldFunction.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/FieldFunction.java @@ -1,6 +1,9 @@ package io.github.javpower.milvus.plus.core; import io.github.javpower.milvus.plus.annotation.MilvusField; +import io.github.javpower.milvus.plus.cache.ConversionCache; +import io.github.javpower.milvus.plus.cache.MilvusCache; +import io.github.javpower.milvus.plus.cache.PropertyCache; import org.apache.commons.lang3.StringUtils; import java.io.Serializable; @@ -8,6 +11,7 @@ import java.lang.invoke.SerializedLambda; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Map; import java.util.function.Function; @FunctionalInterface @@ -49,20 +53,31 @@ public interface FieldFunction extends Function, Serializable { } /** - * 获取实体类的字段名称 + * 获取实体类的字段名称(集合字段名称) * * @param split 分隔符,多个字母自定义分隔符 * @param toType 转换方式,多个字母以大小写方式返回 0.不做转换 1.大写 2.小写 */ default String getFieldName(FieldFunction fn, String split, Integer toType) { SerializedLambda serializedLambda = getSerializedLambdaOne(fn); - + Map milvusCache = MilvusCache.milvusCache; + String implClass = serializedLambda.getImplClass().replace("/", "."); + String implMethodName = serializedLambda.getImplMethodName(); + ConversionCache conversionCache = milvusCache.get(implClass); + String fieldName; + if(conversionCache!=null){ + PropertyCache propertyCache = conversionCache.getPropertyCache(); + fieldName = propertyCache.methodToPropertyMap.get(implMethodName); + if(StringUtils.isNotEmpty(fieldName)){ + return fieldName; + } + } // 从lambda信息取出method、field、class等 - String fieldName = serializedLambda.getImplMethodName().substring("get".length()); + fieldName = implMethodName.substring("get".length()); fieldName = fieldName.replaceFirst(fieldName.charAt(0) + "", (fieldName.charAt(0) + "").toLowerCase()); Field field; try { - field = Class.forName(serializedLambda.getImplClass().replace("/", ".")).getDeclaredField(fieldName); + field = Class.forName(implClass).getDeclaredField(fieldName); } catch (ClassNotFoundException | NoSuchFieldException e) { throw new RuntimeException(e); } diff --git a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/mapper/MilvusMapper.java b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/mapper/MilvusMapper.java index afe7d16..8255ede 100644 --- a/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/mapper/MilvusMapper.java +++ b/milvus-plus-boot-starter/src/main/java/io/github/javpower/milvus/plus/core/mapper/MilvusMapper.java @@ -89,7 +89,7 @@ public class MilvusMapper { if (collectionAnnotation == null) { throw new IllegalStateException("Entity type " + entityType.getName() + " is not annotated with @MilvusCollection."); } - ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType); + ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType.getName()); String collectionName = conversionCache == null ? null : conversionCache.getCollectionName(); // 使用SpringUtil获取MilvusClient实例 MilvusClientV2 client = SpringUtils.getBean(MilvusClientV2.class);