diff --git a/cpp/CHANGELOG.md b/cpp/CHANGELOG.md index de7d8306fdc5a2618129115c95e376062dd5b019..1f0aedef855870c4c05e23489a8cb8f4216d8472 100644 --- a/cpp/CHANGELOG.md +++ b/cpp/CHANGELOG.md @@ -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 diff --git a/cpp/src/db/DB.h b/cpp/src/db/DB.h index 1de424ed78e28e92d865526bca4060c9120f9089..5fe6df491f2d57369bae477a03c120bcdfa1752c 100644 --- a/cpp/src/db/DB.h +++ b/cpp/src/db/DB.h @@ -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 #include diff --git a/cpp/src/db/engine/EngineFactory.h b/cpp/src/db/engine/EngineFactory.h index 906260b966244e1388b267cd5801206fae131a51..c3fb41addeee76e321e0a71f1f38d6a2679c86cf 100644 --- a/cpp/src/db/engine/EngineFactory.h +++ b/cpp/src/db/engine/EngineFactory.h @@ -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 { diff --git a/cpp/src/db/engine/ExecutionEngine.h b/cpp/src/db/engine/ExecutionEngine.h index 417d002a21585dd29be138a1617ddef8865ebe45..5868f15d338a9615f3a3d6d141211661f104e21f 100644 --- a/cpp/src/db/engine/ExecutionEngine.h +++ b/cpp/src/db/engine/ExecutionEngine.h @@ -17,7 +17,7 @@ #pragma once -#include "db/Status.h" +#include "utils/Status.h" #include #include diff --git a/cpp/src/db/insert/MemManager.h b/cpp/src/db/insert/MemManager.h index de6ee85ed179cb72e60090d77aa9cb865c523ef4..37f0548646ba5ec1d4b7817e9827b39965b7fcf6 100644 --- a/cpp/src/db/insert/MemManager.h +++ b/cpp/src/db/insert/MemManager.h @@ -18,7 +18,7 @@ #pragma once -#include "db/Status.h" +#include "utils/Status.h" #include "db/Types.h" #include diff --git a/cpp/src/db/insert/MemManagerImpl.h b/cpp/src/db/insert/MemManagerImpl.h index 9b503216a74f4fd360103baa93d8d04d8753b8a6..51e8f1044e0f8fff48d5b0089a0b2a93c855852e 100644 --- a/cpp/src/db/insert/MemManagerImpl.h +++ b/cpp/src/db/insert/MemManagerImpl.h @@ -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 #include diff --git a/cpp/src/db/insert/MemTable.h b/cpp/src/db/insert/MemTable.h index a44318690060b58782c54c5bdf20e92c9f19cb78..def4a6dccaeb0d5380aec398df555bc1d369f7ce 100644 --- a/cpp/src/db/insert/MemTable.h +++ b/cpp/src/db/insert/MemTable.h @@ -18,9 +18,9 @@ #pragma once -#include "db/Status.h" #include "MemTableFile.h" #include "VectorSource.h" +#include "utils/Status.h" #include diff --git a/cpp/src/db/insert/MemTableFile.h b/cpp/src/db/insert/MemTableFile.h index d4b11cda7af501404382948e2f5fe8954e4724d8..5ac3e15ad29568da93d3d8c7b67f8a015e342a47 100644 --- a/cpp/src/db/insert/MemTableFile.h +++ b/cpp/src/db/insert/MemTableFile.h @@ -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 { diff --git a/cpp/src/db/insert/VectorSource.h b/cpp/src/db/insert/VectorSource.h index 5c44f580d2f9d12a33c6d14e486e5971b681f141..a0292181ce5b1d343e0c84d95f0314a9c1f9d0a1 100644 --- a/cpp/src/db/insert/VectorSource.h +++ b/cpp/src/db/insert/VectorSource.h @@ -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 { diff --git a/cpp/src/db/meta/Meta.h b/cpp/src/db/meta/Meta.h index 755e0170f2c26aed799856db0151ae870dc0d222..ee4972ae6be73c53bff381239eed90c1fa2e4ff7 100644 --- a/cpp/src/db/meta/Meta.h +++ b/cpp/src/db/meta/Meta.h @@ -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 #include diff --git a/cpp/src/metrics/Metrics.h b/cpp/src/metrics/Metrics.h index e298af7acf820a553cdbd4dc66069152ef34e5e0..30d6e68f525778b0b915a7006964cf5fcd3a2f8b 100644 --- a/cpp/src/metrics/Metrics.h +++ b/cpp/src/metrics/Metrics.h @@ -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_; }; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/cpp/src/sdk/CMakeLists.txt b/cpp/src/sdk/CMakeLists.txt index 6c3a68b8b888929f0c8e3c579083d98fafc87015..5d6a1ea465134c22c82434fd192f753a32664695 100644 --- a/cpp/src/sdk/CMakeLists.txt +++ b/cpp/src/sdk/CMakeLists.txt @@ -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) diff --git a/cpp/src/sdk/examples/grpcsimple/src/ClientTest.cpp b/cpp/src/sdk/examples/grpcsimple/src/ClientTest.cpp index abfc92cc0bbd7884fe3d203a315934dcf49a6808..f0f7cf529237738f0a7625b619e0daa21370f57b 100644 --- a/cpp/src/sdk/examples/grpcsimple/src/ClientTest.cpp +++ b/cpp/src/sdk/examples/grpcsimple/src/ClientTest.cpp @@ -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; diff --git a/cpp/src/sdk/grpc/ClientProxy.cpp b/cpp/src/sdk/grpc/ClientProxy.cpp index 56ba1e2de6a6ba8c1a231900c9b7cd137c129b67..2a87f7f92948b5d4ef7fd7bc29524d064e2e6012 100644 --- a/cpp/src/sdk/grpc/ClientProxy.cpp +++ b/cpp/src/sdk/grpc/ClientProxy.cpp @@ -51,7 +51,7 @@ ClientProxy::Connect(const ConnectParam ¶m) { 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); diff --git a/cpp/src/sdk/include/Status.h b/cpp/src/sdk/include/Status.h index 6dbc762c0a2987929827444b31cbb35cfc7f1d07..145d09d47a548eaf981be3bb5ea6387266c31fb0 100644 --- a/cpp/src/sdk/include/Status.h +++ b/cpp/src/sdk/include/Status.h @@ -19,7 +19,6 @@ #pragma once #include -#include /** \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 static Status - OK(Args &&... args) { - return Status(StatusCode::OK, MessageBuilder(std::forward(args)...)); - } - -/** - * @brief Invalid - * - * static Invalid status constructor with a specific message - * - * @param, serveral specific messages - * @return, the status with Invalid. - * - */ -template -static Status -Invalid(Args &&... args) { - return Status(StatusCode::InvalidAgument, - MessageBuilder(std::forward(args)...)); -} - -/** - * @brief Unknown Error - * - * static unknown error status constructor with a specific message - * - * @param, serveral specific messages - * @return, the status with unknown error. - * - */ -template -static Status -UnknownError(Args &&... args) { - return Status(StatusCode::UnknownError, MessageBuilder(std::forward(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 -static Status -NotSupported(Args &&... args) { - return Status(StatusCode::NotSupported, MessageBuilder(std::forward(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 -static void -MessageBuilderRecursive(std::stringstream &stream, Head &&head) { - stream << head; -} + inline void + MoveFrom(Status &s); -template -static void -MessageBuilderRecursive(std::stringstream &stream, Head &&head, Tail &&... tail) { - MessageBuilderRecursive(stream, std::forward(head)); - MessageBuilderRecursive(stream, std::forward(tail)...); -} - -template -static std::string -MessageBuilder(Args &&... args) { - std::stringstream stream; - - MessageBuilderRecursive(stream, std::forward(args)...); +private: + const char *state_ = nullptr; +}; // Status - return stream.str(); -} -}; -} \ No newline at end of file +} //Milvus \ No newline at end of file diff --git a/cpp/src/sdk/interface/Status.cpp b/cpp/src/sdk/interface/Status.cpp index 344d86c12f94c048e7e717425ed05b046825e45d..b04c6581feac1b7bb0ce3960ad84b8ee6e92bc7a 100644 --- a/cpp/src/sdk/interface/Status.cpp +++ b/cpp/src/sdk/interface/Status.cpp @@ -17,126 +17,113 @@ #include "Status.h" +#include + 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; } diff --git a/cpp/src/sdk/util/Exception.h b/cpp/src/sdk/util/Exception.h deleted file mode 100644 index f55b8f063272af1774f352b3c6ca96a54f0f9c0a..0000000000000000000000000000000000000000 --- a/cpp/src/sdk/util/Exception.h +++ /dev/null @@ -1,44 +0,0 @@ -// 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 - -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 diff --git a/cpp/src/server/grpc_impl/GrpcRequestTask.cpp b/cpp/src/server/grpc_impl/GrpcRequestTask.cpp index 8c7697e1dc5a0a9d0afd6a0243749cc0a276c17a..d7b39bea059d48a8d4df20af4e4e583827d94c1b 100644 --- a/cpp/src/server/grpc_impl/GrpcRequestTask.cpp +++ b/cpp/src/server/grpc_impl/GrpcRequestTask.cpp @@ -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 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()); } diff --git a/cpp/src/db/Status.cpp b/cpp/src/utils/Status.cpp similarity index 64% rename from cpp/src/db/Status.cpp rename to cpp/src/utils/Status.cpp index 0bc51ca59e8248b46f472f4ea5aacc6df394a6fa..0232c823f04ac9cebfd5331217f68a5b46455bc6 100644 --- a/cpp/src/db/Status.cpp +++ b/cpp/src/utils/Status.cpp @@ -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 #include -#include -#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(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 diff --git a/cpp/src/db/Status.h b/cpp/src/utils/Status.h similarity index 55% rename from cpp/src/db/Status.h rename to cpp/src/utils/Status.h index e3dae4c0adc3d8eddad1403f2c42c622e063243e..153ff61dc5383c17f0bbe0ceea60fbc82dda7b73 100644 --- a/cpp/src/db/Status.h +++ b/cpp/src/utils/Status.h @@ -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 diff --git a/cpp/unittest/db/CMakeLists.txt b/cpp/unittest/db/CMakeLists.txt index 00b6f64dbf5ceca00660033eb2eb097ca9b23737..29440dcf1a0412a7fc7904030274e5fd74391d16 100644 --- a/cpp/unittest/db/CMakeLists.txt +++ b/cpp/unittest/db/CMakeLists.txt @@ -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) diff --git a/cpp/unittest/db/db_tests.cpp b/cpp/unittest/db/db_tests.cpp index 372dd336d6b5c7aa4fba636c1254d7c68b2a0d45..a1fd14a8f410e3a671674b34c1d69ab7b0f0a257 100644 --- a/cpp/unittest/db/db_tests.cpp +++ b/cpp/unittest/db/db_tests.cpp @@ -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 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 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; diff --git a/cpp/unittest/db/mem_test.cpp b/cpp/unittest/db/mem_test.cpp index 73f6d89f5e81c62ac51d8bd794d856de030dd5dd..4e958795ad84461ced5030aadf95cd7baf14c85f 100644 --- a/cpp/unittest/db/mem_test.cpp +++ b/cpp/unittest/db/mem_test.cpp @@ -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 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 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; diff --git a/cpp/unittest/db/misc_test.cpp b/cpp/unittest/db/misc_test.cpp index 28ec0f2f9c93dc04ba8e710862c2533f491dcaf9..ff530b0a734c5a172aaee1bd9f0e3ed1913a816c 100644 --- a/cpp/unittest/db/misc_test.cpp +++ b/cpp/unittest/db/misc_test.cpp @@ -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("$$##"); diff --git a/cpp/unittest/db/mysql_db_test.cpp b/cpp/unittest/db/mysql_db_test.cpp index 35f086b0338be38134c25fc54cb1b83313a6e5be..9d99b4095becd4528ab7f2a19e6a0114eaf77c8a 100644 --- a/cpp/unittest/db/mysql_db_test.cpp +++ b/cpp/unittest/db/mysql_db_test.cpp @@ -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 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; diff --git a/cpp/unittest/metrics/CMakeLists.txt b/cpp/unittest/metrics/CMakeLists.txt index 07f27e1e67ef443f688e1398ab44b137f2e9e5f3..dddba2906a7129284fcb04ac6a644f45028ce5d7 100644 --- a/cpp/unittest/metrics/CMakeLists.txt +++ b/cpp/unittest/metrics/CMakeLists.txt @@ -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) diff --git a/cpp/unittest/metrics/metrics_test.cpp b/cpp/unittest/metrics/metrics_test.cpp index 2bd06ed091dce45f3079b8f2be9b3d9095befc8f..636e2ffbb1950d74a6f64a4cf3b11cfb450a14e8 100644 --- a/cpp/unittest/metrics/metrics_test.cpp +++ b/cpp/unittest/metrics/metrics_test.cpp @@ -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); diff --git a/cpp/unittest/metrics/utils.h b/cpp/unittest/metrics/utils.h index ef25654ed034003f38198f9bc9e05ea5937c57ee..7f093dd0913ab10feb1fa0c9f8914a2e17916d4e 100644 --- a/cpp/unittest/metrics/utils.h +++ b/cpp/unittest/metrics/utils.h @@ -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: diff --git a/cpp/unittest/scheduler/CMakeLists.txt b/cpp/unittest/scheduler/CMakeLists.txt index 9ada727f8eff6d0cbc14a2d628e4bf5d6bc445d5..7359420e16819bd66f37549714e3a374a1bc86d1 100644 --- a/cpp/unittest/scheduler/CMakeLists.txt +++ b/cpp/unittest/scheduler/CMakeLists.txt @@ -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} diff --git a/cpp/unittest/server/CMakeLists.txt b/cpp/unittest/server/CMakeLists.txt index 6bbe2183ce604f475c9d66dc85c36a7f26795858..8e0685992992c9145eb5b6c7cc09519df81aea2c 100644 --- a/cpp/unittest/server/CMakeLists.txt +++ b/cpp/unittest/server/CMakeLists.txt @@ -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} diff --git a/cpp/unittest/server/util_test.cpp b/cpp/unittest/server/util_test.cpp index 3849f79d19a965fdd1a1078ef4bf4faeeefc3f24..cfbcbab683320fa64bc0ffb315023fbe5bd09139 100644 --- a/cpp/unittest/server/util_test.cpp +++ b/cpp/unittest/server/util_test.cpp @@ -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);