Status.cpp 3.3 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.
S
starlord 已提交
17 18

#include "utils/Status.h"
J
jinhai 已提交
19

X
Xu Peng 已提交
20
#include <cstring>
X
Xu Peng 已提交
21

X
Xu Peng 已提交
22
namespace zilliz {
J
jinhai 已提交
23
namespace milvus {
X
Xu Peng 已提交
24

S
starlord 已提交
25
constexpr int CODE_WIDTH = sizeof(StatusCode);
S
starlord 已提交
26

S
starlord 已提交
27 28 29 30 31
Status::Status(StatusCode code, const std::string& msg) {
    // 4 bytes store code
    // 4 bytes store message length
    // the left bytes store message string
    const uint32_t length = (uint32_t)msg.size();
S
starlord 已提交
32
    auto result = new char[length + sizeof(length) + CODE_WIDTH];
S
starlord 已提交
33 34 35
    std::memcpy(result, &code, CODE_WIDTH);
    std::memcpy(result + CODE_WIDTH, &length, sizeof(length));
    memcpy(result + sizeof(length) + CODE_WIDTH, msg.data(), length);
X
Xu Peng 已提交
36

X
Xu Peng 已提交
37
    state_ = result;
X
Xu Peng 已提交
38 39
}

S
starlord 已提交
40
Status::Status() : state_(nullptr) {
S
starlord 已提交
41 42 43
}

Status::~Status() {
S
starlord 已提交
44 45 46
    delete state_;
}

S
starlord 已提交
47
Status::Status(const Status& s) : state_(nullptr) {
S
starlord 已提交
48
    CopyFrom(s);
S
starlord 已提交
49 50
}

S
starlord 已提交
51 52
Status&
Status::operator=(const Status& s) {
S
starlord 已提交
53 54 55 56
    CopyFrom(s);
    return *this;
}

S
starlord 已提交
57
Status::Status(Status&& s) : state_(nullptr) {
S
starlord 已提交
58 59 60
    MoveFrom(s);
}

S
starlord 已提交
61 62
Status&
Status::operator=(Status&& s) {
S
starlord 已提交
63 64 65 66
    MoveFrom(s);
    return *this;
}

S
starlord 已提交
67
void
S
starlord 已提交
68
Status::CopyFrom(const Status& s) {
S
starlord 已提交
69 70
    delete state_;
    state_ = nullptr;
S
starlord 已提交
71
    if (s.state_ == nullptr) {
S
starlord 已提交
72 73 74
        return;
    }

S
starlord 已提交
75
    uint32_t length = 0;
S
starlord 已提交
76
    memcpy(&length, s.state_ + CODE_WIDTH, sizeof(length));
S
starlord 已提交
77
    int buff_len = length + sizeof(length) + CODE_WIDTH;
S
starlord 已提交
78
    state_ = new char[buff_len];
S
starlord 已提交
79
    memcpy(state_, s.state_, buff_len);
S
starlord 已提交
80 81
}

S
starlord 已提交
82
void
S
starlord 已提交
83
Status::MoveFrom(Status& s) {
S
starlord 已提交
84 85 86
    delete state_;
    state_ = s.state_;
    s.state_ = nullptr;
S
starlord 已提交
87 88
}

S
starlord 已提交
89 90 91 92 93 94 95 96 97
std::string
Status::message() const {
    if (state_ == nullptr) {
        return "";
    }

    std::string msg;
    uint32_t length = 0;
    memcpy(&length, state_ + CODE_WIDTH, sizeof(length));
S
starlord 已提交
98
    if (length > 0) {
S
starlord 已提交
99 100 101 102 103 104 105 106
        msg.append(state_ + sizeof(length) + CODE_WIDTH, length);
    }

    return msg;
}

std::string
Status::ToString() const {
S
starlord 已提交
107 108 109 110 111
    if (state_ == nullptr) {
        return "OK";
    }

    std::string result;
X
Xu Peng 已提交
112
    switch (code()) {
S
starlord 已提交
113 114
        case DB_SUCCESS:
            result = "OK ";
X
Xu Peng 已提交
115
            break;
S
starlord 已提交
116 117
        case DB_ERROR:
            result = "Error: ";
118
            break;
S
starlord 已提交
119 120
        case DB_META_TRANSACTION_FAILED:
            result = "Database error: ";
121
            break;
S
starlord 已提交
122 123
        case DB_NOT_FOUND:
            result = "Not found: ";
S
starlord 已提交
124
            break;
S
starlord 已提交
125 126
        case DB_ALREADY_EXIST:
            result = "Already exist: ";
127
            break;
S
starlord 已提交
128 129
        case DB_INVALID_PATH:
            result = "Invalid path: ";
S
starlord 已提交
130
            break;
S
starlord 已提交
131 132
        default:
            result = "Error code(" + std::to_string(code()) + "): ";
X
Xu Peng 已提交
133 134 135
            break;
    }

S
starlord 已提交
136
    result += message();
X
Xu Peng 已提交
137 138 139
    return result;
}

S
starlord 已提交
140 141
}  // namespace milvus
}  // namespace zilliz