DescribeIndexRequest.cpp 3.6 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
G
groot 已提交
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
G
groot 已提交
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.
G
groot 已提交
11

12
#include "server/delivery/request/DescribeIndexRequest.h"
G
groot 已提交
13 14 15 16 17
#include "server/DBWrapper.h"
#include "utils/Log.h"
#include "utils/TimeRecorder.h"
#include "utils/ValidationUtil.h"

S
shengjh 已提交
18
#include <fiu-local.h>
G
groot 已提交
19 20 21 22 23
#include <memory>

namespace milvus {
namespace server {

24
DescribeIndexRequest::DescribeIndexRequest(const std::shared_ptr<milvus::server::Context>& context,
J
Jin Hai 已提交
25 26
                                           const std::string& collection_name, IndexParam& index_param)
    : BaseRequest(context, BaseRequest::kDescribeIndex), collection_name_(collection_name), index_param_(index_param) {
G
groot 已提交
27 28 29
}

BaseRequestPtr
J
Jin Hai 已提交
30 31 32
DescribeIndexRequest::Create(const std::shared_ptr<milvus::server::Context>& context,
                             const std::string& collection_name, IndexParam& index_param) {
    return std::shared_ptr<BaseRequest>(new DescribeIndexRequest(context, collection_name, index_param));
G
groot 已提交
33 34 35 36 37
}

Status
DescribeIndexRequest::OnExecute() {
    try {
S
shengjh 已提交
38
        fiu_do_on("DescribeIndexRequest.OnExecute.throw_std_exception", throw std::exception());
J
Jin Hai 已提交
39
        std::string hdr = "DescribeIndexRequest(collection=" + collection_name_ + ")";
G
groot 已提交
40
        TimeRecorderAuto rc(hdr);
G
groot 已提交
41 42

        // step 1: check arguments
J
Jin Hai 已提交
43
        auto status = ValidationUtil::ValidateCollectionName(collection_name_);
G
groot 已提交
44 45 46 47
        if (!status.ok()) {
            return status;
        }

J
Jin Hai 已提交
48
        // only process root collection, ignore partition collection
G
groot 已提交
49 50 51
        engine::meta::CollectionSchema collection_schema;
        collection_schema.collection_id_ = collection_name_;
        status = DBWrapper::DB()->DescribeCollection(collection_schema);
52 53
        if (!status.ok()) {
            if (status.code() == DB_NOT_FOUND) {
G
groot 已提交
54
                return Status(SERVER_COLLECTION_NOT_EXIST, CollectionNotExistMsg(collection_name_));
55 56 57 58
            } else {
                return status;
            }
        } else {
G
groot 已提交
59 60
            if (!collection_schema.owner_collection_.empty()) {
                return Status(SERVER_INVALID_COLLECTION_NAME, CollectionNotExistMsg(collection_name_));
61 62 63
            }
        }

J
Jin Hai 已提交
64
        // step 2: check collection existence
65
        engine::CollectionIndex index;
J
Jin Hai 已提交
66
        status = DBWrapper::DB()->DescribeIndex(collection_name_, index);
G
groot 已提交
67 68 69 70
        if (!status.ok()) {
            return status;
        }

G
groot 已提交
71 72 73 74 75 76 77 78
        // for binary vector, IDMAP and IVFLAT will be treated as BIN_IDMAP and BIN_IVFLAT internally
        // return IDMAP and IVFLAT for outside caller
        if (index.engine_type_ == (int32_t)engine::EngineType::FAISS_BIN_IDMAP) {
            index.engine_type_ = (int32_t)engine::EngineType::FAISS_IDMAP;
        } else if (index.engine_type_ == (int32_t)engine::EngineType::FAISS_BIN_IVFFLAT) {
            index.engine_type_ = (int32_t)engine::EngineType::FAISS_IVFFLAT;
        }

J
Jin Hai 已提交
79
        index_param_.collection_name_ = collection_name_;
80
        index_param_.index_type_ = index.engine_type_;
81
        index_param_.extra_params_ = index.extra_params_.dump();
G
groot 已提交
82 83 84 85 86 87 88 89 90
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus