mirror of https://github.com/milvus-io/milvus.git
refine sdk code
Former-commit-id: 30ab87ccf76dadf9c65489d5a19c1134124faec5pull/191/head
parent
5386d5e70e
commit
397457112b
|
@ -162,8 +162,12 @@ namespace {
|
|||
void
|
||||
ClientTest::Test(const std::string& address, const std::string& port) {
|
||||
std::shared_ptr<Connection> conn = Connection::Create();
|
||||
ConnectParam param = { address, port };
|
||||
conn->Connect(param);
|
||||
|
||||
{//connect server
|
||||
ConnectParam param = {address, port};
|
||||
Status stat = conn->Connect(param);
|
||||
std::cout << "Connect function call status: " << stat.ToString() << std::endl;
|
||||
}
|
||||
|
||||
{//server version
|
||||
std::string version = conn->ServerVersion();
|
||||
|
|
|
@ -14,7 +14,8 @@ enum class StatusCode {
|
|||
OK = 0,
|
||||
Invalid = 1,
|
||||
UnknownError = 2,
|
||||
NotSupported = 3
|
||||
NotSupported = 3,
|
||||
NotConnected = 4
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,32 +16,50 @@ ClientProxy::ClientPtr() const {
|
|||
return client_ptr;
|
||||
}
|
||||
|
||||
bool ClientProxy::IsConnected() const {
|
||||
return (client_ptr != nullptr && connected_);
|
||||
}
|
||||
|
||||
Status
|
||||
ClientProxy::Connect(const ConnectParam ¶m) {
|
||||
Disconnect();
|
||||
|
||||
int32_t port = atoi(param.port.c_str());
|
||||
return ClientPtr()->Connect(param.ip_address, port, THRIFT_PROTOCOL_BINARY);
|
||||
Status status = ClientPtr()->Connect(param.ip_address, port, THRIFT_PROTOCOL_BINARY);
|
||||
if(status.ok()) {
|
||||
connected_ = true;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
Status
|
||||
ClientProxy::Connect(const std::string &uri) {
|
||||
Disconnect();
|
||||
|
||||
return Status::NotSupported("Connect interface is not supported.");
|
||||
size_t index = uri.find_first_of(":", 0);
|
||||
if((index == std::string::npos)) {
|
||||
return Status::Invalid("Invalid uri");
|
||||
}
|
||||
|
||||
ConnectParam param;
|
||||
param.ip_address = uri.substr(0, index);
|
||||
param.port = uri.substr(index + 1);
|
||||
|
||||
return Connect(param);
|
||||
}
|
||||
|
||||
Status
|
||||
ClientProxy::Connected() const {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
try {
|
||||
std::string info;
|
||||
ClientPtr()->interface()->Ping(info, "");
|
||||
} catch ( std::exception& ex) {
|
||||
return Status(StatusCode::UnknownError, "connection lost: " + std::string(ex.what()));
|
||||
return Status(StatusCode::NotConnected, "connection lost: " + std::string(ex.what()));
|
||||
}
|
||||
|
||||
return Status::OK();
|
||||
|
@ -49,10 +67,11 @@ ClientProxy::Connected() const {
|
|||
|
||||
Status
|
||||
ClientProxy::Disconnect() {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
connected_ = false;
|
||||
return ClientPtr()->Disconnect();
|
||||
}
|
||||
|
||||
|
@ -63,8 +82,8 @@ ClientProxy::ClientVersion() const {
|
|||
|
||||
Status
|
||||
ClientProxy::CreateTable(const TableSchema ¶m) {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -103,8 +122,8 @@ ClientProxy::CreateTable(const TableSchema ¶m) {
|
|||
|
||||
Status
|
||||
ClientProxy::CreateTablePartition(const CreateTablePartitionParam ¶m) {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -132,8 +151,8 @@ ClientProxy::CreateTablePartition(const CreateTablePartitionParam ¶m) {
|
|||
|
||||
Status
|
||||
ClientProxy::DeleteTablePartition(const DeleteTablePartitionParam ¶m) {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -152,8 +171,8 @@ ClientProxy::DeleteTablePartition(const DeleteTablePartitionParam ¶m) {
|
|||
|
||||
Status
|
||||
ClientProxy::DeleteTable(const std::string &table_name) {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -170,8 +189,8 @@ Status
|
|||
ClientProxy::AddVector(const std::string &table_name,
|
||||
const std::vector<RowRecord> &record_array,
|
||||
std::vector<int64_t> &id_array) {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -205,8 +224,8 @@ ClientProxy::SearchVector(const std::string &table_name,
|
|||
const std::vector<QueryRecord> &query_record_array,
|
||||
std::vector<TopKQueryResult> &topk_query_result_array,
|
||||
int64_t topk) {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -253,8 +272,8 @@ ClientProxy::SearchVector(const std::string &table_name,
|
|||
|
||||
Status
|
||||
ClientProxy::DescribeTable(const std::string &table_name, TableSchema &table_schema) {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -290,8 +309,8 @@ ClientProxy::DescribeTable(const std::string &table_name, TableSchema &table_sch
|
|||
|
||||
Status
|
||||
ClientProxy::ShowTables(std::vector<std::string> &table_array) {
|
||||
if(client_ptr == nullptr) {
|
||||
return Status(StatusCode::UnknownError, "not connected");
|
||||
if(!IsConnected()) {
|
||||
return Status(StatusCode::NotConnected, "not connected to server");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -306,7 +325,7 @@ ClientProxy::ShowTables(std::vector<std::string> &table_array) {
|
|||
|
||||
std::string
|
||||
ClientProxy::ServerVersion() const {
|
||||
if(client_ptr == nullptr) {
|
||||
if(!IsConnected()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
@ -321,8 +340,8 @@ ClientProxy::ServerVersion() const {
|
|||
|
||||
std::string
|
||||
ClientProxy::ServerStatus() const {
|
||||
if(client_ptr == nullptr) {
|
||||
return "not connected";
|
||||
if(!IsConnected()) {
|
||||
return "not connected to server";
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
|
@ -51,9 +51,11 @@ public:
|
|||
private:
|
||||
std::shared_ptr<ThriftClient>& ClientPtr() const;
|
||||
|
||||
bool IsConnected() const;
|
||||
|
||||
private:
|
||||
mutable std::shared_ptr<ThriftClient> client_ptr;
|
||||
|
||||
bool connected_ = false;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ ThriftClient::Connect(const std::string& address, int32_t port, const std::strin
|
|||
client_ = std::make_shared<thrift::MegasearchServiceClient>(protocol_ptr);
|
||||
} catch ( std::exception& ex) {
|
||||
//CLIENT_LOG_ERROR << "connect encounter exception: " << ex.what();
|
||||
return Status(StatusCode::UnknownError, "failed to connect megasearch server" + std::string(ex.what()));
|
||||
return Status(StatusCode::NotConnected, "failed to connect megasearch server" + std::string(ex.what()));
|
||||
}
|
||||
|
||||
return Status::OK();
|
||||
|
|
|
@ -101,6 +101,8 @@ std::string Status::CodeAsString() const {
|
|||
break;
|
||||
case StatusCode::NotSupported: type = "Not Supported";
|
||||
break;
|
||||
case StatusCode::NotConnected: type = "Not Connected";
|
||||
break;
|
||||
default: type = "Unknown";
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue