使用迭代器处理可变参数泛型

This commit is contained in:
xgc 2024-06-04 01:45:42 +08:00
parent 6db4e35fb9
commit 90ea7e3388
4 changed files with 39 additions and 37 deletions

View File

@ -2,7 +2,27 @@ package org.dromara.milvus.plus.core.conditions;
import lombok.Data;
import java.util.Iterator;
import java.util.NoSuchElementException;
@Data
public abstract class AbstractChainWrapper<T> extends ConditionBuilder<T>{
protected 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++];
}
}
}

View File

@ -2,10 +2,6 @@ package org.dromara.milvus.plus.core.conditions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.dromara.milvus.plus.cache.ConversionCache;
import org.dromara.milvus.plus.cache.PropertyCache;
import org.dromara.milvus.plus.core.FieldFunction;
import org.dromara.milvus.plus.model.vo.MilvusResp;
import io.milvus.exception.MilvusException;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.InsertReq;
@ -13,6 +9,10 @@ import io.milvus.v2.service.vector.response.InsertResp;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.milvus.plus.cache.ConversionCache;
import org.dromara.milvus.plus.cache.PropertyCache;
import org.dromara.milvus.plus.core.FieldFunction;
import org.dromara.milvus.plus.model.vo.MilvusResp;
import java.util.*;
@ -83,7 +83,10 @@ public class LambdaInsertWrapper<T> extends AbstractChainWrapper<T> implements
resp.setSuccess(true);
return resp;
}
public MilvusResp<InsertResp> insert(T ... entity) throws MilvusException {
Iterator<T> iterator = new ArrayIterator<>(entity);
return insert(iterator);
}
public MilvusResp<InsertResp> insert(Iterator<T> iterator) throws MilvusException {
PropertyCache propertyCache = conversionCache.getPropertyCache();
List<JSONObject> jsonObjects=new ArrayList<>();

View File

@ -2,11 +2,6 @@ package org.dromara.milvus.plus.core.conditions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.dromara.milvus.plus.cache.CollectionToPrimaryCache;
import org.dromara.milvus.plus.cache.ConversionCache;
import org.dromara.milvus.plus.cache.PropertyCache;
import org.dromara.milvus.plus.core.FieldFunction;
import org.dromara.milvus.plus.model.vo.MilvusResp;
import io.milvus.exception.MilvusException;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.SearchReq;
@ -16,6 +11,11 @@ import io.milvus.v2.service.vector.response.UpsertResp;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.milvus.plus.cache.CollectionToPrimaryCache;
import org.dromara.milvus.plus.cache.ConversionCache;
import org.dromara.milvus.plus.cache.PropertyCache;
import org.dromara.milvus.plus.core.FieldFunction;
import org.dromara.milvus.plus.model.vo.MilvusResp;
import java.util.ArrayList;
import java.util.Iterator;
@ -404,7 +404,10 @@ public class LambdaUpdateWrapper<T> extends AbstractChainWrapper<T> implements
resp.setSuccess(true);
return resp;
}
public MilvusResp<UpsertResp> updateById(T... entity) throws MilvusException {
Iterator<T> iterator = new ArrayIterator<>(entity);
return updateById(iterator);
}
public MilvusResp<UpsertResp> updateById(Iterator<T> iterator) throws MilvusException {
PropertyCache propertyCache = conversionCache.getPropertyCache();
String pk = CollectionToPrimaryCache.collectionToPrimary.get(collectionName);

View File

@ -16,9 +16,7 @@ import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* @author xgc
@ -61,25 +59,6 @@ public abstract class BaseMilvusMapper<T>{
public LambdaInsertWrapper<T> insertWrapper() {
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) {
@ -92,18 +71,15 @@ public abstract class BaseMilvusMapper<T>{
}
public MilvusResp<InsertResp> insert(T ... entity){
LambdaInsertWrapper<T> lambda = insertWrapper();
Iterator<T> iterator = new ArrayIterator<>(entity);
return lambda.insert(iterator);
return lambda.insert(entity);
}
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);
return lambda.updateById(entity);
}
public MilvusResp<UpsertResp> updateById(Collection<T> entity) {
LambdaUpdateWrapper<T> lambda = updateWrapper();