DropPartitionRequest.cpp 3.3 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
G
groot 已提交
2
//
3 4
// 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
G
groot 已提交
5
//
6 7 8 9 10
// 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.
G
groot 已提交
11

12
#include "server/delivery/request/DropPartitionRequest.h"
G
groot 已提交
13 14 15 16 17
#include "server/DBWrapper.h"
#include "utils/Log.h"
#include "utils/TimeRecorder.h"
#include "utils/ValidationUtil.h"

S
shengjh 已提交
18
#include <fiu-local.h>
G
groot 已提交
19
#include <memory>
20
#include <string>
G
groot 已提交
21 22 23 24

namespace milvus {
namespace server {

25
DropPartitionRequest::DropPartitionRequest(const std::shared_ptr<milvus::server::Context>& context,
J
Jin Hai 已提交
26 27
                                           const std::string& collection_name, const std::string& tag)
    : BaseRequest(context, BaseRequest::kDropPartition), collection_name_(collection_name), tag_(tag) {
G
groot 已提交
28 29 30
}

BaseRequestPtr
J
Jin Hai 已提交
31 32 33
DropPartitionRequest::Create(const std::shared_ptr<milvus::server::Context>& context,
                             const std::string& collection_name, const std::string& tag) {
    return std::shared_ptr<BaseRequest>(new DropPartitionRequest(context, collection_name, tag));
G
groot 已提交
34 35 36 37
}

Status
DropPartitionRequest::OnExecute() {
J
Jin Hai 已提交
38
    std::string hdr = "DropPartitionRequest(collection=" + collection_name_ + ", partition_tag=" + tag_ + ")";
G
groot 已提交
39
    TimeRecorderAuto rc(hdr);
G
groot 已提交
40

J
Jin Hai 已提交
41
    std::string collection_name = collection_name_;
42
    std::string partition_tag = tag_;
43

J
Jin Hai 已提交
44 45
    // step 1: check collection name
    auto status = ValidationUtil::ValidateCollectionName(collection_name);
G
groot 已提交
46
    fiu_do_on("DropPartitionRequest.OnExecute.invalid_collection_name",
47
              status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
48 49 50 51
    if (!status.ok()) {
        return status;
    }

52 53 54 55
    // step 2: check partition tag
    if (partition_tag == milvus::engine::DEFAULT_PARTITON_TAG) {
        std::string msg = "Default partition cannot be dropped.";
        SERVER_LOG_ERROR << msg;
G
groot 已提交
56
        return Status(SERVER_INVALID_COLLECTION_NAME, msg);
57 58
    }

59 60 61 62
    status = ValidationUtil::ValidatePartitionTags({partition_tag});
    if (!status.ok()) {
        return status;
    }
63

J
Jin Hai 已提交
64 65
    // step 3: check collection
    // only process root collection, ignore partition collection
G
groot 已提交
66 67 68
    engine::meta::CollectionSchema collection_schema;
    collection_schema.collection_id_ = collection_name_;
    status = DBWrapper::DB()->DescribeCollection(collection_schema);
69 70
    if (!status.ok()) {
        if (status.code() == DB_NOT_FOUND) {
G
groot 已提交
71
            return Status(SERVER_COLLECTION_NOT_EXIST, CollectionNotExistMsg(collection_name_));
72
        } else {
G
groot 已提交
73 74
            return status;
        }
75
    } else {
G
groot 已提交
76 77
        if (!collection_schema.owner_collection_.empty()) {
            return Status(SERVER_INVALID_COLLECTION_NAME, CollectionNotExistMsg(collection_name_));
G
groot 已提交
78 79
        }
    }
80 81 82 83

    rc.RecordSection("check validation");

    // step 4: drop partition
J
Jin Hai 已提交
84
    return DBWrapper::DB()->DropPartitionByTag(collection_name, partition_tag);
G
groot 已提交
85 86 87 88
}

}  // namespace server
}  // namespace milvus