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"
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
CreateCollectionReq::CreateCollectionReq(const std::shared_ptr<milvus::server::Context>& context,
                                         const std::string& collection_name,
C
Cai Yudong 已提交
33
                                         std::unordered_map<std::string, FieldSchema>& fields,
C
Cai Yudong 已提交
34 35
                                         milvus::json& extra_params)
    : BaseReq(context, BaseReq::kCreateCollection),
36
      collection_name_(collection_name),
C
Cai Yudong 已提交
37
      fields_(fields),
38
      extra_params_(extra_params) {
39 40
}

C
Cai Yudong 已提交
41 42
BaseReqPtr
CreateCollectionReq::Create(const std::shared_ptr<milvus::server::Context>& context, const std::string& collection_name,
C
Cai Yudong 已提交
43 44
                            std::unordered_map<std::string, FieldSchema>& fields, milvus::json& extra_params) {
    return std::shared_ptr<BaseReq>(new CreateCollectionReq(context, collection_name, fields, extra_params));
45 46 47
}

Status
C
Cai Yudong 已提交
48 49
CreateCollectionReq::OnExecute() {
    std::string hdr = "CreateCollectionReq(collection=" + collection_name_ + ")";
50 51 52 53
    TimeRecorderAuto rc(hdr);

    try {
        // step 1: check arguments
C
Cai Yudong 已提交
54
        auto status = ValidateCollectionName(collection_name_);
C
Cai Yudong 已提交
55
        fiu_do_on("CreateCollectionReq.OnExecute.invalid_collection_name",
Y
yukun 已提交
56
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
57 58 59 60 61 62
        if (!status.ok()) {
            return status;
        }

        rc.RecordSection("check validation");

G
groot 已提交
63 64
        // step 2: create snapshot collection context
        engine::snapshot::CreateCollectionContext create_collection_context;
C
Cai Yudong 已提交
65 66 67 68 69 70 71 72 73 74 75
        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_;

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

C
Cai Yudong 已提交
81 82 83 84
            std::cout << field_params.dump() << std::endl;
            if (field_type == engine::FieldType::VECTOR_FLOAT || field_type == engine::FieldType::VECTOR_BINARY) {
                if (!field_params.contains(engine::PARAM_DIMENSION)) {
                    return Status(SERVER_INVALID_VECTOR_DIMENSION, "Dimension not defined in field_params");
85
                }
86
            }
87

C
Cai Yudong 已提交
88
            auto field = std::make_shared<engine::snapshot::Field>(field_name, 0, field_type, field_params);
89
            auto field_element = std::make_shared<engine::snapshot::FieldElement>(
G
groot 已提交
90
                0, 0, index_name, engine::FieldElementType::FET_INDEX, index_params);
91 92 93
            create_collection_context.fields_schema[field] = {field_element};
        }

G
groot 已提交
94 95
        if (!extra_params_.contains(engine::PARAM_SEGMENT_ROW_COUNT)) {
            return Status(SERVER_UNEXPECTED_ERROR, "Segment row count not defined");
C
Cai Yudong 已提交
96
        } else {
G
groot 已提交
97 98
            auto segment_row = extra_params_[engine::PARAM_SEGMENT_ROW_COUNT].get<int64_t>();
            STATUS_CHECK(ValidateSegmentRowCount(segment_row));
C
Cai Yudong 已提交
99 100
        }

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

        rc.ElapseFromBegin("done");
114 115 116 117 118 119 120 121 122
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus