123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033 |
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- package io.milvus.client;
- import com.google.common.util.concurrent.ListenableFuture;
- import com.google.protobuf.ByteString;
- import io.milvus.common.clientenum.ConsistencyLevelEnum;
- import io.milvus.exception.IllegalResponseException;
- import io.milvus.exception.ParamException;
- import io.milvus.grpc.*;
- import io.milvus.param.*;
- import io.milvus.param.alias.AlterAliasParam;
- import io.milvus.param.alias.CreateAliasParam;
- import io.milvus.param.alias.DropAliasParam;
- import io.milvus.param.collection.*;
- import io.milvus.param.control.*;
- import io.milvus.param.credential.*;
- import io.milvus.param.dml.*;
- import io.milvus.param.index.*;
- import io.milvus.param.partition.*;
- import io.milvus.response.*;
- import io.milvus.server.MockMilvusServer;
- import io.milvus.server.MockMilvusServerImpl;
- import org.junit.jupiter.api.Test;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.nio.ByteBuffer;
- import java.util.*;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.TimeUnit;
- import static org.junit.jupiter.api.Assertions.*;
- class MilvusServiceClientTest {
- private final int testPort = 53019;
- private MockMilvusServerImpl mockServerImpl;
- private MockMilvusServer startServer() {
- mockServerImpl = new MockMilvusServerImpl();
- MockMilvusServer mockServer = new MockMilvusServer(testPort, mockServerImpl);
- mockServer.start();
- return mockServer;
- }
- private MilvusServiceClient startClient() {
- String testHost = "localhost";
- ConnectParam connectParam = ConnectParam.newBuilder()
- .withHost(testHost)
- .withPort(testPort)
- .build();
- return new MilvusServiceClient(connectParam);
- }
- @SuppressWarnings("unchecked")
- private <T, P> void invokeFunc(Method testFunc, MilvusServiceClient client, T param, int ret, boolean equalRet) {
- try {
- R<P> resp = (R<P>) testFunc.invoke(client, param);
- if (equalRet) {
- assertEquals(ret, resp.getStatus());
- } else {
- assertNotEquals(ret, resp.getStatus());
- }
- } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
- e.printStackTrace();
- System.out.println(e.getMessage());
- fail();
- }
- }
- private <T, P> void testFuncByName(String funcName, T param) {
- // start mock server
- MockMilvusServer server = startServer();
- MilvusServiceClient client = startClient();
- try {
- Class<?> clientClass = MilvusServiceClient.class;
- Method testFunc = clientClass.getMethod(funcName, param.getClass());
- // test return ok with correct input
- invokeFunc(testFunc, client, param, R.Status.Success.getCode(), true);
- // stop mock server
- server.stop();
- // test return error without server
- invokeFunc(testFunc, client, param, R.Status.Success.getCode(), false);
- // test return error when client channel is shutdown
- client.close();
- invokeFunc(testFunc, client, param, R.Status.ClientNotConnected.getCode(), true);
- } catch (NoSuchMethodException | SecurityException e) {
- e.printStackTrace();
- System.out.println(e.getMessage());
- fail();
- } finally {
- server.stop();
- client.close();
- }
- }
- @SuppressWarnings("unchecked")
- private <T, P> void testAsyncFuncByName(String funcName, T param) {
- // start mock server
- MockMilvusServer server = startServer();
- MilvusServiceClient client = startClient();
- try {
- Class<?> clientClass = MilvusServiceClient.class;
- Method testFunc = clientClass.getMethod(funcName, param.getClass());
- // test return ok with correct input
- try {
- ListenableFuture<R<P>> respFuture = (ListenableFuture<R<P>>) testFunc.invoke(client, param);
- R<P> response = respFuture.get();
- assertEquals(R.Status.Success.getCode(), response.getStatus());
- } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException |
- InterruptedException | ExecutionException e) {
- e.printStackTrace();
- System.out.println(e.getMessage());
- fail();
- }
- // stop mock server
- server.stop();
- // test return error without server
- assertThrows(ExecutionException.class, () -> {
- ListenableFuture<R<P>> respFuture = (ListenableFuture<R<P>>) testFunc.invoke(client, param);
- R<P> response = respFuture.get();
- assertNotEquals(R.Status.Success.getCode(), response.getStatus());
- });
- // test return error when client channel is shutdown
- client.close();
- try {
- ListenableFuture<R<P>> respFuture = (ListenableFuture<R<P>>) testFunc.invoke(client, param);
- R<P> response = respFuture.get();
- assertEquals(R.Status.ClientNotConnected.getCode(), response.getStatus());
- } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException |
- InterruptedException | ExecutionException e) {
- e.printStackTrace();
- System.out.println(e.getMessage());
- fail();
- }
- } catch (NoSuchMethodException | SecurityException e) {
- e.printStackTrace();
- System.out.println(e.getMessage());
- fail();
- } finally {
- server.stop();
- client.close();
- }
- }
- @Test
- void r() {
- String msg = "error";
- R<RpcStatus> r = R.failed(ErrorCode.UnexpectedError, msg);
- Exception e = r.getException();
- assertEquals(0, msg.compareTo(e.getMessage()));
- System.out.println(r.toString());
- r = R.success();
- assertEquals(R.Status.Success.getCode(), r.getStatus());
- System.out.println(r.toString());
- }
- @Test
- void connectParam() {
- System.out.println(System.getProperty("os.name"));
- System.out.println(System.getProperty("os.arch"));
- String host = "dummyHost";
- int port = 100;
- long connectTimeoutMs = 1;
- long keepAliveTimeMs = 2;
- long keepAliveTimeoutMs = 3;
- long idleTimeoutMs = 5;
- ConnectParam connectParam = ConnectParam.newBuilder()
- .withHost(host)
- .withPort(port)
- .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
- .keepAliveWithoutCalls(true)
- .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
- .build();
- System.out.println(connectParam.toString());
- assertEquals(0, host.compareTo(connectParam.getHost()));
- assertEquals(port, connectParam.getPort());
- assertEquals(connectTimeoutMs, connectParam.getConnectTimeoutMs());
- assertEquals(keepAliveTimeMs, connectParam.getKeepAliveTimeMs());
- assertEquals(keepAliveTimeoutMs, connectParam.getKeepAliveTimeoutMs());
- assertTrue(connectParam.isKeepAliveWithoutCalls());
- assertEquals(idleTimeoutMs, connectParam.getIdleTimeoutMs());
- assertThrows(ParamException.class, () ->
- ConnectParam.newBuilder()
- .withHost(host)
- .withPort(0xFFFF + 1)
- .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
- .keepAliveWithoutCalls(true)
- .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
- .build()
- );
- assertThrows(ParamException.class, () ->
- ConnectParam.newBuilder()
- .withHost(host)
- .withPort(port)
- .withConnectTimeout(-1, TimeUnit.MILLISECONDS)
- .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
- .keepAliveWithoutCalls(true)
- .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
- .build()
- );
- assertThrows(ParamException.class, () ->
- ConnectParam.newBuilder()
- .withHost(host)
- .withPort(port)
- .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTime(-1, TimeUnit.MILLISECONDS)
- .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
- .keepAliveWithoutCalls(true)
- .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
- .build()
- );
- assertThrows(ParamException.class, () ->
- ConnectParam.newBuilder()
- .withHost(host)
- .withPort(port)
- .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTimeout(-1, TimeUnit.NANOSECONDS)
- .keepAliveWithoutCalls(true)
- .withIdleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS)
- .build()
- );
- assertThrows(ParamException.class, () ->
- ConnectParam.newBuilder()
- .withHost(host)
- .withPort(port)
- .withConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTime(keepAliveTimeMs, TimeUnit.MILLISECONDS)
- .withKeepAliveTimeout(keepAliveTimeoutMs, TimeUnit.MILLISECONDS)
- .keepAliveWithoutCalls(true)
- .withIdleTimeout(-1, TimeUnit.MILLISECONDS)
- .build()
- );
- }
- @Test
- void createCollectionParam() {
- // test throw exception with illegal input for FieldType
- assertThrows(ParamException.class, () ->
- FieldType.newBuilder()
- .withName("")
- .withDataType(DataType.Int64)
- .build()
- );
- assertThrows(ParamException.class, () ->
- FieldType.newBuilder()
- .withName("userID")
- .build()
- );
- assertThrows(ParamException.class, () ->
- FieldType.newBuilder()
- .withName("userID")
- .withDataType(DataType.FloatVector)
- .build()
- );
- assertThrows(ParamException.class, () ->
- FieldType.newBuilder()
- .withName("userID")
- .withDataType(DataType.Int64)
- .withPrimaryKey(true)
- .withPartitionKey(true)
- .build()
- );
- assertThrows(ParamException.class, () ->
- FieldType.newBuilder()
- .withName("userID")
- .withDataType(DataType.FloatVector)
- .withPartitionKey(true)
- .build()
- );
- assertDoesNotThrow(() ->
- FieldType.newBuilder()
- .withName("partitionKey")
- .withDataType(DataType.Int64)
- .withPartitionKey(true)
- .build()
- );
- assertDoesNotThrow(() ->
- FieldType.newBuilder()
- .withName("partitionKey")
- .withDataType(DataType.VarChar)
- .withMaxLength(120)
- .withPartitionKey(true)
- .build()
- );
- Map<String, String> params = new HashMap<>();
- params.put("1", "1");
- assertThrows(ParamException.class, () ->
- FieldType.newBuilder()
- .withName("vec")
- .withDescription("desc")
- .withDataType(DataType.FloatVector)
- .withTypeParams(params)
- .addTypeParam("2", "2")
- .withDimension(-1)
- .build()
- );
- // test throw exception with illegal input for CreateCollectionParam
- assertThrows(ParamException.class, () ->
- CreateCollectionParam
- .newBuilder()
- .withCollectionName("collection1")
- .withShardsNum(2)
- .build()
- );
- FieldType fieldType1 = FieldType.newBuilder()
- .withName("userID")
- .withDescription("userId")
- .withDataType(DataType.Int64)
- .withAutoID(true)
- .withPrimaryKey(true)
- .build();
- assertThrows(ParamException.class, () ->
- CreateCollectionParam
- .newBuilder()
- .withCollectionName("")
- .withShardsNum(2)
- .addFieldType(fieldType1)
- .build()
- );
- assertThrows(ParamException.class, () ->
- CreateCollectionParam
- .newBuilder()
- .withCollectionName("collection1")
- .withShardsNum(-1)
- .addFieldType(fieldType1)
- .build()
- );
- List<FieldType> fields = Collections.singletonList(null);
- assertThrows(ParamException.class, () ->
- CreateCollectionParam
- .newBuilder()
- .withCollectionName("collection1")
- .withShardsNum(2)
- .withFieldTypes(fields)
- .build()
- );
- assertThrows(ParamException.class, () ->
- CreateCollectionParam
- .newBuilder()
- .withCollectionName("collection1")
- .withShardsNum(0)
- .withPartitionsNum(10)
- .addFieldType(fieldType1)
- .build()
- );
- FieldType fieldType2 = FieldType.newBuilder()
- .withName("partitionKey")
- .withDataType(DataType.Int64)
- .withPartitionKey(true)
- .build();
- assertDoesNotThrow(() ->
- CreateCollectionParam
- .newBuilder()
- .withCollectionName("collection1")
- .addFieldType(fieldType1)
- .addFieldType(fieldType2)
- .build()
- );
- assertDoesNotThrow(() ->
- CreateCollectionParam
- .newBuilder()
- .withCollectionName("collection1")
- .withPartitionsNum(100)
- .addFieldType(fieldType1)
- .addFieldType(fieldType2)
- .build()
- );
- }
- @Test
- void createCollection() {
- FieldType fieldType1 = FieldType.newBuilder()
- .withName("userID")
- .withDescription("userId")
- .withDataType(DataType.Int64)
- .withAutoID(true)
- .withPrimaryKey(true)
- .build();
- CreateCollectionParam param = CreateCollectionParam
- .newBuilder()
- .withCollectionName("collection1")
- .withDescription("desc")
- .withShardsNum(2)
- .addFieldType(fieldType1)
- .build();
- testFuncByName("createCollection", param);
- }
- @Test
- void describeCollectionParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () ->
- DescribeCollectionParam.newBuilder()
- .withCollectionName("")
- .build()
- );
- }
- @Test
- void describeCollection() {
- DescribeCollectionParam param = DescribeCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("describeCollection", param);
- }
- @Test
- void dropCollectionParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () ->
- DropCollectionParam.newBuilder()
- .withCollectionName("")
- .build()
- );
- }
- @Test
- void dropCollection() {
- DropCollectionParam param = DropCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("dropCollection", param);
- }
- @Test
- void getCollectionStatisticsParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () ->
- GetCollectionStatisticsParam.newBuilder()
- .withCollectionName("")
- .build()
- );
- }
- @Test
- void getCollectionStatistics() {
- // start mock server
- MockMilvusServer server = startServer();
- MilvusServiceClient client = startClient();
- try {
- final String collectionName = "collection1";
- GetCollectionStatisticsParam param = GetCollectionStatisticsParam.newBuilder()
- .withCollectionName(collectionName)
- .withFlush(Boolean.TRUE)
- .build();
- // test return ok with correct input
- final long segmentID = 2021L;
- mockServerImpl.setFlushResponse(FlushResponse.newBuilder()
- .putCollSegIDs(collectionName, LongArray.newBuilder().addData(segmentID).build())
- .build());
- mockServerImpl.setGetFlushStateResponse(GetFlushStateResponse.newBuilder()
- .setFlushed(false)
- .build());
- new Thread(() -> {
- try {
- TimeUnit.SECONDS.sleep(1);
- } catch (InterruptedException e) {
- System.out.println(e.toString());
- }
- mockServerImpl.setGetFlushStateResponse(GetFlushStateResponse.newBuilder()
- .setFlushed(true)
- .build());
- }, "RefreshFlushState").start();
- R<GetCollectionStatisticsResponse> resp = client.getCollectionStatistics(param);
- assertEquals(R.Status.Success.getCode(), resp.getStatus());
- // stop mock server
- server.stop();
- // test return error without server
- resp = client.getCollectionStatistics(param);
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error when client channel is shutdown
- client.close();
- resp = client.getCollectionStatistics(param);
- assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println(e.getMessage());
- fail();
- } finally {
- server.stop();
- client.close();
- }
- }
- @Test
- void hasCollectionParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () ->
- HasCollectionParam.newBuilder()
- .withCollectionName("")
- .build()
- );
- }
- @Test
- void hasCollection() {
- HasCollectionParam param = HasCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("hasCollection", param);
- }
- @Test
- void loadCollectionParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () ->
- LoadCollectionParam.newBuilder()
- .withCollectionName("")
- .build()
- );
- assertThrows(ParamException.class, () ->
- LoadCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingInterval(0L)
- .build()
- );
- assertThrows(ParamException.class, () ->
- LoadCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingInterval(-1L)
- .build()
- );
- assertThrows(ParamException.class, () ->
- LoadCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingInterval(Constant.MAX_WAITING_LOADING_INTERVAL + 1)
- .build()
- );
- assertThrows(ParamException.class, () ->
- LoadCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingTimeout(0L)
- .build()
- );
- assertThrows(ParamException.class, () ->
- LoadCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingTimeout(-1L)
- .build()
- );
- assertThrows(ParamException.class, () ->
- LoadCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingTimeout(Constant.MAX_WAITING_LOADING_TIMEOUT + 1)
- .build()
- );
- }
- @Test
- void loadCollection() {
- // start mock server
- MockMilvusServer server = startServer();
- MilvusServiceClient client = startClient();
- String collectionName = "collection1";
- LoadCollectionParam param = LoadCollectionParam.newBuilder()
- .withCollectionName(collectionName)
- .withSyncLoad(Boolean.FALSE)
- .build();
- // test return ok with correct input
- R<RpcStatus> resp = client.loadCollection(param);
- assertEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return ok for sync mode loading
- mockServerImpl.setShowCollectionsResponse(ShowCollectionsResponse.newBuilder()
- .addCollectionNames(collectionName)
- .addInMemoryPercentages(0)
- .build());
- new Thread(() -> {
- try {
- for (int i = 0; i <= 10; ++i) {
- TimeUnit.MILLISECONDS.sleep(100);
- mockServerImpl.setShowCollectionsResponse(ShowCollectionsResponse.newBuilder()
- .addCollectionNames(collectionName)
- .addInMemoryPercentages(i * 10)
- .build());
- }
- } catch (InterruptedException e) {
- mockServerImpl.setShowCollectionsResponse(ShowCollectionsResponse.newBuilder()
- .addCollectionNames(collectionName)
- .addInMemoryPercentages(100)
- .build());
- }
- }, "RefreshMemState").start();
- param = LoadCollectionParam.newBuilder()
- .withCollectionName(collectionName)
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingInterval(100L)
- .build();
- resp = client.loadCollection(param);
- assertEquals(R.Status.Success.getCode(), resp.getStatus());
- // stop mock server
- server.stop();
- // test return error without server
- resp = client.loadCollection(param);
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error when client channel is shutdown
- client.close();
- resp = client.loadCollection(param);
- assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
- }
- @Test
- void releaseCollectionParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () ->
- ReleaseCollectionParam.newBuilder()
- .withCollectionName("")
- .build()
- );
- }
- @Test
- void releaseCollection() {
- ReleaseCollectionParam param = ReleaseCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("releaseCollection", param);
- }
- @Test
- void showCollectionsParam() {
- // test throw exception with illegal input
- List<String> names = new ArrayList<>();
- names.add(null);
- assertThrows(NullPointerException.class, () ->
- ShowCollectionsParam.newBuilder()
- .withCollectionNames(names)
- .build()
- );
- assertThrows(ParamException.class, () ->
- ShowCollectionsParam.newBuilder()
- .addCollectionName("")
- .build()
- );
- // verify internal param
- ShowCollectionsParam param = ShowCollectionsParam.newBuilder()
- .build();
- assertEquals(ShowType.All, param.getShowType());
- param = ShowCollectionsParam.newBuilder()
- .addCollectionName("collection1")
- .build();
- assertEquals(ShowType.InMemory, param.getShowType());
- }
- @Test
- void showCollections() {
- ShowCollectionsParam param = ShowCollectionsParam.newBuilder()
- .addCollectionName("collection1")
- .addCollectionName("collection2")
- .build();
- testFuncByName("showCollections", param);
- }
- @Test
- void alterCollectionParam() {
- AlterCollectionParam param = AlterCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- assertEquals("collection1", param.getCollectionName());
- assertTrue(param.getProperties().isEmpty());
- param = AlterCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .withTTL(100)
- .build();
- assertTrue(param.getProperties().containsKey(Constant.TTL_SECONDS));
- assertEquals("100", param.getProperties().get(Constant.TTL_SECONDS));
- assertThrows(ParamException.class, () ->
- AlterCollectionParam.newBuilder()
- .withCollectionName("")
- .build()
- );
- assertThrows(ParamException.class, () ->
- AlterCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .withTTL(-5)
- .build()
- );
- }
- @Test
- void alterCollection() {
- AlterCollectionParam param = AlterCollectionParam.newBuilder()
- .withCollectionName("collection1")
- .withTTL(100)
- .build();
- testFuncByName("alterCollection", param);
- }
- @Test
- void flushParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> FlushParam.newBuilder()
- .build()
- );
- assertThrows(ParamException.class, () -> FlushParam.newBuilder()
- .addCollectionName("")
- .build()
- );
- assertThrows(ParamException.class, () -> FlushParam.newBuilder()
- .addCollectionName("collection1")
- .withSyncFlush(Boolean.TRUE)
- .withSyncFlushWaitingInterval(0L)
- .build()
- );
- assertThrows(ParamException.class, () -> FlushParam.newBuilder()
- .addCollectionName("collection1")
- .withSyncFlush(Boolean.TRUE)
- .withSyncFlushWaitingInterval(-1L)
- .build()
- );
- assertThrows(ParamException.class, () -> FlushParam.newBuilder()
- .withCollectionNames(Collections.singletonList("collection1"))
- .withSyncFlush(Boolean.TRUE)
- .withSyncFlushWaitingInterval(Constant.MAX_WAITING_FLUSHING_INTERVAL + 1)
- .build()
- );
- assertThrows(ParamException.class, () -> FlushParam.newBuilder()
- .addCollectionName("collection1")
- .withSyncFlush(Boolean.TRUE)
- .withSyncFlushWaitingTimeout(0L)
- .build()
- );
- assertThrows(ParamException.class, () -> FlushParam.newBuilder()
- .addCollectionName("collection1")
- .withSyncFlush(Boolean.TRUE)
- .withSyncFlushWaitingTimeout(-1L)
- .build()
- );
- assertThrows(ParamException.class, () -> FlushParam.newBuilder()
- .addCollectionName("collection1")
- .withSyncFlush(Boolean.TRUE)
- .withSyncFlushWaitingTimeout(Constant.MAX_WAITING_FLUSHING_TIMEOUT + 1)
- .build()
- );
- }
- @Test
- void flush() {
- FlushParam param = FlushParam.newBuilder()
- .addCollectionName("collection1")
- .withSyncFlush(Boolean.TRUE)
- .withSyncFlushWaitingTimeout(1L)
- .build();
- testFuncByName("flush", param);
- }
- @Test
- void createPartitionParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> CreatePartitionParam.newBuilder()
- .withCollectionName("")
- .withPartitionName("partition1")
- .build()
- );
- assertThrows(ParamException.class, () -> CreatePartitionParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("")
- .build()
- );
- }
- @Test
- void createPartition() {
- CreatePartitionParam param = CreatePartitionParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("partition1")
- .build();
- testFuncByName("createPartition", param);
- }
- @Test
- void dropPartitionParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> DropPartitionParam.newBuilder()
- .withCollectionName("")
- .withPartitionName("partition1")
- .build()
- );
- assertThrows(ParamException.class, () -> DropPartitionParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("")
- .build()
- );
- }
- @Test
- void dropPartition() {
- DropPartitionParam param = DropPartitionParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("partition1")
- .build();
- testFuncByName("dropPartition", param);
- }
- @Test
- void hasPartitionParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> HasPartitionParam.newBuilder()
- .withCollectionName("")
- .withPartitionName("partition1")
- .build()
- );
- assertThrows(ParamException.class, () -> HasPartitionParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("")
- .build()
- );
- }
- @Test
- void hasPartition() {
- HasPartitionParam param = HasPartitionParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("partition1")
- .build();
- testFuncByName("hasPartition", param);
- }
- @Test
- void loadPartitionsParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("")
- .addPartitionName("partition1")
- .build()
- );
- assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .addPartitionName("")
- .build()
- );
- assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .build()
- );
- List<String> names = new ArrayList<>();
- names.add(null);
- assertThrows(NullPointerException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(names)
- .build()
- );
- assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .addPartitionName("partition1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingInterval(0L)
- .build()
- );
- assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .addPartitionName("partition1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingInterval(-1L)
- .build()
- );
- assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .addPartitionName("partition1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingInterval(Constant.MAX_WAITING_LOADING_INTERVAL + 1)
- .build()
- );
- assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .addPartitionName("partition1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingTimeout(0L)
- .build()
- );
- assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .addPartitionName("partition1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingTimeout(-1L)
- .build()
- );
- assertThrows(ParamException.class, () -> LoadPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .addPartitionName("partition1")
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingTimeout(Constant.MAX_WAITING_LOADING_TIMEOUT + 1)
- .build()
- );
- }
- @Test
- void loadPartitions() {
- // start mock server
- MockMilvusServer server = startServer();
- MilvusServiceClient client = startClient();
- String collectionName = "collection1";
- String partitionName = "partition1";
- LoadPartitionsParam param = LoadPartitionsParam.newBuilder()
- .withCollectionName(collectionName)
- .addPartitionName(partitionName)
- .withSyncLoad(Boolean.FALSE)
- .build();
- // test return ok with correct input
- R<RpcStatus> resp = client.loadPartitions(param);
- assertEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return ok for sync mode loading
- mockServerImpl.setShowPartitionsResponse(ShowPartitionsResponse.newBuilder()
- .addPartitionNames(partitionName)
- .addInMemoryPercentages(0)
- .build());
- new Thread(() -> {
- try {
- for (int i = 0; i <= 10; ++i) {
- TimeUnit.MILLISECONDS.sleep(100);
- mockServerImpl.setShowPartitionsResponse(ShowPartitionsResponse.newBuilder()
- .addPartitionNames(partitionName)
- .addInMemoryPercentages(i * 10)
- .build());
- }
- } catch (InterruptedException e) {
- mockServerImpl.setShowPartitionsResponse(ShowPartitionsResponse.newBuilder()
- .addPartitionNames(partitionName)
- .addInMemoryPercentages(100)
- .build());
- }
- }, "RefreshMemState").start();
- param = LoadPartitionsParam.newBuilder()
- .withCollectionName(collectionName)
- .addPartitionName(partitionName)
- .withSyncLoad(Boolean.TRUE)
- .withSyncLoadWaitingInterval(100L)
- .build();
- resp = client.loadPartitions(param);
- assertEquals(R.Status.Success.getCode(), resp.getStatus());
- // stop mock server
- server.stop();
- // test return error without server
- resp = client.loadPartitions(param);
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error when client channel is shutdown
- client.close();
- resp = client.loadPartitions(param);
- assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
- }
- @Test
- void releasePartitionsParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> ReleasePartitionsParam.newBuilder()
- .withCollectionName("")
- .addPartitionName("partition1")
- .build()
- );
- assertThrows(ParamException.class, () -> ReleasePartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .addPartitionName("")
- .build()
- );
- assertThrows(ParamException.class, () -> ReleasePartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .build()
- );
- List<String> names = new ArrayList<>();
- names.add(null);
- assertThrows(NullPointerException.class, () -> ReleasePartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(names)
- .build()
- );
- }
- @Test
- void releasePartitions() {
- ReleasePartitionsParam param = ReleasePartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .addPartitionName("partition1")
- .build();
- testFuncByName("releasePartitions", param);
- }
- @Test
- void getPartitionStatisticsParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> GetPartitionStatisticsParam.newBuilder()
- .withCollectionName("")
- .withPartitionName("partition1")
- .withFlush(true)
- .build()
- );
- assertThrows(ParamException.class, () -> GetPartitionStatisticsParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("")
- .withFlush(false)
- .build()
- );
- }
- @Test
- void getPartitionStatistics() {
- GetPartitionStatisticsParam param = GetPartitionStatisticsParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("partition1")
- .build();
- testFuncByName("getPartitionStatistics", param);
- }
- @Test
- void showPartitionsParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> ShowPartitionsParam.newBuilder()
- .withCollectionName("")
- .addPartitionName("partition1")
- .build()
- );
- assertThrows(ParamException.class, () -> ShowPartitionsParam.newBuilder()
- .withCollectionName("collection1`")
- .addPartitionName("")
- .build()
- );
- List<String> names = new ArrayList<>();
- names.add(null);
- assertThrows(NullPointerException.class, () -> ShowPartitionsParam.newBuilder()
- .withCollectionName("collection1`")
- .withPartitionNames(names)
- .build()
- );
- // verify internal param
- ShowPartitionsParam param = ShowPartitionsParam.newBuilder()
- .withCollectionName("collection1`")
- .build();
- assertEquals(ShowType.All, param.getShowType());
- param = ShowPartitionsParam.newBuilder()
- .withCollectionName("collection1`")
- .addPartitionName("partition1")
- .build();
- assertEquals(ShowType.InMemory, param.getShowType());
- }
- @Test
- void showPartitions() {
- ShowPartitionsParam param = ShowPartitionsParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("showPartitions", param);
- }
- @Test
- void createAliasParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> CreateAliasParam.newBuilder()
- .withCollectionName("")
- .withAlias("alias1")
- .build()
- );
- assertThrows(ParamException.class, () -> CreateAliasParam.newBuilder()
- .withCollectionName("collection1")
- .withAlias("")
- .build()
- );
- }
- @Test
- void createAlias() {
- CreateAliasParam param = CreateAliasParam.newBuilder()
- .withCollectionName("collection1")
- .withAlias("alias1")
- .build();
- testFuncByName("createAlias", param);
- }
- @Test
- void dropAliasParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> DropAliasParam.newBuilder()
- .withAlias("")
- .build()
- );
- }
- @Test
- void dropAlias() {
- DropAliasParam param = DropAliasParam.newBuilder()
- .withAlias("alias1")
- .build();
- testFuncByName("dropAlias", param);
- }
- @Test
- void alterAliasParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> CreateAliasParam.newBuilder()
- .withCollectionName("")
- .withAlias("alias1")
- .build()
- );
- assertThrows(ParamException.class, () -> CreateAliasParam.newBuilder()
- .withCollectionName("collection1")
- .withAlias("")
- .build()
- );
- }
- @Test
- void alterAlias() {
- AlterAliasParam param = AlterAliasParam.newBuilder()
- .withCollectionName("collection1")
- .withAlias("alias1")
- .build();
- testFuncByName("alterAlias", param);
- }
- @Test
- void createIndexParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
- .withCollectionName("")
- .withFieldName("field1")
- .withIndexType(IndexType.IVF_FLAT)
- .withMetricType(MetricType.L2)
- .withExtraParam("dummy")
- .build()
- );
- assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withFieldName("")
- .withIndexType(IndexType.IVF_FLAT)
- .withMetricType(MetricType.L2)
- .withExtraParam("dummy")
- .build()
- );
- assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withFieldName("field1")
- .withIndexType(IndexType.INVALID)
- .withMetricType(MetricType.L2)
- .withExtraParam("dummy")
- .build()
- );
- assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withFieldName("field1")
- .withIndexType(IndexType.IVF_FLAT)
- .withMetricType(MetricType.INVALID)
- .withExtraParam("dummy")
- .build()
- );
- assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withFieldName("field1")
- .withIndexType(IndexType.IVF_FLAT)
- .withMetricType(MetricType.L2)
- .withSyncMode(Boolean.TRUE)
- .withSyncWaitingInterval(-1L)
- .build()
- );
- assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withFieldName("field1")
- .withIndexType(IndexType.IVF_FLAT)
- .withMetricType(MetricType.L2)
- .withSyncMode(Boolean.TRUE)
- .withSyncWaitingInterval(Constant.MAX_WAITING_INDEX_INTERVAL + 1L)
- .build()
- );
- assertThrows(ParamException.class, () -> CreateIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withFieldName("field1")
- .withIndexType(IndexType.IVF_FLAT)
- .withMetricType(MetricType.L2)
- .withSyncMode(Boolean.TRUE)
- .withSyncWaitingTimeout(0L)
- .build()
- );
- }
- @Test
- void createIndex() {
- // start mock server
- MockMilvusServer server = startServer();
- MilvusServiceClient client = startClient();
- // createIndex() calls describeCollection() to check input
- CollectionSchema schema = CollectionSchema.newBuilder()
- .addFields(FieldSchema.newBuilder()
- .setName("field1")
- .setDataType(DataType.FloatVector)
- .addTypeParams(KeyValuePair.newBuilder().setKey(Constant.VECTOR_DIM).setValue("256").build())
- .build())
- .build();
- mockServerImpl.setDescribeCollectionResponse(DescribeCollectionResponse.newBuilder().setSchema(schema).build());
- // test return ok for sync mode loading
- mockServerImpl.setDescribeIndexResponse(DescribeIndexResponse.newBuilder()
- .addIndexDescriptions(IndexDescription.newBuilder().setState(IndexState.InProgress).build())
- .build());
- // field doesn't exist
- CreateIndexParam param = CreateIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withFieldName("aaa")
- .withIndexType(IndexType.IVF_FLAT)
- .withMetricType(MetricType.L2)
- .withExtraParam("dummy")
- .withSyncMode(Boolean.TRUE)
- .withSyncWaitingInterval(500L)
- .withSyncWaitingTimeout(2L)
- .build();
- R<RpcStatus> resp = client.createIndex(param);
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // index type doesn't match with data type
- param = CreateIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withFieldName("field1")
- .withIndexType(IndexType.BIN_IVF_FLAT)
- .withMetricType(MetricType.L2)
- .withExtraParam("dummy")
- .withSyncMode(Boolean.TRUE)
- .withSyncWaitingInterval(500L)
- .withSyncWaitingTimeout(2L)
- .build();
- resp = client.createIndex(param);
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- new Thread(() -> {
- try {
- TimeUnit.SECONDS.sleep(1);
- mockServerImpl.setDescribeIndexResponse(DescribeIndexResponse.newBuilder()
- .addIndexDescriptions(IndexDescription.newBuilder().setState(IndexState.Finished).build())
- .build());
- } catch (InterruptedException e) {
- mockServerImpl.setDescribeIndexResponse(DescribeIndexResponse.newBuilder()
- .addIndexDescriptions(IndexDescription.newBuilder().setState(IndexState.Finished).build())
- .build());
- }
- }, "RefreshIndexState").start();
- param = CreateIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withFieldName("field1")
- .withIndexType(IndexType.IVF_FLAT)
- .withMetricType(MetricType.L2)
- .withExtraParam("dummy")
- .withSyncMode(Boolean.TRUE)
- .withSyncWaitingInterval(500L)
- .withSyncWaitingTimeout(2L)
- .build();
- // test return ok with correct input
- resp = client.createIndex(param);
- assertEquals(R.Status.Success.getCode(), resp.getStatus());
- // stop mock server
- server.stop();
- // test return error without server
- resp = client.createIndex(param);
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error when client channel is shutdown
- client.close();
- resp = client.createIndex(param);
- assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
- }
- @Test
- void describeIndexParam() {
- DescribeIndexParam param = DescribeIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withIndexName("dummy")
- .build();
- assertEquals("dummy", param.getIndexName());
- assertThrows(ParamException.class, () -> DescribeIndexParam.newBuilder()
- .withCollectionName("")
- .withIndexName("field1")
- .build()
- );
- }
- @Test
- void describeIndex() {
- DescribeIndexParam param = DescribeIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withIndexName("idx")
- .build();
- testFuncByName("describeIndex", param);
- }
- @Test
- void getIndexStateParam() {
- // test throw exception with illegal input
- GetIndexStateParam param = GetIndexStateParam.newBuilder()
- .withCollectionName("collection1")
- .withIndexName("")
- .build();
- assertEquals(Constant.DEFAULT_INDEX_NAME, param.getIndexName());
- param = GetIndexStateParam.newBuilder()
- .withCollectionName("collection1")
- .withIndexName("dummy")
- .build();
- assertEquals("dummy", param.getIndexName());
- assertThrows(ParamException.class, () -> GetIndexStateParam.newBuilder()
- .withCollectionName("")
- .withIndexName("field1")
- .build()
- );
- }
- @Test
- void getIndexState() {
- GetIndexStateParam param = GetIndexStateParam.newBuilder()
- .withCollectionName("collection1")
- .withIndexName("idx")
- .build();
- testFuncByName("getIndexState", param);
- }
- @Test
- void getIndexBuildProgressParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> GetIndexBuildProgressParam.newBuilder()
- .withCollectionName("")
- .build()
- );
- }
- @Test
- void getIndexBuildProgress() {
- GetIndexBuildProgressParam param = GetIndexBuildProgressParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("getIndexBuildProgress", param);
- }
- @Test
- void dropIndexParam() {
- // test throw exception with illegal input
- DropIndexParam param = DropIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withIndexName("")
- .build();
- assertEquals(Constant.DEFAULT_INDEX_NAME, param.getIndexName());
- param = DropIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withIndexName("dummy")
- .build();
- assertEquals("dummy", param.getIndexName());
- assertThrows(ParamException.class, () -> DropIndexParam.newBuilder()
- .withCollectionName("")
- .withIndexName("field1")
- .build()
- );
- }
- @Test
- void dropIndex() {
- // start mock server
- MockMilvusServer server = startServer();
- MilvusServiceClient client = startClient();
- DropIndexParam param = DropIndexParam.newBuilder()
- .withCollectionName("collection1")
- .withIndexName("idx")
- .build();
- // test return ok with correct input
- mockServerImpl.setDescribeIndexResponse(DescribeIndexResponse.newBuilder()
- .addIndexDescriptions(IndexDescription.newBuilder()
- .setIndexName(param.getIndexName())
- .setFieldName("fff")
- .build())
- .build());
- R<RpcStatus> resp = client.dropIndex(param);
- assertEquals(R.Status.Success.getCode(), resp.getStatus());
- // stop mock server
- server.stop();
- // test return error without server
- resp = client.dropIndex(param);
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error when client channel is shutdown
- client.close();
- resp = client.dropIndex(param);
- assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
- }
- @Test
- void insertParam() {
- // test throw exception with illegal input
- List<InsertParam.Field> fields = new ArrayList<>();
- // collection is empty
- assertThrows(ParamException.class, () -> InsertParam.newBuilder()
- .withCollectionName("")
- .withFields(fields)
- .build()
- );
- // fields is empty
- assertThrows(ParamException.class, () -> InsertParam.newBuilder()
- .withCollectionName("collection1")
- .withFields(fields)
- .build()
- );
- // field is null
- fields.add(null);
- assertThrows(ParamException.class, () -> InsertParam.newBuilder()
- .withCollectionName("collection1")
- .withFields(fields)
- .build()
- );
- // field name is empty
- fields.clear();
- List<Long> ids = new ArrayList<>();
- fields.add(new InsertParam.Field("", ids));
- assertThrows(ParamException.class, () -> InsertParam.newBuilder()
- .withCollectionName("collection1")
- .withFields(fields)
- .build()
- );
- // field row count is 0
- fields.clear();
- fields.add(new InsertParam.Field("field1", ids));
- assertThrows(ParamException.class, () -> InsertParam.newBuilder()
- .withCollectionName("collection1")
- .withFields(fields)
- .build()
- );
- // field row count not equal
- fields.clear();
- List<Long> ages = Arrays.asList(1L, 2L);
- fields.add(new InsertParam.Field("field1", ages));
- List<Integer> ports = Arrays.asList(1, 2, 3);
- fields.add(new InsertParam.Field("field2", ports));
- assertThrows(ParamException.class, () -> InsertParam.newBuilder()
- .withCollectionName("collection1")
- .withFields(fields)
- .build()
- );
- }
- @Test
- void insert() {
- // prepare schema
- HashMap<String, FieldSchema> fieldsSchema = new HashMap<>();
- fieldsSchema.put("field0", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field0")
- .withDataType(DataType.Int64)
- .withAutoID(false)
- .withPrimaryKey(true)
- .build()));
- fieldsSchema.put("field1", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field1")
- .withDataType(DataType.Int32)
- .build()));
- fieldsSchema.put("field2", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field2")
- .withDataType(DataType.Int16)
- .build()));
- fieldsSchema.put("field3", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field3")
- .withDataType(DataType.Int8)
- .build()));
- fieldsSchema.put("field4", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field4")
- .withDataType(DataType.Bool)
- .build()));
- fieldsSchema.put("field5", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field5")
- .withDataType(DataType.Float)
- .build()));
- fieldsSchema.put("field6", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field6")
- .withDataType(DataType.Double)
- .build()));
- fieldsSchema.put("field7", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field7")
- .withDataType(DataType.VarChar)
- .withMaxLength(20)
- .build()));
- fieldsSchema.put("field8", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field8")
- .withDataType(DataType.FloatVector)
- .withDimension(2)
- .build()));
- fieldsSchema.put("field9", ParamUtils.ConvertField(FieldType.newBuilder()
- .withName("field9")
- .withDataType(DataType.BinaryVector)
- .withDimension(16)
- .build()));
- // prepare raw data
- List<Long> ids = new ArrayList<>();
- List<Integer> nVal = new ArrayList<>();
- List<Boolean> bVal = new ArrayList<>();
- List<Float> fVal = new ArrayList<>();
- List<Double> dVal = new ArrayList<>();
- List<String> sVal = new ArrayList<>();
- List<ByteBuffer> bVectors = new ArrayList<>();
- List<List<Float>> fVectors = new ArrayList<>();
- int rowCount = 3;
- for (int i = 0; i < rowCount; ++i) {
- ids.add((long) i);
- nVal.add(i);
- bVal.add(Boolean.TRUE);
- fVal.add(0.5f);
- dVal.add(1.0);
- sVal.add(String.valueOf(i));
- ByteBuffer buf = ByteBuffer.allocate(2);
- buf.put((byte) 1);
- buf.put((byte) 2);
- bVectors.add(buf);
- List<Float> vec = Arrays.asList(0.1f, 0.2f);
- fVectors.add(vec);
- }
- CollectionSchema.Builder colBuilder = CollectionSchema.newBuilder();
- colBuilder.addFields(fieldsSchema.get("field0"));
- colBuilder.addFields(fieldsSchema.get("field1"));
- colBuilder.addFields(fieldsSchema.get("field2"));
- colBuilder.addFields(fieldsSchema.get("field3"));
- colBuilder.addFields(fieldsSchema.get("field4"));
- colBuilder.addFields(fieldsSchema.get("field5"));
- colBuilder.addFields(fieldsSchema.get("field6"));
- colBuilder.addFields(fieldsSchema.get("field7"));
- colBuilder.addFields(fieldsSchema.get("field8"));
- colBuilder.addFields(fieldsSchema.get("field9"));
- List<InsertParam.Field> fields = new ArrayList<>();
- fields.add(new InsertParam.Field("field0", ids));
- fields.add(new InsertParam.Field("field1", nVal));
- fields.add(new InsertParam.Field("field2", nVal));
- fields.add(new InsertParam.Field("field3", nVal));
- fields.add(new InsertParam.Field("field4", bVal));
- fields.add(new InsertParam.Field("field5", fVal));
- fields.add(new InsertParam.Field("field6", dVal));
- fields.add(new InsertParam.Field("field7", sVal));
- fields.add(new InsertParam.Field("field8", fVectors));
- fields.add(new InsertParam.Field("field9", bVectors));
- InsertParam param = InsertParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("partition1")
- .withFields(fields)
- .build();
- {
- // start mock server
- MockMilvusServer server = startServer();
- MilvusServiceClient client = startClient();
- // test return ok with correct input
- mockServerImpl.setDescribeCollectionResponse(DescribeCollectionResponse.newBuilder()
- .setCollectionID(1L)
- .setShardsNum(2)
- .setSchema(colBuilder.build())
- .build());
- R<MutationResult> resp = client.insert(param);
- assertEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong int64 type
- InsertParam.Builder paramBuilder = InsertParam.newBuilder();
- paramBuilder.withCollectionName("collection1");
- fields.set(0, new InsertParam.Field("field0", nVal));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong int32 type
- fields.set(0, new InsertParam.Field("field0", ids));
- fields.set(1, new InsertParam.Field("field1", ids));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong int16 type
- fields.set(1, new InsertParam.Field("field1", nVal));
- fields.set(2, new InsertParam.Field("field2", ids));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong int8 type
- fields.set(2, new InsertParam.Field("field2", nVal));
- fields.set(3, new InsertParam.Field("field3", ids));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong bool type
- fields.set(3, new InsertParam.Field("field3", nVal));
- fields.set(4, new InsertParam.Field("field4", nVal));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong float type
- fields.set(4, new InsertParam.Field("field4", bVal));
- fields.set(5, new InsertParam.Field("field5", bVal));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong double type
- fields.set(5, new InsertParam.Field("field5", fVal));
- fields.set(6, new InsertParam.Field("field6", nVal));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong varchar type
- fields.set(6, new InsertParam.Field("field6", dVal));
- fields.set(7, new InsertParam.Field("field7", nVal));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong float vector type
- fields.set(7, new InsertParam.Field("field7", sVal));
- fields.set(8, new InsertParam.Field("field8", nVal));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test throw exception when row count is not equal
- List<List<Long>> fakeList = new ArrayList<>();
- fakeList.add(ids);
- fields.set(8, new InsertParam.Field("field8", fakeList));
- assertThrows(ParamException.class, () -> paramBuilder.withFields(fields).build());
- // test return error with wrong dimension of float vector
- fVectors.remove(rowCount - 1);
- fVectors.add(fVal);
- fields.set(8, new InsertParam.Field("field8", fVectors));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error with wrong binary vector type
- fVectors.remove(rowCount - 1);
- fVectors.add(Arrays.asList(0.1f, 0.2f));
- fields.set(8, new InsertParam.Field("field8", fVectors));
- fields.set(9, new InsertParam.Field("field9", nVal));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test throw exception when row count is not equal
- List<ByteBuffer> fakeList2 = new ArrayList<>();
- fakeList2.add(ByteBuffer.allocate(2));
- fields.set(9, new InsertParam.Field("field9", fakeList2));
- assertThrows(ParamException.class, () -> paramBuilder.withFields(fields).build());
- // test return error with wrong dimension of binary vector
- bVectors.remove(rowCount - 1);
- bVectors.add(ByteBuffer.allocate(1));
- fields.set(9, new InsertParam.Field("field9", bVectors));
- resp = client.insert(paramBuilder.withFields(fields).build());
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error without server
- server.stop();
- resp = client.insert(param);
- assertNotEquals(R.Status.Success.getCode(), resp.getStatus());
- // test return error when client channel is shutdown
- client.close();
- resp = client.insert(param);
- assertEquals(R.Status.ClientNotConnected.getCode(), resp.getStatus());
- // stop mock server
- server.stop();
- }
- {
- // start mock server
- MockMilvusServer server = startServer();
- MilvusServiceClient client = startClient();
- // test return ok with insertAsync
- try {
- ListenableFuture<R<MutationResult>> respFuture = client.insertAsync(param);
- R<MutationResult> response = respFuture.get();
- assertEquals(R.Status.Success.getCode(), response.getStatus());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- // stop mock server
- server.stop();
- // test return error without server
- try {
- ListenableFuture<R<MutationResult>> respFuture = client.insertAsync(param);
- R<MutationResult> response = respFuture.get();
- assertNotEquals(R.Status.Success.getCode(), response.getStatus());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- // test return error when client channel is shutdown
- client.close();
- try {
- ListenableFuture<R<MutationResult>> respFuture = client.insertAsync(param);
- R<MutationResult> response = respFuture.get();
- assertNotEquals(R.Status.Success.getCode(), response.getStatus());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- // stop mock server
- server.stop();
- }
- }
- @Test
- void deleteParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> DeleteParam.newBuilder()
- .withCollectionName("")
- .withExpr("dummy")
- .build()
- );
- assertThrows(ParamException.class, () -> DeleteParam.newBuilder()
- .withCollectionName("collection1")
- .withExpr("")
- .build()
- );
- }
- @Test
- void delete() {
- DeleteParam param = DeleteParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionName("partition1")
- .withExpr("dummy")
- .build();
- testFuncByName("delete", param);
- }
- // @Test
- // void bulkload() {
- // List<String> files = Collections.singletonList("f1");
- // BulkloadParam param = BulkloadParam.newBuilder()
- // .withCollectionName("collection1")
- // .withPartitionName("partition1")
- // .withRowBased(true)
- // .addFile("dummy.json")
- // .withFiles(files)
- // .withBucket("myBucket")
- // .build();
- //
- // testFuncByName("bulkload", param);
- // }
- //
- // @Test
- // void getBulkloadState() {
- // GetBulkloadStateParam param = GetBulkloadStateParam.newBuilder()
- // .withTaskID(100L)
- // .build();
- //
- // testFuncByName("getBulkloadState", param);
- // }
- //
- // @Test
- // void listBulkloadTasks() {
- // ListBulkloadTasksParam param = ListBulkloadTasksParam.newBuilder().build();
- //
- // testFuncByName("listBulkloadTasks", param);
- // }
- @Test
- void searchParam() {
- // test throw exception with illegal input
- List<String> partitions = Collections.singletonList("partition1");
- List<String> outputFields = Collections.singletonList("field1");
- List<List<Float>> vectors = new ArrayList<>();
- // target vector is empty
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .addPartitionName("p2")
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.IP)
- .withTopK(5)
- .withVectors(vectors)
- .withExpr("dummy")
- .build()
- );
- // collection name is empty
- List<Float> vector1 = Collections.singletonList(0.1F);
- vectors.add(vector1);
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.IP)
- .withTopK(5)
- .withVectors(vectors)
- .withExpr("dummy")
- .build()
- );
- // target field name is empty
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("")
- .withMetricType(MetricType.IP)
- .withTopK(5)
- .withVectors(vectors)
- .withExpr("dummy")
- .build()
- );
- // metric type is invalid
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.INVALID)
- .withTopK(5)
- .withVectors(vectors)
- .withExpr("dummy")
- .build()
- );
- // illegal topk value
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.IP)
- .withTopK(0)
- .withVectors(vectors)
- .withExpr("dummy")
- .build()
- );
- // target vector type must be Lst<Float> or ByteBuffer
- List<String> fakeVectors1 = Collections.singletonList("fake");
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.IP)
- .withTopK(5)
- .withVectors(fakeVectors1)
- .withExpr("dummy")
- .build()
- );
- // float vector field's value must be Lst<Float>
- List<List<String>> fakeVectors2 = Collections.singletonList(fakeVectors1);
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.IP)
- .withTopK(5)
- .withVectors(fakeVectors2)
- .withExpr("dummy")
- .build()
- );
- // float vector dimension not equal
- List<Float> vector2 = Arrays.asList(0.1F, 0.2F);
- vectors.add(vector2);
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.IP)
- .withTopK(5)
- .withVectors(vectors)
- .withExpr("dummy")
- .build()
- );
- // binary vector dimension not equal
- ByteBuffer buf1 = ByteBuffer.allocate(1);
- buf1.put((byte) 1);
- ByteBuffer buf2 = ByteBuffer.allocate(2);
- buf2.put((byte) 1);
- buf2.put((byte) 2);
- List<ByteBuffer> binVectors = Arrays.asList(buf1, buf2);
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.HAMMING)
- .withTopK(5)
- .withVectors(binVectors)
- .withExpr("dummy")
- .build()
- );
- // float vector metric type is illegal
- List<List<Float>> vectors2 = Collections.singletonList(vector2);
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.JACCARD)
- .withTopK(5)
- .withVectors(vectors2)
- .withExpr("dummy")
- .build()
- );
- // binary vector metric type is illegal
- List<ByteBuffer> binVectors2 = Collections.singletonList(buf2);
- assertThrows(ParamException.class, () -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.IP)
- .withTopK(5)
- .withVectors(binVectors2)
- .withExpr("dummy")
- .build()
- );
- // succeed float vector case
- assertDoesNotThrow(() -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.L2)
- .withTopK(5)
- .withVectors(vectors2)
- .withExpr("dummy")
- .withIgnoreGrowing(Boolean.TRUE)
- .build()
- );
- // succeed binary vector case
- assertDoesNotThrow(() -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.HAMMING)
- .withTopK(5)
- .withVectors(binVectors2)
- .withExpr("dummy")
- .build()
- );
- // param is not json format
- assertDoesNotThrow(() -> SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("dummy")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.L2)
- .withTopK(5)
- .withVectors(vectors2)
- .withExpr("dummy")
- .build()
- );
- }
- @Test
- void search() {
- List<String> partitions = Collections.singletonList("partition1");
- List<String> outputFields = Collections.singletonList("field2");
- List<List<Float>> vectors = new ArrayList<>();
- List<Float> vector1 = Arrays.asList(0.1f, 0.2f);
- vectors.add(vector1);
- SearchParam param = SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .addOutField("f2")
- .withVectorFieldName("field1")
- .withMetricType(MetricType.IP)
- .withTopK(5)
- .withVectors(vectors)
- .withExpr("dummy")
- .withRoundDecimal(5)
- .withConsistencyLevel(ConsistencyLevelEnum.EVENTUALLY)
- .build();
- testFuncByName("search", param);
- List<ByteBuffer> bVectors = new ArrayList<>();
- ByteBuffer buf = ByteBuffer.allocate(2);
- buf.put((byte) 1);
- buf.put((byte) 2);
- bVectors.add(buf);
- param = SearchParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withParams("{}")
- .withOutFields(outputFields)
- .withVectorFieldName("field1")
- .withMetricType(MetricType.HAMMING)
- .withTopK(5)
- .withVectors(bVectors)
- .withExpr("dummy")
- .build();
- testFuncByName("search", param);
- testAsyncFuncByName("searchAsync", param);
- }
- @Test
- void queryParam() {
- // test throw exception with illegal input
- List<String> partitions = Collections.singletonList("partition1");
- List<String> outputFields = Collections.singletonList("field1");
- // empty collection name
- assertThrows(ParamException.class, () -> QueryParam.newBuilder()
- .withCollectionName("")
- .withPartitionNames(partitions)
- .addPartitionName("p2")
- .withOutFields(outputFields)
- .addOutField("f2")
- .withExpr("dummy")
- .build()
- );
- // negative topk
- assertThrows(ParamException.class, () -> QueryParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withOutFields(outputFields)
- .withExpr("dummy")
- .withLimit(-1L)
- .build()
- );
- // negative offset
- assertThrows(ParamException.class, () -> QueryParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withOutFields(outputFields)
- .withExpr("dummy")
- .withOffset(-1L)
- .build()
- );
- // success
- assertDoesNotThrow(() -> QueryParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withOutFields(outputFields)
- .withExpr("dummy")
- .withOffset(1L)
- .withLimit(1L)
- .withIgnoreGrowing(Boolean.TRUE)
- .build()
- );
- }
- @Test
- void query() {
- List<String> partitions = Collections.singletonList("partition1");
- List<String> outputFields = Collections.singletonList("field2");
- QueryParam param = QueryParam.newBuilder()
- .withCollectionName("collection1")
- .withPartitionNames(partitions)
- .withOutFields(outputFields)
- .addOutField("d1")
- .withExpr("dummy")
- .withConsistencyLevel(ConsistencyLevelEnum.EVENTUALLY)
- .build();
- testFuncByName("query", param);
- testAsyncFuncByName("queryAsync", param);
- }
- @Test
- void getMetricsParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> GetMetricsParam.newBuilder()
- .withRequest("")
- .build()
- );
- }
- @Test
- void getMetrics() {
- GetMetricsParam param = GetMetricsParam.newBuilder()
- .withRequest("{}")
- .build();
- testFuncByName("getMetrics", param);
- }
- @Test
- void getFlushStateParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> GetFlushStateParam.newBuilder()
- .build()
- );
- }
- @Test
- void getFlushState() {
- List<Long> ids = Arrays.asList(1L, 2L);
- GetFlushStateParam param = GetFlushStateParam.newBuilder()
- .withCollectionName("dummy")
- .withFlushTs(100L)
- .build();
- testFuncByName("getFlushState", param);
- }
- @Test
- void getPersistentSegmentInfoParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> GetPersistentSegmentInfoParam.newBuilder()
- .withCollectionName("")
- .build()
- );
- }
- @Test
- void getPersistentSegmentInfo() {
- GetPersistentSegmentInfoParam param = GetPersistentSegmentInfoParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("getPersistentSegmentInfo", param);
- }
- @Test
- void getQuerySegmentInfoParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> GetQuerySegmentInfoParam
- .newBuilder()
- .withCollectionName("")
- .build()
- );
- }
- @Test
- void getQuerySegmentInfo() {
- GetQuerySegmentInfoParam param = GetQuerySegmentInfoParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("getQuerySegmentInfo", param);
- }
- @Test
- void getReplicasParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> GetQuerySegmentInfoParam
- .newBuilder()
- .withCollectionName("")
- .build()
- );
- }
- @Test
- void getReplicas() {
- GetReplicasParam param = GetReplicasParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("getReplicas", param);
- }
- @Test
- void loadBalanceParam() {
- // test throw exception with illegal input
- assertThrows(ParamException.class, () -> LoadBalanceParam
- .newBuilder()
- .withSourceNodeID(1L)
- .withDestinationNodeID(Arrays.asList(2L, 3L))
- .addDestinationNodeID(4L)
- .build()
- );
- assertThrows(ParamException.class, () -> LoadBalanceParam
- .newBuilder()
- .withSourceNodeID(1L)
- .withSegmentIDs(Arrays.asList(2L, 3L))
- .addSegmentID(4L)
- .build()
- );
- }
- @Test
- void loadBalance() {
- LoadBalanceParam param = LoadBalanceParam.newBuilder()
- .withSourceNodeID(1L)
- .addDestinationNodeID(2L)
- .addSegmentID(3L)
- .build();
- testFuncByName("loadBalance", param);
- }
- @Test
- void getCompactionState() {
- GetCompactionStateParam param = GetCompactionStateParam.newBuilder()
- .withCompactionID(1L)
- .build();
- testFuncByName("getCompactionState", param);
- }
- @Test
- void manualCompact() {
- ManualCompactParam param = ManualCompactParam.newBuilder()
- .withCollectionName("collection1")
- .build();
- testFuncByName("manualCompact", param);
- }
- @Test
- void getCompactionStateWithPlans() {
- GetCompactionPlansParam param = GetCompactionPlansParam.newBuilder()
- .withCompactionID(1L)
- .build();
- testFuncByName("getCompactionStateWithPlans", param);
- }
- @Test
- void createCredentialParam() {
- assertThrows(ParamException.class, () -> CreateCredentialParam
- .newBuilder()
- .withUsername(" ")
- .withPassword("password")
- .build()
- );
- assertThrows(ParamException.class, () -> CreateCredentialParam
- .newBuilder()
- .withUsername("username")
- .withPassword(" ")
- .build()
- );
- }
- @Test
- void createCredential() {
- testFuncByName("createCredential", CreateCredentialParam
- .newBuilder()
- .withUsername("username")
- .withPassword("password")
- .build()
- );
- }
- @Test
- void updateCredentialParam() {
- assertThrows(ParamException.class, () -> UpdateCredentialParam
- .newBuilder()
- .withUsername(" ")
- .withOldPassword("oldPassword")
- .withNewPassword("newPassword")
- .build()
- );
- assertDoesNotThrow(() -> UpdateCredentialParam
- .newBuilder()
- .withUsername("username")
- .withOldPassword("")
- .withNewPassword("newPassword")
- .build()
- );
- assertThrows(ParamException.class, () -> UpdateCredentialParam
- .newBuilder()
- .withUsername("username")
- .withOldPassword("oldPassword")
- .withNewPassword(" ")
- .build()
- );
- }
- @Test
- void updateCredential() {
- testFuncByName("updateCredential", UpdateCredentialParam
- .newBuilder()
- .withUsername("username")
- .withOldPassword("oldPassword")
- .withNewPassword("newPassword")
- .build()
- );
- }
- @Test
- void deleteCredentialParam() {
- assertThrows(ParamException.class, () -> DeleteCredentialParam
- .newBuilder()
- .withUsername(" ")
- .build()
- );
- }
- @Test
- void deleteCredential() {
- testFuncByName("deleteCredential", DeleteCredentialParam
- .newBuilder()
- .withUsername("username")
- .build()
- );
- }
- @Test
- void listCredUsers() {
- testFuncByName("listCredUsers", ListCredUsersParam
- .newBuilder()
- .build()
- );
- }
- @Test
- void getLoadingProgress() {
- List<String> partitions = Collections.singletonList("partition1");
- testFuncByName("getLoadingProgress", GetLoadingProgressParam
- .newBuilder()
- .withCollectionName("dummy")
- .withPartitionNames(partitions)
- .build()
- );
- }
- @Test
- void getLoadState() {
- List<String> partitions = Collections.singletonList("partition1");
- testFuncByName("getLoadState", GetLoadStateParam
- .newBuilder()
- .withCollectionName("dummy")
- .withPartitionNames(partitions)
- .build()
- );
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Response wrapper test
- private void testScalarField(ScalarField field, DataType type, long rowCount) {
- FieldData fieldData = FieldData.newBuilder()
- .setFieldName("scalar")
- .setFieldId(1L)
- .setType(type)
- .setScalars(field)
- .build();
- FieldDataWrapper wrapper = new FieldDataWrapper(fieldData);
- assertEquals(rowCount, wrapper.getRowCount());
- List<?> data = wrapper.getFieldData();
- assertEquals(rowCount, data.size());
- assertThrows(IllegalResponseException.class, wrapper::getDim);
- }
- @Test
- void testDescCollResponseWrapper() {
- String collName = "test";
- String collDesc = "test col";
- long collId = 100;
- int shardNum = 10;
- long utcTs = 9999;
- List<String> aliases = Collections.singletonList("a1");
- String fieldName = "f1";
- String fieldDesc = "f1 field";
- final boolean autoId = false;
- final boolean primaryKey = true;
- DataType dt = DataType.Double;
- int dim = 256;
- KeyValuePair kv = KeyValuePair.newBuilder()
- .setKey(Constant.VECTOR_DIM).setValue(String.valueOf(dim)).build();
- FieldSchema field = FieldSchema.newBuilder()
- .setName(fieldName)
- .setDescription(fieldDesc)
- .setAutoID(autoId)
- .setIsPrimaryKey(primaryKey)
- .setDataType(dt)
- .addTypeParams(kv)
- .build();
- CollectionSchema schema = CollectionSchema.newBuilder()
- .setName(collName)
- .setDescription(collDesc)
- .addFields(field)
- .build();
- DescribeCollectionResponse response = DescribeCollectionResponse.newBuilder()
- .setCollectionID(collId)
- .addAllAliases(aliases)
- .setShardsNum(shardNum)
- .setCreatedUtcTimestamp(utcTs)
- .setSchema(schema)
- .build();
- DescCollResponseWrapper wrapper = new DescCollResponseWrapper(response);
- assertEquals(collName, wrapper.getCollectionName());
- assertEquals(collDesc, wrapper.getCollectionDescription());
- assertEquals(collId, wrapper.getCollectionID());
- assertEquals(shardNum, wrapper.getShardNumber());
- assertEquals(aliases.size(), wrapper.getAliases().size());
- assertEquals(utcTs, wrapper.getCreatedUtcTimestamp());
- assertEquals(1, wrapper.getFields().size());
- assertNull(wrapper.getFieldByName(""));
- FieldType ft = wrapper.getFieldByName(fieldName);
- assertEquals(fieldName, ft.getName());
- assertEquals(fieldDesc, ft.getDescription());
- assertEquals(dt, ft.getDataType());
- assertEquals(autoId, ft.isAutoID());
- assertEquals(primaryKey, ft.isPrimaryKey());
- assertEquals(dim, ft.getDimension());
- assertFalse(wrapper.toString().isEmpty());
- }
- @Test
- void testDescIndexResponseWrapper() {
- final long indexId = 888;
- String indexName = "idx";
- String fieldName = "f1";
- IndexType indexType = IndexType.IVF_FLAT;
- MetricType metricType = MetricType.IP;
- String extraParam = "{nlist:10}";
- KeyValuePair kvIndexType = KeyValuePair.newBuilder()
- .setKey(Constant.INDEX_TYPE).setValue(indexType.name()).build();
- KeyValuePair kvMetricType = KeyValuePair.newBuilder()
- .setKey(Constant.METRIC_TYPE).setValue(metricType.name()).build();
- KeyValuePair kvExtraParam = KeyValuePair.newBuilder()
- .setKey(Constant.PARAMS).setValue(extraParam).build();
- IndexDescription desc = IndexDescription.newBuilder()
- .setIndexID(indexId)
- .setIndexName(indexName)
- .setFieldName(fieldName)
- .addParams(kvIndexType)
- .addParams(kvMetricType)
- .addParams(kvExtraParam)
- .build();
- DescribeIndexResponse response = DescribeIndexResponse.newBuilder()
- .addIndexDescriptions(desc)
- .build();
- DescIndexResponseWrapper wrapper = new DescIndexResponseWrapper(response);
- assertEquals(1, wrapper.getIndexDescriptions().size());
- assertNull(wrapper.getIndexDescByFieldName(""));
- DescIndexResponseWrapper.IndexDesc indexDesc = wrapper.getIndexDescByFieldName(fieldName);
- assertEquals(indexId, indexDesc.getId());
- assertEquals(indexName, indexDesc.getIndexName());
- assertEquals(fieldName, indexDesc.getFieldName());
- assertEquals(indexType, indexDesc.getIndexType());
- assertEquals(metricType, indexDesc.getMetricType());
- assertEquals(0, extraParam.compareTo(indexDesc.getExtraParam()));
- assertFalse(wrapper.toString().isEmpty());
- }
- @SuppressWarnings("unchecked")
- @Test
- void testFieldDataWrapper() {
- // for float vector
- long dim = 3;
- List<Float> floatVectors = Arrays.asList(1F, 2F, 3F, 4F, 5F, 6F);
- FieldData fieldData = FieldData.newBuilder()
- .setFieldName("vec")
- .setFieldId(1L)
- .setType(DataType.FloatVector)
- .setVectors(VectorField.newBuilder()
- .setDim(dim)
- .setFloatVector(FloatArray.newBuilder()
- .addAllData(floatVectors)
- .build())
- .build())
- .build();
- FieldDataWrapper wrapper = new FieldDataWrapper(fieldData);
- assertEquals(dim, wrapper.getDim());
- assertEquals(floatVectors.size() / dim, wrapper.getRowCount());
- List<?> floatData = wrapper.getFieldData();
- assertEquals(floatVectors.size() / dim, floatData.size());
- for (Object obj : floatData) {
- List<Float> vec = (List<Float>) obj;
- assertEquals(dim, vec.size());
- }
- // for binary vector
- dim = 16;
- int bytesPerVec = (int) (dim/8);
- int count = 2;
- byte[] binary = new byte[bytesPerVec * count];
- for (int i = 0; i < binary.length; ++i) {
- binary[i] = (byte) i;
- }
- fieldData = FieldData.newBuilder()
- .setFieldName("vec")
- .setFieldId(1L)
- .setType(DataType.BinaryVector)
- .setVectors(VectorField.newBuilder()
- .setDim(dim)
- .setBinaryVector(ByteString.copyFrom(binary))
- .build())
- .build();
- wrapper = new FieldDataWrapper(fieldData);
- assertEquals(dim, wrapper.getDim());
- assertEquals(count, wrapper.getRowCount());
- List<?> binaryData = wrapper.getFieldData();
- assertEquals(count, binaryData.size());
- for(int i = 0; i < binaryData.size(); i++) {
- ByteBuffer vec = (ByteBuffer) binaryData.get(i);
- assertEquals(bytesPerVec, vec.limit());
- for(int j = 0; j < bytesPerVec; j++) {
- assertEquals(binary[i*bytesPerVec + j], vec.get(j));
- }
- }
- // for scalar field
- LongArray.Builder int64Builder = LongArray.newBuilder();
- for (long i = 0; i < dim; ++i) {
- int64Builder.addData(i);
- }
- testScalarField(ScalarField.newBuilder().setLongData(int64Builder).build(),
- DataType.Int64, dim);
- IntArray.Builder intBuilder = IntArray.newBuilder();
- for (int i = 0; i < dim; ++i) {
- intBuilder.addData(i);
- }
- testScalarField(ScalarField.newBuilder().setIntData(intBuilder).build(),
- DataType.Int32, dim);
- testScalarField(ScalarField.newBuilder().setIntData(intBuilder).build(),
- DataType.Int16, dim);
- testScalarField(ScalarField.newBuilder().setIntData(intBuilder).build(),
- DataType.Int8, dim);
- BoolArray.Builder boolBuilder = BoolArray.newBuilder();
- for (long i = 0; i < dim; ++i) {
- boolBuilder.addData(i % 2 == 0);
- }
- testScalarField(ScalarField.newBuilder().setBoolData(boolBuilder).build(),
- DataType.Bool, dim);
- FloatArray.Builder floatBuilder = FloatArray.newBuilder();
- for (long i = 0; i < dim; ++i) {
- floatBuilder.addData((float) i);
- }
- testScalarField(ScalarField.newBuilder().setFloatData(floatBuilder).build(),
- DataType.Float, dim);
- DoubleArray.Builder doubleBuilder = DoubleArray.newBuilder();
- for (long i = 0; i < dim; ++i) {
- doubleBuilder.addData((double) i);
- }
- testScalarField(ScalarField.newBuilder().setDoubleData(doubleBuilder).build(),
- DataType.Double, dim);
- StringArray.Builder strBuilder = StringArray.newBuilder();
- for (long i = 0; i < dim; ++i) {
- strBuilder.addData(String.valueOf(i));
- }
- testScalarField(ScalarField.newBuilder().setStringData(strBuilder).build(),
- DataType.VarChar, dim);
- }
- @Test
- void testGetCollStatResponseWrapper() {
- GetCollectionStatisticsResponse response = GetCollectionStatisticsResponse.newBuilder()
- .addStats(KeyValuePair.newBuilder().setKey(Constant.ROW_COUNT).setValue("invalid").build())
- .build();
- GetCollStatResponseWrapper invalidWrapper = new GetCollStatResponseWrapper(response);
- assertThrows(NumberFormatException.class, invalidWrapper::getRowCount);
- response = GetCollectionStatisticsResponse.newBuilder()
- .addStats(KeyValuePair.newBuilder().setKey(Constant.ROW_COUNT).setValue("10").build())
- .build();
- GetCollStatResponseWrapper wrapper = new GetCollStatResponseWrapper(response);
- assertEquals(10, wrapper.getRowCount());
- response = GetCollectionStatisticsResponse.newBuilder().build();
- wrapper = new GetCollStatResponseWrapper(response);
- assertEquals(0, wrapper.getRowCount());
- }
- @Test
- void testGetPartStatResponseWrapper() {
- final long rowCount = 500;
- KeyValuePair kvStat = KeyValuePair.newBuilder()
- .setKey(Constant.ROW_COUNT).setValue(String.valueOf(rowCount)).build();
- GetPartitionStatisticsResponse response = GetPartitionStatisticsResponse.newBuilder()
- .addStats(kvStat).build();
- GetPartStatResponseWrapper wrapper = new GetPartStatResponseWrapper(response);
- assertEquals(rowCount, wrapper.getRowCount());
- response = GetPartitionStatisticsResponse.newBuilder().build();
- wrapper = new GetPartStatResponseWrapper(response);
- assertEquals(0, wrapper.getRowCount());
- }
- @Test
- void testMutationResultWrapper() {
- List<Long> nID = Arrays.asList(1L, 2L, 3L);
- MutationResult results = MutationResult.newBuilder()
- .setInsertCnt(nID.size())
- .setDeleteCnt(nID.size())
- .setIDs(IDs.newBuilder()
- .setIntId(LongArray.newBuilder()
- .addAllData(nID)
- .build()))
- .setTimestamp(1000)
- .build();
- MutationResultWrapper longWrapper = new MutationResultWrapper(results);
- assertEquals(1000, longWrapper.getOperationTs());
- assertEquals(nID.size(), longWrapper.getInsertCount());
- assertEquals(nID.size(), longWrapper.getDeleteCount());
- assertThrows(ParamException.class, longWrapper::getStringIDs);
- List<Long> longIDs = longWrapper.getLongIDs();
- assertEquals(nID.size(), longIDs.size());
- for (int i = 0; i < longIDs.size(); ++i) {
- assertEquals(nID.get(i), longIDs.get(i));
- }
- List<String> sID = Arrays.asList("1", "2", "3");
- results = MutationResult.newBuilder()
- .setInsertCnt(sID.size())
- .setIDs(IDs.newBuilder()
- .setStrId(StringArray.newBuilder()
- .addAllData(sID)
- .build()))
- .build();
- MutationResultWrapper strWrapper = new MutationResultWrapper(results);
- assertEquals(sID.size(), strWrapper.getInsertCount());
- assertThrows(ParamException.class, strWrapper::getLongIDs);
- List<String> strIDs = strWrapper.getStringIDs();
- assertEquals(sID.size(), strIDs.size());
- for (int i = 0; i < strIDs.size(); ++i) {
- assertEquals(sID.get(i), strIDs.get(i));
- }
- }
- @Test
- void testQueryResultsWrapper() {
- String fieldName = "test";
- QueryResults results = QueryResults.newBuilder()
- .addFieldsData(FieldData.newBuilder()
- .setFieldName(fieldName)
- .build())
- .build();
- QueryResultsWrapper wrapper = new QueryResultsWrapper(results);
- assertThrows(ParamException.class, () -> wrapper.getFieldWrapper("invalid"));
- assertNotNull(wrapper.getFieldWrapper(fieldName));
- }
- @Test
- void testSearchResultsWrapper() {
- long topK = 5;
- long numQueries = 2;
- List<Long> longIDs = new ArrayList<>();
- List<String> strIDs = new ArrayList<>();
- List<Float> scores = new ArrayList<>();
- List<Double> outputField = new ArrayList<>();
- for (long i = 0; i < topK * numQueries; ++i) {
- longIDs.add(i);
- strIDs.add(String.valueOf(i));
- scores.add((float) i);
- outputField.add((double) i);
- }
- // for long id
- DoubleArray.Builder doubleArrayBuilder = DoubleArray.newBuilder();
- outputField.forEach(doubleArrayBuilder::addData);
- String fieldName = "test";
- SearchResultData results = SearchResultData.newBuilder()
- .setTopK(topK)
- .setNumQueries(numQueries)
- .setIds(IDs.newBuilder()
- .setIntId(LongArray.newBuilder()
- .addAllData(longIDs)
- .build()))
- .addAllScores(scores)
- .addFieldsData(FieldData.newBuilder()
- .setFieldName(fieldName)
- .setType(DataType.Double)
- .setScalars(ScalarField.newBuilder()
- .setDoubleData(doubleArrayBuilder.build())
- .build()))
- .build();
- SearchResultsWrapper intWrapper = new SearchResultsWrapper(results);
- assertThrows(ParamException.class, () -> intWrapper.getFieldData(fieldName, -1));
- assertThrows(ParamException.class, () -> intWrapper.getFieldData("invalid", 0));
- assertEquals(topK, intWrapper.getFieldData(fieldName, (int) numQueries - 1).size());
- List<SearchResultsWrapper.IDScore> idScores = intWrapper.getIDScore(1);
- assertFalse(idScores.toString().isEmpty());
- assertEquals(topK, idScores.size());
- assertThrows(ParamException.class, () -> intWrapper.getIDScore((int) numQueries));
- // for string id
- results = SearchResultData.newBuilder()
- .setTopK(topK)
- .setNumQueries(numQueries)
- .setIds(IDs.newBuilder()
- .setStrId(StringArray.newBuilder()
- .addAllData(strIDs)
- .build()))
- .addAllScores(scores)
- .addFieldsData(FieldData.newBuilder()
- .setFieldName(fieldName)
- .build())
- .build();
- SearchResultsWrapper strWrapper = new SearchResultsWrapper(results);
- idScores = strWrapper.getIDScore(0);
- assertFalse(idScores.toString().isEmpty());
- assertEquals(topK, idScores.size());
- idScores.forEach((score) -> assertFalse(score.toString().isEmpty()));
- }
- @Test
- void testShowCollResponseWrapper() {
- List<String> names = Arrays.asList("coll_1", "coll_2");
- List<Long> ids = Arrays.asList(1L, 2L);
- List<Long> ts = Arrays.asList(888L, 999L);
- List<Long> inMemory = Arrays.asList(100L, 50L);
- ShowCollectionsResponse response = ShowCollectionsResponse.newBuilder()
- .addAllCollectionNames(names)
- .addAllCollectionIds(ids)
- .addAllCreatedUtcTimestamps(ts)
- .addAllInMemoryPercentages(inMemory)
- .build();
- ShowCollResponseWrapper wrapper = new ShowCollResponseWrapper(response);
- assertEquals(names.size(), wrapper.getCollectionsInfo().size());
- assertFalse(wrapper.toString().isEmpty());
- for (int i = 0; i < 2; ++i) {
- ShowCollResponseWrapper.CollectionInfo info = wrapper.getCollectionInfoByName(names.get(i));
- assertEquals(0, names.get(i).compareTo(info.getName()));
- assertEquals(ids.get(i), info.getId());
- assertEquals(ts.get(i), info.getUtcTimestamp());
- assertEquals(inMemory.get(i), info.getInMemoryPercentage());
- assertFalse(info.toString().isEmpty());
- }
- }
- @Test
- void testShowPartResponseWrapper() {
- List<String> names = Arrays.asList("part_1", "part_2");
- List<Long> ids = Arrays.asList(1L, 2L);
- List<Long> ts = Arrays.asList(888L, 999L);
- List<Long> inMemory = Arrays.asList(100L, 50L);
- ShowPartitionsResponse response = ShowPartitionsResponse.newBuilder()
- .addAllPartitionNames(names)
- .addAllPartitionIDs(ids)
- .addAllCreatedUtcTimestamps(ts)
- .addAllInMemoryPercentages(inMemory)
- .build();
- ShowPartResponseWrapper wrapper = new ShowPartResponseWrapper(response);
- assertEquals(names.size(), wrapper.getPartitionsInfo().size());
- assertFalse(wrapper.toString().isEmpty());
- for (int i = 0; i < 2; ++i) {
- ShowPartResponseWrapper.PartitionInfo info = wrapper.getPartitionInfoByName(names.get(i));
- assertEquals(0, names.get(i).compareTo(info.getName()));
- assertEquals(ids.get(i), info.getId());
- assertEquals(ts.get(i), info.getUtcTimestamp());
- assertEquals(inMemory.get(i), info.getInMemoryPercentage());
- assertFalse(info.toString().isEmpty());
- }
- }
- @Test
- void testGetBulkInsertStateWrapper() {
- long count = 1000;
- ImportState state = ImportState.ImportStarted;
- String reason = "unexpected error";
- String files = "1.json";
- String collection = "c1";
- String partition = "p1";
- String progress = "50";
- GetImportStateResponse resp = GetImportStateResponse.newBuilder()
- .setState(state)
- .setRowCount(count)
- .addIdList(0)
- .addIdList(99)
- .addInfos(KeyValuePair.newBuilder()
- .setKey(Constant.FAILED_REASON)
- .setValue(reason)
- .build())
- .addInfos(KeyValuePair.newBuilder()
- .setKey(Constant.IMPORT_FILES)
- .setValue(files)
- .build())
- .addInfos(KeyValuePair.newBuilder()
- .setKey(Constant.IMPORT_COLLECTION)
- .setValue(collection)
- .build())
- .addInfos(KeyValuePair.newBuilder()
- .setKey(Constant.IMPORT_PARTITION)
- .setValue(partition)
- .build())
- .addInfos(KeyValuePair.newBuilder()
- .setKey(Constant.IMPORT_PROGRESS)
- .setValue(progress)
- .build())
- .build();
- GetBulkInsertStateWrapper wrapper = new GetBulkInsertStateWrapper(resp);
- assertEquals(count, wrapper.getImportedCount());
- assertEquals(100, wrapper.getAutoGeneratedIDs().size());
- assertEquals(0, wrapper.getAutoGeneratedIDs().get(0));
- assertEquals(99, wrapper.getAutoGeneratedIDs().get(99));
- assertEquals(reason, wrapper.getFailedReason());
- assertEquals(files, wrapper.getFiles());
- assertEquals(collection, wrapper.getCollectionName());
- assertEquals(partition, wrapper.getPartitionName());
- assertEquals(progress, String.valueOf(wrapper.getProgress()));
- assertFalse(wrapper.toString().isEmpty());
- }
- }
|