发布 v1.1.0 #2
@@ -0,0 +1,39 @@
|
||||
package com.easyagents.store.milvus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Milvus VarChar 主键转换工具。
|
||||
*/
|
||||
final class MilvusPrimaryKeySupport {
|
||||
|
||||
/**
|
||||
* 禁止实例化无状态工具类。
|
||||
*/
|
||||
private MilvusPrimaryKeySupport() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将删除主键归一化为 Milvus VarChar 主键值。
|
||||
*
|
||||
* <p>Milvus 向量存储创建的集合固定使用 VarChar 类型的 {@code id} 主键。
|
||||
* Java SDK 会根据传入值类型生成删除表达式,因此数值对象必须先转为字符串。</p>
|
||||
*
|
||||
* @param ids 调用方提供的主键集合
|
||||
* @return 可直接传给 Milvus Java SDK 的字符串主键列表
|
||||
* @throws NullPointerException 主键集合中包含空值时抛出
|
||||
*/
|
||||
static List<Object> normalize(Collection<?> ids) {
|
||||
List<Object> normalizedIds = new ArrayList<Object>(ids.size());
|
||||
for (Object id : ids) {
|
||||
normalizedIds.add(Objects.requireNonNull(
|
||||
id,
|
||||
"Milvus primary key must not be null"
|
||||
).toString());
|
||||
}
|
||||
return normalizedIds;
|
||||
}
|
||||
}
|
||||
@@ -157,12 +157,14 @@ public class MilvusVectorStore extends DocumentStore implements AutoCloseable {
|
||||
}
|
||||
DeleteReq deleteReq = builder
|
||||
.collectionName(collectionName)
|
||||
.ids(new ArrayList<Object>(ids))
|
||||
.ids(MilvusPrimaryKeySupport.normalize(ids))
|
||||
.build();
|
||||
client.delete(deleteReq);
|
||||
return StoreResult.success();
|
||||
} catch (Exception e) {
|
||||
return StoreResult.fail();
|
||||
LOG.error("Milvus delete failed. collection={}, message={}",
|
||||
collectionName, e.getMessage(), e);
|
||||
return StoreResult.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.easyagents.store.milvus;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link MilvusPrimaryKeySupport} 主键归一化测试。
|
||||
*/
|
||||
public class MilvusPrimaryKeySupportTest {
|
||||
|
||||
/**
|
||||
* 验证数值和字符串主键都按 VarChar 类型传给 Milvus。
|
||||
*/
|
||||
@Test
|
||||
public void shouldNormalizeDeleteIdsAsStrings() {
|
||||
List<Object> ids = MilvusPrimaryKeySupport.normalize(Arrays.asList(
|
||||
new BigInteger("105257799143000107"),
|
||||
42L,
|
||||
"faq-1"
|
||||
));
|
||||
|
||||
Assert.assertEquals(
|
||||
Arrays.asList("105257799143000107", "42", "faq-1"),
|
||||
ids
|
||||
);
|
||||
for (Object id : ids) {
|
||||
Assert.assertTrue(id instanceof String);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证空主键会被明确拒绝,避免生成无效删除表达式。
|
||||
*/
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void shouldRejectNullDeleteId() {
|
||||
MilvusPrimaryKeySupport.normalize(Arrays.asList("1", null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user