CreateCollectionReq.cpp 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// 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
//
// 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.

C
Cai Yudong 已提交
12
#include "server/delivery/request/CreateCollectionReq.h"
13 14
#include "db/Utils.h"
#include "server/DBWrapper.h"
C
Cai Yudong 已提交
15
#include "server/ValidationUtil.h"
C
Cai Yudong 已提交
16
#include "server/delivery/request/BaseReq.h"
17
#include "server/web_impl/Constants.h"
18 19 20 21
#include "utils/Log.h"
#include "utils/TimeRecorder.h"

#include <fiu-local.h>
22
#include <src/db/snapshot/Context.h>
23 24
#include <memory>
#include <string>
25
#include <unordered_map>
26 27 28 29 30
#include <vector>

namespace milvus {
namespace server {

G
groot 已提交
31
CreateCollectionRequest::CreateCollectionRequest(
32
    const std::shared_ptr<milvus::server::Context>& context, const std::string& collection_name,
33 34 35
    std::unordered_map<std::string, engine::meta::hybrid::DataType>& field_types,
    std::unordered_map<std::string, milvus::json>& field_index_params,
    std::unordered_map<std::string, std::string>& field_params, milvus::json& extra_params)
C
Cai Yudong 已提交
36
    : BaseRequest(context, BaseRequest::kCreateCollection),
37 38
      collection_name_(collection_name),
      field_types_(field_types),
39 40 41
      field_index_params_(field_index_params),
      field_params_(field_params),
      extra_params_(extra_params) {
42 43 44
}

BaseRequestPtr
G
groot 已提交
45 46 47 48 49 50 51 52
CreateCollectionRequest::Create(const std::shared_ptr<milvus::server::Context>& context,
                                const std::string& collection_name,
                                std::unordered_map<std::string, engine::meta::hybrid::DataType>& field_types,
                                std::unordered_map<std::string, milvus::json>& field_index_params,
                                std::unordered_map<std::string, std::string>& field_params,
                                milvus::json& extra_params) {
    return std::shared_ptr<BaseRequest>(new CreateCollectionRequest(context, collection_name, field_types,
                                                                    field_index_params, field_params, extra_params));
53 54 55
}

Status
G
groot 已提交
56
CreateCollectionRequest::OnExecute() {
57 58 59 60 61
    std::string hdr = "CreateCollectionRequest(collection=" + collection_name_ + ")";
    TimeRecorderAuto rc(hdr);

    try {
        // step 1: check arguments
C
Cai Yudong 已提交
62
        auto status = ValidateCollectionName(collection_name_);
G
groot 已提交
63
        fiu_do_on("CreateCollectionRequest.OnExecute.invalid_collection_name",
Y
yukun 已提交
64
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
65 66 67 68 69 70 71
        if (!status.ok()) {
            return status;
        }

        rc.RecordSection("check validation");

        // step 2: construct collection schema and vector schema
72
        engine::meta::CollectionSchema collection_info;
73 74
        engine::meta::hybrid::FieldsSchema fields_schema;

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        uint16_t dimension = 0;
        milvus::json vector_param;
        for (auto& field_type : field_types_) {
            engine::meta::hybrid::FieldSchema schema;
            auto field_name = field_type.first;
            status = ValidateFieldName(field_name);
            if (!status.ok()) {
                return status;
            }

            auto index_params = field_index_params_.at(field_name);
            schema.collection_id_ = collection_name_;
            schema.field_name_ = field_name;
            schema.field_type_ = (int32_t)field_type.second;
            if (index_params.contains("name")) {
                schema.index_name_ = index_params["name"];
91
            }
92 93 94 95 96 97 98 99
            schema.index_param_ = index_params.dump();

            if (!field_params_.at(field_name).empty()) {
                auto field_param = field_params_.at(field_name);
                schema.field_params_ = field_param;
                if (field_type.second == engine::meta::hybrid::DataType::VECTOR_FLOAT ||
                    field_type.second == engine::meta::hybrid::DataType::VECTOR_BINARY) {
                    vector_param = milvus::json::parse(field_param);
100 101
                    if (vector_param.contains(engine::PARAM_COLLECTION_DIMENSION)) {
                        dimension = vector_param[engine::PARAM_COLLECTION_DIMENSION].get<uint16_t>();
102 103 104 105 106
                    } else {
                        return Status{milvus::SERVER_INVALID_VECTOR_DIMENSION,
                                      "Dimension should be defined in vector field extra_params"};
                    }
                }
107
            }
108 109 110 111 112
            fields_schema.fields_schema_.emplace_back(schema);
        }

        collection_info.collection_id_ = collection_name_;
        collection_info.dimension_ = dimension;
113 114
        if (extra_params_.contains(engine::PARAM_SEGMENT_SIZE)) {
            auto segment_size = extra_params_[engine::PARAM_SEGMENT_SIZE].get<int64_t>();
115
            collection_info.index_file_size_ = segment_size;
C
Cai Yudong 已提交
116
            STATUS_CHECK(ValidateCollectionIndexFileSize(segment_size));
117 118
        }

119
        if (vector_param.contains(engine::PARAM_INDEX_METRIC_TYPE)) {
C
Cai Yudong 已提交
120 121
            auto metric_type = engine::s_map_metric_type.at(vector_param[engine::PARAM_INDEX_METRIC_TYPE]);
            collection_info.metric_type_ = (int32_t)metric_type;
122
        }
123

124 125
        // step 3: create snapshot collection
        engine::snapshot::CreateCollectionContext create_collection_context;
126
        auto ss_collection_schema = std::make_shared<engine::snapshot::Collection>(collection_name_, extra_params_);
127 128 129 130 131 132 133 134 135 136
        create_collection_context.collection = ss_collection_schema;
        for (const auto& schema : fields_schema.fields_schema_) {
            auto field = std::make_shared<engine::snapshot::Field>(
                schema.field_name_, 0, (engine::FieldType)schema.field_type_, json::parse(schema.field_params_));

            auto field_element = std::make_shared<engine::snapshot::FieldElement>(
                0, 0, schema.index_name_, engine::FieldElementType::FET_INDEX, json::parse(schema.index_param_));
            create_collection_context.fields_schema[field] = {field_element};
        }

G
groot 已提交
137 138
        status = DBWrapper::DB()->CreateCollection(create_collection_context);
        fiu_do_on("CreateCollectionRequest.OnExecute.invalid_db_execute",
Y
yukun 已提交
139
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
140 141 142 143 144 145 146
        if (!status.ok()) {
            // collection could exist
            if (status.code() == DB_ALREADY_EXIST) {
                return Status(SERVER_INVALID_COLLECTION_NAME, status.message());
            }
            return status;
        }
C
Cai Yudong 已提交
147 148

        rc.ElapseFromBegin("done");
149 150 151 152 153 154 155 156 157
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus