CreateCollectionReq.cpp 4.7 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"
16 17 18 19 20 21 22 23
#include "utils/Log.h"
#include "utils/TimeRecorder.h"

#include <fiu-local.h>

namespace milvus {
namespace server {

C
Cai Yudong 已提交
24 25 26
CreateCollectionReq::CreateCollectionReq(const ContextPtr& context, const std::string& collection_name,
                                         FieldsType& fields, milvus::json& extra_params)
    : BaseReq(context, ReqType::kCreateCollection),
27
      collection_name_(collection_name),
C
Cai Yudong 已提交
28
      fields_(fields),
29
      extra_params_(extra_params) {
30 31
}

C
Cai Yudong 已提交
32
BaseReqPtr
C
Cai Yudong 已提交
33 34
CreateCollectionReq::Create(const ContextPtr& context, const std::string& collection_name, FieldsType& fields,
                            milvus::json& extra_params) {
C
Cai Yudong 已提交
35
    return std::shared_ptr<BaseReq>(new CreateCollectionReq(context, collection_name, fields, extra_params));
36 37 38
}

Status
C
Cai Yudong 已提交
39
CreateCollectionReq::OnExecute() {
40
    try {
C
Cai Yudong 已提交
41 42 43
        std::string hdr = "CreateCollectionReq(collection=" + collection_name_ + ")";
        TimeRecorderAuto rc(hdr);

44
        // step 1: check arguments
C
Cai Yudong 已提交
45
        auto status = ValidateCollectionName(collection_name_);
C
Cai Yudong 已提交
46
        fiu_do_on("CreateCollectionReq.OnExecute.invalid_collection_name",
Y
yukun 已提交
47
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
48 49 50 51
        if (!status.ok()) {
            return status;
        }

G
groot 已提交
52 53 54 55 56 57 58
        if (!extra_params_.contains(engine::PARAM_SEGMENT_ROW_COUNT)) {
            extra_params_[engine::PARAM_SEGMENT_ROW_COUNT] = engine::DEFAULT_SEGMENT_ROW_COUNT;
        } else {
            auto segment_row = extra_params_[engine::PARAM_SEGMENT_ROW_COUNT].get<int64_t>();
            STATUS_CHECK(ValidateSegmentRowCount(segment_row));
        }

59 60
        rc.RecordSection("check validation");

G
groot 已提交
61 62
        // step 2: create snapshot collection context
        engine::snapshot::CreateCollectionContext create_collection_context;
C
Cai Yudong 已提交
63 64 65 66 67 68 69 70 71 72
        auto collection_schema = std::make_shared<engine::snapshot::Collection>(collection_name_, extra_params_);
        create_collection_context.collection = collection_schema;
        for (auto& field_kv : fields_) {
            auto& field_name = field_kv.first;
            auto& field_schema = field_kv.second;

            auto& field_type = field_schema.field_type_;
            auto& field_params = field_schema.field_params_;
            auto& index_params = field_schema.index_params_;

73 74
            STATUS_CHECK(ValidateFieldName(field_name));

G
groot 已提交
75
            std::string index_name;
76
            if (index_params.contains("name")) {
G
groot 已提交
77
                index_name = index_params["name"];
78
            }
79

G
groot 已提交
80
            if (field_type == engine::DataType::VECTOR_FLOAT || field_type == engine::DataType::VECTOR_BINARY) {
C
Cai Yudong 已提交
81 82
                if (!field_params.contains(engine::PARAM_DIMENSION)) {
                    return Status(SERVER_INVALID_VECTOR_DIMENSION, "Dimension not defined in field_params");
83 84
                } else {
                    auto dim = field_params[engine::PARAM_DIMENSION].get<int64_t>();
G
groot 已提交
85
                    if (field_type == engine::DataType::VECTOR_FLOAT) {
86 87 88 89
                        STATUS_CHECK(ValidateDimension(dim, false));
                    } else {
                        STATUS_CHECK(ValidateDimension(dim, true));
                    }
90
                }
91
            }
92

C
Cai Yudong 已提交
93
            auto field = std::make_shared<engine::snapshot::Field>(field_name, 0, field_type, field_params);
Y
yukun 已提交
94
            create_collection_context.fields_schema[field] = {};
95 96
        }

G
groot 已提交
97
        // step 3: create collection
G
groot 已提交
98
        status = DBWrapper::DB()->CreateCollection(create_collection_context);
C
Cai Yudong 已提交
99
        fiu_do_on("CreateCollectionReq.OnExecute.invalid_db_execute",
Y
yukun 已提交
100
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
101 102 103 104 105 106 107
        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 已提交
108 109

        rc.ElapseFromBegin("done");
110 111 112 113 114 115 116 117 118
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus