Status.cpp 3.4 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
#include "Status.h"
J
jinhai 已提交
18

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

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

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

S
starlord 已提交
26
Status::Status(StatusCode code, const std::string& msg) {
S
starlord 已提交
27 28 29 30 31 32 33 34
    //4 bytes store code
    //4 bytes store message length
    //the left bytes store message string
    const uint32_t length = (uint32_t)msg.size();
    char* result = new char[length + sizeof(length) + CODE_WIDTH];
    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 已提交
35

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

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

}

Status::~Status() {
S
starlord 已提交
45 46 47 48 49 50
    delete state_;
}

Status::Status(const Status &s)
    : state_(nullptr) {
    CopyFrom(s);
S
starlord 已提交
51 52
}

S
starlord 已提交
53 54
Status&
Status::operator=(const Status &s) {
S
starlord 已提交
55 56 57 58 59 60 61 62 63
    CopyFrom(s);
    return *this;
}

Status::Status(Status &&s)
    : state_(nullptr) {
    MoveFrom(s);
}

S
starlord 已提交
64 65
Status&
Status::operator=(Status &&s) {
S
starlord 已提交
66 67 68 69
    MoveFrom(s);
    return *this;
}

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

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

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

S
starlord 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
std::string
Status::message() const {
    if (state_ == nullptr) {
        return "";
    }

    std::string msg;
    uint32_t length = 0;
    memcpy(&length, state_ + CODE_WIDTH, sizeof(length));
    if(length > 0) {
        msg.append(state_ + sizeof(length) + CODE_WIDTH, length);
    }

    return msg;
}

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

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

S
starlord 已提交
139
    result += message();
X
Xu Peng 已提交
140 141 142
    return result;
}

J
jinhai 已提交
143
} // namespace milvus
X
Xu Peng 已提交
144
} // namespace zilliz