mirror of
https://gitee.com/dromara/MilvusPlus.git
synced 2025-12-07 09:28:25 +08:00
优化代码
This commit is contained in:
parent
51940ecf2c
commit
874c575dae
@ -14,10 +14,7 @@ import lombok.Data;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建器内部类,用于构建insert请求
|
* 构建器内部类,用于构建insert请求
|
||||||
@ -87,10 +84,11 @@ public class LambdaInsertWrapper<T> extends AbstractChainWrapper<T> implements
|
|||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MilvusResp<InsertResp> insert(T ...t) throws MilvusException {
|
public MilvusResp<InsertResp> insert(Iterator<T> iterator) throws MilvusException {
|
||||||
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
||||||
List<JSONObject> jsonObjects=new ArrayList<>();
|
List<JSONObject> jsonObjects=new ArrayList<>();
|
||||||
for (T t1 : t) {
|
while (iterator.hasNext()) {
|
||||||
|
T t1 = iterator.next();
|
||||||
Map<String, Object> propertiesMap = getPropertiesMap(t1);
|
Map<String, Object> propertiesMap = getPropertiesMap(t1);
|
||||||
JSONObject jsonObject=new JSONObject();
|
JSONObject jsonObject=new JSONObject();
|
||||||
for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
|
for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
|
||||||
@ -100,7 +98,6 @@ public class LambdaInsertWrapper<T> extends AbstractChainWrapper<T> implements
|
|||||||
jsonObject.put(tk,value);
|
jsonObject.put(tk,value);
|
||||||
}
|
}
|
||||||
jsonObjects.add(jsonObject);
|
jsonObjects.add(jsonObject);
|
||||||
|
|
||||||
}
|
}
|
||||||
return insert(jsonObjects);
|
return insert(jsonObjects);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -404,24 +405,27 @@ public class LambdaUpdateWrapper<T> extends AbstractChainWrapper<T> implements
|
|||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MilvusResp<UpsertResp> updateById(T ...t) throws MilvusException {
|
public MilvusResp<UpsertResp> updateById(Iterator<T> iterator) throws MilvusException {
|
||||||
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
PropertyCache propertyCache = conversionCache.getPropertyCache();
|
||||||
String pk = CollectionToPrimaryCache.collectionToPrimary.get(collectionName);
|
String pk = CollectionToPrimaryCache.collectionToPrimary.get(collectionName);
|
||||||
List<JSONObject> jsonObjects=new ArrayList<>();
|
List<JSONObject> jsonObjects = new ArrayList<>();
|
||||||
for (T t1 : t) {
|
// 使用迭代器遍历可变参数
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
T t1 = iterator.next();
|
||||||
Map<String, Object> propertiesMap = getPropertiesMap(t1);
|
Map<String, Object> propertiesMap = getPropertiesMap(t1);
|
||||||
JSONObject jsonObject=new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
|
for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
|
||||||
String key = entry.getKey();
|
String key = entry.getKey();
|
||||||
Object value = entry.getValue();
|
Object value = entry.getValue();
|
||||||
|
// 根据PropertyCache转换属性名
|
||||||
String tk = propertyCache.functionToPropertyMap.get(key);
|
String tk = propertyCache.functionToPropertyMap.get(key);
|
||||||
jsonObject.put(tk,value);
|
jsonObject.put(tk, value);
|
||||||
}
|
}
|
||||||
|
// 检查是否包含主键
|
||||||
if (!jsonObject.containsKey(pk)) {
|
if (!jsonObject.containsKey(pk)) {
|
||||||
throw new MilvusException("not find primary key",400);
|
throw new MilvusException("not find primary key", 400);
|
||||||
}
|
}
|
||||||
jsonObjects.add(jsonObject);
|
jsonObjects.add(jsonObject);
|
||||||
|
|
||||||
}
|
}
|
||||||
return update(jsonObjects);
|
return update(jsonObjects);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,10 @@ import org.dromara.milvus.plus.model.vo.MilvusResult;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xgc
|
* @author xgc
|
||||||
@ -58,6 +61,25 @@ public abstract class BaseMilvusMapper<T>{
|
|||||||
public LambdaInsertWrapper<T> insertWrapper() {
|
public LambdaInsertWrapper<T> insertWrapper() {
|
||||||
return lambda(new LambdaInsertWrapper<>());
|
return lambda(new LambdaInsertWrapper<>());
|
||||||
}
|
}
|
||||||
|
private static class ArrayIterator<T> implements Iterator<T> {
|
||||||
|
private final T[] array;
|
||||||
|
private int index = 0;
|
||||||
|
|
||||||
|
public ArrayIterator(T[] array) {
|
||||||
|
this.array = array;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return index < array.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T next() {
|
||||||
|
if (!hasNext()) throw new NoSuchElementException();
|
||||||
|
return array[index++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public MilvusResp<List<MilvusResult<T>>> getById(Serializable ... ids) {
|
public MilvusResp<List<MilvusResult<T>>> getById(Serializable ... ids) {
|
||||||
@ -68,15 +90,25 @@ public abstract class BaseMilvusMapper<T>{
|
|||||||
LambdaDeleteWrapper<T> lambda = deleteWrapper();
|
LambdaDeleteWrapper<T> lambda = deleteWrapper();
|
||||||
return lambda.removeById(ids);
|
return lambda.removeById(ids);
|
||||||
}
|
}
|
||||||
public MilvusResp<UpsertResp> updateById(T ... entity){
|
|
||||||
LambdaUpdateWrapper<T> lambda = updateWrapper();
|
|
||||||
return lambda.updateById(entity);
|
|
||||||
}
|
|
||||||
public MilvusResp<InsertResp> insert(T ... entity){
|
public MilvusResp<InsertResp> insert(T ... entity){
|
||||||
LambdaInsertWrapper<T> lambda = insertWrapper();
|
LambdaInsertWrapper<T> lambda = insertWrapper();
|
||||||
return lambda.insert(entity);
|
Iterator<T> iterator = new ArrayIterator<>(entity);
|
||||||
|
return lambda.insert(iterator);
|
||||||
|
}
|
||||||
|
public MilvusResp<InsertResp> insert(Collection<T> entity){
|
||||||
|
LambdaInsertWrapper<T> lambda = insertWrapper();
|
||||||
|
return lambda.insert(entity.iterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MilvusResp<UpsertResp> updateById(T... entity) {
|
||||||
|
LambdaUpdateWrapper<T> lambda = updateWrapper();
|
||||||
|
Iterator<T> iterator = new ArrayIterator<>(entity);
|
||||||
|
return lambda.updateById(iterator);
|
||||||
|
}
|
||||||
|
public MilvusResp<UpsertResp> updateById(Collection<T> entity) {
|
||||||
|
LambdaUpdateWrapper<T> lambda = updateWrapper();
|
||||||
|
return lambda.updateById(entity.iterator());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建通用构建器实例
|
* 创建通用构建器实例
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user