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 17
// 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.

K
kun yu 已提交
18
#include "Status.h"
K
kun yu 已提交
19

S
starlord 已提交
20 21
#include <cstring>

K
kun yu 已提交
22 23
namespace milvus {

S
starlord 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36
constexpr int CODE_WIDTH = sizeof(ErrorCode);

Status::Status(ErrorCode 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();
    char* result = new char[length + sizeof(length) + CODE_WIDTH];
    memcpy(result, &code, CODE_WIDTH);
    memcpy(result + CODE_WIDTH, &length, sizeof(length));
    memcpy(result + sizeof(length) + CODE_WIDTH, msg.data(), length);

    state_ = result;
K
kun yu 已提交
37 38
}

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

K
kun yu 已提交
42 43
}

S
starlord 已提交
44
Status::~Status() {
K
kun yu 已提交
45 46 47 48
    delete state_;
}

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

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

S
starlord 已提交
58 59
Status::Status(Status &&s)
        : state_(nullptr) {
K
kun yu 已提交
60 61 62
    MoveFrom(s);
}

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

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

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

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

S
starlord 已提交
88
std::string Status::ToString() const {
K
kun yu 已提交
89 90 91 92
    if (state_ == nullptr) {
        return "OK";
    }

S
starlord 已提交
93
    std::string result;
K
kun yu 已提交
94 95
    switch (code()) {
        case StatusCode::OK:
S
starlord 已提交
96
            result = "OK ";
K
kun yu 已提交
97 98
            break;
        case StatusCode::UnknownError:
S
starlord 已提交
99
            result = "Unknown error: ";
K
kun yu 已提交
100 101
            break;
        case StatusCode::NotSupported:
S
starlord 已提交
102
            result = "Not supported: ";
K
kun yu 已提交
103 104
            break;
        case StatusCode::NotConnected:
S
starlord 已提交
105 106 107 108 109 110 111 112 113 114
            result = "Not connected: ";
            break;
        case StatusCode::InvalidAgument:
            result = "Invalid agument: ";
            break;
        case StatusCode::RPCFailed:
            result = "Remote call failed: ";
            break;
        case StatusCode::ServerFailed:
            result = "Service error: ";
K
kun yu 已提交
115 116
            break;
        default:
S
starlord 已提交
117
            result = "Error code(" + std::to_string((int)code()) + "): ";
K
kun yu 已提交
118 119 120
            break;
    }

S
starlord 已提交
121 122 123 124
    uint32_t length = 0;
    memcpy(&length, state_ + CODE_WIDTH, sizeof(length));
    if(length > 0) {
        result.append(state_ + sizeof(length) + CODE_WIDTH, length);
K
kun yu 已提交
125
    }
S
starlord 已提交
126

K
kun yu 已提交
127 128 129 130
    return result;
}

}
K
kun yu 已提交
131