mirror of
https://gitee.com/dromara/MilvusPlus.git
synced 2025-12-06 17:08:27 +08:00
增加缓存减少反射
This commit is contained in:
parent
632916a299
commit
a6590d485b
@ -9,6 +9,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
**/
|
||||
@Data
|
||||
public class MilvusCache {
|
||||
public static final Map<Class<?>,ConversionCache> milvusCache=new ConcurrentHashMap<>();
|
||||
public static final Map<String,ConversionCache> milvusCache=new ConcurrentHashMap<>(); //类名-->缓存
|
||||
|
||||
}
|
||||
|
||||
@ -7,7 +7,9 @@ import java.util.Map;
|
||||
**/
|
||||
public class PropertyCache {
|
||||
|
||||
public Map<String, String> functionToPropertyMap = new HashMap<>(); //属性名称->表名称
|
||||
public Map<String, String> functionToPropertyMap = new HashMap<>(); //属性名称->集合属性名称
|
||||
|
||||
public Map<String, String> methodToPropertyMap = new HashMap<>(); //属性get方法名称->集合属性名称
|
||||
|
||||
|
||||
// 根据值查找第一个匹配的键
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ public class SearchRespConverter {
|
||||
*/
|
||||
public static <T> MilvusResp<List<MilvusResult<T>>> convertSearchRespToMilvusResp(SearchResp searchResp, Class<T> entityType) {
|
||||
// 从缓存中获取对应实体类型的转换缓存和属性缓存
|
||||
ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType);
|
||||
ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType.getName());
|
||||
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
||||
|
||||
// 获取原始搜索结果列表,并将其转换为MilvusResult<T>类型的列表
|
||||
@ -113,7 +113,7 @@ public class SearchRespConverter {
|
||||
*/
|
||||
private static <T> MilvusResp<List<MilvusResult<T>>> convertQuery(List<QueryResp.QueryResult> getResults, Class<T> entityType){
|
||||
// 初始化转换缓存和属性缓存,用于帮助将查询结果映射到Java实体
|
||||
ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType);
|
||||
ConversionCache conversionCache = MilvusCache.milvusCache.get(entityType.getName());
|
||||
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
||||
List<T> entities = new ArrayList<>();
|
||||
|
||||
|
||||
@ -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<T,R> extends Function<T,R>, Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类的字段名称
|
||||
* 获取实体类的字段名称(集合字段名称)
|
||||
*
|
||||
* @param split 分隔符,多个字母自定义分隔符
|
||||
* @param toType 转换方式,多个字母以大小写方式返回 0.不做转换 1.大写 2.小写
|
||||
*/
|
||||
default String getFieldName(FieldFunction<T, ?> fn, String split, Integer toType) {
|
||||
SerializedLambda serializedLambda = getSerializedLambdaOne(fn);
|
||||
|
||||
Map<String, ConversionCache> 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);
|
||||
}
|
||||
|
||||
@ -89,7 +89,7 @@ public class MilvusMapper<T> {
|
||||
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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user