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 {

C
Cai Yudong 已提交
31 32 33 34 35 36 37
CreateCollectionReq::CreateCollectionReq(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)
    : BaseReq(context, BaseReq::kCreateCollection),
38 39
      collection_name_(collection_name),
      field_types_(field_types),
40 41 42
      field_index_params_(field_index_params),
      field_params_(field_params),
      extra_params_(extra_params) {
43 44
}

C
Cai Yudong 已提交
45 46 47 48 49 50 51
BaseReqPtr
CreateCollectionReq::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<BaseReq>(
        new CreateCollectionReq(context, collection_name, field_types, field_index_params, field_params, extra_params));
52 53 54
}

Status
C
Cai Yudong 已提交
55 56
CreateCollectionReq::OnExecute() {
    std::string hdr = "CreateCollectionReq(collection=" + collection_name_ + ")";
57 58 59 60
    TimeRecorderAuto rc(hdr);

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

        rc.RecordSection("check validation");

        // step 2: construct collection schema and vector schema
C
Cai Yudong 已提交
71
        engine::meta::CollectionSchema collection_schema;
72 73
        engine::meta::hybrid::FieldsSchema fields_schema;

74 75 76 77 78
        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;
C
Cai Yudong 已提交
79
            STATUS_CHECK(ValidateFieldName(field_name));
80 81 82 83 84 85 86

            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"];
87
            }
88 89 90 91 92 93 94 95
            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);
96
                    if (vector_param.contains(engine::PARAM_COLLECTION_DIMENSION)) {
C
Cai Yudong 已提交
97
                        dimension = vector_param[engine::PARAM_COLLECTION_DIMENSION].get<int64_t>();
98 99 100 101 102
                    } else {
                        return Status{milvus::SERVER_INVALID_VECTOR_DIMENSION,
                                      "Dimension should be defined in vector field extra_params"};
                    }
                }
103
            }
104 105 106
            fields_schema.fields_schema_.emplace_back(schema);
        }

C
Cai Yudong 已提交
107 108
        collection_schema.collection_id_ = collection_name_;
        collection_schema.dimension_ = dimension;
109 110
        if (extra_params_.contains(engine::PARAM_SEGMENT_SIZE)) {
            auto segment_size = extra_params_[engine::PARAM_SEGMENT_SIZE].get<int64_t>();
C
Cai Yudong 已提交
111
            collection_schema.index_file_size_ = segment_size;
C
Cai Yudong 已提交
112
            STATUS_CHECK(ValidateCollectionIndexFileSize(segment_size));
113 114
        }

115
        if (vector_param.contains(engine::PARAM_INDEX_METRIC_TYPE)) {
C
Cai Yudong 已提交
116
            auto metric_type = engine::s_map_metric_type.at(vector_param[engine::PARAM_INDEX_METRIC_TYPE]);
C
Cai Yudong 已提交
117
            collection_schema.metric_type_ = (int32_t)metric_type;
118
        }
119

120 121
        // step 3: create snapshot collection
        engine::snapshot::CreateCollectionContext create_collection_context;
122
        auto ss_collection_schema = std::make_shared<engine::snapshot::Collection>(collection_name_, extra_params_);
123 124 125 126 127 128 129 130 131 132
        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 已提交
133
        status = DBWrapper::DB()->CreateCollection(create_collection_context);
C
Cai Yudong 已提交
134
        fiu_do_on("CreateCollectionReq.OnExecute.invalid_db_execute",
Y
yukun 已提交
135
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
136 137 138 139 140 141 142
        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 已提交
143 144

        rc.ElapseFromBegin("done");
145 146 147 148 149 150 151 152 153
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus