提交 e16ac72a 编写于 作者: S starlord

MS-558 refine status code


Former-commit-id: cb176cf273ec66e1f7f5e89e9ba4c39cabc511ea
上级 dfd9ab16
......@@ -11,6 +11,7 @@ Please mark all change in change log and use the ticket from JIRA.
- MS-553 - Refine cache code
- MS-557 - Merge Log.h
- MS-556 - Add Job Definition in Scheduler
- MS-558 - Refine status code
## New Feature
......
......@@ -15,12 +15,13 @@
// specific language governing permissions and limitations
// under the License.
#pragma once
#include "Options.h"
#include "meta/Meta.h"
#include "Status.h"
#include "Types.h"
#include "meta/Meta.h"
#include "utils/Status.h"
#include <string>
#include <memory>
......
......@@ -15,10 +15,11 @@
// specific language governing permissions and limitations
// under the License.
#pragma once
#include "db/Status.h"
#include "ExecutionEngine.h"
#include "utils/Status.h"
namespace zilliz {
namespace milvus {
......
......@@ -17,7 +17,7 @@
#pragma once
#include "db/Status.h"
#include "utils/Status.h"
#include <vector>
#include <memory>
......
......@@ -18,7 +18,7 @@
#pragma once
#include "db/Status.h"
#include "utils/Status.h"
#include "db/Types.h"
#include <set>
......
......@@ -18,10 +18,10 @@
#pragma once
#include "db/meta/Meta.h"
#include "MemTable.h"
#include "db/Status.h"
#include "MemManager.h"
#include "db/meta/Meta.h"
#include "utils/Status.h"
#include <map>
#include <string>
......
......@@ -18,9 +18,9 @@
#pragma once
#include "db/Status.h"
#include "MemTableFile.h"
#include "VectorSource.h"
#include "utils/Status.h"
#include <mutex>
......
......@@ -18,10 +18,10 @@
#pragma once
#include "db/Status.h"
#include "db/meta/Meta.h"
#include "VectorSource.h"
#include "db/meta/Meta.h"
#include "db/engine/ExecutionEngine.h"
#include "utils/Status.h"
namespace zilliz {
......
......@@ -18,10 +18,10 @@
#pragma once
#include "db/Status.h"
#include "db/meta/Meta.h"
#include "db/IDGenerator.h"
#include "db/engine/ExecutionEngine.h"
#include "utils/Status.h"
namespace zilliz {
......
......@@ -15,12 +15,13 @@
// specific language governing permissions and limitations
// under the License.
#pragma once
#include "MetaTypes.h"
#include "db/Options.h"
#include "db/Status.h"
#include "db/Types.h"
#include "utils/Status.h"
#include <cstddef>
#include <memory>
......
......@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
#pragma once
#include "MetricBase.h"
......@@ -63,7 +64,7 @@ protected:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CollectInsertMetrics : CollectMetricsBase {
public:
CollectInsertMetrics(size_t n, engine::Status& status) : n_(n), status_(status) {
CollectInsertMetrics(size_t n, Status& status) : n_(n), status_(status) {
}
~CollectInsertMetrics() {
......@@ -87,7 +88,7 @@ public:
private:
size_t n_;
engine::Status& status_;
Status& status_;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
......
......@@ -20,8 +20,6 @@
aux_source_directory(interface interface_files)
aux_source_directory(util util_files)
include_directories(/usr/include)
include_directories(include)
include_directories(/usr/local/include)
......@@ -42,12 +40,15 @@ set(grpc_service_files
add_library(milvus_sdk STATIC
${interface_files}
${grpc_client_files}
${util_files}
${grpc_service_files}
)
target_link_libraries(milvus_sdk
${third_party_libs}
${client_grpc_lib}
bzip2
lz4
snappy
zlib
)
install(TARGETS milvus_sdk DESTINATION lib)
......
......@@ -36,10 +36,10 @@ const std::string TABLE_NAME = GetTableName();
constexpr int64_t TABLE_DIMENSION = 512;
constexpr int64_t TABLE_INDEX_FILE_SIZE = 1024;
constexpr int64_t BATCH_ROW_COUNT = 100000;
constexpr int64_t NQ = 100;
constexpr int64_t TOP_K = 1;
constexpr int64_t NQ = 5;
constexpr int64_t TOP_K = 10;
constexpr int64_t SEARCH_TARGET = 5000; //change this value, result is different
constexpr int64_t ADD_VECTOR_LOOP = 1;
constexpr int64_t ADD_VECTOR_LOOP = 10;
constexpr int64_t SECONDS_EACH_HOUR = 3600;
#define BLOCK_SPLITER std::cout << "===========================================" << std::endl;
......
......@@ -51,7 +51,7 @@ ClientProxy::Connect(const ConnectParam &param) {
Status
ClientProxy::Connect(const std::string &uri) {
if (!UriCheck(uri)) {
return Status::Invalid("Invalid uri");
return Status(StatusCode::InvalidAgument, "Invalid uri");
}
size_t index = uri.find_first_of(':', 0);
......
......@@ -19,7 +19,6 @@
#pragma once
#include <string>
#include <sstream>
/** \brief Milvus SDK namespace
*/
......@@ -30,345 +29,61 @@ namespace milvus {
*/
enum class StatusCode {
OK = 0,
// system error section
// system error section
UnknownError = 1,
NotSupported,
NotConnected,
// function error section
// function error section
InvalidAgument = 1000,
RPCFailed,
ServerFailed,
};
using ErrorCode = StatusCode;
/**
* @brief Status for SDK interface return
*/
class Status {
public:
/**
* @brief Status
*
* Default constructor.
*
*/
Status() = default;
/**
* @brief Status
*
* Destructor.
*
*/
~Status() noexcept;
/**
* @brief Status
*
* Constructor
*
* @param code, status code.
* @param message, status message.
*
*/
Status(StatusCode code, const std::string &message);
/**
* @brief Status
*
* Copy constructor
*
* @param status, status to be copied.
*
*/
inline
Status(const Status &status);
/**
* @brief Status
*
* Assignment operator
*
* @param status, status to be copied.
* @return, the status is assigned.
*
*/
Status
&operator=(const Status &s);
Status(ErrorCode code, const std::string &msg);
Status();
~Status();
/**
* @brief Status
*
* Move constructor
*
* @param status, status to be moved.
*
*/
Status(Status &&s) noexcept ;
Status(const Status &s);
/**
* @brief Status
*
* Move assignment operator
*
* @param status, status to be moved.
* @return, the status is moved.
*
*/
Status
&operator=(Status &&s) noexcept;
Status &
operator=(const Status &s);
/**
* @brief Status
*
* AND operator
*
* @param status, status to be AND.
* @return, the status after AND operation.
*
*/
inline
Status operator&(const Status &s) const noexcept;
Status(Status &&s);
/**
* @brief Status
*
* AND operator
*
* @param status, status to be AND.
* @return, the status after AND operation.
*
*/
inline
Status operator&(Status &&s) const noexcept;
Status &
operator=(Status &&s);
/**
* @brief Status
*
* AND operator
*
* @param status, status to be AND.
* @return, the status after AND operation.
*
*/
inline
Status &operator&=(const Status &s) noexcept;
/**
* @brief Status
*
* AND operator
*
* @param status, status to be AND.
* @return, the status after AND operation.
*
*/
inline
Status &operator&=(Status &&s) noexcept;
/**
* @brief OK
*
* static OK status constructor
*
* @return, the status with OK.
*
*/
static
Status OK() { return Status(); }
/**
* @brief OK
*
* static OK status constructor with a specific message
*
* @param, serveral specific messages
* @return, the status with OK.
*
*/
template<typename... Args>
static Status
OK(Args &&... args) {
return Status(StatusCode::OK, MessageBuilder(std::forward<Args>(args)...));
}
/**
* @brief Invalid
*
* static Invalid status constructor with a specific message
*
* @param, serveral specific messages
* @return, the status with Invalid.
*
*/
template<typename... Args>
static Status
Invalid(Args &&... args) {
return Status(StatusCode::InvalidAgument,
MessageBuilder(std::forward<Args>(args)...));
}
/**
* @brief Unknown Error
*
* static unknown error status constructor with a specific message
*
* @param, serveral specific messages
* @return, the status with unknown error.
*
*/
template<typename... Args>
static Status
UnknownError(Args &&... args) {
return Status(StatusCode::UnknownError, MessageBuilder(std::forward<Args>(args)...));
}
/**
* @brief not supported Error
*
* static not supported status constructor with a specific message
*
* @param, serveral specific messages
* @return, the status with not supported error.
*
*/
template<typename... Args>
static Status
NotSupported(Args &&... args) {
return Status(StatusCode::NotSupported, MessageBuilder(std::forward<Args>(args)...));
}
/**
* @brief ok
*
* Return true iff the status indicates success.
*
* @return, if the status indicates success.
*
*/
bool
ok() const { return (state_ == nullptr); }
/**
* @brief IsInvalid
*
* Return true iff the status indicates invalid.
*
* @return, if the status indicates invalid.
*
*/
bool
IsInvalid() const { return code() == StatusCode::InvalidAgument; }
OK() { return Status(); }
/**
* @brief IsUnknownError
*
* Return true iff the status indicates unknown error.
*
* @return, if the status indicates unknown error.
*
*/
bool
IsUnknownError() const { return code() == StatusCode::UnknownError; }
/**
* @brief IsNotSupported
*
* Return true iff the status indicates not supported.
*
* @return, if the status indicates not supported.
*
*/
bool
IsNotSupported() const { return code() == StatusCode::NotSupported; }
/**
* @brief ToString
*
* Return error message string.
*
* @return, error message string.
*
*/
std::string
ToString() const;
bool
ok() const { return state_ == nullptr || code() == StatusCode::OK; }
/**
* @brief CodeAsString
*
* Return a string representation of the status code.
*
* @return, a string representation of the status code.
*
*/
std::string
CodeAsString() const;
/**
* @brief code
*
* Return the StatusCode value attached to this status.
*
* @return, the status code value attached to this status.
*
*/
StatusCode
code() const { return ok() ? StatusCode::OK : state_->code; }
std::string
ToString() const;
/**
* @brief message
*
* Return the specific error message attached to this status.
*
* @return, the specific error message attached to this status.
*
*/
std::string
message() const { return ok() ? "" : state_->message; }
ErrorCode
code() const {
return (state_ == nullptr) ? StatusCode::OK : *(ErrorCode*)(state_);
}
private:
struct State {
StatusCode code;
std::string message;
};
// OK status has a `nullptr` state_. Otherwise, `state_` points to
// a `State` structure containing the error code and message.
State *state_ = nullptr;
void
DeleteState() {
delete state_;
state_ = nullptr;
}
void
CopyFrom(const Status &s);
inline void
MoveFrom(Status &s);
inline void
CopyFrom(const Status &s);
template<typename Head>
static void
MessageBuilderRecursive(std::stringstream &stream, Head &&head) {
stream << head;
}
inline void
MoveFrom(Status &s);
template<typename Head, typename... Tail>
static void
MessageBuilderRecursive(std::stringstream &stream, Head &&head, Tail &&... tail) {
MessageBuilderRecursive(stream, std::forward<Head>(head));
MessageBuilderRecursive(stream, std::forward<Tail>(tail)...);
}
template<typename... Args>
static std::string
MessageBuilder(Args &&... args) {
std::stringstream stream;
MessageBuilderRecursive(stream, std::forward<Args>(args)...);
private:
const char *state_ = nullptr;
}; // Status
return stream.str();
}
};
}
\ No newline at end of file
} //Milvus
\ No newline at end of file
......@@ -17,126 +17,113 @@
#include "Status.h"
#include <cstring>
namespace milvus {
Status::~Status() noexcept {
if (state_ != nullptr) {
delete state_;
state_ = nullptr;
}
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;
}
static inline std::ostream &operator<<(std::ostream &os, const Status &x) {
os << x.ToString();
return os;
Status::Status()
: state_(nullptr) {
}
void Status::MoveFrom(Status &s) {
Status::~Status() {
delete state_;
state_ = s.state_;
s.state_ = nullptr;
}
Status::Status(const Status &s)
: state_((s.state_ == nullptr) ? nullptr : new State(*s.state_)) {}
Status::Status(Status &&s) noexcept {
MoveFrom(s);
: state_(nullptr) {
CopyFrom(s);
}
Status &Status::operator=(const Status &s) {
if (state_ != s.state_) {
CopyFrom(s);
}
CopyFrom(s);
return *this;
}
Status &Status::operator=(Status &&s) noexcept {
Status::Status(Status &&s)
: state_(nullptr) {
MoveFrom(s);
return *this;
}
Status Status::operator&(const Status &status) const noexcept {
if (ok()) {
return status;
} else {
return *this;
}
}
Status Status::operator&(Status &&s) const noexcept {
if (ok()) {
return std::move(s);
} else {
return *this;
}
}
Status &Status::operator&=(const Status &s) noexcept {
if (ok() && !s.ok()) {
CopyFrom(s);
}
Status &Status::operator=(Status &&s) {
MoveFrom(s);
return *this;
}
Status &Status::operator&=(Status &&s) noexcept {
if (ok() && !s.ok()) {
MoveFrom(s);
void Status::CopyFrom(const Status &s) {
delete state_;
state_ = nullptr;
if(s.state_ == nullptr) {
return;
}
return *this;
}
Status::Status(StatusCode code, const std::string &message) {
state_ = new State;
state_->code = code;
state_->message = message;
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);
}
void Status::CopyFrom(const Status &status) {
void Status::MoveFrom(Status &s) {
delete state_;
if (status.state_ == nullptr) {
state_ = nullptr;
} else {
state_ = new State(*status.state_);
}
state_ = s.state_;
s.state_ = nullptr;
}
std::string Status::CodeAsString() const {
std::string Status::ToString() const {
if (state_ == nullptr) {
return "OK";
}
const char *type = nullptr;
std::string result;
switch (code()) {
case StatusCode::OK:
type = "OK";
break;
case StatusCode::InvalidAgument:
type = "Invalid agument";
result = "OK ";
break;
case StatusCode::UnknownError:
type = "Unknown error";
result = "Unknown error: ";
break;
case StatusCode::NotSupported:
type = "Not Supported";
result = "Not supported: ";
break;
case StatusCode::NotConnected:
type = "Not Connected";
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: ";
break;
default:
type = "Unknown";
result = "Error code(" + std::to_string((int)code()) + "): ";
break;
}
return std::string(type);
}
std::string Status::ToString() const {
std::string result(CodeAsString());
if (state_ == nullptr) {
return result;
uint32_t length = 0;
memcpy(&length, state_ + CODE_WIDTH, sizeof(length));
if(length > 0) {
result.append(state_ + sizeof(length) + CODE_WIDTH, length);
}
result += ": ";
result += state_->message;
return result;
}
......
// 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.
#pragma once
#include "Status.h"
#include <exception>
namespace milvus {
class Exception : public std::exception {
public:
Exception(StatusCode error_code,
const std::string &message = std::string())
: error_code_(error_code), message_(message) {}
public:
StatusCode error_code() const {
return error_code_;
}
virtual const char *what() const noexcept {
return message_.c_str();
}
private:
StatusCode error_code_;
std::string message_;
};
}
\ No newline at end of file
......@@ -170,7 +170,7 @@ CreateTableTask::OnExecute() {
table_info.metric_type_ = schema_->metric_type();
//step 3: create table
engine::Status stat = DBWrapper::DB()->CreateTable(table_info);
auto stat = DBWrapper::DB()->CreateTable(table_info);
if (!stat.ok()) {
//table could exist
if(stat.code() == DB_ALREADY_EXIST) {
......@@ -214,7 +214,7 @@ DescribeTableTask::OnExecute() {
//step 2: get table info
engine::meta::TableSchema table_info;
table_info.table_id_ = table_name_;
engine::Status stat = DBWrapper::DB()->DescribeTable(table_info);
auto stat = DBWrapper::DB()->DescribeTable(table_info);
if (!stat.ok()) {
return SetError(DB_META_TRANSACTION_FAILED, stat.ToString());
}
......@@ -261,7 +261,7 @@ CreateIndexTask::OnExecute() {
}
bool has_table = false;
engine::Status stat = DBWrapper::DB()->HasTable(table_name_, has_table);
auto stat = DBWrapper::DB()->HasTable(table_name_, has_table);
if (!stat.ok()) {
return SetError(DB_META_TRANSACTION_FAILED, stat.ToString());
}
......@@ -323,7 +323,7 @@ HasTableTask::OnExecute() {
}
//step 2: check table existence
engine::Status stat = DBWrapper::DB()->HasTable(table_name_, has_table_);
auto stat = DBWrapper::DB()->HasTable(table_name_, has_table_);
if (!stat.ok()) {
return SetError(DB_META_TRANSACTION_FAILED, stat.ToString());
}
......@@ -362,7 +362,7 @@ DropTableTask::OnExecute() {
//step 2: check table existence
engine::meta::TableSchema table_info;
table_info.table_id_ = table_name_;
engine::Status stat = DBWrapper::DB()->DescribeTable(table_info);
auto stat = DBWrapper::DB()->DescribeTable(table_info);
if (!stat.ok()) {
if (stat.code() == DB_NOT_FOUND) {
return SetError(SERVER_TABLE_NOT_EXIST, "Table " + table_name_ + " not exists");
......@@ -403,7 +403,7 @@ ShowTablesTask::Create(::grpc::ServerWriter<::milvus::grpc::TableName> *writer)
ErrorCode
ShowTablesTask::OnExecute() {
std::vector<engine::meta::TableSchema> schema_array;
engine::Status stat = DBWrapper::DB()->AllTables(schema_array);
auto stat = DBWrapper::DB()->AllTables(schema_array);
if (!stat.ok()) {
return SetError(DB_META_TRANSACTION_FAILED, stat.ToString());
}
......@@ -460,7 +460,7 @@ InsertTask::OnExecute() {
//step 2: check table existence
engine::meta::TableSchema table_info;
table_info.table_id_ = insert_param_->table_name();
engine::Status stat = DBWrapper::DB()->DescribeTable(table_info);
auto stat = DBWrapper::DB()->DescribeTable(table_info);
if (!stat.ok()) {
if (stat.code() == DB_NOT_FOUND) {
return SetError(SERVER_TABLE_NOT_EXIST,
......@@ -599,7 +599,7 @@ SearchTask::OnExecute() {
//step 2: check table existence
engine::meta::TableSchema table_info;
table_info.table_id_ = table_name_;
engine::Status stat = DBWrapper::DB()->DescribeTable(table_info);
auto stat = DBWrapper::DB()->DescribeTable(table_info);
if (!stat.ok()) {
if (stat.code() == DB_NOT_FOUND) {
return SetError(SERVER_TABLE_NOT_EXIST, "Table " + table_name_ + " not exists");
......@@ -746,7 +746,7 @@ CountTableTask::OnExecute() {
//step 2: get row count
uint64_t row_count = 0;
engine::Status stat = DBWrapper::DB()->GetTableRowCount(table_name_, row_count);
auto stat = DBWrapper::DB()->GetTableRowCount(table_name_, row_count);
if (!stat.ok()) {
return SetError(DB_META_TRANSACTION_FAILED, stat.ToString());
}
......@@ -819,7 +819,7 @@ DeleteByRangeTask::OnExecute() {
//step 2: check table existence
engine::meta::TableSchema table_info;
table_info.table_id_ = table_name;
engine::Status stat = DBWrapper::DB()->DescribeTable(table_info);
auto stat = DBWrapper::DB()->DescribeTable(table_info);
if (!stat.ok()) {
if (stat.code(), DB_NOT_FOUND) {
return SetError(SERVER_TABLE_NOT_EXIST, "Table " + table_name + " not exists");
......@@ -846,8 +846,8 @@ DeleteByRangeTask::OnExecute() {
std::string fname = "/tmp/search_nq_" + this->delete_by_range_param_->table_name() + ".profiling";
ProfilerStart(fname.c_str());
#endif
engine::Status status = DBWrapper::DB()->DeleteTable(table_name, dates);
if (!status.ok()) {
stat = DBWrapper::DB()->DeleteTable(table_name, dates);
if (!stat.ok()) {
return SetError(DB_META_TRANSACTION_FAILED, stat.ToString());
}
......@@ -882,7 +882,7 @@ PreloadTableTask::OnExecute() {
}
//step 2: check table existence
engine::Status stat = DBWrapper::DB()->PreloadTable(table_name_);
auto stat = DBWrapper::DB()->PreloadTable(table_name_);
if (!stat.ok()) {
return SetError(DB_META_TRANSACTION_FAILED, stat.ToString());
}
......@@ -923,7 +923,7 @@ DescribeIndexTask::OnExecute() {
//step 2: check table existence
engine::TableIndex index;
engine::Status stat = DBWrapper::DB()->DescribeIndex(table_name_, index);
auto stat = DBWrapper::DB()->DescribeIndex(table_name_, index);
if (!stat.ok()) {
return SetError(DB_META_TRANSACTION_FAILED, stat.ToString());
}
......
......@@ -14,15 +14,12 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "Status.h"
#include <stdio.h>
#include <cstring>
#include <assert.h>
#include "Status.h"
namespace zilliz {
namespace milvus {
namespace engine {
constexpr int CODE_WIDTH = sizeof(ErrorCode);
......@@ -45,55 +42,87 @@ Status::Status()
}
Status::~Status() {
delete[] state_;
delete state_;
}
Status::Status(const Status &s)
: state_(nullptr) {
CopyFrom(s);
}
const char* Status::CopyState(const char* state) {
Status &Status::operator=(const Status &s) {
CopyFrom(s);
return *this;
}
Status::Status(Status &&s)
: state_(nullptr) {
MoveFrom(s);
}
Status &Status::operator=(Status &&s) {
MoveFrom(s);
return *this;
}
void Status::CopyFrom(const Status &s) {
delete state_;
state_ = nullptr;
if(s.state_ == nullptr) {
return;
}
uint32_t length = 0;
std::memcpy(&length, state + CODE_WIDTH, sizeof(length));
std::memcpy(&length, s.state_ + CODE_WIDTH, sizeof(length));
int buff_len = length + sizeof(length) + CODE_WIDTH;
char* result = new char[buff_len];
memcpy(result, state, buff_len);
return result;
state_ = new char[buff_len];
memcpy((void*)state_, (void*)s.state_, buff_len);
}
void Status::MoveFrom(Status &s) {
delete state_;
state_ = s.state_;
s.state_ = nullptr;
}
std::string Status::ToString() const {
if (state_ == nullptr) return "OK";
char tmp[32];
const char* type;
if (state_ == nullptr) {
return "OK";
}
std::string result;
switch (code()) {
case DB_SUCCESS:
type = "OK";
result = "OK ";
break;
case DB_ERROR:
type = "Error: ";
result = "Error: ";
break;
case DB_META_TRANSACTION_FAILED:
type = "DBTransactionError: ";
result = "Database error: ";
break;
case DB_NOT_FOUND:
type = "NotFound: ";
result = "Not found: ";
break;
case DB_ALREADY_EXIST:
type = "AlreadyExist: ";
result = "Already exist: ";
break;
case DB_INVALID_PATH:
type = "InvalidPath: ";
result = "Invalid path: ";
break;
default:
snprintf(tmp, sizeof(tmp), "Error code(0x%x): ",
static_cast<int>(code()));
type = tmp;
result = "Error code(" + std::to_string(code()) + "): ";
break;
}
std::string result(type);
uint32_t length = 0;
memcpy(&length, state_ + CODE_WIDTH, sizeof(length));
result.append(state_ + sizeof(length) + CODE_WIDTH, length);
if(length > 0) {
result.append(state_ + sizeof(length) + CODE_WIDTH, length);
}
return result;
}
} // namespace engine
} // namespace milvus
} // namespace zilliz
......@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
#pragma once
#include "utils/Error.h"
......@@ -23,7 +24,6 @@
namespace zilliz {
namespace milvus {
namespace engine {
class Status {
public:
......@@ -31,51 +31,40 @@ class Status {
Status();
~Status();
Status(const Status &rhs);
Status(const Status &s);
Status &
operator=(const Status &rhs);
operator=(const Status &s);
Status(Status &&rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; }
Status(Status &&s);
Status &
operator=(Status &&rhs_) noexcept;
operator=(Status &&s);
static Status
OK() { return Status(); }
bool ok() const { return state_ == nullptr || code() == DB_SUCCESS; }
bool
ok() const { return state_ == nullptr || code() == 0; }
std::string ToString() const;
std::string
ToString() const;
ErrorCode code() const {
return (state_ == nullptr) ? DB_SUCCESS : *(ErrorCode*)(state_);
ErrorCode
code() const {
return (state_ == nullptr) ? 0 : *(ErrorCode*)(state_);
}
private:
const char *state_ = nullptr;
private:
inline void
CopyFrom(const Status &s);
static const char *CopyState(const char *s);
inline void
MoveFrom(Status &s);
private:
const char *state_ = nullptr;
}; // Status
inline Status::Status(const Status &rhs) {
state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_);
}
inline Status &Status::operator=(const Status &rhs) {
if (state_ != rhs.state_) {
delete[] state_;
state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_);
}
return *this;
}
inline Status &Status::operator=(Status &&rhs) noexcept {
std::swap(state_, rhs.state_);
return *this;
}
} // namespace engine
} // namespace milvus
} // namespace zilliz
......@@ -33,9 +33,9 @@ aux_source_directory(${MILVUS_ENGINE_SRC}/scheduler scheduler_srcs)
aux_source_directory(./ test_srcs)
set(util_files
${MILVUS_ENGINE_SRC}/utils/Status.cpp
${MILVUS_ENGINE_SRC}/utils/ValidationUtil.cpp
${MILVUS_ENGINE_SRC}/utils/easylogging++.cc
${MILVUS_ENGINE_SRC}/utils/easylogging++.h)
${MILVUS_ENGINE_SRC}/utils/easylogging++.cc)
aux_source_directory(${MILVUS_ENGINE_SRC}/db/scheduler scheduler_files)
aux_source_directory(${MILVUS_ENGINE_SRC}/db/scheduler/context scheduler_context_files)
......
......@@ -154,7 +154,7 @@ TEST_F(DBTest, CONFIG_TEST) {
TEST_F(DBTest, DB_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......@@ -230,7 +230,7 @@ TEST_F(DBTest, DB_TEST) {
TEST_F(DBTest, SEARCH_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......@@ -296,7 +296,7 @@ TEST_F(DBTest, SEARCH_TEST) {
TEST_F(DBTest, PRELOADTABLE_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......@@ -331,7 +331,7 @@ TEST_F(DBTest, SHUTDOWN_TEST) {
db_->Stop();
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
ASSERT_FALSE(stat.ok());
stat = db_->DescribeTable(table_info);
......@@ -373,7 +373,7 @@ TEST_F(DBTest, SHUTDOWN_TEST) {
TEST_F(DBTest, INDEX_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
int64_t nb = VECTOR_COUNT;
std::vector<float> xb;
......@@ -403,7 +403,7 @@ TEST_F(DBTest, INDEX_TEST) {
TEST_F(DBTest2, ARHIVE_DISK_CHECK) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
std::vector<engine::meta::TableSchema> table_schema_array;
stat = db_->AllTables(table_schema_array);
......@@ -446,7 +446,7 @@ TEST_F(DBTest2, ARHIVE_DISK_CHECK) {
TEST_F(DBTest2, DELETE_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......@@ -480,7 +480,7 @@ TEST_F(DBTest2, DELETE_TEST) {
TEST_F(DBTest2, DELETE_BY_RANGE_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......
......@@ -216,7 +216,7 @@ TEST_F(MemManagerTest, MEM_TABLE_TEST) {
TEST_F(MemManagerTest2, SERIAL_INSERT_SEARCH_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......@@ -230,8 +230,8 @@ TEST_F(MemManagerTest2, SERIAL_INSERT_SEARCH_TEST) {
int64_t nb = 100000;
std::vector<float> xb;
BuildVectors(nb, xb);
engine::Status status = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
ASSERT_TRUE(status.ok());
stat = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
ASSERT_TRUE(stat.ok());
std::this_thread::sleep_for(std::chrono::seconds(3));
......@@ -262,7 +262,7 @@ TEST_F(MemManagerTest2, SERIAL_INSERT_SEARCH_TEST) {
TEST_F(MemManagerTest2, INSERT_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......@@ -278,8 +278,8 @@ TEST_F(MemManagerTest2, INSERT_TEST) {
std::vector<float> xb;
BuildVectors(nb, xb);
engine::IDNumbers vector_ids;
engine::Status status = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
ASSERT_TRUE(status.ok());
stat = db_->InsertVectors(TABLE_NAME, nb, xb.data(), vector_ids);
ASSERT_TRUE(stat.ok());
}
auto end_time = METRICS_NOW_TIME;
auto total_time = METRICS_MICROSECONDS(start_time, end_time);
......@@ -288,7 +288,7 @@ TEST_F(MemManagerTest2, INSERT_TEST) {
TEST_F(MemManagerTest2, CONCURRENT_INSERT_SEARCH_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......@@ -359,7 +359,7 @@ TEST_F(MemManagerTest2, CONCURRENT_INSERT_SEARCH_TEST) {
TEST_F(MemManagerTest2, VECTOR_IDS_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......
......@@ -15,11 +15,11 @@
// specific language governing permissions and limitations
// under the License.
#include "db/Status.h"
#include "db/Options.h"
#include "db/meta/SqliteMetaImpl.h"
#include "db/engine/EngineFactory.h"
#include "db/Utils.h"
#include "utils/Status.h"
#include "utils/Exception.h"
#include "utils/easylogging++.h"
......@@ -30,13 +30,6 @@
using namespace zilliz::milvus;
namespace {
void CopyStatus(engine::Status& st1, engine::Status& st2) {
st1 = st2;
}
}
TEST(DBMiscTest, EXCEPTION_TEST) {
Exception ex1("");
std::string what = ex1.what();
......@@ -47,36 +40,6 @@ TEST(DBMiscTest, EXCEPTION_TEST) {
ASSERT_FALSE(what.empty());
}
TEST(DBMiscTest, STATUS_TEST) {
engine::Status status = engine::Status::OK();
std::string str = status.ToString();
ASSERT_FALSE(str.empty());
status = engine::Status(DB_ERROR, "mistake");
ASSERT_EQ(status.code(), DB_ERROR);
str = status.ToString();
ASSERT_FALSE(str.empty());
status = engine::Status(DB_NOT_FOUND, "mistake");
ASSERT_EQ(status.code(), DB_NOT_FOUND);
str = status.ToString();
ASSERT_FALSE(str.empty());
status = engine::Status(DB_ALREADY_EXIST, "mistake");
ASSERT_EQ(status.code(), DB_ALREADY_EXIST);
str = status.ToString();
ASSERT_FALSE(str.empty());
status = engine::Status(DB_META_TRANSACTION_FAILED, "mistake");
ASSERT_EQ(status.code(), DB_META_TRANSACTION_FAILED);
str = status.ToString();
ASSERT_FALSE(str.empty());
engine::Status status_copy = engine::Status::OK();
CopyStatus(status_copy, status);
ASSERT_EQ(status.code(), DB_META_TRANSACTION_FAILED);
}
TEST(DBMiscTest, OPTIONS_TEST) {
try {
engine::ArchiveConf archive("$$##");
......
......@@ -59,7 +59,7 @@ namespace {
TEST_F(MySqlDBTest, DB_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......@@ -144,7 +144,7 @@ TEST_F(MySqlDBTest, DB_TEST) {
TEST_F(MySqlDBTest, SEARCH_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
engine::meta::TableSchema table_info_get;
table_info_get.table_id_ = TABLE_NAME;
......@@ -196,7 +196,7 @@ TEST_F(MySqlDBTest, SEARCH_TEST) {
TEST_F(MySqlDBTest, ARHIVE_DISK_CHECK) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
std::vector<engine::meta::TableSchema> table_schema_array;
stat = db_->AllTables(table_schema_array);
......@@ -241,7 +241,7 @@ TEST_F(MySqlDBTest, ARHIVE_DISK_CHECK) {
TEST_F(MySqlDBTest, DELETE_TEST) {
engine::meta::TableSchema table_info = BuildTableSchema();
engine::Status stat = db_->CreateTable(table_info);
auto stat = db_->CreateTable(table_info);
// std::cout << stat.ToString() << std::endl;
engine::meta::TableSchema table_info_get;
......
......@@ -37,9 +37,9 @@ aux_source_directory(${MILVUS_ENGINE_SRC}/src/metrics metrics_src)
aux_source_directory(./ test_srcs)
set(util_files
${MILVUS_ENGINE_SRC}/utils/Status.cpp
${MILVUS_ENGINE_SRC}/utils/ValidationUtil.cpp
${MILVUS_ENGINE_SRC}/utils/easylogging++.cc
${MILVUS_ENGINE_SRC}/utils/easylogging++.h)
${MILVUS_ENGINE_SRC}/utils/easylogging++.cc)
aux_source_directory(${MILVUS_ENGINE_SRC}/db/scheduler scheduler_files)
aux_source_directory(${MILVUS_ENGINE_SRC}/db/scheduler/context scheduler_context_files)
......
......@@ -57,7 +57,7 @@ TEST_F(MetricTest, METRIC_TEST) {
engine::meta::TableSchema group_info;
group_info.dimension_ = group_dim;
group_info.table_id_ = group_name;
engine::Status stat = db_->CreateTable(group_info);
auto stat = db_->CreateTable(group_info);
engine::meta::TableSchema group_info_get;
group_info_get.table_id_ = group_name;
......@@ -134,9 +134,9 @@ TEST_F(MetricTest, METRIC_TEST) {
};
TEST_F(MetricTest, COLLECTOR_METRICS_TEST){
engine::Status status = engine::Status::OK();
auto status = Status::OK();
server::CollectInsertMetrics insert_metrics0(0, status);
status = engine::Status(DB_ERROR, "error");
status = Status(DB_ERROR, "error");
server::CollectInsertMetrics insert_metrics1(0, status);
server::CollectQueryMetrics query_metrics(10);
......
......@@ -42,7 +42,7 @@
#define STOP_TIMER(name)
#endif
void ASSERT_STATS(zilliz::milvus::engine::Status& stat);
void ASSERT_STATS(zilliz::milvus::Status& stat);
//class TestEnv : public ::testing::Environment {
//public:
......
......@@ -36,9 +36,9 @@ aux_source_directory(${MILVUS_ENGINE_SRC}/scheduler scheduler_srcs)
aux_source_directory(./ test_srcs)
set(util_files
${MILVUS_ENGINE_SRC}/utils/Status.cpp
${MILVUS_ENGINE_SRC}/utils/ValidationUtil.cpp
${MILVUS_ENGINE_SRC}/utils/easylogging++.cc
${MILVUS_ENGINE_SRC}/utils/easylogging++.h)
${MILVUS_ENGINE_SRC}/utils/easylogging++.cc)
set(db_scheduler_srcs
${scheduler_files}
......
......@@ -54,9 +54,9 @@ set(db_scheduler_srcs
)
set(util_files
${MILVUS_ENGINE_SRC}/utils/Status.cpp
${MILVUS_ENGINE_SRC}/utils/ValidationUtil.cpp
${MILVUS_ENGINE_SRC}/utils/easylogging++.cc
${MILVUS_ENGINE_SRC}/utils/easylogging++.h)
${MILVUS_ENGINE_SRC}/utils/easylogging++.cc)
set(db_src
${config_files}
......
......@@ -37,6 +37,10 @@ namespace {
static const char* LOG_FILE_PATH = "./milvus/conf/log_config.conf";
void CopyStatus(Status& st1, Status& st2) {
st1 = st2;
}
}
TEST(UtilTest, EXCEPTION_TEST) {
......@@ -183,6 +187,44 @@ TEST(UtilTest, TIMERECORDER_TEST) {
}
}
TEST(UtilTest, STATUS_TEST) {
auto status = Status::OK();
std::string str = status.ToString();
ASSERT_FALSE(str.empty());
status = Status(DB_ERROR, "mistake");
ASSERT_EQ(status.code(), DB_ERROR);
str = status.ToString();
ASSERT_FALSE(str.empty());
status = Status(DB_NOT_FOUND, "mistake");
ASSERT_EQ(status.code(), DB_NOT_FOUND);
str = status.ToString();
ASSERT_FALSE(str.empty());
status = Status(DB_ALREADY_EXIST, "mistake");
ASSERT_EQ(status.code(), DB_ALREADY_EXIST);
str = status.ToString();
ASSERT_FALSE(str.empty());
status = Status(DB_META_TRANSACTION_FAILED, "mistake");
ASSERT_EQ(status.code(), DB_META_TRANSACTION_FAILED);
str = status.ToString();
ASSERT_FALSE(str.empty());
auto status_copy = Status::OK();
CopyStatus(status_copy, status);
ASSERT_EQ(status.code(), DB_META_TRANSACTION_FAILED);
auto status_ref(status);
ASSERT_EQ(status_ref.code(), status.code());
ASSERT_EQ(status_ref.ToString(), status.ToString());
auto status_move = std::move(status);
ASSERT_EQ(status_move.code(), status_ref.code());
ASSERT_EQ(status_move.ToString(), status_ref.ToString());
}
TEST(ValidationUtilTest, VALIDATE_TABLENAME_TEST) {
std::string table_name = "Normal123_";
ErrorCode res = server::ValidationUtil::ValidateTableName(table_name);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册