提交 a42ade88 编写于 作者: G groot

format utils code


Former-commit-id: ce1b8b9fca25a8df2634600e9ec30b4fcbe8e20c
上级 c1d5f4c8
......@@ -29,8 +29,9 @@ namespace server {
template<typename T>
class BlockingQueue {
public:
BlockingQueue() : mtx(), full_(), empty_() {}
public:
BlockingQueue() : mtx(), full_(), empty_() {
}
BlockingQueue(const BlockingQueue &rhs) = delete;
......@@ -50,7 +51,7 @@ public:
void SetCapacity(const size_t capacity);
private:
private:
mutable std::mutex mtx;
std::condition_variable full_;
std::condition_variable empty_;
......@@ -58,9 +59,8 @@ private:
size_t capacity_ = 32;
};
}
}
}
} // namespace server
} // namespace milvus
} // namespace zilliz
#include "./BlockingQueue.inl"
......@@ -18,7 +18,6 @@
#pragma once
namespace zilliz {
namespace milvus {
namespace server {
......@@ -26,8 +25,10 @@ namespace server {
template<typename T>
void
BlockingQueue<T>::Put(const T &task) {
std::unique_lock <std::mutex> lock(mtx);
full_.wait(lock, [this] { return (queue_.size() < capacity_); });
std::unique_lock<std::mutex> lock(mtx);
full_.wait(lock, [this] {
return (queue_.size() < capacity_);
});
queue_.push(task);
empty_.notify_all();
......@@ -36,8 +37,10 @@ BlockingQueue<T>::Put(const T &task) {
template<typename T>
T
BlockingQueue<T>::Take() {
std::unique_lock <std::mutex> lock(mtx);
empty_.wait(lock, [this] { return !queue_.empty(); });
std::unique_lock<std::mutex> lock(mtx);
empty_.wait(lock, [this] {
return !queue_.empty();
});
T front(queue_.front());
queue_.pop();
......@@ -48,15 +51,17 @@ BlockingQueue<T>::Take() {
template<typename T>
size_t
BlockingQueue<T>::Size() {
std::lock_guard <std::mutex> lock(mtx);
std::lock_guard<std::mutex> lock(mtx);
return queue_.size();
}
template<typename T>
T
BlockingQueue<T>::Front() {
std::unique_lock <std::mutex> lock(mtx);
empty_.wait(lock, [this] { return !queue_.empty(); });
std::unique_lock<std::mutex> lock(mtx);
empty_.wait(lock, [this] {
return !queue_.empty();
});
T front(queue_.front());
return front;
......@@ -65,8 +70,10 @@ BlockingQueue<T>::Front() {
template<typename T>
T
BlockingQueue<T>::Back() {
std::unique_lock <std::mutex> lock(mtx);
empty_.wait(lock, [this] { return !queue_.empty(); });
std::unique_lock<std::mutex> lock(mtx);
empty_.wait(lock, [this] {
return !queue_.empty();
});
T back(queue_.back());
return back;
......@@ -75,7 +82,7 @@ BlockingQueue<T>::Back() {
template<typename T>
bool
BlockingQueue<T>::Empty() {
std::unique_lock <std::mutex> lock(mtx);
std::unique_lock<std::mutex> lock(mtx);
return queue_.empty();
}
......@@ -85,7 +92,7 @@ BlockingQueue<T>::SetCapacity(const size_t capacity) {
capacity_ = (capacity > 0 ? capacity : capacity_);
}
}
}
}
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.
#include "CommonUtil.h"
#include "utils/CommonUtil.h"
#include "utils/Log.h"
#include <unistd.h>
......@@ -44,7 +44,8 @@ namespace server {
namespace fs = boost::filesystem;
bool CommonUtil::GetSystemMemInfo(unsigned long &total_mem, unsigned long &free_mem) {
bool
CommonUtil::GetSystemMemInfo(uint64_t &total_mem, uint64_t &free_mem) {
struct sysinfo info;
int ret = sysinfo(&info);
total_mem = info.totalram;
......@@ -53,7 +54,8 @@ bool CommonUtil::GetSystemMemInfo(unsigned long &total_mem, unsigned long &free_
return ret == 0;//succeed 0, failed -1
}
bool CommonUtil::GetSystemAvailableThreads(unsigned int &thread_count) {
bool
CommonUtil::GetSystemAvailableThreads(uint32_t &thread_count) {
//threadCnt = std::thread::hardware_concurrency();
thread_count = sysconf(_SC_NPROCESSORS_CONF);
thread_count *= THREAD_MULTIPLY_CPU;
......@@ -63,7 +65,8 @@ bool CommonUtil::GetSystemAvailableThreads(unsigned int &thread_count) {
return true;
}
bool CommonUtil::IsDirectoryExist(const std::string &path) {
bool
CommonUtil::IsDirectoryExist(const std::string &path) {
DIR *dp = nullptr;
if ((dp = opendir(path.c_str())) == nullptr) {
return false;
......@@ -73,8 +76,9 @@ bool CommonUtil::IsDirectoryExist(const std::string &path) {
return true;
}
Status CommonUtil::CreateDirectory(const std::string &path) {
if(path.empty()) {
Status
CommonUtil::CreateDirectory(const std::string &path) {
if (path.empty()) {
return Status::OK();
}
......@@ -87,7 +91,7 @@ Status CommonUtil::CreateDirectory(const std::string &path) {
fs::path fs_path(path);
fs::path parent_path = fs_path.parent_path();
Status err_status = CreateDirectory(parent_path.string());
if(!err_status.ok()){
if (!err_status.ok()) {
return err_status;
}
......@@ -96,7 +100,7 @@ Status CommonUtil::CreateDirectory(const std::string &path) {
return Status::OK();//already exist
}
int makeOK = mkdir(path.c_str(), S_IRWXU|S_IRGRP|S_IROTH);
int makeOK = mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IROTH);
if (makeOK != 0) {
return Status(SERVER_UNEXPECTED_ERROR, "failed to create directory: " + path);
}
......@@ -105,37 +109,38 @@ Status CommonUtil::CreateDirectory(const std::string &path) {
}
namespace {
void RemoveDirectory(const std::string &path) {
DIR *dir = nullptr;
struct dirent *dmsg;
char file_name[256];
char folder_name[256];
strcpy(folder_name, path.c_str());
strcat(folder_name, "/%s");
if ((dir = opendir(path.c_str())) != nullptr) {
while ((dmsg = readdir(dir)) != nullptr) {
if (strcmp(dmsg->d_name, ".") != 0
&& strcmp(dmsg->d_name, "..") != 0) {
sprintf(file_name, folder_name, dmsg->d_name);
std::string tmp = file_name;
if (tmp.find(".") == std::string::npos) {
RemoveDirectory(file_name);
}
remove(file_name);
void
RemoveDirectory(const std::string &path) {
DIR *dir = nullptr;
struct dirent *dmsg;
const int32_t buf_size = 256;
char file_name[buf_size];
std::string folder_name = path + "/%s";
if ((dir = opendir(path.c_str())) != nullptr) {
while ((dmsg = readdir(dir)) != nullptr) {
if (strcmp(dmsg->d_name, ".") != 0
&& strcmp(dmsg->d_name, "..") != 0) {
snprintf(file_name, buf_size, folder_name.c_str(), dmsg->d_name);
std::string tmp = file_name;
if (tmp.find(".") == std::string::npos) {
RemoveDirectory(file_name);
}
remove(file_name);
}
}
}
if (dir != nullptr) {
closedir(dir);
}
remove(path.c_str());
if (dir != nullptr) {
closedir(dir);
}
remove(path.c_str());
}
} // namespace
Status CommonUtil::DeleteDirectory(const std::string &path) {
if(path.empty()) {
Status
CommonUtil::DeleteDirectory(const std::string &path) {
if (path.empty()) {
return Status::OK();
}
......@@ -149,58 +154,63 @@ Status CommonUtil::DeleteDirectory(const std::string &path) {
return Status::OK();
}
bool CommonUtil::IsFileExist(const std::string &path) {
bool
CommonUtil::IsFileExist(const std::string &path) {
return (access(path.c_str(), F_OK) == 0);
}
uint64_t CommonUtil::GetFileSize(const std::string &path) {
uint64_t
CommonUtil::GetFileSize(const std::string &path) {
struct stat file_info;
if (stat(path.c_str(), &file_info) < 0) {
return 0;
} else {
return (uint64_t)file_info.st_size;
return (uint64_t) file_info.st_size;
}
}
std::string CommonUtil::GetFileName(std::string filename) {
std::string
CommonUtil::GetFileName(std::string filename) {
int pos = filename.find_last_of('/');
return filename.substr(pos + 1);
}
std::string CommonUtil::GetExePath() {
std::string
CommonUtil::GetExePath() {
const size_t buf_len = 1024;
char buf[buf_len];
size_t cnt = readlink("/proc/self/exe", buf, buf_len);
if(cnt < 0|| cnt >= buf_len) {
if (cnt < 0 || cnt >= buf_len) {
return "";
}
buf[cnt] = '\0';
std::string exe_path = buf;
if(exe_path.rfind('/') != exe_path.length()){
if (exe_path.rfind('/') != exe_path.length()) {
std::string sub_str = exe_path.substr(0, exe_path.rfind('/'));
return sub_str + "/";
}
return exe_path;
}
bool CommonUtil::TimeStrToTime(const std::string& time_str,
time_t &time_integer,
tm &time_struct,
const std::string& format) {
bool
CommonUtil::TimeStrToTime(const std::string &time_str,
time_t &time_integer,
tm &time_struct,
const std::string &format) {
time_integer = 0;
memset(&time_struct, 0, sizeof(tm));
int ret = sscanf(time_str.c_str(),
format.c_str(),
&(time_struct.tm_year),
&(time_struct.tm_mon),
&(time_struct.tm_mday),
&(time_struct.tm_hour),
&(time_struct.tm_min),
&(time_struct.tm_sec));
if(ret <= 0) {
format.c_str(),
&(time_struct.tm_year),
&(time_struct.tm_mon),
&(time_struct.tm_mday),
&(time_struct.tm_hour),
&(time_struct.tm_min),
&(time_struct.tm_sec));
if (ret <= 0) {
return false;
}
......@@ -211,15 +221,16 @@ bool CommonUtil::TimeStrToTime(const std::string& time_str,
return true;
}
void CommonUtil::ConvertTime(time_t time_integer, tm &time_struct) {
tm* t_m = localtime (&time_integer);
memcpy(&time_struct, t_m, sizeof(tm));
void
CommonUtil::ConvertTime(time_t time_integer, tm &time_struct) {
localtime_r(&time_integer, &time_struct);
}
void CommonUtil::ConvertTime(tm time_struct, time_t &time_integer) {
void
CommonUtil::ConvertTime(tm time_struct, time_t &time_integer) {
time_integer = mktime(&time_struct);
}
}
}
}
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -22,15 +22,14 @@
#include <string>
#include <time.h>
namespace zilliz {
namespace milvus {
namespace server {
class CommonUtil {
public:
static bool GetSystemMemInfo(unsigned long &total_mem, unsigned long &free_mem);
static bool GetSystemAvailableThreads(unsigned int &thread_count);
static bool GetSystemMemInfo(uint64_t &total_mem, uint64_t &free_mem);
static bool GetSystemAvailableThreads(uint32_t &thread_count);
static bool IsFileExist(const std::string &path);
static uint64_t GetFileSize(const std::string &path);
......@@ -41,16 +40,16 @@ class CommonUtil {
static std::string GetFileName(std::string filename);
static std::string GetExePath();
static bool TimeStrToTime(const std::string& time_str,
time_t &time_integer,
tm &time_struct,
const std::string& format = "%d-%d-%d %d:%d:%d");
static bool TimeStrToTime(const std::string &time_str,
time_t &time_integer,
tm &time_struct,
const std::string &format = "%d-%d-%d %d:%d:%d");
static void ConvertTime(time_t time_integer, tm &time_struct);
static void ConvertTime(tm time_struct, time_t &time_integer);
};
}
}
}
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -28,6 +28,7 @@ using ErrorCode = int32_t;
constexpr ErrorCode SERVER_SUCCESS = 0;
constexpr ErrorCode SERVER_ERROR_CODE_BASE = 0x30000;
constexpr ErrorCode
ToServerErrorCode(const ErrorCode error_code) {
return SERVER_ERROR_CODE_BASE + error_code;
......@@ -35,6 +36,7 @@ ToServerErrorCode(const ErrorCode error_code) {
constexpr ErrorCode DB_SUCCESS = 0;
constexpr ErrorCode DB_ERROR_CODE_BASE = 0x40000;
constexpr ErrorCode
ToDbErrorCode(const ErrorCode error_code) {
return DB_ERROR_CODE_BASE + error_code;
......@@ -42,6 +44,7 @@ ToDbErrorCode(const ErrorCode error_code) {
constexpr ErrorCode KNOWHERE_SUCCESS = 0;
constexpr ErrorCode KNOWHERE_ERROR_CODE_BASE = 0x50000;
constexpr ErrorCode
ToKnowhereErrorCode(const ErrorCode error_code) {
return KNOWHERE_ERROR_CODE_BASE + error_code;
......@@ -96,26 +99,27 @@ constexpr ErrorCode KNOWHERE_UNEXPECTED_ERROR = ToKnowhereErrorCode(3);
constexpr ErrorCode KNOWHERE_NO_SPACE = ToKnowhereErrorCode(4);
namespace server {
class ServerException : public std::exception {
public:
ServerException(ErrorCode error_code,
const std::string &message = std::string())
: error_code_(error_code), message_(message) {}
public:
ErrorCode error_code() const {
return error_code_;
}
virtual const char *what() const noexcept {
return message_.c_str();
}
private:
ErrorCode error_code_;
std::string message_;
};
}
class ServerException : public std::exception {
public:
ServerException(ErrorCode error_code,
const std::string &message = std::string())
: error_code_(error_code), message_(message) {
}
public:
ErrorCode error_code() const {
return error_code_;
}
virtual const char *what() const noexcept {
return message_.c_str();
}
private:
ErrorCode error_code_;
std::string message_;
};
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -26,17 +26,17 @@ namespace zilliz {
namespace milvus {
class Exception : public std::exception {
public:
Exception(ErrorCode code, const std::string& message)
public:
Exception(ErrorCode code, const std::string &message)
: code_(code),
message_(message) {
}
}
ErrorCode code() const throw() {
return code_;
}
virtual const char* what() const throw() {
virtual const char *what() const throw() {
if (message_.empty()) {
return "Default Exception.";
} else {
......@@ -44,24 +44,23 @@ public:
}
}
virtual ~Exception() throw() {};
virtual ~Exception() throw() {
}
protected:
protected:
ErrorCode code_;
std::string message_;
};
class InvalidArgumentException : public Exception {
public:
public:
InvalidArgumentException()
: Exception(SERVER_INVALID_ARGUMENT, "Invalid Argument") {
}
};
InvalidArgumentException(const std::string& message)
explicit InvalidArgumentException(const std::string &message)
: Exception(SERVER_INVALID_ARGUMENT, message) {
};
}
};
} // namespace milvus
......
......@@ -17,7 +17,7 @@
#pragma once
#include "easylogging++.h"
#include "utils/easylogging++.h"
namespace zilliz {
namespace milvus {
......
......@@ -15,12 +15,12 @@
// specific language governing permissions and limitations
// under the License.
#include "utils/LogUtil.h"
#include <ctype.h>
#include <string>
#include <libgen.h>
#include "LogUtil.h"
namespace zilliz {
namespace milvus {
namespace server {
......@@ -35,7 +35,8 @@ static int fatal_idx = 0;
}
// TODO(yzb) : change the easylogging library to get the log level from parameter rather than filename
void RolloutHandler(const char *filename, std::size_t size, el::Level level) {
void
RolloutHandler(const char *filename, std::size_t size, el::Level level) {
char *dirc = strdup(filename);
char *basec = strdup(filename);
char *dir = dirname(dirc);
......@@ -80,7 +81,8 @@ void RolloutHandler(const char *filename, std::size_t size, el::Level level) {
}
}
Status InitLog(const std::string &log_config_file) {
Status
InitLog(const std::string &log_config_file) {
el::Configurations conf(log_config_file);
el::Loggers::reconfigureAllLoggers(conf);
......@@ -91,7 +93,6 @@ Status InitLog(const std::string &log_config_file) {
return Status::OK();
}
} // server
} // milvus
} // zilliz
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -18,18 +18,20 @@
#pragma once
#include "utils/Status.h"
#include "utils/easylogging++.h"
#include <string>
#include <sstream>
#include "easylogging++.h"
namespace zilliz {
namespace milvus {
namespace server {
Status InitLog(const std::string& log_config_file);
Status
InitLog(const std::string &log_config_file);
void RolloutHandler(const char *filename, std::size_t size, el::Level level);
void
RolloutHandler(const char *filename, std::size_t size, el::Level level);
#define SHOW_LOCATION
#ifdef SHOW_LOCATION
......@@ -38,6 +40,6 @@ void RolloutHandler(const char *filename, std::size_t size, el::Level level);
#define LOCATION_INFO ""
#endif
}
}
}
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -15,20 +15,20 @@
// specific language governing permissions and limitations
// under the License.
#include "SignalUtil.h"
#include "utils/SignalUtil.h"
#include "src/server/Server.h"
#include "utils/Log.h"
#include <string>
#include <signal.h>
#include <execinfo.h>
namespace zilliz {
namespace milvus {
namespace server {
void SignalUtil::HandleSignal(int signum) {
void
SignalUtil::HandleSignal(int signum) {
switch (signum) {
case SIGINT:
case SIGUSR2: {
......@@ -51,7 +51,8 @@ void SignalUtil::HandleSignal(int signum) {
}
}
void SignalUtil::PrintStacktrace() {
void
SignalUtil::PrintStacktrace() {
SERVER_LOG_INFO << "Call stack:";
const int size = 32;
......@@ -65,6 +66,6 @@ void SignalUtil::PrintStacktrace() {
free(stacktrace);
}
}
}
}
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -27,6 +27,6 @@ class SignalUtil {
static void PrintStacktrace();
};
}
}
}
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -14,7 +14,8 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "Status.h"
#include "utils/Status.h"
#include <cstring>
......@@ -23,12 +24,12 @@ namespace milvus {
constexpr int CODE_WIDTH = sizeof(StatusCode);
Status::Status(StatusCode code, const std::string& msg) {
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();
char* result = new char[length + sizeof(length) + CODE_WIDTH];
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);
......@@ -38,7 +39,6 @@ Status::Status(StatusCode code, const std::string& msg) {
Status::Status()
: state_(nullptr) {
}
Status::~Status() {
......@@ -50,7 +50,7 @@ Status::Status(const Status &s)
CopyFrom(s);
}
Status&
Status &
Status::operator=(const Status &s) {
CopyFrom(s);
return *this;
......@@ -61,7 +61,7 @@ Status::Status(Status &&s)
MoveFrom(s);
}
Status&
Status &
Status::operator=(Status &&s) {
MoveFrom(s);
return *this;
......@@ -71,7 +71,7 @@ void
Status::CopyFrom(const Status &s) {
delete state_;
state_ = nullptr;
if(s.state_ == nullptr) {
if (s.state_ == nullptr) {
return;
}
......@@ -79,7 +79,7 @@ Status::CopyFrom(const Status &s) {
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);
memcpy((void *) state_, (void *) s.state_, buff_len);
}
void
......@@ -98,7 +98,7 @@ Status::message() const {
std::string msg;
uint32_t length = 0;
memcpy(&length, state_ + CODE_WIDTH, sizeof(length));
if(length > 0) {
if (length > 0) {
msg.append(state_ + sizeof(length) + CODE_WIDTH, length);
}
......@@ -113,26 +113,19 @@ Status::ToString() const {
std::string result;
switch (code()) {
case DB_SUCCESS:
result = "OK ";
case DB_SUCCESS:result = "OK ";
break;
case DB_ERROR:
result = "Error: ";
case DB_ERROR:result = "Error: ";
break;
case DB_META_TRANSACTION_FAILED:
result = "Database error: ";
case DB_META_TRANSACTION_FAILED:result = "Database error: ";
break;
case DB_NOT_FOUND:
result = "Not found: ";
case DB_NOT_FOUND:result = "Not found: ";
break;
case DB_ALREADY_EXIST:
result = "Already exist: ";
case DB_ALREADY_EXIST:result = "Already exist: ";
break;
case DB_INVALID_PATH:
result = "Invalid path: ";
case DB_INVALID_PATH:result = "Invalid path: ";
break;
default:
result = "Error code(" + std::to_string(code()) + "): ";
default:result = "Error code(" + std::to_string(code()) + "): ";
break;
}
......
......@@ -44,14 +44,18 @@ class Status {
operator=(Status &&s);
static Status
OK() { return Status(); }
OK() {
return Status();
}
bool
ok() const { return state_ == nullptr || code() == 0; }
ok() const {
return state_ == nullptr || code() == 0;
}
StatusCode
code() const {
return (state_ == nullptr) ? 0 : *(StatusCode*)(state_);
return (state_ == nullptr) ? 0 : *(StatusCode *) (state_);
}
std::string
......@@ -60,14 +64,14 @@ class Status {
std::string
ToString() const;
private:
private:
inline void
CopyFrom(const Status &s);
inline void
MoveFrom(Status &s);
private:
private:
const char *state_ = nullptr;
}; // Status
......
......@@ -15,13 +15,16 @@
// specific language governing permissions and limitations
// under the License.
#include "StringHelpFunctions.h"
#include "utils/StringHelpFunctions.h"
#include <string>
namespace zilliz {
namespace milvus {
namespace server {
void StringHelpFunctions::TrimStringBlank(std::string &string) {
void
StringHelpFunctions::TrimStringBlank(std::string &string) {
if (!string.empty()) {
static std::string s_format(" \n\r\t");
string.erase(0, string.find_first_not_of(s_format));
......@@ -29,17 +32,19 @@ void StringHelpFunctions::TrimStringBlank(std::string &string) {
}
}
void StringHelpFunctions::TrimStringQuote(std::string &string, const std::string &qoute) {
void
StringHelpFunctions::TrimStringQuote(std::string &string, const std::string &qoute) {
if (!string.empty()) {
string.erase(0, string.find_first_not_of(qoute));
string.erase(string.find_last_not_of(qoute) + 1);
}
}
Status StringHelpFunctions::SplitStringByDelimeter(const std::string &str,
const std::string &delimeter,
std::vector<std::string> &result) {
if(str.empty()) {
Status
StringHelpFunctions::SplitStringByDelimeter(const std::string &str,
const std::string &delimeter,
std::vector<std::string> &result) {
if (str.empty()) {
return Status::OK();
}
......@@ -58,10 +63,11 @@ Status StringHelpFunctions::SplitStringByDelimeter(const std::string &str,
return Status::OK();
}
Status StringHelpFunctions::SplitStringByQuote(const std::string &str,
const std::string &delimeter,
const std::string &quote,
std::vector<std::string> &result) {
Status
StringHelpFunctions::SplitStringByQuote(const std::string &str,
const std::string &delimeter,
const std::string &quote,
std::vector<std::string> &result) {
if (quote.empty()) {
return SplitStringByDelimeter(str, delimeter, result);
}
......@@ -120,6 +126,6 @@ Status StringHelpFunctions::SplitStringByQuote(const std::string &str,
return Status::OK();
}
}
}
}
\ No newline at end of file
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -20,16 +20,17 @@
#include "utils/Status.h"
#include <vector>
#include <string>
namespace zilliz {
namespace milvus {
namespace server {
class StringHelpFunctions {
private:
private:
StringHelpFunctions() = default;
public:
public:
static void TrimStringBlank(std::string &string);
static void TrimStringQuote(std::string &string, const std::string &qoute);
......@@ -56,9 +57,8 @@ public:
const std::string &delimeter,
const std::string &quote,
std::vector<std::string> &result);
};
}
}
}
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -26,7 +26,7 @@
#include <future>
#include <functional>
#include <stdexcept>
#include <utility>
#define MAX_THREADS_NUM 32
......@@ -34,8 +34,8 @@ namespace zilliz {
namespace milvus {
class ThreadPool {
public:
ThreadPool(size_t threads, size_t queue_size = 1000);
public:
explicit ThreadPool(size_t threads, size_t queue_size = 1000);
template<class F, class... Args>
auto enqueue(F &&f, Args &&... args)
......@@ -43,7 +43,7 @@ public:
~ThreadPool();
private:
private:
// need to keep track of threads so we can join them
std::vector<std::thread> workers_;
......@@ -60,53 +60,57 @@ private:
bool stop;
};
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads, size_t queue_size)
: max_queue_size_(queue_size), stop(false) {
: max_queue_size_(queue_size), stop(false) {
for (size_t i = 0; i < threads; ++i)
workers_.emplace_back(
[this] {
for (;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex_);
this->condition_.wait(lock,
[this] { return this->stop || !this->tasks_.empty(); });
if (this->stop && this->tasks_.empty())
return;
task = std::move(this->tasks_.front());
this->tasks_.pop();
}
this->condition_.notify_all();
task();
[this] {
for (;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex_);
this->condition_.wait(lock,
[this] {
return this->stop || !this->tasks_.empty();
});
if (this->stop && this->tasks_.empty())
return;
task = std::move(this->tasks_.front());
this->tasks_.pop();
}
this->condition_.notify_all();
task();
}
);
});
}
// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F &&f, Args &&... args)
auto
ThreadPool::enqueue(F &&f, Args &&... args)
-> std::future<typename std::result_of<F(Args...)>::type> {
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared<std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::bind(std::forward<F>(f), std::forward<Args>(args)...));
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex_);
this->condition_.wait(lock,
[this] { return this->tasks_.size() < max_queue_size_; });
[this] {
return this->tasks_.size() < max_queue_size_;
});
// don't allow enqueueing after stopping the pool
if (stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks_.emplace([task]() { (*task)(); });
tasks_.emplace([task]() {
(*task)();
});
}
condition_.notify_all();
return res;
......@@ -119,10 +123,11 @@ inline ThreadPool::~ThreadPool() {
stop = true;
}
condition_.notify_all();
for (std::thread &worker: workers_)
for (std::thread &worker : workers_) {
worker.join();
}
}
}
}
} // namespace milvus
} // namespace zilliz
......@@ -15,10 +15,9 @@
// specific language governing permissions and limitations
// under the License.
#include "TimeRecorder.h"
#include "utils/TimeRecorder.h"
#include "utils/Log.h"
namespace zilliz {
namespace milvus {
......@@ -100,5 +99,5 @@ TimeRecorder::ElapseFromBegin(const std::string &msg) {
return span;
}
}
}
} // namespace milvus
} // namespace zilliz
......@@ -20,14 +20,13 @@
#include <string>
#include <chrono>
namespace zilliz {
namespace milvus {
class TimeRecorder {
using stdclock = std::chrono::high_resolution_clock;
public:
public:
TimeRecorder(const std::string &header,
int64_t log_level = 1);
......@@ -39,15 +38,15 @@ public:
static std::string GetTimeSpanStr(double span);
private:
private:
void PrintTimeRecord(const std::string &msg, double span);
private:
private:
std::string header_;
stdclock::time_point start_;
stdclock::time_point last_;
int64_t log_level_;
};
}
}
} // namespace milvus
} // namespace zilliz
......@@ -16,16 +16,16 @@
// under the License.
#include "utils/ValidationUtil.h"
#include "db/engine/ExecutionEngine.h"
#include "ValidationUtil.h"
#include "Log.h"
#include <string>
#include <cuda_runtime.h>
#include <arpa/inet.h>
#include <regex>
#include <algorithm>
namespace zilliz {
namespace milvus {
namespace server {
......@@ -36,7 +36,6 @@ constexpr int32_t INDEX_FILE_SIZE_LIMIT = 4096; //index trigger size max = 4096
Status
ValidationUtil::ValidateTableName(const std::string &table_name) {
// Table name shouldn't be empty.
if (table_name.empty()) {
std::string msg = "Empty table name";
......@@ -78,8 +77,7 @@ ValidationUtil::ValidateTableDimension(int64_t dimension) {
std::string msg = "Table dimension excceed the limitation: " + std::to_string(TABLE_DIMENSION_LIMIT);
SERVER_LOG_ERROR << msg;
return Status(SERVER_INVALID_VECTOR_DIMENSION, msg);
}
else {
} else {
return Status::OK();
}
}
......@@ -185,7 +183,6 @@ ValidationUtil::GetGpuMemory(uint32_t gpu_index, size_t &memory) {
Status
ValidationUtil::ValidateIpAddress(const std::string &ip_address) {
struct in_addr address;
int result = inet_pton(AF_INET, ip_address.c_str(), &address);
......@@ -212,7 +209,7 @@ ValidationUtil::ValidateStringIsNumber(const std::string &str) {
}
try {
int32_t value = std::stoi(str);
} catch(...) {
} catch (...) {
return Status(SERVER_INVALID_ARGUMENT, "Invalid number");
}
return Status::OK();
......@@ -226,8 +223,7 @@ ValidationUtil::ValidateStringIsBool(const std::string &str) {
s == "false" || s == "off" || s == "no" || s == "0" ||
s.empty()) {
return Status::OK();
}
else {
} else {
return Status(SERVER_INVALID_ARGUMENT, "Invalid boolean: " + str);
}
}
......@@ -236,7 +232,7 @@ Status
ValidationUtil::ValidateStringIsFloat(const std::string &str) {
try {
float val = std::stof(str);
} catch(...) {
} catch (...) {
return Status(SERVER_INVALID_ARGUMENT, "Invalid float: " + str);
}
return Status::OK();
......@@ -289,8 +285,7 @@ ValidationUtil::ValidateDbURI(const std::string &uri) {
okay = false;
}
}
}
else {
} else {
SERVER_LOG_ERROR << "Wrong URI format: URI = " << uri;
okay = false;
}
......@@ -298,6 +293,6 @@ ValidationUtil::ValidateDbURI(const std::string &uri) {
return (okay ? Status::OK() : Status(SERVER_INVALID_ARGUMENT, "Invalid db backend uri"));
}
}
}
}
\ No newline at end of file
} // namespace server
} // namespace milvus
} // namespace zilliz
......@@ -21,15 +21,17 @@
#include "db/meta/MetaTypes.h"
#include "utils/Status.h"
#include <string>
namespace zilliz {
namespace milvus {
namespace server {
class ValidationUtil {
private:
private:
ValidationUtil() = default;
public:
public:
static Status
ValidateTableName(const std::string &table_name);
......@@ -49,10 +51,10 @@ public:
ValidateTableIndexMetricType(int32_t metric_type);
static Status
ValidateSearchTopk(int64_t top_k, const engine::meta::TableSchema& table_schema);
ValidateSearchTopk(int64_t top_k, const engine::meta::TableSchema &table_schema);
static Status
ValidateSearchNprobe(int64_t nprobe, const engine::meta::TableSchema& table_schema);
ValidateSearchNprobe(int64_t nprobe, const engine::meta::TableSchema &table_schema);
static Status
ValidateGpuIndex(uint32_t gpu_index);
......@@ -76,6 +78,6 @@ public:
ValidateDbURI(const std::string &uri);
};
}
}
}
\ No newline at end of file
} // namespace server
} // namespace milvus
} // namespace zilliz
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册