enhance: Add try-catch and return CStatus for NewCollection (#39279)

Related to #28795

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
pull/39154/head
congqixia 2025-01-15 19:17:01 +08:00 committed by GitHub
parent d2834a1812
commit eb63334312
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 15 deletions

View File

@ -9,6 +9,7 @@
// 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
#include "common/type_c.h"
#ifdef __linux__
#include <malloc.h>
#endif
@ -17,29 +18,41 @@
#include "segcore/collection_c.h"
#include "segcore/Collection.h"
CCollection
NewCollection(const void* schema_proto_blob, const int64_t length) {
auto collection = std::make_unique<milvus::segcore::Collection>(
schema_proto_blob, length);
return (void*)collection.release();
CStatus
NewCollection(const void* schema_proto_blob,
const int64_t length,
CCollection* newCollection) {
try {
auto collection = std::make_unique<milvus::segcore::Collection>(
schema_proto_blob, length);
*newCollection = collection.release();
return milvus::SuccessCStatus();
} catch (std::exception& e) {
return milvus::FailureCStatus(&e);
}
}
void
CStatus
SetIndexMeta(CCollection collection,
const void* proto_blob,
const int64_t length) {
auto col = (milvus::segcore::Collection*)collection;
col->parseIndexMeta(proto_blob, length);
try {
auto col = static_cast<milvus::segcore::Collection*>(collection);
col->parseIndexMeta(proto_blob, length);
return milvus::SuccessCStatus();
} catch (std::exception& e) {
return milvus::FailureCStatus(&e);
}
}
void
DeleteCollection(CCollection collection) {
auto col = (milvus::segcore::Collection*)collection;
auto col = static_cast<milvus::segcore::Collection*>(collection);
delete col;
}
const char*
GetCollectionName(CCollection collection) {
auto col = (milvus::segcore::Collection*)collection;
auto col = static_cast<milvus::segcore::Collection*>(collection);
return strdup(col->get_collection_name().data());
}

View File

@ -12,6 +12,7 @@
#pragma once
#include <stdint.h>
#include "common/type_c.h"
#ifdef __cplusplus
extern "C" {
@ -19,10 +20,12 @@ extern "C" {
typedef void* CCollection;
CCollection
NewCollection(const void* schema_proto_blob, const int64_t length);
CStatus
NewCollection(const void* schema_proto_blob,
const int64_t length,
CCollection* collection);
void
CStatus
SetIndexMeta(CCollection collection,
const void* proto_blob,
const int64_t length);

View File

@ -38,9 +38,17 @@ func CreateCCollection(req *CreateCCollectionRequest) (*CCollection, error) {
return nil, errors.New("marshal index meta failed")
}
}
ptr := C.NewCollection(unsafe.Pointer(&schemaBlob[0]), (C.int64_t)(len(schemaBlob)))
var ptr C.CCollection
status := C.NewCollection(unsafe.Pointer(&schemaBlob[0]), (C.int64_t)(len(schemaBlob)), &ptr)
if err := ConsumeCStatusIntoError(&status); err != nil {
return nil, err
}
if indexMetaBlob != nil {
C.SetIndexMeta(ptr, unsafe.Pointer(&indexMetaBlob[0]), (C.int64_t)(len(indexMetaBlob)))
status = C.SetIndexMeta(ptr, unsafe.Pointer(&indexMetaBlob[0]), (C.int64_t)(len(indexMetaBlob)))
if err := ConsumeCStatusIntoError(&status); err != nil {
C.DeleteCollection(ptr)
return nil, err
}
}
return &CCollection{
collectionID: req.CollectionID,