mirror of https://github.com/milvus-io/milvus.git
refine sample code
Former-commit-id: 18bda8e3585dfd0bcfc699c6788a9af0eb53fc5fpull/191/head
parent
360d56fd48
commit
5386d5e70e
|
@ -8,14 +8,12 @@ aux_source_directory(src src_files)
|
|||
|
||||
include_directories(src)
|
||||
include_directories(../../megasearch_sdk/include)
|
||||
include_directories(/usr/include)
|
||||
|
||||
link_directories(${CMAKE_BINARY_DIR}/megasearch_sdk)
|
||||
|
||||
add_executable(sdk_simple
|
||||
./main.cpp
|
||||
${src_files}
|
||||
${service_files}
|
||||
)
|
||||
|
||||
target_link_libraries(sdk_simple
|
||||
|
|
|
@ -19,7 +19,8 @@ main(int argc, char *argv[]) {
|
|||
printf("Client start...\n");
|
||||
|
||||
std::string app_name = basename(argv[0]);
|
||||
static struct option long_options[] = {{"conf_file", optional_argument, 0, 'c'},
|
||||
static struct option long_options[] = {{"server", optional_argument, 0, 's'},
|
||||
{"port", optional_argument, 0, 'p'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{NULL, 0, 0, 0}};
|
||||
|
||||
|
@ -28,9 +29,9 @@ main(int argc, char *argv[]) {
|
|||
app_name = argv[0];
|
||||
|
||||
int value;
|
||||
while ((value = getopt_long(argc, argv, "c:p:dh", long_options, &option_index)) != -1) {
|
||||
while ((value = getopt_long(argc, argv, "s:p:h", long_options, &option_index)) != -1) {
|
||||
switch (value) {
|
||||
case 'h': {
|
||||
case 's': {
|
||||
char *address_ptr = strdup(optarg);
|
||||
address = address_ptr;
|
||||
free(address_ptr);
|
||||
|
@ -38,12 +39,14 @@ main(int argc, char *argv[]) {
|
|||
}
|
||||
case 'p': {
|
||||
char *port_ptr = strdup(optarg);
|
||||
address = port_ptr;
|
||||
port = port_ptr;
|
||||
free(port_ptr);
|
||||
break;
|
||||
}
|
||||
case 'h':
|
||||
default:
|
||||
break;
|
||||
print_help(app_name);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,7 +61,8 @@ void
|
|||
print_help(const std::string &app_name) {
|
||||
printf("\n Usage: %s [OPTIONS]\n\n", app_name.c_str());
|
||||
printf(" Options:\n");
|
||||
printf(" -h Megasearch server address\n");
|
||||
printf(" -p Megasearch server port\n");
|
||||
printf(" -s --server Server address, default 127.0.0.1\n");
|
||||
printf(" -p --port Server port, default 33001\n");
|
||||
printf(" -h --help Print help information\n");
|
||||
printf("\n");
|
||||
}
|
|
@ -13,6 +13,16 @@
|
|||
using namespace megasearch;
|
||||
|
||||
namespace {
|
||||
std::string GetTableName();
|
||||
|
||||
static const std::string TABLE_NAME = GetTableName();
|
||||
static const std::string VECTOR_COLUMN_NAME = "face_vector";
|
||||
static const std::string ID_COLUMN_NAME = "aid";
|
||||
static const std::string CITY_COLUMN_NAME = "city";
|
||||
static constexpr int64_t TABLE_DIMENSION = 512;
|
||||
static constexpr int64_t TOTAL_ROW_COUNT = 100000;
|
||||
static constexpr int64_t TOP_K = 10;
|
||||
static constexpr int64_t SEARCH_TARGET = 5000; //change this value, result is different
|
||||
|
||||
#define BLOCK_SPLITER std::cout << "===========================================" << std::endl;
|
||||
|
||||
|
@ -76,12 +86,6 @@ namespace {
|
|||
return s_id;
|
||||
}
|
||||
|
||||
static const std::string TABLE_NAME = GetTableName();
|
||||
static const std::string VECTOR_COLUMN_NAME = "face_vector";
|
||||
static const std::string AGE_COLUMN_NAME = "age";
|
||||
static const std::string CITY_COLUMN_NAME = "city";
|
||||
static const int64_t TABLE_DIMENSION = 512;
|
||||
|
||||
TableSchema BuildTableSchema() {
|
||||
TableSchema tb_schema;
|
||||
VectorColumn col1;
|
||||
|
@ -90,7 +94,7 @@ namespace {
|
|||
col1.store_raw_vector = true;
|
||||
tb_schema.vector_column_array.emplace_back(col1);
|
||||
|
||||
Column col2 = {ColumnType::int8, AGE_COLUMN_NAME};
|
||||
Column col2 = {ColumnType::int8, ID_COLUMN_NAME};
|
||||
tb_schema.attribute_column_array.emplace_back(col2);
|
||||
|
||||
Column col3 = {ColumnType::int16, CITY_COLUMN_NAME};
|
||||
|
@ -139,7 +143,7 @@ namespace {
|
|||
if(vector_record_array) {
|
||||
RowRecord record;
|
||||
record.vector_map.insert(std::make_pair(VECTOR_COLUMN_NAME, f_p));
|
||||
record.attribute_map[AGE_COLUMN_NAME] = std::to_string(k%100);
|
||||
record.attribute_map[ID_COLUMN_NAME] = std::to_string(k);
|
||||
record.attribute_map[CITY_COLUMN_NAME] = CITY_MAP.at(k%CITY_MAP.size());
|
||||
vector_record_array->emplace_back(record);
|
||||
}
|
||||
|
@ -147,7 +151,7 @@ namespace {
|
|||
if(query_record_array) {
|
||||
QueryRecord record;
|
||||
record.vector_map.insert(std::make_pair(VECTOR_COLUMN_NAME, f_p));
|
||||
record.selected_column_array.push_back(AGE_COLUMN_NAME);
|
||||
record.selected_column_array.push_back(ID_COLUMN_NAME);
|
||||
record.selected_column_array.push_back(CITY_COLUMN_NAME);
|
||||
query_record_array->emplace_back(record);
|
||||
}
|
||||
|
@ -161,16 +165,20 @@ ClientTest::Test(const std::string& address, const std::string& port) {
|
|||
ConnectParam param = { address, port };
|
||||
conn->Connect(param);
|
||||
|
||||
{//get server version
|
||||
{//server version
|
||||
std::string version = conn->ServerVersion();
|
||||
std::cout << "MegaSearch server version: " << version << std::endl;
|
||||
}
|
||||
|
||||
{//sdk version
|
||||
std::string version = conn->ClientVersion();
|
||||
std::cout << "SDK version: " << version << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
std::cout << "ShowTables" << std::endl;
|
||||
std::vector<std::string> tables;
|
||||
Status stat = conn->ShowTables(tables);
|
||||
std::cout << "Function call status: " << stat.ToString() << std::endl;
|
||||
std::cout << "ShowTables function call status: " << stat.ToString() << std::endl;
|
||||
std::cout << "All tables: " << std::endl;
|
||||
for(auto& table : tables) {
|
||||
std::cout << "\t" << table << std::endl;
|
||||
|
@ -180,26 +188,23 @@ ClientTest::Test(const std::string& address, const std::string& port) {
|
|||
{//create table
|
||||
TableSchema tb_schema = BuildTableSchema();
|
||||
PrintTableSchema(tb_schema);
|
||||
std::cout << "CreateTable" << std::endl;
|
||||
Status stat = conn->CreateTable(tb_schema);
|
||||
std::cout << "Function call status: " << stat.ToString() << std::endl;
|
||||
std::cout << "CreateTable function call status: " << stat.ToString() << std::endl;
|
||||
}
|
||||
|
||||
{//describe table
|
||||
TableSchema tb_schema;
|
||||
std::cout << "DescribeTable" << std::endl;
|
||||
Status stat = conn->DescribeTable(TABLE_NAME, tb_schema);
|
||||
std::cout << "Function call status: " << stat.ToString() << std::endl;
|
||||
std::cout << "DescribeTable function call status: " << stat.ToString() << std::endl;
|
||||
PrintTableSchema(tb_schema);
|
||||
}
|
||||
|
||||
{//add vectors
|
||||
std::vector<RowRecord> record_array;
|
||||
BuildVectors(0, 10000, &record_array, nullptr);
|
||||
BuildVectors(0, TOTAL_ROW_COUNT, &record_array, nullptr);
|
||||
std::vector<int64_t> record_ids;
|
||||
std::cout << "AddVector" << std::endl;
|
||||
Status stat = conn->AddVector(TABLE_NAME, record_array, record_ids);
|
||||
std::cout << "Function call status: " << stat.ToString() << std::endl;
|
||||
std::cout << "AddVector function call status: " << stat.ToString() << std::endl;
|
||||
PrintRecordIdArray(record_ids);
|
||||
}
|
||||
|
||||
|
@ -207,18 +212,17 @@ ClientTest::Test(const std::string& address, const std::string& port) {
|
|||
std::cout << "Waiting data persist. Sleep 10 seconds ..." << std::endl;
|
||||
sleep(10);
|
||||
std::vector<QueryRecord> record_array;
|
||||
BuildVectors(500, 510, nullptr, &record_array);
|
||||
BuildVectors(SEARCH_TARGET, SEARCH_TARGET + 10, nullptr, &record_array);
|
||||
|
||||
std::vector<TopKQueryResult> topk_query_result_array;
|
||||
std::cout << "SearchVector" << std::endl;
|
||||
Status stat = conn->SearchVector(TABLE_NAME, record_array, topk_query_result_array, 10);
|
||||
std::cout << "Function call status: " << stat.ToString() << std::endl;
|
||||
Status stat = conn->SearchVector(TABLE_NAME, record_array, topk_query_result_array, TOP_K);
|
||||
std::cout << "SearchVector function call status: " << stat.ToString() << std::endl;
|
||||
PrintSearchResult(topk_query_result_array);
|
||||
}
|
||||
|
||||
// {//delete table
|
||||
// Status stat = conn->DeleteTable(TABLE_NAME);
|
||||
// std::cout << "Delete table result: " << stat.ToString() << std::endl;
|
||||
// std::cout << "DeleteTable function call status: " << stat.ToString() << std::endl;
|
||||
// }
|
||||
|
||||
{//server status
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "utils/Log.h"
|
||||
#include "utils/CommonUtil.h"
|
||||
|
||||
#include "rocksdb/db.h"
|
||||
#include "rocksdb/slice.h"
|
||||
#include "rocksdb/options.h"
|
||||
|
||||
|
|
|
@ -8,13 +8,16 @@
|
|||
#include "utils/Error.h"
|
||||
#include "VecIdMapper.h"
|
||||
|
||||
#include "rocksdb/db.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
|
||||
namespace rocksdb {
|
||||
class DB;
|
||||
class ColumnFamilyHandle;
|
||||
}
|
||||
|
||||
namespace zilliz {
|
||||
namespace vecwise {
|
||||
namespace server {
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "rocksdb/options.h"
|
||||
|
||||
#include <exception>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace zilliz {
|
||||
namespace vecwise {
|
||||
|
@ -30,6 +31,30 @@ IVecIdMapper* IVecIdMapper::GetInstance() {
|
|||
#endif
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
class SimpleIdMapper : public IVecIdMapper{
|
||||
public:
|
||||
SimpleIdMapper();
|
||||
~SimpleIdMapper();
|
||||
|
||||
ServerError AddGroup(const std::string& group) override;
|
||||
bool IsGroupExist(const std::string& group) const override;
|
||||
ServerError AllGroups(std::vector<std::string>& groups) const override;
|
||||
|
||||
ServerError Put(const std::string& nid, const std::string& sid, const std::string& group = "") override;
|
||||
ServerError Put(const std::vector<std::string>& nid, const std::vector<std::string>& sid, const std::string& group = "") override;
|
||||
|
||||
ServerError Get(const std::string& nid, std::string& sid, const std::string& group = "") const override;
|
||||
ServerError Get(const std::vector<std::string>& nid, std::vector<std::string>& sid, const std::string& group = "") const override;
|
||||
|
||||
ServerError Delete(const std::string& nid, const std::string& group = "") override;
|
||||
ServerError DeleteGroup(const std::string& group) override;
|
||||
|
||||
private:
|
||||
using ID_MAPPING = std::unordered_map<std::string, std::string>;
|
||||
mutable std::unordered_map<std::string, ID_MAPPING> id_groups_;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
SimpleIdMapper::SimpleIdMapper() {
|
||||
|
||||
|
|
|
@ -9,11 +9,6 @@
|
|||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace rocksdb {
|
||||
class DB;
|
||||
}
|
||||
|
||||
namespace zilliz {
|
||||
namespace vecwise {
|
||||
|
@ -40,29 +35,6 @@ public:
|
|||
virtual ServerError DeleteGroup(const std::string& group) = 0;
|
||||
};
|
||||
|
||||
class SimpleIdMapper : public IVecIdMapper{
|
||||
public:
|
||||
SimpleIdMapper();
|
||||
~SimpleIdMapper();
|
||||
|
||||
ServerError AddGroup(const std::string& group) override;
|
||||
bool IsGroupExist(const std::string& group) const override;
|
||||
ServerError AllGroups(std::vector<std::string>& groups) const override;
|
||||
|
||||
ServerError Put(const std::string& nid, const std::string& sid, const std::string& group = "") override;
|
||||
ServerError Put(const std::vector<std::string>& nid, const std::vector<std::string>& sid, const std::string& group = "") override;
|
||||
|
||||
ServerError Get(const std::string& nid, std::string& sid, const std::string& group = "") const override;
|
||||
ServerError Get(const std::vector<std::string>& nid, std::vector<std::string>& sid, const std::string& group = "") const override;
|
||||
|
||||
ServerError Delete(const std::string& nid, const std::string& group = "") override;
|
||||
ServerError DeleteGroup(const std::string& group) override;
|
||||
|
||||
private:
|
||||
using ID_MAPPING = std::unordered_map<std::string, std::string>;
|
||||
mutable std::unordered_map<std::string, ID_MAPPING> id_groups_;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue