未验证 提交 befc21ec 编写于 作者: T ThreadDao 提交者: GitHub

[skip ci] fix java sdk test case (#3375)

* java main class
Signed-off-by: Nzongyufen <zongyufen@foxmail.com>

* [skip ci] fix java sdk test
Signed-off-by: Nzongyufen <zongyufen@foxmail.com>

* [skip ci] fix java sdk test
Signed-off-by: Nzongyufen <zongyufen@foxmail.com>
上级 38f83f73
......@@ -116,7 +116,7 @@
<dependency>
<groupId>io.milvus</groupId>
<artifactId>milvus-sdk-java</artifactId>
<version>0.8.0-SNAPSHOT</version>
<version>0.9.0-SNAPSHOT</version>
</dependency>
<!-- <dependency>-->
......
......@@ -5,6 +5,9 @@ import org.apache.commons.cli.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.testng.SkipException;
import org.testng.TestNG;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.DataProvider;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
......@@ -12,32 +15,37 @@ import org.testng.xml.XmlTest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MainClass {
private static String host = "127.0.0.1";
private static int port = 19530;
private int index_file_size = 50;
private static String HOST = "127.0.0.1";
private static int PORT = 19530;
private int segmentRowCount = 5000;
public int dimension = 128;
private static ConnectParam CONNECT_PARAM = new ConnectParam.Builder()
.withHost(HOST)
.withPort(PORT)
.build();
public static void setHost(String host) {
MainClass.host = host;
MainClass.HOST = host;
}
public static void setPort(int port) {
MainClass.port = port;
MainClass.PORT = port;
}
@DataProvider(name="DefaultConnectArgs")
public static Object[][] defaultConnectArgs(){
return new Object[][]{{host, port}};
return new Object[][]{{HOST, PORT}};
}
@DataProvider(name="ConnectInstance")
public Object[][] connectInstance() throws ConnectFailedException {
MilvusClient client = new MilvusGrpcClient();
ConnectParam connectParam = new ConnectParam.Builder()
.withHost(host)
.withPort(port)
.withHost(HOST)
.withPort(PORT)
.build();
client.connect(connectParam);
String collectionName = RandomStringUtils.randomAlphabetic(10);
......@@ -48,11 +56,7 @@ public class MainClass {
public Object[][] disConnectInstance() throws ConnectFailedException {
// Generate connection instance
MilvusClient client = new MilvusGrpcClient();
ConnectParam connectParam = new ConnectParam.Builder()
.withHost(host)
.withPort(port)
.build();
client.connect(connectParam);
client.connect(CONNECT_PARAM);
try {
client.disconnect();
} catch (InterruptedException e) {
......@@ -62,72 +66,66 @@ public class MainClass {
return new Object[][]{{client, collectionName}};
}
private Object[][] genCollection(boolean isBinary, boolean autoId) throws ConnectFailedException {
Object[][] collection;
String collectionName = Utils.genUniqueStr("collection");
List<Map<String, Object>> defaultFields = Utils.genDefaultFields(dimension,isBinary);
String jsonParams = String.format("{\"segment_row_count\": %s, \"auto_id\": %s}",segmentRowCount, autoId);
// Generate connection instance
MilvusClient client = new MilvusGrpcClient();
client.connect(CONNECT_PARAM);
CollectionMapping cm = new CollectionMapping.Builder(collectionName)
.withFields(defaultFields)
.withParamsInJson(jsonParams)
.build();
Response res = client.createCollection(cm);
if (!res.ok()) {
System.out.println(res.getMessage());
throw new SkipException("Collection created failed");
}
collection = new Object[][]{{client, collectionName}};
return collection;
}
@DataProvider(name="Collection")
public Object[][] provideCollection() throws ConnectFailedException, InterruptedException {
Object[][] collections = new Object[2][2];
MetricType[] metricTypes = { MetricType.L2, MetricType.IP };
for (int i = 0; i < metricTypes.length; ++i) {
String collectionName = metricTypes[i].toString()+"_"+RandomStringUtils.randomAlphabetic(10);
// Generate connection instance
MilvusClient client = new MilvusGrpcClient();
ConnectParam connectParam = new ConnectParam.Builder()
.withHost(host)
.withPort(port)
.build();
client.connect(connectParam);
Object[][] collection = genCollection(false,true);
return collection;
// List<String> tableNames = client.listCollections().getCollectionNames();
// for (int j = 0; j < tableNames.size(); ++j
// ) {
// client.dropCollection(tableNames.get(j));
// }
// Thread.currentThread().sleep(2000);
CollectionMapping cm = new CollectionMapping.Builder(collectionName, dimension)
.withIndexFileSize(index_file_size)
.withMetricType(metricTypes[i])
.build();
Response res = client.createCollection(cm);
if (!res.ok()) {
System.out.println(res.getMessage());
throw new SkipException("Collection created failed");
}
collections[i] = new Object[]{client, collectionName};
}
return collections;
}
@DataProvider(name="IdCollection")
public Object[][] provideIdCollection() throws ConnectFailedException, InterruptedException {
Object[][] idCollection = genCollection(false,false);
return idCollection;
}
@DataProvider(name="BinaryCollection")
public Object[][] provideBinaryCollection() throws ConnectFailedException, InterruptedException {
Object[][] collections = new Object[3][2];
MetricType[] metricTypes = { MetricType.JACCARD, MetricType.HAMMING, MetricType.TANIMOTO };
for (int i = 0; i < metricTypes.length; ++i) {
String collectionName = metricTypes[i].toString()+"_"+RandomStringUtils.randomAlphabetic(10);
// Generate connection instance
MilvusClient client = new MilvusGrpcClient();
ConnectParam connectParam = new ConnectParam.Builder()
.withHost(host)
.withPort(port)
.build();
client.connect(connectParam);
// List<String> tableNames = client.listCollections().getCollectionNames();
// for (int j = 0; j < tableNames.size(); ++j
// ) {
// client.dropCollection(tableNames.get(j));
// }
// Thread.currentThread().sleep(2000);
CollectionMapping cm = new CollectionMapping.Builder(collectionName, dimension)
.withIndexFileSize(index_file_size)
.withMetricType(metricTypes[i])
.build();
Response res = client.createCollection(cm);
if (!res.ok()) {
System.out.println(res.getMessage());
throw new SkipException("Collection created failed");
}
collections[i] = new Object[]{client, collectionName};
}
return collections;
Object[][] binaryCollection = genCollection(true,true);
return binaryCollection;
}
@DataProvider(name="BinaryIdCollection")
public Object[][] provideBinaryIdCollection() throws ConnectFailedException, InterruptedException {
Object[][] binaryIdCollection = genCollection(true,false);
return binaryIdCollection;
}
@AfterSuite
public void dropCollection(){
// MilvusClient client = new MilvusGrpcClient();
// List<String> collectionNames = client.listCollections().getCollectionNames();
// collectionNames.forEach(client::dropCollection);
System.out.println("after suite");
}
@AfterMethod
public void after(){
System.out.println("after method");
}
public static void main(String[] args) {
CommandLineParser parser = new DefaultParser();
......@@ -157,19 +155,19 @@ public class MainClass {
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("com.TestPing"));
classes.add(new XmlClass("com.TestAddVectors"));
// classes.add(new XmlClass("com.TestPing"));
// classes.add(new XmlClass("com.TestAddVectors"));
classes.add(new XmlClass("com.TestConnect"));
classes.add(new XmlClass("com.TestDeleteVectors"));
classes.add(new XmlClass("com.TestIndex"));
classes.add(new XmlClass("com.TestCompact"));
classes.add(new XmlClass("com.TestSearchVectors"));
// classes.add(new XmlClass("com.TestDeleteVectors"));
// classes.add(new XmlClass("com.TestIndex"));
// classes.add(new XmlClass("com.TestCompact"));
// classes.add(new XmlClass("com.TestSearchVectors"));
classes.add(new XmlClass("com.TestCollection"));
classes.add(new XmlClass("com.TestCollectionCount"));
classes.add(new XmlClass("com.TestFlush"));
classes.add(new XmlClass("com.TestPartition"));
classes.add(new XmlClass("com.TestGetVectorByID"));
classes.add(new XmlClass("com.TestCollectionInfo"));
// classes.add(new XmlClass("com.TestCollectionCount"));
// classes.add(new XmlClass("com.TestFlush"));
// classes.add(new XmlClass("com.TestPartition"));
// classes.add(new XmlClass("com.TestGetVectorByID"));
// classes.add(new XmlClass("com.TestCollectionInfo"));
// classes.add(new XmlClass("com.TestSearchByIds"));
test.setXmlClasses(classes) ;
......
package com;
import com.alibaba.fastjson.JSONObject;
import io.milvus.client.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.*;
import java.util.List;
import java.util.Map;
public class TestCollection {
int index_file_size = 50;
int segmentRowCount = 5000;
int dimension = 128;
@BeforeClass
public MilvusClient setUp() throws ConnectFailedException {
MilvusClient client = new MilvusGrpcClient();
ConnectParam connectParam = new ConnectParam.Builder()
.withHost("192.168.1.6")
.withPort(19530)
.build();
client.connect(connectParam);
return client;
}
@AfterClass
public void tearDown() throws ConnectFailedException {
MilvusClient client = setUp();
List<String> collectionNames = client.listCollections().getCollectionNames();
// collectionNames.forEach(collection -> {client.dropCollection(collection);});
for(String collection: collectionNames){
System.out.print(collection+" ");
client.dropCollection(collection);
}
System.out.println("After Test");
}
@Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class)
public void test_create_table(MilvusClient client, String collectionName){
CollectionMapping tableSchema = new CollectionMapping.Builder(collectionName, dimension)
.withIndexFileSize(index_file_size)
.withMetricType(MetricType.L2)
public void testCreateCollection(MilvusClient client, String collectionName){
CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionName)
.withFields(Utils.genDefaultFields(dimension,false))
.withParamsInJson(String.format("{\"segment_row_count\": %s}",segmentRowCount))
.build();
Response res = client.createCollection(tableSchema);
Response res = client.createCollection(collectionSchema);
assert(res.ok());
Assert.assertEquals(res.ok(), true);
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_create_table_disconnect(MilvusClient client, String collectionName){
CollectionMapping tableSchema = new CollectionMapping.Builder(collectionName, dimension)
.withIndexFileSize(index_file_size)
.withMetricType(MetricType.L2)
public void testCreateCollectionDisconnect(MilvusClient client, String collectionName){
CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionName)
.withFields(Utils.genDefaultFields(dimension,false))
.withParamsInJson(String.format("{\"segment_row_count\": %s}",segmentRowCount))
.build();
Response res = client.createCollection(tableSchema);
Response res = client.createCollection(collectionSchema);
assert(!res.ok());
}
@Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class)
public void test_create_table_repeatably(MilvusClient client, String collectionName){
CollectionMapping tableSchema = new CollectionMapping.Builder(collectionName, dimension)
.withIndexFileSize(index_file_size)
.withMetricType(MetricType.L2)
public void testCreateCollectionRepeatably(MilvusClient client, String collectionName){
CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionName)
.withFields(Utils.genDefaultFields(dimension,false))
.withParamsInJson(String.format("{\"segment_row_count\": %s}",segmentRowCount))
.build();
Response res = client.createCollection(tableSchema);
Response res = client.createCollection(collectionSchema);
Assert.assertEquals(res.ok(), true);
Response res_new = client.createCollection(tableSchema);
Assert.assertEquals(res_new.ok(), false);
Response resNew = client.createCollection(collectionSchema);
Assert.assertEquals(resNew.ok(), false);
}
@Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class)
public void test_create_table_wrong_params(MilvusClient client, String collectionName){
Integer dimension = 0;
CollectionMapping tableSchema = new CollectionMapping.Builder(collectionName, dimension)
.withIndexFileSize(index_file_size)
.withMetricType(MetricType.L2)
public void testCreateCollectionWrongParams(MilvusClient client, String collectionName){
Integer dim = 0;
CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionName)
.withFields(Utils.genDefaultFields(dim,false))
.withParamsInJson(String.format("{\"segment_row_count\": %s}",segmentRowCount))
.build();
Response res = client.createCollection(tableSchema);
Response res = client.createCollection(collectionSchema);
System.out.println(res.toString());
Assert.assertEquals(res.ok(), false);
}
@Test(dataProvider = "ConnectInstance", dataProviderClass = MainClass.class)
public void test_show_tables(MilvusClient client, String collectionName){
Integer tableNum = 10;
public void testShowCollections(MilvusClient client, String collectionName){
Integer collectionNum = 10;
ListCollectionsResponse res = null;
for (int i = 0; i < tableNum; ++i) {
for (int i = 0; i < collectionNum; ++i) {
String collectionNameNew = collectionName+"_"+Integer.toString(i);
CollectionMapping tableSchema = new CollectionMapping.Builder(collectionNameNew, dimension)
.withIndexFileSize(index_file_size)
.withMetricType(MetricType.L2)
CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionNameNew)
.withFields(Utils.genDefaultFields(dimension,false))
.withParamsInJson(String.format("{\"segment_row_count\": %s}",segmentRowCount))
.build();
client.createCollection(tableSchema);
client.createCollection(collectionSchema);
List<String> collectionNames = client.listCollections().getCollectionNames();
Assert.assertTrue(collectionNames.contains(collectionNameNew));
}
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_show_tables_without_connect(MilvusClient client, String collectionName){
public void testShowCollectionsWithoutConnect(MilvusClient client, String collectionName){
ListCollectionsResponse res = client.listCollections();
assert(!res.getResponse().ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_drop_table(MilvusClient client, String collectionName) throws InterruptedException {
public void testDropCollection(MilvusClient client, String collectionName) throws InterruptedException {
Response res = client.dropCollection(collectionName);
assert(res.ok());
Thread.currentThread().sleep(1000);
......@@ -88,7 +112,7 @@ public class TestCollection {
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_drop_table_not_existed(MilvusClient client, String collectionName) {
public void testDropCollectionNotExisted(MilvusClient client, String collectionName) {
Response res = client.dropCollection(collectionName+"_");
assert(!res.ok());
List<String> collectionNames = client.listCollections().getCollectionNames();
......@@ -96,47 +120,56 @@ public class TestCollection {
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_drop_table_without_connect(MilvusClient client, String collectionName) {
public void testDropCollectionWithoutConnect(MilvusClient client, String collectionName) {
Response res = client.dropCollection(collectionName);
assert(!res.ok());
}
// TODO
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_describe_table(MilvusClient client, String collectionName) {
public void testDescribeCollection(MilvusClient client, String collectionName) {
GetCollectionInfoResponse res = client.getCollectionInfo(collectionName);
assert(res.getResponse().ok());
CollectionMapping tableSchema = res.getCollectionMapping().get();
Assert.assertEquals(tableSchema.getDimension(), dimension);
Assert.assertEquals(tableSchema.getCollectionName(), collectionName);
Assert.assertEquals(tableSchema.getIndexFileSize(), index_file_size);
Assert.assertEquals(tableSchema.getMetricType().name(), collectionName.substring(0,2));
CollectionMapping collectionSchema = res.getCollectionMapping().get();
List<Map<String,Object>> fields = (List<Map<String, Object>>) collectionSchema.getFields();
int dim = 0;
for(Map<String,Object> field: fields){
if ("float_vector".equals(field.get("field"))) {
JSONObject jsonObject = JSONObject.parseObject(field.get("params").toString());
String dimParams = jsonObject.getString("params");
dim = Utils.getParam(dimParams,"dim");
}
continue;
}
String segmentParams = collectionSchema.getParamsInJson();
Assert.assertEquals(dim, dimension);
Assert.assertEquals(collectionSchema.getCollectionName(), collectionName);
Assert.assertEquals(Utils.getParam(segmentParams,"segment_row_count"), segmentRowCount);
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_describe_table_without_connect(MilvusClient client, String collectionName) {
public void testDescribeCollectionWithoutConnect(MilvusClient client, String collectionName) {
GetCollectionInfoResponse res = client.getCollectionInfo(collectionName);
assert(!res.getResponse().ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_has_table_not_existed(MilvusClient client, String collectionName) {
public void testHasCollectionNotExisted(MilvusClient client, String collectionName) {
HasCollectionResponse res = client.hasCollection(collectionName+"_");
assert(res.getResponse().ok());
Assert.assertFalse(res.hasCollection());
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_has_table_without_connect(MilvusClient client, String collectionName) {
public void testHasCollectionWithoutConnect(MilvusClient client, String collectionName) {
HasCollectionResponse res = client.hasCollection(collectionName);
assert(!res.getResponse().ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_has_table(MilvusClient client, String collectionName) {
public void testHasCollection(MilvusClient client, String collectionName) {
HasCollectionResponse res = client.hasCollection(collectionName);
assert(res.getResponse().ok());
Assert.assertTrue(res.hasCollection());
}
}
......@@ -4,66 +4,82 @@ import io.milvus.client.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
public class TestCollectionCount {
int index_file_size = 50;
int segmentRowCount = 5000;
int dimension = 128;
int nb = 10000;
List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
// List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
// List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
List<Map<String,Object>> defaultEntities = Utils.genDefaultEntities(dimension,nb,false);
List<Map<String,Object>> defaultBinaryEntities = Utils.genDefaultEntities(dimension,nb,true);
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_collection_count_no_vectors(MilvusClient client, String collectionName) {
public void testCollectionCountNoVectors(MilvusClient client, String collectionName) {
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_collection_count_collection_not_existed(MilvusClient client, String collectionName) {
public void testCollectionCountCollectionNotExisted(MilvusClient client, String collectionName) {
CountEntitiesResponse res = client.countEntities(collectionName+"_");
assert(!res.getResponse().ok());
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_collection_count_without_connect(MilvusClient client, String collectionName) {
public void testCollectionCountWithoutConnect(MilvusClient client, String collectionName) {
CountEntitiesResponse res = client.countEntities(collectionName+"_");
assert(!res.getResponse().ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_collection_count(MilvusClient client, String collectionName) throws InterruptedException {
public void testCollectionCount(MilvusClient client, String collectionName) throws InterruptedException {
InsertParam insertParam =
new InsertParam.Builder(collectionName)
.withFields(defaultEntities)
.build();
InsertResponse insertResponse = client.insert(insertParam);
// Insert returns a list of entity ids that you will be using (if you did not supply the yourself) to reference the entities you just inserted
List<Long> vectorIds = insertResponse.getEntityIds();
// Add vectors
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
client.insert(insertParam);
client.flush(collectionName);
Response flushResponse = client.flush(collectionName);
Assert.assertTrue(flushResponse.ok());
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_collection_count_binary(MilvusClient client, String collectionName) throws InterruptedException {
public void testCollectionCountBinary(MilvusClient client, String collectionName) throws InterruptedException {
// Add vectors
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
InsertParam insertParam = new InsertParam.Builder(collectionName)
.withFields(defaultBinaryEntities)
.build();
client.insert(insertParam);
client.flush(collectionName);
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_collection_count_multi_collections(MilvusClient client, String collectionName) throws InterruptedException {
public void testCollectionCountMultiCollections(MilvusClient client, String collectionName) throws InterruptedException {
Integer collectionNum = 10;
CountEntitiesResponse res;
for (int i = 0; i < collectionNum; ++i) {
String collectionNameNew = collectionName + "_" + i;
CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionNameNew, dimension)
.withIndexFileSize(index_file_size)
.withMetricType(MetricType.L2)
CollectionMapping collectionSchema = new CollectionMapping.Builder(collectionNameNew)
.withFields(Utils.genDefaultFields(dimension,false))
.withParamsInJson(String.format("{\"segment_row_count\": %s}",segmentRowCount))
.build();
client.createCollection(collectionSchema);
Response cteateRes = client.createCollection(collectionSchema);
Assert.assertEquals(cteateRes.ok(), true);
// Add vectors
InsertParam insertParam = new InsertParam.Builder(collectionNameNew).withFloatVectors(vectors).build();
client.insert(insertParam);
client.flush(collectionNameNew);
InsertParam insertParam = new InsertParam.Builder(collectionNameNew)
.withFields(defaultEntities)
.build();
InsertResponse insertRes = client.insert(insertParam);
Assert.assertEquals(insertRes.ok(), true);
Response flushRes = client.flush(collectionNameNew);
Assert.assertEquals(flushRes.ok(), true);
}
for (int i = 0; i < collectionNum; ++i) {
String collectionNameNew = collectionName + "_" + i;
......
......@@ -9,63 +9,75 @@ import org.testng.annotations.Test;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class TestCollectionInfo {
int dimension = 128;
int nb = 8000;
int n_list = 1024;
int default_n_list = 16384;
IndexType indexType = IndexType.IVF_SQ8;
IndexType defaultIndexType = IndexType.FLAT;
String indexParam = Utils.setIndexParam(n_list);
List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
int nList = 1024;
int defaultNList = 16384;
String indexType = "IVF_SQ8";
String defaultIndexType = "FLAT";
String metricType = "L2";
String indexParam = Utils.setIndexParam(indexType,metricType,nList);
// List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
// List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
List<Map<String,Object>> defaultEntities = Utils.genDefaultEntities(dimension,nb,false);
List<Map<String,Object>> defaultBinaryEntities = Utils.genDefaultEntities(dimension,nb,true);
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_get_vector_ids_after_delete_vectors(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
public void testGetEntityIdsAfterDeleteEntities(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName)
.withFields(defaultEntities)
.build();
InsertResponse resInsert = client.insert(insertParam);
client.flush(collectionName);
List<Long> idsBefore = resInsert.getVectorIds();
List<Long> idsBefore = resInsert.getEntityIds();
client.deleteEntityByID(collectionName, Collections.singletonList(idsBefore.get(0)));
client.flush(collectionName);
Response res = client.getCollectionStats(collectionName);
System.out.println(res.getMessage());
JSONObject collectionInfo = Utils.getCollectionInfo(res.getMessage());
int row_count = collectionInfo.getIntValue("row_count");
assert(row_count == nb-1);
int rowCount = collectionInfo.getIntValue("row_count");
assert(rowCount == nb-1);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_get_vector_ids_after_delete_vectors_indexed(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
public void testGetEntityIdsAterDeleteEntitiesIndexed(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName)
.withFields(defaultEntities)
.build();
InsertResponse resInsert = client.insert(insertParam);
client.flush(collectionName);
Index index = new Index.Builder(collectionName, indexType).withParamsInJson(indexParam).build();
client.createIndex(index);
List<Long> idsBefore = resInsert.getVectorIds();
Index index = new Index.Builder(collectionName, "float_vector")
.withParamsInJson(indexParam).build();
Response createIndexResponse = client.createIndex(index);
assert(createIndexResponse.ok());
List<Long> idsBefore = resInsert.getEntityIds();
client.deleteEntityByID(collectionName, Collections.singletonList(idsBefore.get(0)));
client.flush(collectionName);
Response res = client.getCollectionStats(collectionName);
System.out.println(res.getMessage());
JSONObject collectionInfo = Utils.getCollectionInfo(res.getMessage());
int row_count = collectionInfo.getIntValue("row_count");
assert(row_count == nb-1);
int rowCount = collectionInfo.getIntValue("row_count");
assert(rowCount == nb-1);
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_get_vector_ids_after_delete_vectors_binary(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
public void testGetEntityIdsAfterDeleteEntitiesBinary(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName)
.withFields(defaultBinaryEntities)
.build();
InsertResponse resInsert = client.insert(insertParam);
client.flush(collectionName);
List<Long> idsBefore = resInsert.getVectorIds();
List<Long> idsBefore = resInsert.getEntityIds();
client.deleteEntityByID(collectionName, Collections.singletonList(idsBefore.get(0)));
client.flush(collectionName);
Response res = client.getCollectionStats(collectionName);
System.out.println(res.getMessage());
JSONObject collectionInfo = Utils.getCollectionInfo(res.getMessage());
int row_count = collectionInfo.getIntValue("row_count");
assert(row_count == nb-1);
int rowCount = collectionInfo.getIntValue("row_count");
assert(rowCount == nb-1);
}
}
\ No newline at end of file
package com;
import io.milvus.client.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.nio.ByteBuffer;
import java.util.List;
public class TestCompact {
int dimension = 128;
int nb = 8000;
List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_compact_after_delete(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getVectorIds();
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, ids);
assert(res_delete.ok());
client.flush(collectionName);
Response res_compact = client.compact(collectionName);
assert(res_compact.ok());
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_compact_after_delete_binary(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getVectorIds();
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, ids);
assert(res_delete.ok());
client.flush(collectionName);
Response res_compact = client.compact(collectionName);
assert(res_compact.ok());
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_compact_no_table(MilvusClient client, String collectionName) {
String name = "";
Response res_compact = client.compact(name);
assert(!res_compact.ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_compact_empty_table(MilvusClient client, String collectionName) {
Response res_compact = client.compact(collectionName);
assert(res_compact.ok());
}
}
//package com;
//
//import io.milvus.client.*;
//import org.testng.Assert;
//import org.testng.annotations.Test;
//
//import java.nio.ByteBuffer;
//import java.util.List;
//
//public class TestCompact {
//
// int dimension = 128;
// int nb = 8000;
// List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
// List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_compact_after_delete(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// Response res_compact = client.compact(collectionName);
// assert(res_compact.ok());
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_compact_after_delete_binary(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// Response res_compact = client.compact(collectionName);
// assert(res_compact.ok());
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_compact_no_table(MilvusClient client, String collectionName) {
// String name = "";
// Response res_compact = client.compact(name);
// assert(!res_compact.ok());
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_compact_empty_table(MilvusClient client, String collectionName) {
// Response res_compact = client.compact(collectionName);
// assert(res_compact.ok());
// }
//
//}
......@@ -7,7 +7,7 @@ import org.testng.annotations.Test;
public class TestConnect {
@Test(dataProvider = "DefaultConnectArgs", dataProviderClass = MainClass.class)
public void test_connect(String host, int port) throws ConnectFailedException {
public void testConnect(String host, int port) throws ConnectFailedException {
System.out.println("Host: "+host+", Port: "+port);
MilvusClient client = new MilvusGrpcClient();
ConnectParam connectParam = new ConnectParam.Builder()
......@@ -16,11 +16,11 @@ public class TestConnect {
.build();
Response res = client.connect(connectParam);
assert(res.ok());
assert(client.isConnected());
// assert(client.isConnected());
}
@Test(dataProvider = "DefaultConnectArgs", dataProviderClass = MainClass.class)
public void test_connect_repeat(String host, int port) {
public void testConnectRepeat(String host, int port) {
MilvusGrpcClient client = new MilvusGrpcClient();
Response res = null;
......@@ -35,11 +35,11 @@ public class TestConnect {
e.printStackTrace();
}
assert (res.ok());
assert(client.isConnected());
// assert(client.isConnected());
}
@Test(dataProvider="InvalidConnectArgs")
public void test_connect_invalid_connect_args(String ip, int port) {
public void testConnectInvalidConnect_args(String ip, int port) {
MilvusClient client = new MilvusGrpcClient();
Response res = null;
try {
......@@ -52,30 +52,30 @@ public class TestConnect {
e.printStackTrace();
}
Assert.assertEquals(res, null);
assert(!client.isConnected());
// assert(!client.isConnected());
}
@DataProvider(name="InvalidConnectArgs")
public Object[][] generate_invalid_connect_args() {
public Object[][] generateInvalidConnectArgs() {
int port = 19530;
return new Object[][]{
{"1.1.1.1", port},
{"255.255.0.0", port},
{"1.2.2", port},
{"中文", port},
{"www.baidu.com", 100000},
// {"中文", port},
// {"www.baidu.com", 100000},
{"127.0.0.1", 100000},
{"www.baidu.com", 80},
};
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_disconnect(MilvusClient client, String collectionName){
assert(!client.isConnected());
public void testDisconnect(MilvusClient client, String collectionName){
// assert(!client.isConnected());
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_disconnect_repeatably(MilvusClient client, String collectionName){
public void testDisconnectRepeatably(MilvusClient client, String collectionName){
Response res = null;
try {
res = client.disconnect();
......@@ -83,6 +83,6 @@ public class TestConnect {
e.printStackTrace();
}
assert(!res.ok());
assert(!client.isConnected());
// assert(!client.isConnected());
}
}
package com;
import io.milvus.client.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestDeleteVectors {
int dimension = 128;
int nb = 8000;
List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_delete_vectors(MilvusClient client, String collectionName) {
// Add vectors
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getVectorIds();
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, ids);
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_delete_single_vector(MilvusClient client, String collectionName) {
List<List<Float>> del_vector = new ArrayList<>();
del_vector.add(vectors.get(0));
List<Long> del_ids = new ArrayList<>();
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getVectorIds();
del_ids.add(ids.get(0));
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb - 1);
GetEntityByIDResponse res_get = client.getEntityByID(collectionName, del_ids);
assert(res_get.getResponse().ok());
assert(res_get.getFloatVectors().get(0).size() == 0);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_delete_vectors_collection_not_existed(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
client.flush(collectionName);
List<Long> ids = res.getVectorIds();
Response res_delete = client.deleteEntityByID(collectionName + "_not_existed", ids);
assert(!res_delete.ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_delete_vector_id_not_existed(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = new ArrayList<Long>();
ids.add((long)123456);
ids.add((long)1234561);
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, ids);
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
}
// Below tests binary vectors
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_delete_vectors_binary(MilvusClient client, String collectionName) {
// Add vectors
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getVectorIds();
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, ids);
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_delete_single_vector_binary(MilvusClient client, String collectionName) {
List<ByteBuffer> del_vector = new ArrayList<>();
del_vector.add(vectorsBinary.get(0));
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getVectorIds();
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb - 1);
// Cannot search for the vector
SearchParam searchParam = new SearchParam.Builder(collectionName)
.withBinaryVectors(del_vector)
.withTopK(1)
.withParamsInJson("{\"nprobe\": 20}")
.build();
SearchResponse res_search = client.search(searchParam);
assert(res_search.getResultIdsList().size() == 1);
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_delete_vectors_collection_not_existed_binary(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = res.getVectorIds();
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName + "_not_existed", ids);
assert(!res_delete.ok());
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_delete_vector_id_not_existed_binary(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
List<Long> ids = new ArrayList<Long>();
ids.add((long)123456);
ids.add((long)1234561);
client.flush(collectionName);
Response res_delete = client.deleteEntityByID(collectionName, ids);
assert(res_delete.ok());
client.flush(collectionName);
// Assert collection row count
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
}
}
//package com;
//
//import io.milvus.client.*;
//import org.testng.Assert;
//import org.testng.annotations.Test;
//
//import java.nio.ByteBuffer;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//
//public class TestDeleteVectors {
// int dimension = 128;
// int nb = 8000;
//
// List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
// List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_delete_vectors(MilvusClient client, String collectionName) {
// // Add vectors
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_delete_single_vector(MilvusClient client, String collectionName) {
// List<List<Float>> del_vector = new ArrayList<>();
// del_vector.add(vectors.get(0));
// List<Long> del_ids = new ArrayList<>();
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// del_ids.add(ids.get(0));
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb - 1);
// GetEntityByIDResponse res_get = client.getEntityByID(collectionName, del_ids);
// assert(res_get.getResponse().ok());
// assert(res_get.getFloatVectors().get(0).size() == 0);
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_delete_vectors_collection_not_existed(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// client.flush(collectionName);
// List<Long> ids = res.getVectorIds();
// Response res_delete = client.deleteEntityByID(collectionName + "_not_existed", ids);
// assert(!res_delete.ok());
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_delete_vector_id_not_existed(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = new ArrayList<Long>();
// ids.add((long)123456);
// ids.add((long)1234561);
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
// }
//
//
// // Below tests binary vectors
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_delete_vectors_binary(MilvusClient client, String collectionName) {
// // Add vectors
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), 0);
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_delete_single_vector_binary(MilvusClient client, String collectionName) {
// List<ByteBuffer> del_vector = new ArrayList<>();
// del_vector.add(vectorsBinary.get(0));
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb - 1);
// // Cannot search for the vector
// SearchParam searchParam = new SearchParam.Builder(collectionName)
// .withBinaryVectors(del_vector)
// .withTopK(1)
// .withParamsInJson("{\"nprobe\": 20}")
// .build();
// SearchResponse res_search = client.search(searchParam);
// assert(res_search.getResultIdsList().size() == 1);
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_delete_vectors_collection_not_existed_binary(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = res.getVectorIds();
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName + "_not_existed", ids);
// assert(!res_delete.ok());
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_delete_vector_id_not_existed_binary(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse res = client.insert(insertParam);
// assert(res.getResponse().ok());
// List<Long> ids = new ArrayList<Long>();
// ids.add((long)123456);
// ids.add((long)1234561);
// client.flush(collectionName);
// Response res_delete = client.deleteEntityByID(collectionName, ids);
// assert(res_delete.ok());
// client.flush(collectionName);
// // Assert collection row count
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
// }
//
//}
package com;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.concurrent.ExecutionException;
import io.milvus.client.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
public class TestFlush {
int index_file_size = 50;
int dimension = 128;
int nb = 8000;
List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_flush_collection_not_existed(MilvusClient client, String collectionName) {
String newCollection = "not_existed";
Response res = client.flush(newCollection);
assert(!res.ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_flush_empty_collection(MilvusClient client, String collectionName) {
Response res = client.flush(collectionName);
assert(res.ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_collections_flush(MilvusClient client, String collectionName) {
List<String> names = new ArrayList<>();
for (int i = 0; i < 10; i++) {
names.add(RandomStringUtils.randomAlphabetic(10));
CollectionMapping tableSchema = new CollectionMapping.Builder(names.get(i), dimension)
.withIndexFileSize(index_file_size)
.withMetricType(MetricType.IP)
.build();
client.createCollection(tableSchema);
InsertParam insertParam = new InsertParam.Builder(names.get(i)).withFloatVectors(vectors).build();
client.insert(insertParam);
System.out.println("Table " + names.get(i) + " created.");
}
Response res = client.flush(names);
assert(res.ok());
for (int i = 0; i < 10; i++) {
// check row count
Assert.assertEquals(client.countEntities(names.get(i)).getCollectionEntityCount(), nb);
}
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_collections_flush_async(MilvusClient client, String collectionName) throws ExecutionException, InterruptedException {
List<String> names = new ArrayList<>();
for (int i = 0; i < 100; i++) {
names.add(RandomStringUtils.randomAlphabetic(10));
CollectionMapping tableSchema = new CollectionMapping.Builder(names.get(i), dimension)
.withIndexFileSize(index_file_size)
.withMetricType(MetricType.IP)
.build();
client.createCollection(tableSchema);
InsertParam insertParam = new InsertParam.Builder(names.get(i)).withFloatVectors(vectors).build();
client.insert(insertParam);
System.out.println("Collection " + names.get(i) + " created.");
}
ListenableFuture<Response> flushResponseFuture = client.flushAsync(names);
flushResponseFuture.get();
for (int i = 0; i < 100; i++) {
// check row count
Assert.assertEquals(client.countEntities(names.get(i)).getCollectionEntityCount(), nb);
}
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_flush_multiple_times(MilvusClient client, String collectionName) {
for (int i = 0; i < 10; i++) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
client.insert(insertParam);
Response res = client.flush(collectionName);
assert(res.ok());
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb * (i+1));
}
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_add_flush_multiple_times_binary(MilvusClient client, String collectionName) {
for (int i = 0; i < 10; i++) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
client.insert(insertParam);
Response res = client.flush(collectionName);
assert(res.ok());
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb * (i+1));
}
}
}
\ No newline at end of file
//package com;
//
//import com.google.common.util.concurrent.ListenableFuture;
//import java.util.concurrent.ExecutionException;
//import io.milvus.client.*;
//
//import org.apache.commons.lang3.RandomStringUtils;
//import org.testng.Assert;
//import org.testng.annotations.Test;
//
//import java.nio.ByteBuffer;
//import java.util.ArrayList;
//import java.util.List;
//
//public class TestFlush {
// int index_file_size = 50;
// int dimension = 128;
// int nb = 8000;
//
// List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
// List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
//
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_flush_collection_not_existed(MilvusClient client, String collectionName) {
// String newCollection = "not_existed";
// Response res = client.flush(newCollection);
// assert(!res.ok());
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_flush_empty_collection(MilvusClient client, String collectionName) {
// Response res = client.flush(collectionName);
// assert(res.ok());
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_add_collections_flush(MilvusClient client, String collectionName) {
// List<String> names = new ArrayList<>();
// for (int i = 0; i < 10; i++) {
// names.add(RandomStringUtils.randomAlphabetic(10));
// CollectionMapping tableSchema = new CollectionMapping.Builder(names.get(i), dimension)
// .withIndexFileSize(index_file_size)
// .withMetricType(MetricType.IP)
// .build();
// client.createCollection(tableSchema);
// InsertParam insertParam = new InsertParam.Builder(names.get(i)).withFloatVectors(vectors).build();
// client.insert(insertParam);
// System.out.println("Table " + names.get(i) + " created.");
// }
// Response res = client.flush(names);
// assert(res.ok());
// for (int i = 0; i < 10; i++) {
// // check row count
// Assert.assertEquals(client.countEntities(names.get(i)).getCollectionEntityCount(), nb);
// }
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_add_collections_flush_async(MilvusClient client, String collectionName) throws ExecutionException, InterruptedException {
// List<String> names = new ArrayList<>();
// for (int i = 0; i < 100; i++) {
// names.add(RandomStringUtils.randomAlphabetic(10));
// CollectionMapping tableSchema = new CollectionMapping.Builder(names.get(i), dimension)
// .withIndexFileSize(index_file_size)
// .withMetricType(MetricType.IP)
// .build();
// client.createCollection(tableSchema);
// InsertParam insertParam = new InsertParam.Builder(names.get(i)).withFloatVectors(vectors).build();
// client.insert(insertParam);
// System.out.println("Collection " + names.get(i) + " created.");
// }
// ListenableFuture<Response> flushResponseFuture = client.flushAsync(names);
// flushResponseFuture.get();
// for (int i = 0; i < 100; i++) {
// // check row count
// Assert.assertEquals(client.countEntities(names.get(i)).getCollectionEntityCount(), nb);
// }
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_add_flush_multiple_times(MilvusClient client, String collectionName) {
// for (int i = 0; i < 10; i++) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// client.insert(insertParam);
// Response res = client.flush(collectionName);
// assert(res.ok());
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb * (i+1));
// }
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_add_flush_multiple_times_binary(MilvusClient client, String collectionName) {
// for (int i = 0; i < 10; i++) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// client.insert(insertParam);
// Response res = client.flush(collectionName);
// assert(res.ok());
// Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb * (i+1));
// }
// }
//}
\ No newline at end of file
//package com;
//
//import io.milvus.client.*;
//import org.testng.annotations.Test;
//
//import java.nio.ByteBuffer;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//import java.util.Map;
//
//public class TestGetEntityByID {
// int dimension = 128;
// int nb = 8000;
// public List<Long> get_ids = Utils.toListIds(1111);
//// List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
//// List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
// List<Map<String,Object>> defaultEntities = Utils.genDefaultEntities(dimension,nb,false);
// List<Map<String,Object>> defaultBinaryEntities = Utils.genDefaultEntities(dimension,nb,true);
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_get_vector_by_id_valid(MilvusClient client, String collectionName) {
// int get_length = 100;
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultEntities).build();
// InsertResponse resInsert = client.insert(insertParam);
// List<Long> ids = resInsert.getEntityIds();
// client.flush(collectionName);
// GetEntityByIDResponse res = client.getEntityByID(collectionName, ids.subList(0, get_length));
// assert (res.getResponse().ok());
// for (int i = 0; i < get_length; i++) {
// List<Map<String,Object>> fields = res.getFieldsMap();
// assert (res.getFieldsMap().get(i).equals(vectors.get(i)));
// }
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_get_vector_by_id_after_delete(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// InsertResponse resInsert = client.insert(insertParam);
// List<Long> ids = resInsert.getVectorIds();
// Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
// assert(res_delete.ok());
// client.flush(collectionName);
// GetEntityByIDResponse res = client.getEntityByID(collectionName, ids.subList(0, 1));
// assert (res.getResponse().ok());
// assert (res.getFloatVectors().get(0).size() == 0);
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_get_vector_by_id_collection_name_not_existed(MilvusClient client, String collectionName) {
// String newCollection = "not_existed";
// GetEntityByIDResponse res = client.getEntityByID(newCollection, get_ids);
// assert(!res.getResponse().ok());
// }
//
// @Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
// public void test_get_vector_id_not_existed(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
// client.insert(insertParam);
// client.flush(collectionName);
// GetEntityByIDResponse res = client.getEntityByID(collectionName, get_ids);
// assert (res.getFloatVectors().get(0).size() == 0);
// }
//
// // Binary tests
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_get_vector_by_id_valid_binary(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse resInsert = client.insert(insertParam);
// List<Long> ids = resInsert.getVectorIds();
// client.flush(collectionName);
// GetEntityByIDResponse res = client.getEntityByID(collectionName, ids.subList(0, 1));
// assert res.getBinaryVectors().get(0).equals(vectorsBinary.get(0).rewind());
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_get_vector_by_id_after_delete_binary(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// InsertResponse resInsert = client.insert(insertParam);
// List<Long> ids = resInsert.getVectorIds();
// Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
// assert(res_delete.ok());
// client.flush(collectionName);
// GetEntityByIDResponse res = client.getEntityByID(collectionName, ids.subList(0, 1));
// assert (res.getFloatVectors().get(0).size() == 0);
// }
//
// @Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
// public void test_get_vector_id_not_existed_binary(MilvusClient client, String collectionName) {
// InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
// client.insert(insertParam);
// client.flush(collectionName);
// GetEntityByIDResponse res = client.getEntityByID(collectionName, get_ids);
// assert (res.getFloatVectors().get(0).size() == 0);
// }
//}
\ No newline at end of file
package com;
import io.milvus.client.*;
import org.testng.annotations.Test;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestGetVectorByID {
int dimension = 128;
int nb = 8000;
public List<Long> get_ids = Utils.toListIds(1111);
List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_get_vector_by_id_valid(MilvusClient client, String collectionName) {
int get_length = 100;
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
InsertResponse resInsert = client.insert(insertParam);
List<Long> ids = resInsert.getVectorIds();
client.flush(collectionName);
GetEntityByIDResponse res = client.getEntityByID(collectionName, ids.subList(0, get_length));
assert (res.getResponse().ok());
for (int i = 0; i < get_length; i++) {
assert (res.getFloatVectors().get(i).equals(vectors.get(i)));
}
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_get_vector_by_id_after_delete(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
InsertResponse resInsert = client.insert(insertParam);
List<Long> ids = resInsert.getVectorIds();
Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
assert(res_delete.ok());
client.flush(collectionName);
GetEntityByIDResponse res = client.getEntityByID(collectionName, ids.subList(0, 1));
assert (res.getResponse().ok());
assert (res.getFloatVectors().get(0).size() == 0);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_get_vector_by_id_collection_name_not_existed(MilvusClient client, String collectionName) {
String newCollection = "not_existed";
GetEntityByIDResponse res = client.getEntityByID(newCollection, get_ids);
assert(!res.getResponse().ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_get_vector_id_not_existed(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
client.insert(insertParam);
client.flush(collectionName);
GetEntityByIDResponse res = client.getEntityByID(collectionName, get_ids);
assert (res.getFloatVectors().get(0).size() == 0);
}
// Binary tests
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_get_vector_by_id_valid_binary(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
InsertResponse resInsert = client.insert(insertParam);
List<Long> ids = resInsert.getVectorIds();
client.flush(collectionName);
GetEntityByIDResponse res = client.getEntityByID(collectionName, ids.subList(0, 1));
assert res.getBinaryVectors().get(0).equals(vectorsBinary.get(0).rewind());
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_get_vector_by_id_after_delete_binary(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
InsertResponse resInsert = client.insert(insertParam);
List<Long> ids = resInsert.getVectorIds();
Response res_delete = client.deleteEntityByID(collectionName, Collections.singletonList(ids.get(0)));
assert(res_delete.ok());
client.flush(collectionName);
GetEntityByIDResponse res = client.getEntityByID(collectionName, ids.subList(0, 1));
assert (res.getFloatVectors().get(0).size() == 0);
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_get_vector_id_not_existed_binary(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
client.insert(insertParam);
client.flush(collectionName);
GetEntityByIDResponse res = client.getEntityByID(collectionName, get_ids);
assert (res.getFloatVectors().get(0).size() == 0);
}
}
\ No newline at end of file
......@@ -8,33 +8,37 @@ import org.testng.annotations.Test;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import java.util.stream.Stream;
public class TestAddVectors {
public class TestInsertEntities {
int dimension = 128;
String tag = "tag";
int nb = 8000;
List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
// List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
// List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension);
List<Map<String,Object>> defaultEntities = Utils.genDefaultEntities(dimension,nb,false);
List<Map<String,Object>> defaultBinaryEntities = Utils.genDefaultEntities(dimension,nb,true);
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_vectors_collection_not_existed(MilvusClient client, String collectionName) throws InterruptedException {
public void testInsertEntitiesCollectionNotExisted(MilvusClient client, String collectionName) throws InterruptedException {
String collectionNameNew = collectionName + "_";
InsertParam insertParam = new InsertParam.Builder(collectionNameNew).withFloatVectors(vectors).build();
InsertParam insertParam = new InsertParam.Builder(collectionNameNew)
.withFields(defaultEntities).build();
InsertResponse res = client.insert(insertParam);
assert(!res.getResponse().ok());
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_add_vectors_without_connect(MilvusClient client, String collectionName) throws InterruptedException {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
public void testInsertEntitiesWithoutConnect(MilvusClient client, String collectionName) throws InterruptedException {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultEntities).build();
InsertResponse res = client.insert(insertParam);
assert(!res.getResponse().ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_vectors(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
public void testInsertEntities(MilvusClient client, String collectionName) {
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultEntities).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
Response res_flush = client.flush(collectionName);
......@@ -43,14 +47,14 @@ public class TestAddVectors {
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_vectors_with_ids(MilvusClient client, String collectionName) {
@Test(dataProvider = "IdCollection", dataProviderClass = MainClass.class)
public void testInsertEntityWithIds(MilvusClient client, String collectionName) {
// Add vectors with ids
List<Long> vectorIds;
vectorIds = Stream.iterate(0L, n -> n)
.limit(nb)
.collect(Collectors.toList());
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
List<Long> entityIds = LongStream.range(0, nb).boxed().collect(Collectors.toList());
InsertParam insertParam = new InsertParam.Builder(collectionName)
.withFields(defaultEntities)
.withEntityIds(entityIds)
.build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
Response res_flush = client.flush(collectionName);
......@@ -59,30 +63,34 @@ public class TestAddVectors {
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_vectors_with_invalid_ids(MilvusClient client, String collectionName) {
@Test(dataProvider = "IdCollection", dataProviderClass = MainClass.class)
public void testInsertEntityWithInvalidIds(MilvusClient client, String collectionName) {
// Add vectors with ids
List<Long> vectorIds;
vectorIds = Stream.iterate(0L, n -> n)
.limit(nb+1)
.collect(Collectors.toList());
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
List<Long> entityIds = LongStream.range(0, nb+1).boxed().collect(Collectors.toList());
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultEntities).withEntityIds(entityIds).build();
InsertResponse res = client.insert(insertParam);
assert(!res.getResponse().ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_vectors_with_invalid_dimension(MilvusClient client, String collectionName) {
vectors.get(0).add((float) 0);
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
public void testInsertEntityWithInvalidDimension(MilvusClient client, String collectionName) {
// vectors.get(0).add((float) 0);
List<Map<String,Object>> entities = Utils.genDefaultEntities(dimension+1,nb,false);
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(entities).build();
InsertResponse res = client.insert(insertParam);
assert(!res.getResponse().ok());
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_vectors_with_invalid_vectors(MilvusClient client, String collectionName) {
vectors.set(0, new ArrayList<>());
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).build();
public void testInsertEntityWithInvalidVectors(MilvusClient client, String collectionName) {
// vectors.set(0, new ArrayList<>());
List<Map<String,Object>> invalidEntities = Utils.genDefaultEntities(dimension,nb,false);
invalidEntities.forEach(entity ->{
if("float_vector".equals(entity.get("field"))){
entity.put("values",new ArrayList<>());
}
});
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(invalidEntities).build();
InsertResponse res = client.insert(insertParam);
assert(!res.getResponse().ok());
}
......@@ -90,10 +98,10 @@ public class TestAddVectors {
// ----------------------------- partition cases in Insert ---------------------------------
// Add vectors into collection with given tag
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_vectors_partition(MilvusClient client, String collectionName) {
public void testInsertEntityPartition(MilvusClient client, String collectionName) {
Response createpResponse = client.createPartition(collectionName, tag);
assert(createpResponse.ok());
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).withPartitionTag(tag).build();
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultEntities).withPartitionTag(tag).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
Response res_flush = client.flush(collectionName);
......@@ -104,20 +112,20 @@ public class TestAddVectors {
// Add vectors into collection, which tag not existed
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_add_vectors_partition_tag_not_existed(MilvusClient client, String collectionName) {
public void testInsertEntityPartitionTagNotExisted(MilvusClient client, String collectionName) {
Response createpResponse = client.createPartition(collectionName, tag);
assert(createpResponse.ok());
String tag = RandomStringUtils.randomAlphabetic(10);
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).withPartitionTag(tag).build();
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultEntities).withPartitionTag(tag).build();
InsertResponse res = client.insert(insertParam);
assert(!res.getResponse().ok());
}
// Binary tests
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_add_vectors_partition_A_binary(MilvusClient client, String collectionName) {
public void testInsertEntityPartitionABinary(MilvusClient client, String collectionName) {
Response createpResponse = client.createPartition(collectionName, tag);
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).withPartitionTag(tag).build();
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultBinaryEntities).withPartitionTag(tag).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
Response res_flush = client.flush(collectionName);
......@@ -127,9 +135,9 @@ public class TestAddVectors {
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_add_vectors_binary(MilvusClient client, String collectionName) {
public void testInsertEntityBinary(MilvusClient client, String collectionName) {
System.out.println(collectionName);
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultBinaryEntities).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
Response res_flush = client.flush(collectionName);
......@@ -138,14 +146,11 @@ public class TestAddVectors {
Assert.assertEquals(client.countEntities(collectionName).getCollectionEntityCount(), nb);
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_add_vectors_with_ids_binary(MilvusClient client, String collectionName) {
@Test(dataProvider = "BinaryIdCollection", dataProviderClass = MainClass.class)
public void testInsertBinaryEntityWithIds(MilvusClient client, String collectionName) {
// Add vectors with ids
List<Long> vectorIds;
vectorIds = Stream.iterate(0L, n -> n)
.limit(nb)
.collect(Collectors.toList());
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
List<Long> entityIds = LongStream.range(0, nb).boxed().collect(Collectors.toList());
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultBinaryEntities).withEntityIds(entityIds).build();
InsertResponse res = client.insert(insertParam);
assert(res.getResponse().ok());
Response res_flush = client.flush(collectionName);
......@@ -155,21 +160,19 @@ public class TestAddVectors {
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_add_vectors_with_invalid_ids_binary(MilvusClient client, String collectionName) {
public void testInsertBinaryEntityWithInvalidIds(MilvusClient client, String collectionName) {
// Add vectors with ids
List<Long> vectorIds;
vectorIds = Stream.iterate(0L, n -> n)
.limit(nb+1)
.collect(Collectors.toList());
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).withVectorIds(vectorIds).build();
List<Long> invalidEntityIds = LongStream.range(0, nb+1).boxed().collect(Collectors.toList());
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(defaultBinaryEntities).withEntityIds(invalidEntityIds).build();
InsertResponse res = client.insert(insertParam);
assert(!res.getResponse().ok());
}
@Test(dataProvider = "BinaryCollection", dataProviderClass = MainClass.class)
public void test_add_vectors_with_invalid_dimension_binary(MilvusClient client, String collectionName) {
public void testInsertBinaryEntityWithInvalidDimension(MilvusClient client, String collectionName) {
List<ByteBuffer> vectorsBinary = Utils.genBinaryVectors(nb, dimension-1);
InsertParam insertParam = new InsertParam.Builder(collectionName).withBinaryVectors(vectorsBinary).build();
List<Map<String,Object>> binaryEntities = Utils.genDefaultEntities(dimension-1,nb,true);
InsertParam insertParam = new InsertParam.Builder(collectionName).withFields(binaryEntities).build();
InsertResponse res = client.insert(insertParam);
assert(!res.getResponse().ok());
}
......
package com;
import io.milvus.client.*;
import org.apache.commons.cli.*;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TestPS {
private static int dimension = 128;
private static String host = "127.0.0.1";
private static String port = "19530";
public static void setHost(String host) {
TestPS.host = host;
}
public static void setPort(String port) {
TestPS.port = port;
}
public static void main(String[] args) throws ConnectFailedException {
int nb = 10000;
int nq = 5;
int nprobe = 32;
int top_k = 10;
int loops = 100000;
// int index_file_size = 1024;
String collectionName = "sift_1b_2048_128_l2";
List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
List<List<Float>> queryVectors = vectors.subList(0, nq);
CommandLineParser parser = new DefaultParser();
Options options = new Options();
options.addOption("h", "host", true, "milvus-server hostname/ip");
options.addOption("p", "port", true, "milvus-server port");
try {
CommandLine cmd = parser.parse(options, args);
String host = cmd.getOptionValue("host");
if (host != null) {
setHost(host);
}
String port = cmd.getOptionValue("port");
if (port != null) {
setPort(port);
}
System.out.println("Host: "+host+", Port: "+port);
}
catch(ParseException exp) {
System.err.println("Parsing failed. Reason: " + exp.getMessage() );
}
MilvusClient client = new MilvusGrpcClient();
ConnectParam connectParam = new ConnectParam.Builder()
.withHost(host)
.withPort(Integer.parseInt(port))
.build();
client.connect(connectParam);
// String collectionName = RandomStringUtils.randomAlphabetic(10);
// TableSchema tableSchema = new TableSchema.Builder(collectionName, dimension)
// .withIndexFileSize(index_file_size)
// .withMetricType(MetricType.IP)
//package com;
//
//import io.milvus.client.*;
//import org.apache.commons.cli.*;
//
//import java.util.List;
//import java.util.concurrent.ForkJoinPool;
//import java.util.concurrent.TimeUnit;
//import java.util.stream.Collectors;
//import java.util.stream.Stream;
//
//public class TestPS {
// private static int dimension = 128;
// private static String host = "127.0.0.1";
// private static String port = "19530";
//
// public static void setHost(String host) {
// TestPS.host = host;
// }
//
// public static void setPort(String port) {
// TestPS.port = port;
// }
//
//
//
// public static void main(String[] args) throws ConnectFailedException {
// int nb = 10000;
// int nq = 5;
// int nprobe = 32;
// int top_k = 10;
// int loops = 100000;
//// int index_file_size = 1024;
// String collectionName = "sift_1b_2048_128_l2";
//
//
// List<List<Float>> vectors = Utils.genVectors(nb, dimension, true);
// List<List<Float>> queryVectors = vectors.subList(0, nq);
//
// CommandLineParser parser = new DefaultParser();
// Options options = new Options();
// options.addOption("h", "host", true, "milvus-server hostname/ip");
// options.addOption("p", "port", true, "milvus-server port");
// try {
// CommandLine cmd = parser.parse(options, args);
// String host = cmd.getOptionValue("host");
// if (host != null) {
// setHost(host);
// }
// String port = cmd.getOptionValue("port");
// if (port != null) {
// setPort(port);
// }
// System.out.println("Host: "+host+", Port: "+port);
// }
// catch(ParseException exp) {
// System.err.println("Parsing failed. Reason: " + exp.getMessage() );
// }
//
// MilvusClient client = new MilvusGrpcClient();
// ConnectParam connectParam = new ConnectParam.Builder()
// .withHost(host)
// .withPort(Integer.parseInt(port))
// .build();
// Response res = client.createTable(tableSchema);
List<Long> vectorIds;
vectorIds = Stream.iterate(0L, n -> n)
.limit(nb)
.collect(Collectors.toList());
InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).withVectorIds(vectorIds).build();
ForkJoinPool executor_search = new ForkJoinPool();
for (int i = 0; i < loops; i++) {
executor_search.execute(
() -> {
InsertResponse res_insert = client.insert(insertParam);
assert (res_insert.getResponse().ok());
System.out.println("In insert");
SearchParam searchParam = new SearchParam.Builder(collectionName).withFloatVectors(queryVectors).withTopK(top_k).build();
SearchResponse res_search = client.search(searchParam);
assert (res_search.getResponse().ok());
});
}
executor_search.awaitQuiescence(300, TimeUnit.SECONDS);
executor_search.shutdown();
CountEntitiesResponse getTableRowCountResponse = client.countEntities(collectionName);
System.out.println(getTableRowCountResponse.getCollectionEntityCount());
}
}
// client.connect(connectParam);
//// String collectionName = RandomStringUtils.randomAlphabetic(10);
//// TableSchema tableSchema = new TableSchema.Builder(collectionName, dimension)
//// .withIndexFileSize(index_file_size)
//// .withMetricType(MetricType.IP)
//// .build();
//// Response res = client.createTable(tableSchema);
// List<Long> vectorIds;
// vectorIds = Stream.iterate(0L, n -> n)
// .limit(nb)
// .collect(Collectors.toList());
// InsertParam insertParam = new InsertParam.Builder(collectionName).withFloatVectors(vectors).withVectorIds(vectorIds).build();
// ForkJoinPool executor_search = new ForkJoinPool();
// for (int i = 0; i < loops; i++) {
// executor_search.execute(
// () -> {
// InsertResponse res_insert = client.insert(insertParam);
// assert (res_insert.getResponse().ok());
// System.out.println("In insert");
// SearchParam searchParam = new SearchParam.Builder(collectionName).withFloatVectors(queryVectors).withTopK(top_k).build();
// SearchResponse res_search = client.search(searchParam);
// assert (res_search.getResponse().ok());
// });
// }
// executor_search.awaitQuiescence(300, TimeUnit.SECONDS);
// executor_search.shutdown();
// CountEntitiesResponse getTableRowCountResponse = client.countEntities(collectionName);
// System.out.println(getTableRowCountResponse.getCollectionEntityCount());
// }
//}
......@@ -13,7 +13,7 @@ public class TestPartition {
// create partition
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_create_partition(MilvusClient client, String collectionName) {
public void testCreatePartition(MilvusClient client, String collectionName) {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponse = client.createPartition(collectionName, tag);
assert (createpResponse.ok());
......@@ -25,7 +25,7 @@ public class TestPartition {
// create partition, tag name existed
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_create_partition_tag_name_existed(MilvusClient client, String collectionName) {
public void testCreatePartitionTagNameExisted(MilvusClient client, String collectionName) {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponse = client.createPartition(collectionName, tag);
assert (createpResponse.ok());
......@@ -36,7 +36,7 @@ public class TestPartition {
// ----------------------------- has partition cases in ---------------------------------
// has partition, tag name not existed
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_has_partition_tag_name_not_existed(MilvusClient client, String collectionName) {
public void testHasPartitionTagNameNotExisted(MilvusClient client, String collectionName) {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponse = client.createPartition(collectionName, tag);
assert (createpResponse.ok());
......@@ -48,7 +48,7 @@ public class TestPartition {
// has partition, tag name existed
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_has_partition_tag_name_existed(MilvusClient client, String collectionName) {
public void testHasPartitionTagNameExisted(MilvusClient client, String collectionName) {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponse = client.createPartition(collectionName, tag);
assert (createpResponse.ok());
......@@ -61,7 +61,7 @@ public class TestPartition {
// drop a partition created before, drop by partition name
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_drop_partition(MilvusClient client, String collectionName) {
public void testDropPartition(MilvusClient client, String collectionName) {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponseNew = client.createPartition(collectionName, tag);
assert (createpResponseNew.ok());
......@@ -75,7 +75,7 @@ public class TestPartition {
}
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_drop_partition_default(MilvusClient client, String collectionName) {
public void testDropPartitionDefault(MilvusClient client, String collectionName) {
String tag = "_default";
Response createpResponseNew = client.createPartition(collectionName, tag);
assert (!createpResponseNew.ok());
......@@ -88,7 +88,7 @@ public class TestPartition {
// drop a partition repeat created before, drop by partition name
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_drop_partition_repeat(MilvusClient client, String collectionName) throws InterruptedException {
public void testDropPartitionRepeat(MilvusClient client, String collectionName) throws InterruptedException {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponse = client.createPartition(collectionName, tag);
assert (createpResponse.ok());
......@@ -101,7 +101,7 @@ public class TestPartition {
// drop a partition not created before
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_drop_partition_not_existed(MilvusClient client, String collectionName) {
public void testDropPartitionNotExisted(MilvusClient client, String collectionName) {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponse = client.createPartition(collectionName, tag);
assert (createpResponse.ok());
......@@ -112,7 +112,7 @@ public class TestPartition {
// drop a partition not created before
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_drop_partition_tag_not_existed(MilvusClient client, String collectionName) {
public void testDropPartitionTagNotExisted(MilvusClient client, String collectionName) {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponse = client.createPartition(collectionName, tag);
assert(createpResponse.ok());
......@@ -125,7 +125,7 @@ public class TestPartition {
// create partition, then show partitions
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_show_partitions(MilvusClient client, String collectionName) {
public void testShowPartitions(MilvusClient client, String collectionName) {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponse = client.createPartition(collectionName, tag);
assert (createpResponse.ok());
......@@ -136,7 +136,7 @@ public class TestPartition {
// create multi partition, then show partitions
@Test(dataProvider = "Collection", dataProviderClass = MainClass.class)
public void test_show_partitions_multi(MilvusClient client, String collectionName) {
public void testShowPartitionsMulti(MilvusClient client, String collectionName) {
String tag = RandomStringUtils.randomAlphabetic(10);
Response createpResponse = client.createPartition(collectionName, tag);
assert (createpResponse.ok());
......
......@@ -5,7 +5,7 @@ import org.testng.annotations.Test;
public class TestPing {
@Test(dataProvider = "DefaultConnectArgs", dataProviderClass = MainClass.class)
public void test_server_status(String host, int port) throws ConnectFailedException {
public void testServerStatus(String host, int port) throws ConnectFailedException {
System.out.println("Host: "+host+", Port: "+port);
MilvusClient client = new MilvusGrpcClient();
ConnectParam connectParam = new ConnectParam.Builder()
......@@ -18,7 +18,7 @@ public class TestPing {
}
@Test(dataProvider = "DisConnectInstance", dataProviderClass = MainClass.class)
public void test_server_status_without_connected(MilvusClient client, String collectionName) throws ConnectFailedException {
public void testServerStatusWithoutConnected(MilvusClient client, String collectionName) throws ConnectFailedException {
Response res = client.getServerStatus();
assert (!res.ok());
}
......
package com;
import io.milvus.client.*;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.*;
import java.util.stream.Collectors;
public class Utils {
......@@ -16,21 +16,25 @@ public class Utils {
w2v = w2v.stream().map(x -> x / norm).collect(Collectors.toList());
return w2v;
}
public static List<List<Float>> genVectors(int nb, int dimension, boolean norm) {
List<List<Float>> xb = new ArrayList<>();
public static String genUniqueStr(String str_value){
String prefix = "_"+RandomStringUtils.randomAlphabetic(10);
String str = str_value == null || str_value.trim().isEmpty() ? "test" : str_value;
return str.trim()+prefix;
}
public static List<List<Float>> genVectors(int vectorCount, int dimension, boolean norm) {
List<List<Float>> vectors = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < nb; ++i) {
for (int i = 0; i < vectorCount; ++i) {
List<Float> vector = new ArrayList<>();
for (int j = 0; j < dimension; j++) {
for (int j = 0; j < dimension; ++j) {
vector.add(random.nextFloat());
}
if (norm == true) {
vector = normalize(vector);
}
xb.add(vector);
vectors.add(vector);
}
return xb;
return vectors;
}
static List<ByteBuffer> genBinaryVectors(long vectorCount, long dimension) {
......@@ -44,11 +48,73 @@ public class Utils {
}
return vectors;
}
private static List<Map<String, Object>> genBaseFieldsWithoutVector(){
List<Map<String,Object>> fieldsList = new ArrayList<>();
Map<String, Object> intFields = new HashMap<>();
intFields.put("field","int64");
intFields.put("type",DataType.INT64);
Map<String, Object> floatField = new HashMap<>();
floatField.put("field","float");
floatField.put("type",DataType.FLOAT);
fieldsList.add(intFields);
fieldsList.add(floatField);
return fieldsList;
}
public static List<Map<String, Object>> genDefaultFields(int dimension, boolean isBinary){
List<Map<String, Object>> defaultFieldList = genBaseFieldsWithoutVector();
Map<String, Object> vectorField = new HashMap<>();
if (isBinary){
vectorField.put("field","binary_vector");
vectorField.put("type",DataType.VECTOR_BINARY);
}else {
vectorField.put("field","float_vector");
vectorField.put("type",DataType.VECTOR_FLOAT);
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("dim", dimension);
vectorField.put("params", jsonObject.toString());
public static String setIndexParam(int nlist) {
JSONObject indexParam = new JSONObject();
indexParam.put("nlist", nlist);
return JSONObject.toJSONString(indexParam);
defaultFieldList.add(vectorField);
return defaultFieldList;
}
public static List<Map<String,Object>> genDefaultEntities(int dimension, int vectorCount, boolean isBinary){
List<Map<String,Object>> fields = genDefaultFields(dimension, isBinary);
List<Long> intValues = new ArrayList<>(vectorCount);
List<Float> floatValues = new ArrayList<>(vectorCount);
List<List<Float>> vectors = genVectors(vectorCount,dimension,false);
List<ByteBuffer> binaryVectors = genBinaryVectors(vectorCount,dimension);
for (int i = 0; i < vectorCount; ++i) {
intValues.add((long) i);
floatValues.add((float) i);
}
for(Map<String,Object> field: fields){
String fieldType = field.get("field").toString();
switch (fieldType){
case "int64":
field.put("values",intValues);
break;
case "float":
field.put("values",floatValues);
break;
case "float_vector":
field.put("values",vectors);
break;
case "binary_vector":
field.put("values",binaryVectors);
}
}
return fields;
}
public static String setIndexParam(String indexType, String metricType, int nlist) {
// ("{\"index_type\": \"IVF_SQ8\", \"metric_type\": \"L2\", \"\"params\": {\"nlist\": 2048}}")
// JSONObject indexParam = new JSONObject();
// indexParam.put("nlist", nlist);
// return JSONObject.toJSONString(indexParam);
String indexParams = String.format("{\"index_type\": %s, \"metric_type\": %s, \"params\": {\"nlist\": %s}}", indexType, metricType, nlist);
return indexParams;
}
public static String setSearchParam(int nprobe) {
......@@ -76,4 +142,19 @@ public class Utils {
ids.add(id);
return ids;
}
public static int getParam(String params, String key){
JSONObject jsonObject = JSONObject.parseObject(params);
System.out.println(jsonObject.toString());
Integer value = jsonObject.getInteger(key);
return value;
}
// public static List<Float> getVector(List<Map<String,Object>> entities, int i){
// List<Float> vector = new ArrayList<>();
// entities.forEach(entity -> {
// if("float_vector".equals(entity.get("field"))){
// vector.add(entity.get("values").get(i));
// }
// });
// }
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册