diff --git a/cpp/src/cache/Cache.h b/cpp/src/cache/Cache.h index b2a57ebdfb2d8c38e37e04cc01521127cd4de31e..b526e9339f7197ddc7f75d1c0384c48b23878c6c 100644 --- a/cpp/src/cache/Cache.h +++ b/cpp/src/cache/Cache.h @@ -32,30 +32,40 @@ namespace cache { template class Cache { -public: + public: //mem_capacity, units:GB Cache(int64_t capacity_gb, uint64_t cache_max_count); ~Cache() = default; - int64_t usage() const { return usage_; } - int64_t capacity() const { return capacity_; } //unit: BYTE + int64_t usage() const { + return usage_; + } + + int64_t capacity() const { + return capacity_; + } //unit: BYTE void set_capacity(int64_t capacity); //unit: BYTE - double freemem_percent() const { return freemem_percent_; }; - void set_freemem_percent(double percent) { freemem_percent_ = percent; } + double freemem_percent() const { + return freemem_percent_; + } + + void set_freemem_percent(double percent) { + freemem_percent_ = percent; + } size_t size() const; - bool exists(const std::string& key); - ItemObj get(const std::string& key); - void insert(const std::string& key, const ItemObj& item); - void erase(const std::string& key); + bool exists(const std::string &key); + ItemObj get(const std::string &key); + void insert(const std::string &key, const ItemObj &item); + void erase(const std::string &key); void print(); void clear(); -private: + private: void free_memory(); -private: + private: int64_t usage_; int64_t capacity_; double freemem_percent_; @@ -64,8 +74,8 @@ private: mutable std::mutex mutex_; }; -} // cache -} // milvus -} // zilliz +} // namespace cache +} // namespace milvus +} // namespace zilliz -#include "cache/Cache.inl" \ No newline at end of file +#include "cache/Cache.inl" diff --git a/cpp/src/cache/Cache.inl b/cpp/src/cache/Cache.inl index 3eaffc45568debd547fa33810d4ccd4c955e8578..ebd8f3c25d2b23fd5adce6e3d50e36efb26a7e0e 100644 --- a/cpp/src/cache/Cache.inl +++ b/cpp/src/cache/Cache.inl @@ -33,29 +33,33 @@ Cache::Cache(int64_t capacity, uint64_t cache_max_count) } template -void Cache::set_capacity(int64_t capacity) { - if(capacity > 0) { +void +Cache::set_capacity(int64_t capacity) { + if (capacity > 0) { capacity_ = capacity; free_memory(); } } template -size_t Cache::size() const { +size_t +Cache::size() const { std::lock_guard lock(mutex_); return lru_.size(); } template -bool Cache::exists(const std::string& key) { +bool +Cache::exists(const std::string &key) { std::lock_guard lock(mutex_); return lru_.exists(key); } template -ItemObj Cache::get(const std::string& key) { +ItemObj +Cache::get(const std::string &key) { std::lock_guard lock(mutex_); - if(!lru_.exists(key)){ + if (!lru_.exists(key)) { return nullptr; } @@ -63,8 +67,9 @@ ItemObj Cache::get(const std::string& key) { } template -void Cache::insert(const std::string& key, const ItemObj& item) { - if(item == nullptr) { +void +Cache::insert(const std::string &key, const ItemObj &item) { + if (item == nullptr) { return; } @@ -80,7 +85,7 @@ void Cache::insert(const std::string& key, const ItemObj& item) { //if key already exist, subtract old item size if (lru_.exists(key)) { - const ItemObj& old_item = lru_.get(key); + const ItemObj &old_item = lru_.get(key); usage_ -= old_item->size(); } @@ -107,13 +112,14 @@ void Cache::insert(const std::string& key, const ItemObj& item) { } template -void Cache::erase(const std::string& key) { +void +Cache::erase(const std::string &key) { std::lock_guard lock(mutex_); - if(!lru_.exists(key)){ + if (!lru_.exists(key)) { return; } - const ItemObj& old_item = lru_.get(key); + const ItemObj &old_item = lru_.get(key); usage_ -= old_item->size(); SERVER_LOG_DEBUG << "Erase " << key << " size: " << old_item->size(); @@ -122,7 +128,8 @@ void Cache::erase(const std::string& key) { } template -void Cache::clear() { +void +Cache::clear() { std::lock_guard lock(mutex_); lru_.clear(); usage_ = 0; @@ -131,12 +138,13 @@ void Cache::clear() { /* free memory space when CACHE occupation exceed its capacity */ template -void Cache::free_memory() { +void +Cache::free_memory() { if (usage_ <= capacity_) return; int64_t threshhold = capacity_ * freemem_percent_; int64_t delta_size = usage_ - threshhold; - if(delta_size <= 0) { + if (delta_size <= 0) { delta_size = 1;//ensure at least one item erased } @@ -148,8 +156,8 @@ void Cache::free_memory() { auto it = lru_.rbegin(); while (it != lru_.rend() && released_size < delta_size) { - auto& key = it->first; - auto& obj_ptr = it->second; + auto &key = it->first; + auto &obj_ptr = it->second; key_array.emplace(key); released_size += obj_ptr->size(); @@ -159,7 +167,7 @@ void Cache::free_memory() { SERVER_LOG_DEBUG << "to be released memory size: " << released_size; - for (auto& key : key_array) { + for (auto &key : key_array) { erase(key); } @@ -167,7 +175,8 @@ void Cache::free_memory() { } template -void Cache::print() { +void +Cache::print() { size_t cache_count = 0; { std::lock_guard lock(mutex_); @@ -179,7 +188,7 @@ void Cache::print() { SERVER_LOG_DEBUG << "[Cache capacity]: " << capacity_ << " bytes"; } -} // cache -} // milvus -} // zilliz +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/CacheMgr.h b/cpp/src/cache/CacheMgr.h index 1dd630e14dc7a1507b881431f8e620b94e943c1e..53004d10b23b41a90a7090a114f2ad730e05bff9 100644 --- a/cpp/src/cache/CacheMgr.h +++ b/cpp/src/cache/CacheMgr.h @@ -22,22 +22,25 @@ #include "utils/Log.h" #include "metrics/Metrics.h" +#include +#include + namespace zilliz { namespace milvus { namespace cache { template class CacheMgr { -public: + public: virtual uint64_t ItemCount() const; - virtual bool ItemExists(const std::string& key); + virtual bool ItemExists(const std::string &key); - virtual ItemObj GetItem(const std::string& key); + virtual ItemObj GetItem(const std::string &key); - virtual void InsertItem(const std::string& key, const ItemObj& data); + virtual void InsertItem(const std::string &key, const ItemObj &data); - virtual void EraseItem(const std::string& key); + virtual void EraseItem(const std::string &key); virtual void PrintInfo(); @@ -47,18 +50,17 @@ public: int64_t CacheCapacity() const; void SetCapacity(int64_t capacity); -protected: + protected: CacheMgr(); virtual ~CacheMgr(); -protected: + protected: using CachePtr = std::shared_ptr>; CachePtr cache_; }; +} // namespace cache +} // namespace milvus +} // namespace zilliz -} -} -} - -#include "cache/CacheMgr.inl" \ No newline at end of file +#include "cache/CacheMgr.inl" diff --git a/cpp/src/cache/CacheMgr.inl b/cpp/src/cache/CacheMgr.inl index 36c3d94642d7372f96522810c40cca93c19ce774..b0c47b3dc37f54b6b336fe26f3b292d45ba5697f 100644 --- a/cpp/src/cache/CacheMgr.inl +++ b/cpp/src/cache/CacheMgr.inl @@ -30,18 +30,20 @@ CacheMgr::~CacheMgr() { } template -uint64_t CacheMgr::ItemCount() const { - if(cache_ == nullptr) { +uint64_t +CacheMgr::ItemCount() const { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return 0; } - return (uint64_t)(cache_->size()); + return (uint64_t) (cache_->size()); } template -bool CacheMgr::ItemExists(const std::string& key) { - if(cache_ == nullptr) { +bool +CacheMgr::ItemExists(const std::string &key) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return false; } @@ -50,8 +52,9 @@ bool CacheMgr::ItemExists(const std::string& key) { } template -ItemObj CacheMgr::GetItem(const std::string& key) { - if(cache_ == nullptr) { +ItemObj +CacheMgr::GetItem(const std::string &key) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return nullptr; } @@ -60,8 +63,9 @@ ItemObj CacheMgr::GetItem(const std::string& key) { } template -void CacheMgr::InsertItem(const std::string& key, const ItemObj& data) { - if(cache_ == nullptr) { +void +CacheMgr::InsertItem(const std::string &key, const ItemObj &data) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } @@ -71,8 +75,9 @@ void CacheMgr::InsertItem(const std::string& key, const ItemObj& data) } template -void CacheMgr::EraseItem(const std::string& key) { - if(cache_ == nullptr) { +void +CacheMgr::EraseItem(const std::string &key) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } @@ -82,8 +87,9 @@ void CacheMgr::EraseItem(const std::string& key) { } template -void CacheMgr::PrintInfo() { - if(cache_ == nullptr) { +void +CacheMgr::PrintInfo() { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } @@ -92,8 +98,9 @@ void CacheMgr::PrintInfo() { } template -void CacheMgr::ClearCache() { - if(cache_ == nullptr) { +void +CacheMgr::ClearCache() { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } @@ -102,8 +109,9 @@ void CacheMgr::ClearCache() { } template -int64_t CacheMgr::CacheUsage() const { - if(cache_ == nullptr) { +int64_t +CacheMgr::CacheUsage() const { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return 0; } @@ -112,8 +120,9 @@ int64_t CacheMgr::CacheUsage() const { } template -int64_t CacheMgr::CacheCapacity() const { - if(cache_ == nullptr) { +int64_t +CacheMgr::CacheCapacity() const { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return 0; } @@ -122,14 +131,15 @@ int64_t CacheMgr::CacheCapacity() const { } template -void CacheMgr::SetCapacity(int64_t capacity) { - if(cache_ == nullptr) { +void +CacheMgr::SetCapacity(int64_t capacity) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } cache_->set_capacity(capacity); } -} -} -} +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/CpuCacheMgr.cpp b/cpp/src/cache/CpuCacheMgr.cpp index 1f93ec92bdd006d4e445f2308d9fb3e860e5e0e8..d26004b2bef5a843700a288075f825022f5db565 100644 --- a/cpp/src/cache/CpuCacheMgr.cpp +++ b/cpp/src/cache/CpuCacheMgr.cpp @@ -16,20 +16,22 @@ // under the License. -#include "CpuCacheMgr.h" +#include "cache/CpuCacheMgr.h" #include "server/Config.h" #include "utils/Log.h" +#include + namespace zilliz { namespace milvus { namespace cache { namespace { - constexpr int64_t unit = 1024 * 1024 * 1024; +constexpr int64_t unit = 1024 * 1024 * 1024; } CpuCacheMgr::CpuCacheMgr() { - server::Config& config = server::Config::GetInstance(); + server::Config &config = server::Config::GetInstance(); Status s; int32_t cpu_mem_cap; @@ -38,7 +40,7 @@ CpuCacheMgr::CpuCacheMgr() { SERVER_LOG_ERROR << s.message(); } int64_t cap = cpu_mem_cap * unit; - cache_ = std::make_shared>(cap, 1UL<<32); + cache_ = std::make_shared>(cap, 1UL << 32); float cpu_mem_threshold; s = config.GetCacheConfigCpuMemThreshold(cpu_mem_threshold); @@ -53,20 +55,22 @@ CpuCacheMgr::CpuCacheMgr() { } } -CpuCacheMgr* CpuCacheMgr::GetInstance() { +CpuCacheMgr * +CpuCacheMgr::GetInstance() { static CpuCacheMgr s_mgr; return &s_mgr; } -engine::VecIndexPtr CpuCacheMgr::GetIndex(const std::string& key) { +engine::VecIndexPtr +CpuCacheMgr::GetIndex(const std::string &key) { DataObjPtr obj = GetItem(key); - if(obj != nullptr) { + if (obj != nullptr) { return obj->data(); } return nullptr; } -} -} -} \ No newline at end of file +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/CpuCacheMgr.h b/cpp/src/cache/CpuCacheMgr.h index 5a2cdb599acdc8a7e846e0251179746ab39f52b0..32a83c4cc0dd6a510f3d9386e410db5651951d8b 100644 --- a/cpp/src/cache/CpuCacheMgr.h +++ b/cpp/src/cache/CpuCacheMgr.h @@ -20,21 +20,24 @@ #include "CacheMgr.h" #include "DataObj.h" +#include +#include + namespace zilliz { namespace milvus { namespace cache { class CpuCacheMgr : public CacheMgr { -private: + private: CpuCacheMgr(); -public: + public: //TODO: use smart pointer instead - static CpuCacheMgr* GetInstance(); + static CpuCacheMgr *GetInstance(); - engine::VecIndexPtr GetIndex(const std::string& key); + engine::VecIndexPtr GetIndex(const std::string &key); }; -} -} -} +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/DataObj.h b/cpp/src/cache/DataObj.h index f9215bc152096c1444297a2b954d16351cd54e2e..6ed3256eee1bb22f7a9535af666deceab4509a9b 100644 --- a/cpp/src/cache/DataObj.h +++ b/cpp/src/cache/DataObj.h @@ -27,38 +27,43 @@ namespace milvus { namespace cache { class DataObj { -public: - DataObj(const engine::VecIndexPtr& index) - : index_(index) - {} + public: + explicit DataObj(const engine::VecIndexPtr &index) + : index_(index) { + } - DataObj(const engine::VecIndexPtr& index, int64_t size) - : index_(index), - size_(size) - {} + DataObj(const engine::VecIndexPtr &index, int64_t size) + : index_(index), + size_(size) { + } - engine::VecIndexPtr data() { return index_; } - const engine::VecIndexPtr& data() const { return index_; } + engine::VecIndexPtr data() { + return index_; + } + + const engine::VecIndexPtr &data() const { + return index_; + } int64_t size() const { - if(index_ == nullptr) { + if (index_ == nullptr) { return 0; } - if(size_ > 0) { + if (size_ > 0) { return size_; } return index_->Count() * index_->Dimension() * sizeof(float); } -private: + private: engine::VecIndexPtr index_ = nullptr; int64_t size_ = 0; }; using DataObjPtr = std::shared_ptr; -} -} -} \ No newline at end of file +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/GpuCacheMgr.cpp b/cpp/src/cache/GpuCacheMgr.cpp index 5c104ba0e7452fa9eea6de8c0dc30bf566664d9b..ad68a6ebef47ee903e954796abbb05affd4c3192 100644 --- a/cpp/src/cache/GpuCacheMgr.cpp +++ b/cpp/src/cache/GpuCacheMgr.cpp @@ -16,11 +16,13 @@ // under the License. -#include +#include "cache/GpuCacheMgr.h" #include "utils/Log.h" -#include "GpuCacheMgr.h" #include "server/Config.h" +#include +#include + namespace zilliz { namespace milvus { namespace cache { @@ -29,11 +31,11 @@ std::mutex GpuCacheMgr::mutex_; std::unordered_map GpuCacheMgr::instance_; namespace { - constexpr int64_t G_BYTE = 1024 * 1024 * 1024; +constexpr int64_t G_BYTE = 1024 * 1024 * 1024; } GpuCacheMgr::GpuCacheMgr() { - server::Config& config = server::Config::GetInstance(); + server::Config &config = server::Config::GetInstance(); Status s; int32_t gpu_mem_cap; @@ -42,7 +44,7 @@ GpuCacheMgr::GpuCacheMgr() { SERVER_LOG_ERROR << s.message(); } int32_t cap = gpu_mem_cap * G_BYTE; - cache_ = std::make_shared>(cap, 1UL<<32); + cache_ = std::make_shared>(cap, 1UL << 32); float gpu_mem_threshold; s = config.GetCacheConfigGpuMemThreshold(gpu_mem_threshold); @@ -57,7 +59,8 @@ GpuCacheMgr::GpuCacheMgr() { } } -GpuCacheMgr* GpuCacheMgr::GetInstance(uint64_t gpu_id) { +GpuCacheMgr * +GpuCacheMgr::GetInstance(uint64_t gpu_id) { if (instance_.find(gpu_id) == instance_.end()) { std::lock_guard lock(mutex_); if (instance_.find(gpu_id) == instance_.end()) { @@ -70,15 +73,16 @@ GpuCacheMgr* GpuCacheMgr::GetInstance(uint64_t gpu_id) { } } -engine::VecIndexPtr GpuCacheMgr::GetIndex(const std::string& key) { +engine::VecIndexPtr +GpuCacheMgr::GetIndex(const std::string &key) { DataObjPtr obj = GetItem(key); - if(obj != nullptr) { + if (obj != nullptr) { return obj->data(); } return nullptr; } -} -} -} \ No newline at end of file +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/GpuCacheMgr.h b/cpp/src/cache/GpuCacheMgr.h index 57539b698192fc5858633f31040d06d1483a177a..843a5ff67d7a5479c9c54e677345701ae7125fee 100644 --- a/cpp/src/cache/GpuCacheMgr.h +++ b/cpp/src/cache/GpuCacheMgr.h @@ -21,6 +21,7 @@ #include #include +#include namespace zilliz { namespace milvus { @@ -30,18 +31,18 @@ class GpuCacheMgr; using GpuCacheMgrPtr = std::shared_ptr; class GpuCacheMgr : public CacheMgr { -public: + public: GpuCacheMgr(); - static GpuCacheMgr* GetInstance(uint64_t gpu_id); + static GpuCacheMgr *GetInstance(uint64_t gpu_id); - engine::VecIndexPtr GetIndex(const std::string& key); + engine::VecIndexPtr GetIndex(const std::string &key); -private: + private: static std::mutex mutex_; static std::unordered_map instance_; }; -} -} -} +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/LRU.h b/cpp/src/cache/LRU.h index a4f4dcf5f21106f74de79bb70961a31f53c862ea..5446dd0f14ec909c761f05f125fcc13d3d4bfcaa 100644 --- a/cpp/src/cache/LRU.h +++ b/cpp/src/cache/LRU.h @@ -22,6 +22,7 @@ #include #include #include +#include namespace zilliz { namespace milvus { @@ -29,14 +30,15 @@ namespace cache { template class LRU { -public: + public: typedef typename std::pair key_value_pair_t; typedef typename std::list::iterator list_iterator_t; typedef typename std::list::reverse_iterator reverse_list_iterator_t; - LRU(size_t max_size) : max_size_(max_size) {} + explicit LRU(size_t max_size) : max_size_(max_size) { + } - void put(const key_t& key, const value_t& value) { + void put(const key_t &key, const value_t &value) { auto it = cache_items_map_.find(key); cache_items_list_.push_front(key_value_pair_t(key, value)); if (it != cache_items_map_.end()) { @@ -53,7 +55,7 @@ public: } } - const value_t& get(const key_t& key) { + const value_t &get(const key_t &key) { auto it = cache_items_map_.find(key); if (it == cache_items_map_.end()) { throw std::range_error("There is no such key in cache"); @@ -63,7 +65,7 @@ public: } } - void erase(const key_t& key) { + void erase(const key_t &key) { auto it = cache_items_map_.find(key); if (it != cache_items_map_.end()) { cache_items_list_.erase(it->second); @@ -71,7 +73,7 @@ public: } } - bool exists(const key_t& key) const { + bool exists(const key_t &key) const { return cache_items_map_.find(key) != cache_items_map_.end(); } @@ -101,14 +103,14 @@ public: cache_items_map_.clear(); } -private: + private: std::list cache_items_list_; std::unordered_map cache_items_map_; size_t max_size_; list_iterator_t iter_; }; -} // cache -} // milvus -} // zilliz +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp index ca3283f4d08f0161049c5aae78e7deb51627fc55..22c282d4ea0f4150dfa406a3ae37a0730844c823 100644 --- a/cpp/src/main.cpp +++ b/cpp/src/main.cpp @@ -76,7 +76,6 @@ main(int argc, char *argv[]) { std::cout << "Initial log config from: " << log_config_file << std::endl; break; } - case 'p': { char *pid_filename_ptr = strdup(optarg); pid_filename = pid_filename_ptr; @@ -84,7 +83,6 @@ main(int argc, char *argv[]) { std::cout << pid_filename << std::endl; break; } - case 'd': start_daemonized = 1; break; diff --git a/cpp/src/sdk/examples/grpcsimple/main.cpp b/cpp/src/sdk/examples/grpcsimple/main.cpp index 4de060590c9bb76d837ccac90ff3e7829eb3c366..166707259a46849b672abb0bba17153f2f405737 100644 --- a/cpp/src/sdk/examples/grpcsimple/main.cpp +++ b/cpp/src/sdk/examples/grpcsimple/main.cpp @@ -23,8 +23,8 @@ #include "src/ClientTest.h" -void print_help(const std::string &app_name); - +void +print_help(const std::string &app_name); int main(int argc, char *argv[]) { @@ -56,8 +56,7 @@ main(int argc, char *argv[]) { break; } case 'h': - default: - print_help(app_name); + default:print_help(app_name); return EXIT_SUCCESS; } } @@ -77,4 +76,4 @@ print_help(const std::string &app_name) { printf(" -p --port Server port, default 19530\n"); printf(" -h --help Print help information\n"); printf("\n"); -} \ No newline at end of file +} diff --git a/cpp/src/sdk/examples/grpcsimple/src/ClientTest.cpp b/cpp/src/sdk/examples/grpcsimple/src/ClientTest.cpp index 16fc2ee16883bc599646481d5d96b751a82d71a3..e29bcc30f380b41e2303bacf6bf6ea4d3245e372 100644 --- a/cpp/src/sdk/examples/grpcsimple/src/ClientTest.cpp +++ b/cpp/src/sdk/examples/grpcsimple/src/ClientTest.cpp @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -#include "ClientTest.h" +#include "sdk/examples/grpcsimple/src/ClientTest.h" #include "MilvusApi.h" #include "cache/CpuCacheMgr.h" @@ -24,15 +24,17 @@ #include #include #include - -using namespace milvus; +#include +#include +#include //#define SET_VECTOR_IDS; namespace { -std::string GetTableName(); +const std::string& +GetTableName(); -const std::string TABLE_NAME = GetTableName(); +const char* TABLE_NAME = GetTableName().c_str(); constexpr int64_t TABLE_DIMENSION = 512; constexpr int64_t TABLE_INDEX_FILE_SIZE = 1024; constexpr int64_t BATCH_ROW_COUNT = 100000; @@ -44,26 +46,28 @@ constexpr int64_t SECONDS_EACH_HOUR = 3600; #define BLOCK_SPLITER std::cout << "===========================================" << std::endl; -void PrintTableSchema(const TableSchema& tb_schema) { +void +PrintTableSchema(const milvus::TableSchema &tb_schema) { BLOCK_SPLITER std::cout << "Table name: " << tb_schema.table_name << std::endl; std::cout << "Table dimension: " << tb_schema.dimension << std::endl; BLOCK_SPLITER } -void PrintSearchResult(const std::vector>& search_record_array, - const std::vector& topk_query_result_array) { +void +PrintSearchResult(const std::vector> &search_record_array, + const std::vector &topk_query_result_array) { BLOCK_SPLITER std::cout << "Returned result count: " << topk_query_result_array.size() << std::endl; int32_t index = 0; - for(auto& result : topk_query_result_array) { + for (auto &result : topk_query_result_array) { auto search_id = search_record_array[index].first; index++; std::cout << "No." << std::to_string(index) << " vector " << std::to_string(search_id) << " top " << std::to_string(result.query_result_arrays.size()) << " search result:" << std::endl; - for(auto& item : result.query_result_arrays) { + for (auto &item : result.query_result_arrays) { std::cout << "\t" << std::to_string(item.id) << "\tdistance:" << std::to_string(item.distance); std::cout << std::endl; } @@ -72,80 +76,88 @@ void PrintSearchResult(const std::vector>& search_ BLOCK_SPLITER } -std::string CurrentTime() { +std::string +CurrentTime() { time_t tt; - time( &tt ); - tt = tt + 8*SECONDS_EACH_HOUR; - tm* t= gmtime( &tt ); + time(&tt); + tt = tt + 8 * SECONDS_EACH_HOUR; + tm t; + gmtime_r(&tt, &t); - std::string str = std::to_string(t->tm_year + 1900) + "_" + std::to_string(t->tm_mon + 1) - + "_" + std::to_string(t->tm_mday) + "_" + std::to_string(t->tm_hour) - + "_" + std::to_string(t->tm_min) + "_" + std::to_string(t->tm_sec); + std::string str = std::to_string(t.tm_year + 1900) + "_" + std::to_string(t.tm_mon + 1) + + "_" + std::to_string(t.tm_mday) + "_" + std::to_string(t.tm_hour) + + "_" + std::to_string(t.tm_min) + "_" + std::to_string(t.tm_sec); return str; } -std::string CurrentTmDate(int64_t offset_day = 0) { +std::string +CurrentTmDate(int64_t offset_day = 0) { time_t tt; - time( &tt ); - tt = tt + 8*SECONDS_EACH_HOUR; - tt = tt + 24*SECONDS_EACH_HOUR*offset_day; - tm* t= gmtime( &tt ); + time(&tt); + tt = tt + 8 * SECONDS_EACH_HOUR; + tt = tt + 24 * SECONDS_EACH_HOUR * offset_day; + tm t; + gmtime_r(&tt, &t); - std::string str = std::to_string(t->tm_year + 1900) + "-" + std::to_string(t->tm_mon + 1) - + "-" + std::to_string(t->tm_mday); + std::string str = std::to_string(t.tm_year + 1900) + "-" + std::to_string(t.tm_mon + 1) + + "-" + std::to_string(t.tm_mday); return str; } -std::string GetTableName() { - static std::string s_id(CurrentTime()); - return "tbl_" + s_id; +const std::string& +GetTableName() { + static std::string s_id("tbl_" + CurrentTime()); + return s_id; } -TableSchema BuildTableSchema() { - TableSchema tb_schema; +milvus::TableSchema +BuildTableSchema() { + milvus::TableSchema tb_schema; tb_schema.table_name = TABLE_NAME; tb_schema.dimension = TABLE_DIMENSION; tb_schema.index_file_size = TABLE_INDEX_FILE_SIZE; - tb_schema.metric_type = MetricType::L2; + tb_schema.metric_type = milvus::MetricType::L2; return tb_schema; } -void BuildVectors(int64_t from, int64_t to, - std::vector& vector_record_array) { - if(to <= from){ +void +BuildVectors(int64_t from, int64_t to, + std::vector &vector_record_array) { + if (to <= from) { return; } vector_record_array.clear(); for (int64_t k = from; k < to; k++) { - RowRecord record; + milvus::RowRecord record; record.data.resize(TABLE_DIMENSION); - for(int64_t i = 0; i < TABLE_DIMENSION; i++) { - record.data[i] = (float)(k%(i+1)); + for (int64_t i = 0; i < TABLE_DIMENSION; i++) { + record.data[i] = (float) (k % (i + 1)); } vector_record_array.emplace_back(record); } } -void Sleep(int seconds) { +void +Sleep(int seconds) { std::cout << "Waiting " << seconds << " seconds ..." << std::endl; sleep(seconds); } class TimeRecorder { public: - explicit TimeRecorder(const std::string& title) + explicit TimeRecorder(const std::string &title) : title_(title) { start_ = std::chrono::system_clock::now(); } ~TimeRecorder() { std::chrono::system_clock::time_point end = std::chrono::system_clock::now(); - long span = (std::chrono::duration_cast (end - start_)).count(); + int64_t span = (std::chrono::duration_cast(end - start_)).count(); std::cout << title_ << " totally cost: " << span << " ms" << std::endl; } @@ -154,14 +166,15 @@ class TimeRecorder { std::chrono::system_clock::time_point start_; }; -void CheckResult(const std::vector>& search_record_array, - const std::vector& topk_query_result_array) { +void +CheckResult(const std::vector> &search_record_array, + const std::vector &topk_query_result_array) { BLOCK_SPLITER int64_t index = 0; - for(auto& result : topk_query_result_array) { + for (auto &result : topk_query_result_array) { auto result_id = result.query_result_arrays[0].id; auto search_id = search_record_array[index++].first; - if(result_id != search_id) { + if (result_id != search_id) { std::cout << "The top 1 result is wrong: " << result_id << " vs. " << search_id << std::endl; } else { @@ -171,42 +184,45 @@ void CheckResult(const std::vector>& search_record BLOCK_SPLITER } -void DoSearch(std::shared_ptr conn, - const std::vector>& search_record_array, - const std::string& phase_name) { - std::vector query_range_array; - Range rg; +void +DoSearch(std::shared_ptr conn, + const std::vector> &search_record_array, + const std::string &phase_name) { + std::vector query_range_array; + milvus::Range rg; rg.start_value = CurrentTmDate(); rg.end_value = CurrentTmDate(1); query_range_array.emplace_back(rg); - std::vector record_array; - for(auto& pair : search_record_array) { + std::vector record_array; + for (auto &pair : search_record_array) { record_array.push_back(pair.second); } auto start = std::chrono::high_resolution_clock::now(); - std::vector topk_query_result_array; + std::vector topk_query_result_array; { TimeRecorder rc(phase_name); - Status stat = conn->Search(TABLE_NAME, record_array, query_range_array, TOP_K, 32, topk_query_result_array); + milvus::Status stat = + conn->Search(TABLE_NAME, record_array, query_range_array, TOP_K, 32, topk_query_result_array); std::cout << "SearchVector function call status: " << stat.message() << std::endl; } auto finish = std::chrono::high_resolution_clock::now(); - std::cout << "SEARCHVECTOR COST: " << std::chrono::duration_cast>(finish - start).count() << "s\n"; + std::cout << "SEARCHVECTOR COST: " + << std::chrono::duration_cast>(finish - start).count() << "s\n"; PrintSearchResult(search_record_array, topk_query_result_array); CheckResult(search_record_array, topk_query_result_array); } -} +} // namespace void -ClientTest::Test(const std::string& address, const std::string& port) { - std::shared_ptr conn = Connection::Create(); +ClientTest::Test(const std::string &address, const std::string &port) { + std::shared_ptr conn = milvus::Connection::Create(); {//connect server - ConnectParam param = {address, port}; - Status stat = conn->Connect(param); + milvus::ConnectParam param = {address, port}; + milvus::Status stat = conn->Connect(param); std::cout << "Connect function call status: " << stat.message() << std::endl; } @@ -222,10 +238,10 @@ ClientTest::Test(const std::string& address, const std::string& port) { { std::vector tables; - Status stat = conn->ShowTables(tables); + milvus::Status stat = conn->ShowTables(tables); std::cout << "ShowTables function call status: " << stat.message() << std::endl; std::cout << "All tables: " << std::endl; - for(auto& table : tables) { + for (auto &table : tables) { int64_t row_count = 0; // conn->DropTable(table); stat = conn->CountTable(table, row_count); @@ -234,28 +250,28 @@ ClientTest::Test(const std::string& address, const std::string& port) { } {//create table - TableSchema tb_schema = BuildTableSchema(); - Status stat = conn->CreateTable(tb_schema); + milvus::TableSchema tb_schema = BuildTableSchema(); + milvus::Status stat = conn->CreateTable(tb_schema); std::cout << "CreateTable function call status: " << stat.message() << std::endl; PrintTableSchema(tb_schema); bool has_table = conn->HasTable(tb_schema.table_name); - if(has_table) { + if (has_table) { std::cout << "Table is created" << std::endl; } } {//describe table - TableSchema tb_schema; - Status stat = conn->DescribeTable(TABLE_NAME, tb_schema); + milvus::TableSchema tb_schema; + milvus::Status stat = conn->DescribeTable(TABLE_NAME, tb_schema); std::cout << "DescribeTable function call status: " << stat.message() << std::endl; PrintTableSchema(tb_schema); } - std::vector> search_record_array; + std::vector> search_record_array; {//insert vectors for (int i = 0; i < ADD_VECTOR_LOOP; i++) {//add vectors - std::vector record_array; + std::vector record_array; int64_t begin_index = i * BATCH_ROW_COUNT; BuildVectors(begin_index, begin_index + BATCH_ROW_COUNT, record_array); @@ -268,21 +284,21 @@ ClientTest::Test(const std::string& address, const std::string& port) { std::vector record_ids; //generate user defined ids - for(int k = 0; k < BATCH_ROW_COUNT; k++) { - record_ids.push_back(i*BATCH_ROW_COUNT+k); + for (int k = 0; k < BATCH_ROW_COUNT; k++) { + record_ids.push_back(i * BATCH_ROW_COUNT + k); } auto start = std::chrono::high_resolution_clock::now(); - Status stat = conn->Insert(TABLE_NAME, record_array, record_ids); + milvus::Status stat = conn->Insert(TABLE_NAME, record_array, record_ids); auto finish = std::chrono::high_resolution_clock::now(); - std::cout << "InsertVector cost: " << std::chrono::duration_cast>(finish - start).count() << "s\n"; - + std::cout << "InsertVector cost: " + << std::chrono::duration_cast>(finish - start).count() << "s\n"; std::cout << "InsertVector function call status: " << stat.message() << std::endl; std::cout << "Returned id array count: " << record_ids.size() << std::endl; - if(search_record_array.size() < NQ) { + if (search_record_array.size() < NQ) { search_record_array.push_back( std::make_pair(record_ids[SEARCH_TARGET], record_array[SEARCH_TARGET])); } @@ -293,27 +309,27 @@ ClientTest::Test(const std::string& address, const std::string& port) { Sleep(2); int64_t row_count = 0; - Status stat = conn->CountTable(TABLE_NAME, row_count); + milvus::Status stat = conn->CountTable(TABLE_NAME, row_count); std::cout << TABLE_NAME << "(" << row_count << " rows)" << std::endl; // DoSearch(conn, search_record_array, "Search without index"); } {//wait unit build index finish std::cout << "Wait until create all index done" << std::endl; - IndexParam index; + milvus::IndexParam index; index.table_name = TABLE_NAME; - index.index_type = IndexType::gpu_ivfsq8; + index.index_type = milvus::IndexType::gpu_ivfsq8; index.nlist = 16384; - Status stat = conn->CreateIndex(index); + milvus::Status stat = conn->CreateIndex(index); std::cout << "CreateIndex function call status: " << stat.message() << std::endl; - IndexParam index2; + milvus::IndexParam index2; stat = conn->DescribeIndex(TABLE_NAME, index2); std::cout << "DescribeIndex function call status: " << stat.message() << std::endl; } {//preload table - Status stat = conn->PreloadTable(TABLE_NAME); + milvus::Status stat = conn->PreloadTable(TABLE_NAME); std::cout << "PreloadTable function call status: " << stat.message() << std::endl; } @@ -325,7 +341,7 @@ ClientTest::Test(const std::string& address, const std::string& port) { } {//delete index - Status stat = conn->DropIndex(TABLE_NAME); + milvus::Status stat = conn->DropIndex(TABLE_NAME); std::cout << "DropIndex function call status: " << stat.message() << std::endl; int64_t row_count = 0; @@ -334,11 +350,11 @@ ClientTest::Test(const std::string& address, const std::string& port) { } {//delete by range - Range rg; + milvus::Range rg; rg.start_value = CurrentTmDate(-2); rg.end_value = CurrentTmDate(-3); - Status stat = conn->DeleteByRange(rg, TABLE_NAME); + milvus::Status stat = conn->DeleteByRange(rg, TABLE_NAME); std::cout << "DeleteByRange function call status: " << stat.message() << std::endl; } @@ -351,7 +367,7 @@ ClientTest::Test(const std::string& address, const std::string& port) { std::string status = conn->ServerStatus(); std::cout << "Server status before disconnect: " << status << std::endl; } - Connection::Destroy(conn); + milvus::Connection::Destroy(conn); {//server status std::string status = conn->ServerStatus(); std::cout << "Server status after disconnect: " << status << std::endl; diff --git a/cpp/src/sdk/examples/grpcsimple/src/ClientTest.h b/cpp/src/sdk/examples/grpcsimple/src/ClientTest.h index d95897927e546ed89c5163dd98f3800c3353154f..165c36a180efabd8476353a3dd0bb92fa3417d25 100644 --- a/cpp/src/sdk/examples/grpcsimple/src/ClientTest.h +++ b/cpp/src/sdk/examples/grpcsimple/src/ClientTest.h @@ -20,6 +20,6 @@ #include class ClientTest { -public: - void Test(const std::string& address, const std::string& port); -}; \ No newline at end of file + public: + void Test(const std::string &address, const std::string &port); +}; diff --git a/cpp/src/sdk/grpc/ClientProxy.cpp b/cpp/src/sdk/grpc/ClientProxy.cpp index 3bcfd699fe6dc65d6c45b502f5287f6453aa7bd5..f4cd1e9a38bf30306cdb49cc22e58022795df150 100644 --- a/cpp/src/sdk/grpc/ClientProxy.cpp +++ b/cpp/src/sdk/grpc/ClientProxy.cpp @@ -15,16 +15,20 @@ // specific language governing permissions and limitations // under the License. -#include "ClientProxy.h" -#include "version.h" -#include "milvus.grpc.pb.h" +#include "sdk/grpc/ClientProxy.h" +#include "../../../version.h" +#include "grpc/gen-milvus/milvus.grpc.pb.h" + +#include +#include +#include + //#define GRPC_MULTIPLE_THREAD; namespace milvus { - bool UriCheck(const std::string &uri) { - size_t index = uri.find_first_of(':', 0); + size_t index = uri.find_first_of(':', 0); if (index == std::string::npos) { return false; } else { @@ -79,7 +83,7 @@ ClientProxy::Disconnect() { connected_ = false; channel_.reset(); return status; - }catch (std::exception &ex) { + } catch (std::exception &ex) { return Status(StatusCode::UnknownError, "failed to disconnect: " + std::string(ex.what())); } } @@ -96,7 +100,7 @@ ClientProxy::CreateTable(const TableSchema ¶m) { schema.set_table_name(param.table_name); schema.set_dimension(param.dimension); schema.set_index_file_size(param.index_file_size); - schema.set_metric_type((int32_t)param.metric_type); + schema.set_metric_type((int32_t) param.metric_type); return client_ptr_->CreateTable(schema); } catch (std::exception &ex) { @@ -127,13 +131,12 @@ ClientProxy::DropTable(const std::string &table_name) { Status ClientProxy::CreateIndex(const IndexParam &index_param) { try { - //TODO:add index params + //TODO: add index params ::milvus::grpc::IndexParam grpc_index_param; grpc_index_param.set_table_name(index_param.table_name); - grpc_index_param.mutable_index()->set_index_type((int32_t)index_param.index_type); + grpc_index_param.mutable_index()->set_index_type((int32_t) index_param.index_type); grpc_index_param.mutable_index()->set_nlist(index_param.nlist); return client_ptr_->CreateIndex(grpc_index_param); - } catch (std::exception &ex) { return Status(StatusCode::UnknownError, "failed to build index: " + std::string(ex.what())); } @@ -141,8 +144,8 @@ ClientProxy::CreateIndex(const IndexParam &index_param) { Status ClientProxy::Insert(const std::string &table_name, - const std::vector &record_array, - std::vector &id_array) { + const std::vector &record_array, + std::vector &id_array) { Status status = Status::OK(); try { //////////////////////////////////////////////////////////////////////////// @@ -181,7 +184,9 @@ ClientProxy::Insert(const std::string &table_name, } std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join)); auto finish = std::chrono::high_resolution_clock::now(); - std::cout << "InsertVector cost: " << std::chrono::duration_cast>(finish - start).count() << "s\n"; + std::cout << + "InsertVector cost: " << std::chrono::duration_cast>(finish - start).count() + << "s\n"; std::cout << "*****************************************************\n"; for (size_t i = 0; i < thread_count; i++) { @@ -213,9 +218,7 @@ ClientProxy::Insert(const std::string &table_name, id_array.push_back(vector_ids.vector_id_array(i)); } } - #endif - } catch (std::exception &ex) { return Status(StatusCode::UnknownError, "fail to add vector: " + std::string(ex.what())); } @@ -225,11 +228,11 @@ ClientProxy::Insert(const std::string &table_name, Status ClientProxy::Search(const std::string &table_name, - const std::vector &query_record_array, - const std::vector &query_range_array, - int64_t topk, - int64_t nprobe, - std::vector &topk_query_result_array) { + const std::vector &query_record_array, + const std::vector &query_range_array, + int64_t topk, + int64_t nprobe, + std::vector &topk_query_result_array) { try { //step 1: convert vectors data ::milvus::grpc::SearchParam search_param; @@ -267,11 +270,9 @@ ClientProxy::Search(const std::string &table_name, topk_query_result_array.emplace_back(result); } return status; - } catch (std::exception &ex) { return Status(StatusCode::UnknownError, "fail to search vectors: " + std::string(ex.what())); } - } Status @@ -284,13 +285,12 @@ ClientProxy::DescribeTable(const std::string &table_name, TableSchema &table_sch table_schema.table_name = grpc_schema.table_name(); table_schema.dimension = grpc_schema.dimension(); table_schema.index_file_size = grpc_schema.index_file_size(); - table_schema.metric_type = (MetricType)grpc_schema.metric_type(); + table_schema.metric_type = (MetricType) grpc_schema.metric_type(); return status; } catch (std::exception &ex) { return Status(StatusCode::UnknownError, "fail to describe table: " + std::string(ex.what())); } - } Status @@ -316,7 +316,6 @@ ClientProxy::ShowTables(std::vector &table_array) { table_array[i] = table_name_list.table_names(i); } return status; - } catch (std::exception &ex) { return Status(StatusCode::UnknownError, "fail to show tables: " + std::string(ex.what())); } @@ -396,11 +395,10 @@ ClientProxy::DescribeIndex(const std::string &table_name, IndexParam &index_para grpc_table_name.set_table_name(table_name); ::milvus::grpc::IndexParam grpc_index_param; Status status = client_ptr_->DescribeIndex(grpc_table_name, grpc_index_param); - index_param.index_type = (IndexType)(grpc_index_param.mutable_index()->index_type()); + index_param.index_type = (IndexType) (grpc_index_param.mutable_index()->index_type()); index_param.nlist = grpc_index_param.mutable_index()->nlist(); return status; - } catch (std::exception &ex) { return Status(StatusCode::UnknownError, "fail to describe index: " + std::string(ex.what())); } @@ -418,4 +416,4 @@ ClientProxy::DropIndex(const std::string &table_name) const { } } -} +} // namespace milvus diff --git a/cpp/src/sdk/grpc/ClientProxy.h b/cpp/src/sdk/grpc/ClientProxy.h index c3f79d7209b4a0ee1fc0d37934e5dc70c3e07f73..044ce44847d1e8c27b6e50482cc4b05e2431e877 100644 --- a/cpp/src/sdk/grpc/ClientProxy.h +++ b/cpp/src/sdk/grpc/ClientProxy.h @@ -20,88 +20,92 @@ #include "MilvusApi.h" #include "GrpcClient.h" +#include +#include +#include + namespace milvus { class ClientProxy : public Connection { -public: + public: // Implementations of the Connection interface - virtual Status + Status Connect(const ConnectParam ¶m) override; - virtual Status + Status Connect(const std::string &uri) override; - virtual Status + Status Connected() const override; - virtual Status + Status Disconnect() override; - virtual Status + Status CreateTable(const TableSchema ¶m) override; - virtual bool + bool HasTable(const std::string &table_name) override; - virtual Status + Status DropTable(const std::string &table_name) override; - virtual Status + Status CreateIndex(const IndexParam &index_param) override; - virtual Status + Status Insert(const std::string &table_name, - const std::vector &record_array, - std::vector &id_array) override; + const std::vector &record_array, + std::vector &id_array) override; - virtual Status + Status Search(const std::string &table_name, - const std::vector &query_record_array, - const std::vector &query_range_array, - int64_t topk, - int64_t nprobe, - std::vector &topk_query_result_array) override; + const std::vector &query_record_array, + const std::vector &query_range_array, + int64_t topk, + int64_t nprobe, + std::vector &topk_query_result_array) override; - virtual Status + Status DescribeTable(const std::string &table_name, TableSchema &table_schema) override; - virtual Status + Status CountTable(const std::string &table_name, int64_t &row_count) override; - virtual Status + Status ShowTables(std::vector &table_array) override; - virtual std::string + std::string ClientVersion() const override; - virtual std::string + std::string ServerVersion() const override; - virtual std::string + std::string ServerStatus() const override; - virtual std::string + std::string DumpTaskTables() const override; - virtual Status + Status DeleteByRange(Range &range, const std::string &table_name) override; - virtual Status + Status PreloadTable(const std::string &table_name) const override; - virtual Status + Status DescribeIndex(const std::string &table_name, IndexParam &index_param) const override; - virtual Status + Status DropIndex(const std::string &table_name) const override; -private: + private: std::shared_ptr<::grpc::Channel> channel_; -private: + private: std::shared_ptr client_ptr_; bool connected_ = false; }; -} +} // namespace milvus diff --git a/cpp/src/sdk/grpc/GrpcClient.cpp b/cpp/src/sdk/grpc/GrpcClient.cpp index 67bb85da8ef2d37d324fb006ca3cf4375d01107f..df86f0a3e7402bb05be25956287a2821ef9fbbef 100644 --- a/cpp/src/sdk/grpc/GrpcClient.cpp +++ b/cpp/src/sdk/grpc/GrpcClient.cpp @@ -15,13 +15,17 @@ // specific language governing permissions and limitations // under the License. +#include "sdk/grpc/GrpcClient.h" + #include #include #include #include #include -#include "GrpcClient.h" +#include +#include +#include using grpc::Channel; using grpc::ClientContext; @@ -31,15 +35,14 @@ using grpc::ClientWriter; using grpc::Status; namespace milvus { -GrpcClient::GrpcClient(std::shared_ptr<::grpc::Channel>& channel) - : stub_(::milvus::grpc::MilvusService::NewStub(channel)) { - +GrpcClient::GrpcClient(std::shared_ptr<::grpc::Channel> &channel) + : stub_(::milvus::grpc::MilvusService::NewStub(channel)) { } GrpcClient::~GrpcClient() = default; Status -GrpcClient::CreateTable(const ::milvus::grpc::TableSchema& table_schema) { +GrpcClient::CreateTable(const ::milvus::grpc::TableSchema &table_schema) { ClientContext context; grpc::Status response; ::grpc::Status grpc_status = stub_->CreateTable(&context, table_schema, &response); @@ -57,8 +60,8 @@ GrpcClient::CreateTable(const ::milvus::grpc::TableSchema& table_schema) { } bool -GrpcClient::HasTable(const ::milvus::grpc::TableName& table_name, - Status& status) { +GrpcClient::HasTable(const ::milvus::grpc::TableName &table_name, + Status &status) { ClientContext context; ::milvus::grpc::BoolReply response; ::grpc::Status grpc_status = stub_->HasTable(&context, table_name, &response); @@ -76,7 +79,7 @@ GrpcClient::HasTable(const ::milvus::grpc::TableName& table_name, } Status -GrpcClient::DropTable(const ::milvus::grpc::TableName& table_name) { +GrpcClient::DropTable(const ::milvus::grpc::TableName &table_name) { ClientContext context; grpc::Status response; ::grpc::Status grpc_status = stub_->DropTable(&context, table_name, &response); @@ -94,7 +97,7 @@ GrpcClient::DropTable(const ::milvus::grpc::TableName& table_name) { } Status -GrpcClient::CreateIndex(const ::milvus::grpc::IndexParam& index_param) { +GrpcClient::CreateIndex(const ::milvus::grpc::IndexParam &index_param) { ClientContext context; grpc::Status response; ::grpc::Status grpc_status = stub_->CreateIndex(&context, index_param, &response); @@ -112,9 +115,9 @@ GrpcClient::CreateIndex(const ::milvus::grpc::IndexParam& index_param) { } void -GrpcClient::Insert(::milvus::grpc::VectorIds& vector_ids, - const ::milvus::grpc::InsertParam& insert_param, - Status& status) { +GrpcClient::Insert(::milvus::grpc::VectorIds &vector_ids, + const ::milvus::grpc::InsertParam &insert_param, + Status &status) { ClientContext context; ::grpc::Status grpc_status = stub_->Insert(&context, insert_param, &vector_ids); @@ -133,7 +136,7 @@ GrpcClient::Insert(::milvus::grpc::VectorIds& vector_ids, } Status -GrpcClient::Search(::milvus::grpc::TopKQueryResultList& topk_query_result_list, +GrpcClient::Search(::milvus::grpc::TopKQueryResultList &topk_query_result_list, const ::milvus::grpc::SearchParam &search_param) { ::milvus::grpc::TopKQueryResult query_result; ClientContext context; @@ -154,8 +157,8 @@ GrpcClient::Search(::milvus::grpc::TopKQueryResultList& topk_query_result_list, } Status -GrpcClient::DescribeTable(::milvus::grpc::TableSchema& grpc_schema, - const std::string& table_name) { +GrpcClient::DescribeTable(::milvus::grpc::TableSchema &grpc_schema, + const std::string &table_name) { ClientContext context; ::milvus::grpc::TableName grpc_tablename; grpc_tablename.set_table_name(table_name); @@ -170,14 +173,14 @@ GrpcClient::DescribeTable(::milvus::grpc::TableSchema& grpc_schema, if (grpc_schema.status().error_code() != grpc::SUCCESS) { std::cerr << grpc_schema.status().reason() << std::endl; return Status(StatusCode::ServerFailed, - grpc_schema.status().reason()); + grpc_schema.status().reason()); } return Status::OK(); } int64_t -GrpcClient::CountTable(const std::string& table_name, Status& status) { +GrpcClient::CountTable(const std::string &table_name, Status &status) { ClientContext context; ::milvus::grpc::TableRowCount response; ::milvus::grpc::TableName grpc_tablename; @@ -186,7 +189,7 @@ GrpcClient::CountTable(const std::string& table_name, Status& status) { if (!grpc_status.ok()) { std::cerr << "DescribeTable rpc failed!" << std::endl; - status = Status(StatusCode::RPCFailed, grpc_status.error_message()); + status = Status(StatusCode::RPCFailed, grpc_status.error_message()); return -1; } @@ -223,7 +226,7 @@ GrpcClient::ShowTables(milvus::grpc::TableNameList &table_name_list) { Status GrpcClient::Cmd(std::string &result, - const std::string& cmd) { + const std::string &cmd) { ClientContext context; ::milvus::grpc::StringReply response; ::milvus::grpc::Command command; @@ -321,4 +324,4 @@ GrpcClient::DropIndex(grpc::TableName &table_name) { return Status::OK(); } -} \ No newline at end of file +} // namespace milvus diff --git a/cpp/src/sdk/grpc/GrpcClient.h b/cpp/src/sdk/grpc/GrpcClient.h index 08714d10e097738646e3b6658b1acae1bf76a50b..8f81e83ae8fb35bbed04ed5bf8105b2c58747e44 100644 --- a/cpp/src/sdk/grpc/GrpcClient.h +++ b/cpp/src/sdk/grpc/GrpcClient.h @@ -16,6 +16,11 @@ // under the License. #pragma once + +#include "MilvusApi.h" +#include "grpc/gen-milvus/milvus.grpc.pb.h" +//#include "grpc/gen-status/status.grpc.pb.h" + #include #include #include @@ -28,55 +33,48 @@ #include #include #include -#include "MilvusApi.h" - -#include "milvus.grpc.pb.h" -//#include "status.grpc.pb.h" - -#include namespace milvus { class GrpcClient { -public: - explicit - GrpcClient(std::shared_ptr<::grpc::Channel>& channel); + public: + explicit GrpcClient(std::shared_ptr<::grpc::Channel> &channel); virtual ~GrpcClient(); Status - CreateTable(const grpc::TableSchema& table_schema); + CreateTable(const grpc::TableSchema &table_schema); bool - HasTable(const grpc::TableName& table_name, Status& status); + HasTable(const grpc::TableName &table_name, Status &status); Status - DropTable(const grpc::TableName& table_name); + DropTable(const grpc::TableName &table_name); Status - CreateIndex(const grpc::IndexParam& index_param); + CreateIndex(const grpc::IndexParam &index_param); void - Insert(grpc::VectorIds& vector_ids, - const grpc::InsertParam& insert_param, - Status& status); + Insert(grpc::VectorIds &vector_ids, + const grpc::InsertParam &insert_param, + Status &status); Status - Search(::milvus::grpc::TopKQueryResultList& topk_query_result_list, + Search(::milvus::grpc::TopKQueryResultList &topk_query_result_list, const grpc::SearchParam &search_param); Status - DescribeTable(grpc::TableSchema& grpc_schema, - const std::string& table_name); + DescribeTable(grpc::TableSchema &grpc_schema, + const std::string &table_name); int64_t - CountTable(const std::string& table_name, Status& status); + CountTable(const std::string &table_name, Status &status); Status ShowTables(milvus::grpc::TableNameList &table_name_list); Status - Cmd(std::string &result, const std::string& cmd); + Cmd(std::string &result, const std::string &cmd); Status DeleteByRange(grpc::DeleteByRangeParam &delete_by_range_param); @@ -93,8 +91,8 @@ public: Status Disconnect(); -private: + private: std::unique_ptr stub_; }; -} +} // namespace milvus diff --git a/cpp/src/sdk/include/MilvusApi.h b/cpp/src/sdk/include/MilvusApi.h index 05de495fbc35b574ed419bb2dfaf107b3b7f9421..e6025fd52e541c7c36e612f50c7d483ab901869b 100644 --- a/cpp/src/sdk/include/MilvusApi.h +++ b/cpp/src/sdk/include/MilvusApi.h @@ -28,7 +28,6 @@ */ namespace milvus { - /** * @brief Index Type */ @@ -108,7 +107,6 @@ struct IndexParam { */ class Connection { public: - /** * @brief CreateConnection * @@ -131,7 +129,7 @@ class Connection { */ static Status - Destroy(std::shared_ptr& connection_ptr); + Destroy(std::shared_ptr &connection_ptr); /** * @brief Connect @@ -180,7 +178,6 @@ class Connection { virtual Status Disconnect() = 0; - /** * @brief Create table method * @@ -193,7 +190,6 @@ class Connection { virtual Status CreateTable(const TableSchema ¶m) = 0; - /** * @brief Test table existence method * @@ -206,7 +202,6 @@ class Connection { virtual bool HasTable(const std::string &table_name) = 0; - /** * @brief Delete table method * @@ -248,8 +243,8 @@ class Connection { */ virtual Status Insert(const std::string &table_name, - const std::vector &record_array, - std::vector &id_array) = 0; + const std::vector &record_array, + std::vector &id_array) = 0; /** * @brief Search vector @@ -266,11 +261,11 @@ class Connection { */ virtual Status Search(const std::string &table_name, - const std::vector &query_record_array, - const std::vector &query_range_array, - int64_t topk, - int64_t nprobe, - std::vector &topk_query_result_array) = 0; + const std::vector &query_record_array, + const std::vector &query_range_array, + int64_t topk, + int64_t nprobe, + std::vector &topk_query_result_array) = 0; /** * @brief Show table description @@ -297,7 +292,7 @@ class Connection { */ virtual Status CountTable(const std::string &table_name, - int64_t &row_count) = 0; + int64_t &row_count) = 0; /** * @brief Show all tables in database @@ -395,4 +390,4 @@ class Connection { DropIndex(const std::string &table_name) const = 0; }; -} \ No newline at end of file +} // namespace milvus diff --git a/cpp/src/sdk/include/Status.h b/cpp/src/sdk/include/Status.h index cdb5f54ede9131fc80c5c05500d246cd5c818d52..670d9662b4a23700fb39d163225a1c5071366acf 100644 --- a/cpp/src/sdk/include/Status.h +++ b/cpp/src/sdk/include/Status.h @@ -29,6 +29,7 @@ namespace milvus { */ enum class StatusCode { OK = 0, + // system error section UnknownError = 1, NotSupported, @@ -44,7 +45,7 @@ enum class StatusCode { * @brief Status for SDK interface return */ class Status { -public: + public: Status(StatusCode code, const std::string &msg); Status(); ~Status(); @@ -60,28 +61,32 @@ public: operator=(Status &&s); static Status - OK() { return Status(); } + OK() { + return Status(); + } bool - ok() const { return state_ == nullptr || code() == StatusCode::OK; } + ok() const { + return state_ == nullptr || code() == StatusCode::OK; + } StatusCode code() const { - return (state_ == nullptr) ? StatusCode::OK : *(StatusCode*)(state_); + return (state_ == nullptr) ? StatusCode::OK : *(StatusCode *) (state_); } std::string message() const; -private: + private: inline void CopyFrom(const Status &s); inline void MoveFrom(Status &s); -private: + private: const char *state_ = nullptr; }; // Status -} //Milvus \ No newline at end of file +} // namespace milvus diff --git a/cpp/src/sdk/interface/ConnectionImpl.cpp b/cpp/src/sdk/interface/ConnectionImpl.cpp index 3730cfd191c01950306f45f7cd7ce185eb596ade..5a01cd1a797bf536ad24d52b70711eeda48da1c9 100644 --- a/cpp/src/sdk/interface/ConnectionImpl.cpp +++ b/cpp/src/sdk/interface/ConnectionImpl.cpp @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -#include "ConnectionImpl.h" +#include "sdk/interface/ConnectionImpl.h" namespace milvus { @@ -25,7 +25,7 @@ Connection::Create() { } Status -Connection::Destroy(std::shared_ptr& connection_ptr) { +Connection::Destroy(std::shared_ptr &connection_ptr) { if (connection_ptr != nullptr) { return connection_ptr->Disconnect(); } @@ -84,19 +84,18 @@ ConnectionImpl::CreateIndex(const IndexParam &index_param) { Status ConnectionImpl::Insert(const std::string &table_name, - const std::vector &record_array, - std::vector &id_array) { + const std::vector &record_array, + std::vector &id_array) { return client_proxy_->Insert(table_name, record_array, id_array); } - Status ConnectionImpl::Search(const std::string &table_name, - const std::vector &query_record_array, - const std::vector &query_range_array, - int64_t topk, - int64_t nprobe, - std::vector &topk_query_result_array) { + const std::vector &query_record_array, + const std::vector &query_range_array, + int64_t topk, + int64_t nprobe, + std::vector &topk_query_result_array) { return client_proxy_->Search(table_name, query_record_array, query_range_array, topk, nprobe, topk_query_result_array); } @@ -133,7 +132,7 @@ ConnectionImpl::DumpTaskTables() const { Status ConnectionImpl::DeleteByRange(Range &range, - const std::string &table_name) { + const std::string &table_name) { return client_proxy_->DeleteByRange(range, table_name); } @@ -143,7 +142,7 @@ ConnectionImpl::PreloadTable(const std::string &table_name) const { } Status -ConnectionImpl::DescribeIndex(const std::string &table_name, IndexParam& index_param) const { +ConnectionImpl::DescribeIndex(const std::string &table_name, IndexParam &index_param) const { return client_proxy_->DescribeIndex(table_name, index_param); } @@ -152,4 +151,4 @@ ConnectionImpl::DropIndex(const std::string &table_name) const { return client_proxy_->DropIndex(table_name); } -} +} // namespace milvus diff --git a/cpp/src/sdk/interface/ConnectionImpl.h b/cpp/src/sdk/interface/ConnectionImpl.h index 6a402fcb0d8a08cdccecc6f6c93efa92e421a7d9..7d5e09088249b58b74d0d3f90dea822f80d9528b 100644 --- a/cpp/src/sdk/interface/ConnectionImpl.h +++ b/cpp/src/sdk/interface/ConnectionImpl.h @@ -18,88 +18,92 @@ #pragma once #include "MilvusApi.h" -#include "src/sdk/grpc/ClientProxy.h" +#include "sdk/grpc/ClientProxy.h" + +#include +#include +#include namespace milvus { class ConnectionImpl : public Connection { -public: + public: ConnectionImpl(); // Implementations of the Connection interface - virtual Status + Status Connect(const ConnectParam ¶m) override; - virtual Status + Status Connect(const std::string &uri) override; - virtual Status + Status Connected() const override; - virtual Status + Status Disconnect() override; - virtual Status + Status CreateTable(const TableSchema ¶m) override; - virtual - bool HasTable(const std::string &table_name) override; + bool + HasTable(const std::string &table_name) override; - virtual Status + Status DropTable(const std::string &table_name) override; - virtual Status + Status CreateIndex(const IndexParam &index_param) override; - virtual Status + Status Insert(const std::string &table_name, const std::vector &record_array, std::vector &id_array) override; - virtual Status + Status Search(const std::string &table_name, - const std::vector &query_record_array, - const std::vector &query_range_array, - int64_t topk, - int64_t nprobe, - std::vector &topk_query_result_array) override; + const std::vector &query_record_array, + const std::vector &query_range_array, + int64_t topk, + int64_t nprobe, + std::vector &topk_query_result_array) override; - virtual Status + Status DescribeTable(const std::string &table_name, TableSchema &table_schema) override; - virtual Status + Status CountTable(const std::string &table_name, int64_t &row_count) override; - virtual Status + Status ShowTables(std::vector &table_array) override; - virtual std::string + std::string ClientVersion() const override; - virtual std::string + std::string ServerVersion() const override; - virtual std::string + std::string ServerStatus() const override; - virtual std::string + std::string DumpTaskTables() const override; - virtual Status + Status DeleteByRange(Range &range, const std::string &table_name) override; - virtual Status + Status PreloadTable(const std::string &table_name) const override; - virtual Status - DescribeIndex(const std::string &table_name, IndexParam& index_param) const override; + Status + DescribeIndex(const std::string &table_name, IndexParam &index_param) const override; - virtual Status + Status DropIndex(const std::string &table_name) const override; -private: + private: std::shared_ptr client_proxy_; }; -} +} // namespace milvus diff --git a/cpp/src/sdk/interface/Status.cpp b/cpp/src/sdk/interface/Status.cpp index e6fd06c72e8db09be76cb4a30919b0e251d01369..a8780f2ddde94e992c5360596ca72c13aff09fa6 100644 --- a/cpp/src/sdk/interface/Status.cpp +++ b/cpp/src/sdk/interface/Status.cpp @@ -23,12 +23,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]; memcpy(result, &code, CODE_WIDTH); memcpy(result + CODE_WIDTH, &length, sizeof(length)); memcpy(result + sizeof(length) + CODE_WIDTH, msg.data(), length); @@ -37,8 +37,7 @@ Status::Status(StatusCode code, const std::string& msg) { } Status::Status() - : state_(nullptr) { - + : state_(nullptr) { } Status::~Status() { @@ -46,22 +45,22 @@ Status::~Status() { } Status::Status(const Status &s) - : state_(nullptr) { + : state_(nullptr) { CopyFrom(s); } -Status& +Status & Status::operator=(const Status &s) { CopyFrom(s); return *this; } Status::Status(Status &&s) - : state_(nullptr) { + : state_(nullptr) { MoveFrom(s); } -Status& +Status & Status::operator=(Status &&s) { MoveFrom(s); return *this; @@ -71,7 +70,7 @@ void Status::CopyFrom(const Status &s) { delete state_; state_ = nullptr; - if(s.state_ == nullptr) { + if (s.state_ == nullptr) { return; } @@ -79,7 +78,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,12 +97,13 @@ 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); } return msg; } -} +} // namespace milvus +