CreateCollectionRequest.cpp 4.7 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/CreateCollectionRequest.h"
13
#include "db/Utils.h"
G
groot 已提交
14
#include "server/DBWrapper.h"
15
#include "server/delivery/request/BaseRequest.h"
G
groot 已提交
16 17 18 19
#include "utils/Log.h"
#include "utils/TimeRecorder.h"
#include "utils/ValidationUtil.h"

S
shengjh 已提交
20
#include <fiu-local.h>
G
groot 已提交
21
#include <memory>
G
groot 已提交
22
#include <string>
G
groot 已提交
23 24 25 26

namespace milvus {
namespace server {

27 28 29 30
CreateCollectionRequest::CreateCollectionRequest(const std::shared_ptr<milvus::server::Context>& context,
                                                 const std::string& collection_name, int64_t dimension,
                                                 int64_t index_file_size, int64_t metric_type)
    : BaseRequest(context, BaseRequest::kCreateCollection),
J
Jin Hai 已提交
31
      collection_name_(collection_name),
32 33 34
      dimension_(dimension),
      index_file_size_(index_file_size),
      metric_type_(metric_type) {
G
groot 已提交
35 36 37
}

BaseRequestPtr
38 39 40
CreateCollectionRequest::Create(const std::shared_ptr<milvus::server::Context>& context,
                                const std::string& collection_name, int64_t dimension, int64_t index_file_size,
                                int64_t metric_type) {
41
    return std::shared_ptr<BaseRequest>(
42
        new CreateCollectionRequest(context, collection_name, dimension, index_file_size, metric_type));
G
groot 已提交
43 44 45
}

Status
46
CreateCollectionRequest::OnExecute() {
J
Jin Hai 已提交
47
    std::string hdr =
48
        "CreateCollectionRequest(collection=" + collection_name_ + ", dimension=" + std::to_string(dimension_) + ")";
G
groot 已提交
49
    TimeRecorderAuto rc(hdr);
G
groot 已提交
50 51 52

    try {
        // step 1: check arguments
J
Jin Hai 已提交
53
        auto status = ValidationUtil::ValidateCollectionName(collection_name_);
G
groot 已提交
54 55 56 57
        if (!status.ok()) {
            return status;
        }

58
        status = ValidationUtil::ValidateTableDimension(dimension_, metric_type_);
G
groot 已提交
59 60 61 62
        if (!status.ok()) {
            return status;
        }

63 64
        status = ValidationUtil::ValidateCollectionIndexFileSize(index_file_size_);
        fiu_do_on("CreateCollectionRequest.OnExecute.invalid_index_file_size",
S
shengjh 已提交
65
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
G
groot 已提交
66 67 68 69
        if (!status.ok()) {
            return status;
        }

70
        status = ValidationUtil::ValidateCollectionIndexMetricType(metric_type_);
G
groot 已提交
71 72 73 74
        if (!status.ok()) {
            return status;
        }

75 76
        rc.RecordSection("check validation");

J
Jin Hai 已提交
77
        // step 2: construct collection schema
78 79 80 81 82
        engine::meta::CollectionSchema collection_info;
        collection_info.collection_id_ = collection_name_;
        collection_info.dimension_ = static_cast<uint16_t>(dimension_);
        collection_info.index_file_size_ = index_file_size_;
        collection_info.metric_type_ = metric_type_;
G
groot 已提交
83

G
groot 已提交
84
        // some metric type only support binary vector, adapt the index type
85
        if (engine::utils::IsBinaryMetricType(metric_type_)) {
86 87 88 89
            if (collection_info.engine_type_ == static_cast<int32_t>(engine::EngineType::FAISS_IDMAP)) {
                collection_info.engine_type_ = static_cast<int32_t>(engine::EngineType::FAISS_BIN_IDMAP);
            } else if (collection_info.engine_type_ == static_cast<int32_t>(engine::EngineType::FAISS_IVFFLAT)) {
                collection_info.engine_type_ = static_cast<int32_t>(engine::EngineType::FAISS_BIN_IVFFLAT);
G
groot 已提交
90 91 92
            }
        }

J
Jin Hai 已提交
93
        // step 3: create collection
94 95
        status = DBWrapper::DB()->CreateCollection(collection_info);
        fiu_do_on("CreateCollectionRequest.OnExecute.db_already_exist", status = Status(milvus::DB_ALREADY_EXIST, ""));
G
groot 已提交
96
        fiu_do_on("CreateCollectionRequest.OnExecute.create_collection_fail",
S
shengjh 已提交
97
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
98
        fiu_do_on("CreateCollectionRequest.OnExecute.throw_std_exception", throw std::exception());
G
groot 已提交
99
        if (!status.ok()) {
J
Jin Hai 已提交
100
            // collection could exist
G
groot 已提交
101
            if (status.code() == DB_ALREADY_EXIST) {
G
groot 已提交
102
                return Status(SERVER_INVALID_COLLECTION_NAME, status.message());
G
groot 已提交
103 104 105 106 107 108 109 110 111 112 113 114
            }
            return status;
        }
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus