@@ -152,8 +152,33 @@ MilvusClientV2Impl::CreateCollection(const CreateCollectionRequest& request) {
152152 return Status::OK ();
153153 };
154154
155+ auto post = [this , &request, &schema](const proto::common::Status& rpc_response) {
156+ if (request.Indexes ().empty ()) {
157+ return Status::OK ();
158+ }
159+
160+ // if user has defined indexes, create indexes immediately after collection is created.
161+ // note that Sync is false since the new collection empty, no need to wait index.
162+ const auto & descs = request.Indexes ();
163+ for (const auto & desc : descs) {
164+ auto status = createIndex (request.DatabaseName (), schema.Name (), desc, false , 0 );
165+ if (!status.IsOk ()) {
166+ return status;
167+ }
168+ }
169+
170+ // load collection automatically
171+ LoadCollectionRequest load_req =
172+ LoadCollectionRequest ()
173+ .WithDatabaseName (request.DatabaseName ())
174+ .WithCollectionName (schema.Name ())
175+ .WithSync (false ); // set sync to false since no need to wait loading progress
176+
177+ return LoadCollection (load_req);
178+ };
179+
155180 return connection_.Invoke <proto::milvus::CreateCollectionRequest, proto::common::Status>(
156- validate, pre , &MilvusConnection::CreateCollection, nullptr );
181+ validate, pre , &MilvusConnection::CreateCollection, post );
157182}
158183
159184Status
@@ -185,11 +210,9 @@ MilvusClientV2Impl::DropCollection(const DropCollectionRequest& request) {
185210 auto post = [this , &request](const proto::common::Status& status) {
186211 if (status.error_code () == proto::common::ErrorCode::Success && status.code () == 0 ) {
187212 // compile warning at this line since proto deprecates this method error_code()
188- // TODO: if the parameters provides db_name in future, we need to set the correct
189- // db_name to RemoveCollectionTs()
190213 auto db_name = connection_.CurrentDbName (request.DatabaseName ());
191214 auto collection_name = request.CollectionName ();
192- GtsDict::GetInstance ().RemoveCollectionTs (connection_. CurrentDbName ( db_name) , collection_name);
215+ GtsDict::GetInstance ().RemoveCollectionTs (db_name, collection_name);
193216 removeCollectionDesc (db_name, collection_name);
194217 }
195218 return Status::OK ();
@@ -222,8 +245,15 @@ MilvusClientV2Impl::LoadCollection(const LoadCollectionRequest& request) {
222245 pre , &MilvusConnection::LoadCollection);
223246 }
224247
225- // TODO: check timeout value in sync mode
248+ // wait loading progress, check load state in interval 500ms, until the time cost exceeds request.TimeoutMs()
249+ // ProgressMonitor timeout unit is second, it is a history problem.
250+ // request.TimeoutMs() 0ms is treated as 0 second, which means "forever".
251+ // request.TimeoutMs() in [1, 1000] is treated as 1 second, request.
252+ // request.TimeoutMs() in [1001, 2000] is treated as 2 seconds, etc.
226253 ProgressMonitor progress_monitor = ProgressMonitor::Forever ();
254+ if (request.TimeoutMs () > 0 ) {
255+ progress_monitor = ProgressMonitor{static_cast <uint32_t >(request.TimeoutMs () + 999 ) / 1000 };
256+ }
227257 auto wait_for_status = [this , &request, &progress_monitor](const proto::common::Status&) {
228258 return ConnectionHandler::WaitForStatus (
229259 [&request, this ](Progress& progress) -> Status {
@@ -274,9 +304,14 @@ MilvusClientV2Impl::DescribeCollection(const DescribeCollectionRequest& request,
274304 aliases.insert (aliases.end (), rpc_response.aliases ().begin (), rpc_response.aliases ().end ());
275305 collection_desc.SetAlias (std::move (aliases));
276306
277- response.SetDesc (std::move (collection_desc));
307+ std::unordered_map<std::string, std::string> properties;
308+ for (int i = 0 ; i < rpc_response.properties_size (); i++) {
309+ const auto & prop = rpc_response.properties (i);
310+ properties[prop.key ()] = prop.value ();
311+ }
312+ collection_desc.SetProperties (std::move (properties));
278313
279- // TODO: set properties
314+ response. SetDesc ( std::move (collection_desc));
280315 return Status::OK ();
281316 };
282317
@@ -356,10 +391,21 @@ MilvusClientV2Impl::GetLoadState(const GetLoadStateRequest& request, GetLoadStat
356391 return Status::OK ();
357392 };
358393
359- auto post = [&response](const proto::milvus::GetLoadStateResponse& rpc_response) {
360- response.SetState (LoadStateCast (rpc_response.state ()));
394+ auto post = [this , &request, &response](const proto::milvus::GetLoadStateResponse& rpc_response) {
395+ auto state = rpc_response.state ();
396+ response.SetState (LoadStateCast (state));
361397
362- // TODO: set progress percent if state is LoadStateLoading
398+ if (state == proto::common::LoadState::LoadStateLoading) {
399+ uint32_t progress = 0 ;
400+ auto status = connection_.GetLoadingProgress (request.DatabaseName (), request.CollectionName (),
401+ request.PartitionNames (), progress);
402+ if (!status.IsOk ()) {
403+ return status;
404+ }
405+ response.SetProgress (progress);
406+ } else if (state == proto::common::LoadState::LoadStateLoaded) {
407+ response.SetProgress (100 );
408+ }
363409 return Status::OK ();
364410 };
365411
@@ -503,8 +549,15 @@ MilvusClientV2Impl::LoadPartitions(const LoadPartitionsRequest& request) {
503549 pre , &MilvusConnection::LoadPartitions);
504550 }
505551
506- // TODO: check timeout value in sync mode
552+ // wait loading progress, check load state in interval 500ms, until the time cost exceeds request.TimeoutMs()
553+ // ProgressMonitor timeout unit is second, it is a history problem.
554+ // request.TimeoutMs() 0ms is treated as 0 second, which means "forever".
555+ // request.TimeoutMs() in [1, 1000] is treated as 1 second, request.
556+ // request.TimeoutMs() in [1001, 2000] is treated as 2 seconds, etc.
507557 ProgressMonitor progress_monitor = ProgressMonitor::Forever ();
558+ if (request.TimeoutMs () > 0 ) {
559+ progress_monitor = ProgressMonitor{static_cast <uint32_t >(request.TimeoutMs () + 999 ) / 1000 };
560+ }
508561 auto wait_for_status = [this , &request, &progress_monitor](const proto::common::Status&) {
509562 return ConnectionHandler::WaitForStatus (
510563 [&request, this ](Progress& progress) -> Status {
@@ -782,7 +835,7 @@ MilvusClientV2Impl::DescribeDatabase(const DescribeDatabaseRequest& request, Des
782835 const auto & prop = rpc_response.properties (i);
783836 properties[prop.key ()] = prop.value ();
784837 }
785- db_desc.SetProperties (properties);
838+ db_desc.SetProperties (std::move ( properties) );
786839
787840 response.SetDesc (std::move (db_desc));
788841 return Status::OK ();
@@ -796,13 +849,13 @@ Status
796849MilvusClientV2Impl::CreateIndex (const CreateIndexRequest& request) {
797850 const auto & descs = request.Indexes ();
798851 for (const auto & desc : descs) {
799- auto status = createIndex (request.DatabaseName (), request.CollectionName (), desc, request.Sync ());
852+ auto status =
853+ createIndex (request.DatabaseName (), request.CollectionName (), desc, request.Sync (), request.TimeoutMs ());
800854 if (!status.IsOk ()) {
801855 return status;
802856 }
803-
804- // TODO: check timeout value in sync mode
805857 }
858+
806859 return Status::OK ();
807860}
808861
@@ -822,9 +875,15 @@ MilvusClientV2Impl::DescribeIndex(const DescribeIndexRequest& request, DescribeI
822875 return Status{StatusCode::SERVER_FAILED, " Index not found:" + request.FieldName ()};
823876 }
824877
878+ // althought we have specified the field_name, the server returns all the indexes of the collection,
879+ // pick the correct index from the list.
825880 std::vector<IndexDesc> descs;
826881 for (auto i = 0 ; i < count; i++) {
827- auto rpc_desc = rpc_response.index_descriptions (0 );
882+ auto rpc_desc = rpc_response.index_descriptions (i);
883+ if (rpc_desc.field_name () != request.FieldName ()) {
884+ continue ;
885+ }
886+
828887 IndexDesc index_desc;
829888 index_desc.SetFieldName (rpc_desc.field_name ());
830889 index_desc.SetIndexName (rpc_desc.index_name ());
@@ -1428,8 +1487,15 @@ MilvusClientV2Impl::Flush(const FlushRequest& request) {
14281487 return Status::OK ();
14291488 };
14301489
1431- // TODO: check timeout value in sync mode
1490+ // wait flush progress, check flush state in interval 500ms, until the time cost exceeds request.WaitFlushedMs()
1491+ // ProgressMonitor timeout unit is second, it is a history problem.
1492+ // request.WaitFlushedMs() 0ms is treated as 0 second, which means "forever".
1493+ // request.WaitFlushedMs() in [1, 1000] is treated as 1 second, request.
1494+ // request.WaitFlushedMs() in [1001, 2000] is treated as 2 seconds, etc.
14321495 ProgressMonitor progress_monitor = ProgressMonitor::Forever ();
1496+ if (request.WaitFlushedMs () > 0 ) {
1497+ progress_monitor = ProgressMonitor{static_cast <uint32_t >(request.WaitFlushedMs () + 999 ) / 1000 };
1498+ }
14331499 auto wait_for_status = [this , &progress_monitor](const proto::milvus::FlushResponse& response) {
14341500 std::map<std::string, std::vector<int64_t >> flush_segments;
14351501 for (const auto & iter : response.coll_segids ()) {
@@ -2014,7 +2080,7 @@ MilvusClientV2Impl::RemovePrivilegesFromGroup(const RemovePrivilegesFromGroupReq
20142080// internal used methods
20152081Status
20162082MilvusClientV2Impl::createIndex (const std::string& db_name, const std::string& collection_name, const IndexDesc& desc,
2017- bool sync) {
2083+ bool sync, int64_t timeout_ms ) {
20182084 auto pre = [&db_name, &collection_name, &desc](proto::milvus::CreateIndexRequest& rpc_request) {
20192085 rpc_request.set_db_name (db_name);
20202086 rpc_request.set_collection_name (collection_name);
@@ -2046,7 +2112,16 @@ MilvusClientV2Impl::createIndex(const std::string& db_name, const std::string& c
20462112 pre , &MilvusConnection::CreateIndex);
20472113 }
20482114
2115+ // wait index progress, check index state in interval 500ms, until the time cost exceeds timeout_ms
2116+ // ProgressMonitor timeout unit is second, it is a history problem.
2117+ // timeout_ms 0ms is treated as 0 second, which means "forever".
2118+ // timeout_ms in [1, 1000] is treated as 1 second, request.
2119+ // timeout_ms in [1001, 2000] is treated as 2 seconds, etc.
2120+ // Note: wait timeout_ms for each index, means N indexes will wait N * timeout_ms.
20492121 ProgressMonitor progress_monitor = ProgressMonitor::Forever ();
2122+ if (timeout_ms > 0 ) {
2123+ progress_monitor = ProgressMonitor{static_cast <uint32_t >(timeout_ms + 999 ) / 1000 };
2124+ }
20502125 auto wait_for_status = [&db_name, &collection_name, &desc, &progress_monitor, this ](const proto::common::Status&) {
20512126 return ConnectionHandler::WaitForStatus (
20522127 [&db_name, &collection_name, &desc, this ](Progress& progress) -> Status {
@@ -2112,6 +2187,16 @@ combineDbCollectionName(const std::string& db_name, const std::string& collectio
21122187Status
21132188MilvusClientV2Impl::getCollectionDesc (const std::string& db_name, const std::string& collection_name, bool force_update,
21142189 CollectionDescPtr& desc_ptr) {
2190+ // if connection is connected to "", equals "default" db, the input db_name is "", actual_db is "default"
2191+ // if connection is connected to "default", the input db_name is "" or "default", actual_db is "default"
2192+ // if connection is connected to "A" but the input db_name is "B", actual_db is "B"
2193+ // if connection is connected to "A" but the input db_name is "", actual_db is "A"
2194+ // if connection is connected to "A" but the input db_name is "A", actual_db is "A"
2195+ auto actual_db = connection_.CurrentDbName (db_name);
2196+ if (actual_db.empty ()) {
2197+ actual_db = " default" ;
2198+ }
2199+
21152200 // this lock locks the entire section, including the call of DescribeCollection()
21162201 // the reason is: describeCollection() could be limited by server-side(DDL request throttling is enabled)
21172202 // we don't intend to allow too many threads run into describeCollection() in this method
@@ -2125,12 +2210,12 @@ MilvusClientV2Impl::getCollectionDesc(const std::string& db_name, const std::str
21252210 }
21262211
21272212 DescribeCollectionRequest rquest =
2128- DescribeCollectionRequest ().WithDatabaseName (db_name ).WithCollectionName (collection_name);
2213+ DescribeCollectionRequest ().WithDatabaseName (actual_db ).WithCollectionName (collection_name);
21292214 DescribeCollectionResponse response;
21302215 auto status = DescribeCollection (rquest, response);
21312216 if (status.IsOk ()) {
21322217 desc_ptr = std::make_shared<CollectionDesc>(response.Desc ());
2133- auto name = combineDbCollectionName (db_name , collection_name);
2218+ auto name = combineDbCollectionName (actual_db , collection_name);
21342219 collection_desc_cache_[name] = desc_ptr;
21352220 return status;
21362221 }
@@ -2145,7 +2230,17 @@ MilvusClientV2Impl::cleanCollectionDescCache() {
21452230
21462231void
21472232MilvusClientV2Impl::removeCollectionDesc (const std::string& db_name, const std::string& collection_name) {
2148- auto name = combineDbCollectionName (db_name, collection_name);
2233+ // if connection is connected to "", equals "default" db, the input db_name is "", actual_db is "default"
2234+ // if connection is connected to "default", the input db_name is "" or "default", actual_db is "default"
2235+ // if connection is connected to "A" but the input db_name is "B", actual_db is "B"
2236+ // if connection is connected to "A" but the input db_name is "", actual_db is "A"
2237+ // if connection is connected to "A" but the input db_name is "A", actual_db is "A"
2238+ auto actual_db = connection_.CurrentDbName (db_name);
2239+ if (actual_db.empty ()) {
2240+ actual_db = " default" ;
2241+ }
2242+
2243+ auto name = combineDbCollectionName (actual_db, collection_name);
21492244 std::lock_guard<std::mutex> lock (collection_desc_cache_mtx_);
21502245 collection_desc_cache_.erase (name);
21512246}
0 commit comments