ClientProxy.cpp 20.5 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

Y
yukun 已提交
12
#include "grpc/ClientProxy.h"
S
starlord 已提交
13 14 15

#include <memory>
#include <string>
S
starlord 已提交
16
#include <vector>
S
starlord 已提交
17

18 19
#include "grpc-gen/gen-milvus/milvus.grpc.pb.h"

J
Jin Hai 已提交
20
#define MILVUS_SDK_VERSION "0.7.1";
K
kun yu 已提交
21 22

namespace milvus {
23 24 25

static const char* EXTRA_PARAM_KEY = "params";

K
kun yu 已提交
26
bool
S
starlord 已提交
27
UriCheck(const std::string& uri) {
S
starlord 已提交
28
    size_t index = uri.find_first_of(':', 0);
S
starlord 已提交
29
    return (index != std::string::npos);
K
kun yu 已提交
30 31
}

32 33
template<typename T>
void
G
groot 已提交
34
ConstructSearchParam(const std::string& collection_name,
35 36 37 38
                     const std::vector<std::string>& partition_tag_array,
                     int64_t topk,
                     const std::string& extra_params,
                     T& search_param) {
G
groot 已提交
39
    search_param.set_table_name(collection_name);
40 41 42 43 44 45 46 47 48 49
    search_param.set_topk(topk);
    milvus::grpc::KeyValuePair* kv = search_param.add_extra_params();
    kv->set_key(EXTRA_PARAM_KEY);
    kv->set_value(extra_params);

    for (auto& tag : partition_tag_array) {
        search_param.add_partition_tag_array(tag);
    }
}

G
groot 已提交
50
void
G
groot 已提交
51
CopyRowRecord(::milvus::grpc::RowRecord* target, const Entity& src) {
G
groot 已提交
52 53 54 55 56 57 58 59 60
    if (!src.float_data.empty()) {
        auto vector_data = target->mutable_float_data();
        vector_data->Resize(static_cast<int>(src.float_data.size()), 0.0);
        memcpy(vector_data->mutable_data(), src.float_data.data(), src.float_data.size() * sizeof(float));
    }

    if (!src.binary_data.empty()) {
        target->set_binary_data(src.binary_data.data(), src.binary_data.size());
    }
G
groot 已提交
61 62
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
void
ConstructPartitionStat(const ::milvus::grpc::PartitionStat& grpc_partition_stat, PartitionStat& partition_stat) {
    partition_stat.tag = grpc_partition_stat.tag();
    partition_stat.row_count = grpc_partition_stat.total_row_count();
    for (int i = 0; i < grpc_partition_stat.segments_stat_size(); i++) {
        auto& grpc_seg_stat = grpc_partition_stat.segments_stat(i);
        SegmentStat seg_stat;
        seg_stat.row_count = grpc_seg_stat.row_count();
        seg_stat.segment_name = grpc_seg_stat.segment_name();
        seg_stat.index_name = grpc_seg_stat.index_name();
        seg_stat.data_size = grpc_seg_stat.data_size();
        partition_stat.segments_stat.emplace_back(seg_stat);
    }
}

K
kun yu 已提交
78
Status
S
starlord 已提交
79
ClientProxy::Connect(const ConnectParam& param) {
K
kun yu 已提交
80 81 82 83 84
    std::string uri = param.ip_address + ":" + param.port;

    channel_ = ::grpc::CreateChannel(uri, ::grpc::InsecureChannelCredentials());
    if (channel_ != nullptr) {
        connected_ = true;
K
kun yu 已提交
85 86
        client_ptr_ = std::make_shared<GrpcClient>(channel_);
        return Status::OK();
K
kun yu 已提交
87
    }
S
starlord 已提交
88

G
groot 已提交
89
    std::string reason = "Connect failed!";
S
starlord 已提交
90 91
    connected_ = false;
    return Status(StatusCode::NotConnected, reason);
K
kun yu 已提交
92 93 94
}

Status
S
starlord 已提交
95
ClientProxy::Connect(const std::string& uri) {
K
kun yu 已提交
96
    if (!UriCheck(uri)) {
S
starlord 已提交
97
        return Status(StatusCode::InvalidAgument, "Invalid uri");
K
kun yu 已提交
98
    }
K
kun yu 已提交
99
    size_t index = uri.find_first_of(':', 0);
K
kun yu 已提交
100 101 102 103 104 105 106 107 108 109 110 111

    ConnectParam param;
    param.ip_address = uri.substr(0, index);
    param.port = uri.substr(index + 1);

    return Connect(param);
}

Status
ClientProxy::Connected() const {
    try {
        std::string info;
112
        return client_ptr_->Cmd("", info);
S
starlord 已提交
113
    } catch (std::exception& ex) {
G
groot 已提交
114
        return Status(StatusCode::NotConnected, "Connection lost: " + std::string(ex.what()));
K
kun yu 已提交
115 116 117 118 119
    }
}

Status
ClientProxy::Disconnect() {
K
kun yu 已提交
120
    try {
K
kun yu 已提交
121
        Status status = client_ptr_->Disconnect();
K
kun yu 已提交
122 123
        connected_ = false;
        channel_.reset();
K
kun yu 已提交
124
        return status;
S
starlord 已提交
125
    } catch (std::exception& ex) {
126
        return Status(StatusCode::UnknownError, "Failed to disconnect: " + std::string(ex.what()));
K
kun yu 已提交
127
    }
K
kun yu 已提交
128 129 130 131
}

std::string
ClientProxy::ClientVersion() const {
Y
yukun 已提交
132
    return MILVUS_SDK_VERSION;
K
kun yu 已提交
133 134
}

G
groot 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
std::string
ClientProxy::ServerVersion() const {
    Status status = Status::OK();
    try {
        std::string version;
        Status status = client_ptr_->Cmd("version", version);
        return version;
    } catch (std::exception& ex) {
        return "";
    }
}

std::string
ClientProxy::ServerStatus() const {
    if (channel_ == nullptr) {
        return "not connected to server";
    }

    try {
        std::string dummy;
        Status status = client_ptr_->Cmd("", dummy);
        return "server alive";
    } catch (std::exception& ex) {
        return "connection lost";
    }
}

K
kun yu 已提交
162
Status
G
groot 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
ClientProxy::GetConfig(const std::string& node_name, std::string& value) const {
    try {
        return client_ptr_->Cmd("get_config " + node_name, value);
    } catch (std::exception& ex) {
        return Status(StatusCode::UnknownError, "Failed to get config: " + node_name);
    }
}

Status
ClientProxy::SetConfig(const std::string& node_name, const std::string& value) const {
    try {
        std::string dummy;
        return client_ptr_->Cmd("set_config " + node_name + " " + value, dummy);
    } catch (std::exception& ex) {
        return Status(StatusCode::UnknownError, "Failed to set config: " + node_name);
    }
}

Status
ClientProxy::CreateCollection(const CollectionParam& param) {
K
kun yu 已提交
183 184
    try {
        ::milvus::grpc::TableSchema schema;
G
groot 已提交
185
        schema.set_table_name(param.collection_name);
K
kun yu 已提交
186
        schema.set_dimension(param.dimension);
187
        schema.set_index_file_size(param.index_file_size);
S
starlord 已提交
188
        schema.set_metric_type(static_cast<int32_t>(param.metric_type));
K
kun yu 已提交
189

K
kun yu 已提交
190
        return client_ptr_->CreateTable(schema);
S
starlord 已提交
191
    } catch (std::exception& ex) {
G
groot 已提交
192
        return Status(StatusCode::UnknownError, "Failed to create collection: " + std::string(ex.what()));
K
kun yu 已提交
193 194 195 196
    }
}

bool
G
groot 已提交
197
ClientProxy::HasCollection(const std::string& collection_name) {
K
kun yu 已提交
198
    Status status = Status::OK();
G
groot 已提交
199 200 201
    ::milvus::grpc::TableName grpc_collection_name;
    grpc_collection_name.set_table_name(collection_name);
    bool result = client_ptr_->HasTable(grpc_collection_name, status);
K
kun yu 已提交
202
    return result;
K
kun yu 已提交
203 204 205
}

Status
G
groot 已提交
206
ClientProxy::DropCollection(const std::string& collection_name) {
K
kun yu 已提交
207
    try {
G
groot 已提交
208 209 210
        ::milvus::grpc::TableName grpc_collection_name;
        grpc_collection_name.set_table_name(collection_name);
        return client_ptr_->DropTable(grpc_collection_name);
S
starlord 已提交
211
    } catch (std::exception& ex) {
G
groot 已提交
212
        return Status(StatusCode::UnknownError, "Failed to drop collection: " + std::string(ex.what()));
K
kun yu 已提交
213 214 215 216
    }
}

Status
S
starlord 已提交
217
ClientProxy::CreateIndex(const IndexParam& index_param) {
K
kun yu 已提交
218
    try {
Y
Yu Kun 已提交
219
        ::milvus::grpc::IndexParam grpc_index_param;
G
groot 已提交
220
        grpc_index_param.set_table_name(index_param.collection_name);
221 222 223 224
        grpc_index_param.set_index_type(static_cast<int32_t>(index_param.index_type));
        milvus::grpc::KeyValuePair* kv = grpc_index_param.add_extra_params();
        kv->set_key(EXTRA_PARAM_KEY);
        kv->set_value(index_param.extra_params);
Y
Yu Kun 已提交
225
        return client_ptr_->CreateIndex(grpc_index_param);
S
starlord 已提交
226
    } catch (std::exception& ex) {
227
        return Status(StatusCode::UnknownError, "Failed to build index: " + std::string(ex.what()));
K
kun yu 已提交
228 229 230
    }
}

K
kun yu 已提交
231
Status
G
groot 已提交
232 233
ClientProxy::Insert(const std::string& collection_name, const std::string& partition_tag,
                    const std::vector<Entity>& entity_array, std::vector<int64_t>& id_array) {
K
kun yu 已提交
234
    Status status = Status::OK();
K
kun yu 已提交
235
    try {
Y
Yu Kun 已提交
236
        ::milvus::grpc::InsertParam insert_param;
G
groot 已提交
237
        insert_param.set_table_name(collection_name);
G
groot 已提交
238
        insert_param.set_partition_tag(partition_tag);
K
kun yu 已提交
239

G
groot 已提交
240
        for (auto& entity : entity_array) {
S
starlord 已提交
241
            ::milvus::grpc::RowRecord* grpc_record = insert_param.add_row_record_array();
G
groot 已提交
242
            CopyRowRecord(grpc_record, entity);
K
kun yu 已提交
243 244
        }

S
starlord 已提交
245
        // Single thread
Y
Yu Kun 已提交
246 247
        ::milvus::grpc::VectorIds vector_ids;
        if (!id_array.empty()) {
248
            /* set user's ids */
G
groot 已提交
249 250 251
            auto row_ids = insert_param.mutable_row_id_array();
            row_ids->Resize(static_cast<int>(id_array.size()), -1);
            memcpy(row_ids->mutable_data(), id_array.data(), id_array.size() * sizeof(int64_t));
252
            status = client_ptr_->Insert(insert_param, vector_ids);
Y
Yu Kun 已提交
253
        } else {
254
            status = client_ptr_->Insert(insert_param, vector_ids);
255 256
            /* return Milvus generated ids back to user */
            id_array.insert(id_array.end(), vector_ids.vector_id_array().begin(), vector_ids.vector_id_array().end());
K
kun yu 已提交
257
        }
S
starlord 已提交
258
    } catch (std::exception& ex) {
G
groot 已提交
259
        return Status(StatusCode::UnknownError, "Failed to add entities: " + std::string(ex.what()));
K
kun yu 已提交
260 261
    }

K
kun yu 已提交
262
    return status;
K
kun yu 已提交
263 264
}

265
Status
G
groot 已提交
266
ClientProxy::GetEntityByID(const std::string& collection_name, int64_t entity_id, Entity& entity_data) {
267 268
    try {
        ::milvus::grpc::VectorIdentity vector_identity;
G
groot 已提交
269 270
        vector_identity.set_table_name(collection_name);
        vector_identity.set_id(entity_id);
271 272 273 274 275 276 277 278 279

        ::milvus::grpc::VectorData grpc_data;
        Status status = client_ptr_->GetVectorByID(vector_identity, grpc_data);
        if (!status.ok()) {
            return status;
        }

        int float_size = grpc_data.vector_data().float_data_size();
        if (float_size > 0) {
G
groot 已提交
280 281
            entity_data.float_data.resize(float_size);
            memcpy(entity_data.float_data.data(), grpc_data.vector_data().float_data().data(),
282 283 284 285 286
                   float_size * sizeof(float));
        }

        auto byte_size = grpc_data.vector_data().binary_data().length();
        if (byte_size > 0) {
G
groot 已提交
287 288
            entity_data.binary_data.resize(byte_size);
            memcpy(entity_data.binary_data.data(), grpc_data.vector_data().binary_data().data(), byte_size);
289 290 291 292
        }

        return status;
    } catch (std::exception& ex) {
G
groot 已提交
293
        return Status(StatusCode::UnknownError, "Failed to get entity by id: " + std::string(ex.what()));
294 295 296 297
    }
}

Status
G
groot 已提交
298
ClientProxy::GetIDsInSegment(const std::string& collection_name, const std::string& segment_name,
299 300 301
                             std::vector<int64_t>& id_array) {
    try {
        ::milvus::grpc::GetVectorIDsParam param;
G
groot 已提交
302
        param.set_table_name(collection_name);
303 304 305 306 307 308 309 310 311 312 313 314
        param.set_segment_name(segment_name);

        ::milvus::grpc::VectorIds vector_ids;
        Status status = client_ptr_->GetIDsInSegment(param, vector_ids);
        if (!status.ok()) {
            return status;
        }

        id_array.insert(id_array.end(), vector_ids.vector_id_array().begin(), vector_ids.vector_id_array().end());

        return status;
    } catch (std::exception& ex) {
G
groot 已提交
315
        return Status(StatusCode::UnknownError, "Failed to get ids from segment: " + std::string(ex.what()));
316 317 318
    }
}

K
kun yu 已提交
319
Status
G
groot 已提交
320 321
ClientProxy::Search(const std::string& collection_name, const std::vector<std::string>& partition_tag_array,
                    const std::vector<Entity>& entity_array, int64_t topk, const std::string& extra_params,
322
                    TopKQueryResult& topk_query_result) {
K
kun yu 已提交
323
    try {
S
starlord 已提交
324
        // step 1: convert vectors data
Y
Yu Kun 已提交
325
        ::milvus::grpc::SearchParam search_param;
G
groot 已提交
326
        ConstructSearchParam(collection_name,
327 328 329 330 331
                             partition_tag_array,
                             topk,
                             extra_params,
                             search_param);

G
groot 已提交
332
        for (auto& entity : entity_array) {
S
starlord 已提交
333
            ::milvus::grpc::RowRecord* row_record = search_param.add_query_record_array();
G
groot 已提交
334
            CopyRowRecord(row_record, entity);
K
kun yu 已提交
335 336
        }

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        // step 2: search vectors
        ::milvus::grpc::TopKQueryResult result;
        Status status = client_ptr_->Search(search_param, result);
        if (result.row_num() == 0) {
            return status;
        }

        // step 3: convert result array
        topk_query_result.reserve(result.row_num());
        int64_t nq = result.row_num();
        int64_t topk = result.ids().size() / nq;
        for (int64_t i = 0; i < result.row_num(); i++) {
            milvus::QueryResult one_result;
            one_result.ids.resize(topk);
            one_result.distances.resize(topk);
            memcpy(one_result.ids.data(), result.ids().data() + topk * i, topk * sizeof(int64_t));
            memcpy(one_result.distances.data(), result.distances().data() + topk * i, topk * sizeof(float));
            topk_query_result.emplace_back(one_result);
K
kun yu 已提交
355 356
        }

357 358
        return status;
    } catch (std::exception& ex) {
G
groot 已提交
359
        return Status(StatusCode::UnknownError, "Failed to search entities: " + std::string(ex.what()));
360 361 362
    }
}

K
kun yu 已提交
363
Status
G
groot 已提交
364
ClientProxy::DescribeCollection(const std::string& collection_name, CollectionParam& collection_param) {
K
kun yu 已提交
365 366 367
    try {
        ::milvus::grpc::TableSchema grpc_schema;

G
groot 已提交
368
        Status status = client_ptr_->DescribeTable(collection_name, grpc_schema);
K
kun yu 已提交
369

G
groot 已提交
370 371 372 373
        collection_param.collection_name = grpc_schema.table_name();
        collection_param.dimension = grpc_schema.dimension();
        collection_param.index_file_size = grpc_schema.index_file_size();
        collection_param.metric_type = static_cast<MetricType>(grpc_schema.metric_type());
K
kun yu 已提交
374 375

        return status;
S
starlord 已提交
376
    } catch (std::exception& ex) {
G
groot 已提交
377
        return Status(StatusCode::UnknownError, "Failed to describe collection: " + std::string(ex.what()));
K
kun yu 已提交
378 379 380 381
    }
}

Status
G
groot 已提交
382
ClientProxy::CountCollection(const std::string& collection_name, int64_t& row_count) {
K
kun yu 已提交
383
    try {
K
kun yu 已提交
384
        Status status;
G
groot 已提交
385 386 387
        ::milvus::grpc::TableName grpc_collection_name;
        grpc_collection_name.set_table_name(collection_name);
        row_count = client_ptr_->CountTable(grpc_collection_name, status);
K
kun yu 已提交
388
        return status;
S
starlord 已提交
389
    } catch (std::exception& ex) {
G
groot 已提交
390
        return Status(StatusCode::UnknownError, "Failed to count collection: " + std::string(ex.what()));
K
kun yu 已提交
391 392 393 394
    }
}

Status
G
groot 已提交
395
ClientProxy::ShowCollections(std::vector<std::string>& collection_array) {
K
kun yu 已提交
396
    try {
397
        Status status;
G
groot 已提交
398 399
        milvus::grpc::TableNameList collection_name_list;
        status = client_ptr_->ShowTables(collection_name_list);
400

G
groot 已提交
401 402 403
        collection_array.resize(collection_name_list.table_names_size());
        for (uint64_t i = 0; i < collection_name_list.table_names_size(); ++i) {
            collection_array[i] = collection_name_list.table_names(i);
404 405
        }
        return status;
S
starlord 已提交
406
    } catch (std::exception& ex) {
G
groot 已提交
407
        return Status(StatusCode::UnknownError, "Failed to show collections: " + std::string(ex.what()));
408 409 410 411
    }
}

Status
G
groot 已提交
412
ClientProxy::ShowCollectionInfo(const std::string& collection_name, CollectionInfo& collection_info) {
413 414
    try {
        Status status;
G
groot 已提交
415 416 417 418
        ::milvus::grpc::TableName grpc_collection_name;
        grpc_collection_name.set_table_name(collection_name);
        milvus::grpc::TableInfo grpc_collection_info;
        status = client_ptr_->ShowTableInfo(grpc_collection_name, grpc_collection_info);
419 420

        // get native info
G
groot 已提交
421
        collection_info.total_row_count = grpc_collection_info.total_row_count();
422 423

        // get partitions info
G
groot 已提交
424 425
        for (int i = 0; i < grpc_collection_info.partitions_stat_size(); i++) {
            auto& grpc_partition_stat = grpc_collection_info.partitions_stat(i);
426 427
            PartitionStat partition_stat;
            ConstructPartitionStat(grpc_partition_stat, partition_stat);
G
groot 已提交
428
            collection_info.partitions_stat.emplace_back(partition_stat);
429 430 431 432
        }

        return status;
    } catch (std::exception& ex) {
G
groot 已提交
433
        return Status(StatusCode::UnknownError, "Failed to show collection info: " + std::string(ex.what()));
K
kun yu 已提交
434 435 436
    }
}

Y
Yu Kun 已提交
437
Status
G
groot 已提交
438
ClientProxy::DeleteByID(const std::string& collection_name,  const std::vector<int64_t>& id_array) {
439
    try {
440
        ::milvus::grpc::DeleteByIDParam delete_by_id_param;
G
groot 已提交
441
        delete_by_id_param.set_table_name(collection_name);
442 443 444 445 446
        for (auto id : id_array) {
            delete_by_id_param.add_id_array(id);
        }

        return client_ptr_->DeleteByID(delete_by_id_param);
S
starlord 已提交
447
    } catch (std::exception& ex) {
G
groot 已提交
448
        return Status(StatusCode::UnknownError, "Failed to delete entity id: " + std::string(ex.what()));
449
    }
Y
Yu Kun 已提交
450 451 452
}

Status
G
groot 已提交
453
ClientProxy::PreloadCollection(const std::string& collection_name) const {
Y
Yu Kun 已提交
454
    try {
G
groot 已提交
455 456 457
        ::milvus::grpc::TableName grpc_collection_name;
        grpc_collection_name.set_table_name(collection_name);
        Status status = client_ptr_->PreloadTable(grpc_collection_name);
Y
Yu Kun 已提交
458
        return status;
S
starlord 已提交
459
    } catch (std::exception& ex) {
G
groot 已提交
460
        return Status(StatusCode::UnknownError, "Failed to preload collection: " + std::string(ex.what()));
Y
Yu Kun 已提交
461
    }
Y
Yu Kun 已提交
462 463
}

464
Status
G
groot 已提交
465
ClientProxy::DescribeIndex(const std::string& collection_name, IndexParam& index_param) const {
466
    try {
G
groot 已提交
467 468
        ::milvus::grpc::TableName grpc_collection_name;
        grpc_collection_name.set_table_name(collection_name);
469

470
        ::milvus::grpc::IndexParam grpc_index_param;
G
groot 已提交
471
        Status status = client_ptr_->DescribeIndex(grpc_collection_name, grpc_index_param);
472 473 474 475 476 477 478 479
        index_param.index_type = static_cast<IndexType>(grpc_index_param.index_type());

        for (int i = 0; i < grpc_index_param.extra_params_size(); i++) {
            const milvus::grpc::KeyValuePair& kv = grpc_index_param.extra_params(i);
            if (kv.key() == EXTRA_PARAM_KEY) {
                index_param.extra_params = kv.value();
            }
        }
480 481

        return status;
S
starlord 已提交
482
    } catch (std::exception& ex) {
483
        return Status(StatusCode::UnknownError, "Failed to describe index: " + std::string(ex.what()));
484
    }
Y
Yu Kun 已提交
485 486 487
}

Status
G
groot 已提交
488
ClientProxy::DropIndex(const std::string& collection_name) const {
489
    try {
G
groot 已提交
490 491 492
        ::milvus::grpc::TableName grpc_collection_name;
        grpc_collection_name.set_table_name(collection_name);
        Status status = client_ptr_->DropIndex(grpc_collection_name);
493
        return status;
S
starlord 已提交
494
    } catch (std::exception& ex) {
495
        return Status(StatusCode::UnknownError, "Failed to drop index: " + std::string(ex.what()));
496
    }
Y
Yu Kun 已提交
497 498
}

G
groot 已提交
499 500 501 502
Status
ClientProxy::CreatePartition(const PartitionParam& partition_param) {
    try {
        ::milvus::grpc::PartitionParam grpc_partition_param;
G
groot 已提交
503
        grpc_partition_param.set_table_name(partition_param.collection_name);
G
groot 已提交
504 505 506 507
        grpc_partition_param.set_tag(partition_param.partition_tag);
        Status status = client_ptr_->CreatePartition(grpc_partition_param);
        return status;
    } catch (std::exception& ex) {
508
        return Status(StatusCode::UnknownError, "Failed to create partition: " + std::string(ex.what()));
G
groot 已提交
509 510 511 512
    }
}

Status
G
groot 已提交
513
ClientProxy::ShowPartitions(const std::string& collection_name, PartitionTagList& partition_tag_array) const {
G
groot 已提交
514
    try {
G
groot 已提交
515 516
        ::milvus::grpc::TableName grpc_collection_name;
        grpc_collection_name.set_table_name(collection_name);
G
groot 已提交
517
        ::milvus::grpc::PartitionList grpc_partition_list;
G
groot 已提交
518
        Status status = client_ptr_->ShowPartitions(grpc_collection_name, grpc_partition_list);
519
        partition_tag_array.resize(grpc_partition_list.partition_tag_array_size());
520
        for (uint64_t i = 0; i < grpc_partition_list.partition_tag_array_size(); ++i) {
521
            partition_tag_array[i] = grpc_partition_list.partition_tag_array(i);
G
groot 已提交
522 523 524
        }
        return status;
    } catch (std::exception& ex) {
525
        return Status(StatusCode::UnknownError, "Failed to show partitions: " + std::string(ex.what()));
G
groot 已提交
526 527 528 529 530 531 532
    }
}

Status
ClientProxy::DropPartition(const PartitionParam& partition_param) {
    try {
        ::milvus::grpc::PartitionParam grpc_partition_param;
G
groot 已提交
533
        grpc_partition_param.set_table_name(partition_param.collection_name);
G
groot 已提交
534 535 536 537
        grpc_partition_param.set_tag(partition_param.partition_tag);
        Status status = client_ptr_->DropPartition(grpc_partition_param);
        return status;
    } catch (std::exception& ex) {
538
        return Status(StatusCode::UnknownError, "Failed to drop partition: " + std::string(ex.what()));
G
groot 已提交
539 540 541
    }
}

542
Status
G
groot 已提交
543
ClientProxy::FlushCollection(const std::string& collection_name) {
544 545
    try {
        std::string dummy;
G
groot 已提交
546
        return client_ptr_->Flush(collection_name);
547
    } catch (std::exception& ex) {
G
groot 已提交
548
        return Status(StatusCode::UnknownError, "Failed to flush collection");
549 550 551 552 553 554 555 556 557
    }
}

Status
ClientProxy::Flush() {
    try {
        std::string dummy;
        return client_ptr_->Flush("");
    } catch (std::exception& ex) {
G
groot 已提交
558
        return Status(StatusCode::UnknownError, "Failed to flush collections");
559 560 561 562
    }
}

Status
G
groot 已提交
563
ClientProxy::CompactCollection(const std::string& collection_name) {
564
    try {
G
groot 已提交
565 566 567
        ::milvus::grpc::TableName grpc_collection_name;
        grpc_collection_name.set_table_name(collection_name);
        Status status = client_ptr_->Compact(grpc_collection_name);
568 569
        return status;
    } catch (std::exception& ex) {
G
groot 已提交
570
        return Status(StatusCode::UnknownError, "Failed to compact collection: " + std::string(ex.what()));
571 572
    }
}
573

S
starlord 已提交
574
}  // namespace milvus