forked from nmslib/nmslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryClient.cpp
201 lines (165 loc) · 5.54 KB
/
QueryClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Non-metric Space Library
*
* Authors: Bilegsaikhan Naidan (https://github.com/bileg), Leonid Boytsov (http://boytsov.info).
* With contributions from Lawrence Cayton (http://lcayton.com/) and others.
*
* For the complete list of contributors and further details see:
* https://github.com/searchivarius/NonMetricSpaceLib
*
* Copyright (c) 2015
*
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
*/
#include <memory>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include "QueryService.h"
#include "ztimer.h"
#include "params_def.h"
#include <boost/program_options.hpp>
using std::string;
using std::exception;
using std::stringstream;
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
using std::unique_ptr;
using namespace ::similarity;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
namespace po = boost::program_options;
enum SearchType {
kNoSearch, kKNNSearch, kRangeSearch
};
static void Usage(const char *prog,
const po::options_description& desc) {
std::cout << prog << std::endl
<< desc << std::endl;
}
void ParseCommandLineForClient(int argc, char*argv[],
string& host,
int& port,
SearchType& searchType,
int& k,
double& r,
bool& retExternId,
bool& retObj,
string& queryTimeParams
) {
po::options_description ProgOptDesc("Allowed options");
ProgOptDesc.add_options()
(HELP_PARAM_OPT.c_str(), HELP_PARAM_MSG.c_str())
(PORT_PARAM_OPT.c_str(), po::value<int>(&port)->required(), PORT_PARAM_MSG.c_str())
(ADDR_PARAM_OPT.c_str(), po::value<string>(&host)->required(), ADDR_PARAM_MSG.c_str())
(KNN_PARAM_OPT.c_str(), po::value<int>(&k), KNN_PARAM_MSG.c_str())
(RANGE_PARAM_OPT.c_str(), po::value<double>(&r), RANGE_PARAM_MSG.c_str())
(QUERY_TIME_PARAMS_PARAM_OPT.c_str(), po::value<string>(&queryTimeParams)->default_value(""), QUERY_TIME_PARAMS_PARAM_MSG.c_str())
(RET_EXT_ID_PARAM_OPT.c_str(), RET_EXT_ID_PARAM_MSG.c_str())
(RET_OBJ_PARAM_OPT.c_str(), RET_EXT_ID_PARAM_MSG.c_str())
;
po::variables_map vm;
try {
po::store(po::parse_command_line(argc, argv, ProgOptDesc), vm);
po::notify(vm);
} catch (const exception& e) {
Usage(argv[0], ProgOptDesc);
cerr << e.what() << endl;
exit(1);
}
if (vm.count("knn") == 1) {
if (vm.count("range") != 0) {
cerr << "Range search is not allowed if the KNN search is specified!";
Usage(argv[0], ProgOptDesc);
exit(1);
}
searchType = kKNNSearch;
} else if (vm.count("range") == 1) {
if (vm.count("knn") != 0) {
cerr << "KNN search is not allowed if the range search is specified";
Usage(argv[0], ProgOptDesc);
exit(1);
}
searchType = kRangeSearch;
} else {
searchType = kNoSearch;
}
retExternId = vm.count("retExternId") != 0;
retObj = vm.count("retObj") != 0;
if (vm.count("help") ) {
Usage(argv[0], ProgOptDesc);
exit(0);
}
}
int main(int argc, char *argv[]) {
string host;
int port = 0;
int k;
double r;
bool retExternId;
bool retObj;
SearchType searchType;
string queryTimeParams;
ParseCommandLineForClient(argc, argv,
host,
port,
searchType,
k, r,
retExternId,
retObj,
queryTimeParams);
// Let's read the query from the input stream
string s;
stringstream ss;
if (kNoSearch != searchType) {
while (getline(cin, s)) {
ss << s << endl;
}
}
string queryObjStr = ss.str();
boost::shared_ptr<TTransport> socket(new TSocket(host, port));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
QueryServiceClient client(protocol);
try {
transport->open();
try {
if (!queryTimeParams.empty()) {
client.setQueryTimeParams(queryTimeParams);
}
WallClockTimer wtm;
wtm.reset();
ReplyEntryList res;
if (kKNNSearch == searchType) {
cout << "Running a " << k << "-NN query" << endl;;
client.knnQuery(res, k, queryObjStr, retExternId, retObj);
}
if (kRangeSearch == searchType) {
cout << "Running a range query with radius = " << r << endl;
client.rangeQuery(res, r, queryObjStr, retExternId, retObj);
}
wtm.split();
cout << "Finished in: " << wtm.elapsed() / 1e3f << " ms" << endl;
for (auto e: res) {
cout << "id=" << e.id << " dist=" << e.dist << ( retExternId ? " externId=" + e.externId : string("")) << endl;
if (retObj) cout << e.obj << endl;
}
} catch (const QueryException& e) {
cerr << "Query execution error: " << e.message << endl;
exit(1);
}
transport->close(); // Close transport !!!
} catch (const TException& tx) {
cerr << "Connection error: " << tx.what() << endl;
exit(1);
}
return 0;
}