CreateCollectionReq.cpp 5.0 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
#include "utils/Log.h"
#include "utils/TimeRecorder.h"

19
#include <fiu/fiu-local.h>
G
groot 已提交
20
#include <set>
21 22 23 24

namespace milvus {
namespace server {

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

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

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

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

G
groot 已提交
53 54 55 56 57 58 59
        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));
        }

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

G
groot 已提交
62 63
        // step 2: create snapshot collection context
        engine::snapshot::CreateCollectionContext create_collection_context;
C
Cai Yudong 已提交
64
        auto collection_schema = std::make_shared<engine::snapshot::Collection>(collection_name_, extra_params_);
65

C
Cai Yudong 已提交
66 67 68 69 70 71 72 73 74
        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_;

75 76
            STATUS_CHECK(ValidateFieldName(field_name));

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

G
groot 已提交
82
            // validate id field
G
groot 已提交
83
            if (field_name == engine::FIELD_UID) {
G
groot 已提交
84 85 86 87 88 89
                if (field_type != engine::DataType::INT64) {
                    return Status(DB_ERROR, "Field '_id' data type must be int64");
                }
            }

            // validate vector field dimension
G
groot 已提交
90
            if (field_type == engine::DataType::VECTOR_FLOAT || field_type == engine::DataType::VECTOR_BINARY) {
C
Cai Yudong 已提交
91 92
                if (!field_params.contains(engine::PARAM_DIMENSION)) {
                    return Status(SERVER_INVALID_VECTOR_DIMENSION, "Dimension not defined in field_params");
93 94
                } else {
                    auto dim = field_params[engine::PARAM_DIMENSION].get<int64_t>();
G
groot 已提交
95
                    if (field_type == engine::DataType::VECTOR_FLOAT) {
96 97 98 99
                        STATUS_CHECK(ValidateDimension(dim, false));
                    } else {
                        STATUS_CHECK(ValidateDimension(dim, true));
                    }
100
                }
101
            }
102

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

G
groot 已提交
107
        // step 3: create collection
G
groot 已提交
108
        status = DBWrapper::DB()->CreateCollection(create_collection_context);
C
Cai Yudong 已提交
109
        fiu_do_on("CreateCollectionReq.OnExecute.invalid_db_execute",
Y
yukun 已提交
110
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
111 112 113 114 115 116 117
        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 已提交
118 119

        rc.ElapseFromBegin("done");
120 121 122 123 124 125 126 127 128
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus