forked from nmslib/nmslib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_client.py
More file actions
executable file
·96 lines (73 loc) · 2.76 KB
/
query_client.py
File metadata and controls
executable file
·96 lines (73 loc) · 2.76 KB
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
#!/usr/bin/env python
import sys, glob
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from protocol import QueryService
from protocol import ttypes
from datetime import datetime
import argparse
def error_exit(s):
print("Error: %s" % s)
sys.exit(1)
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', help='TCP/IP server port number', action='store', type=int,required=True)
parser.add_argument('-a', '--addr', help='TCP/IP server address', action='store', required=True)
parser.add_argument('-k', '--knn', help='k for k-NN search', action='store', required=False,type=int)
parser.add_argument('-r', '--range', help='range for the range search', action='store', required=False,type=float)
parser.add_argument('-t', '--queryTimeParams', help='Query time parameter', action='store', default='')
parser.add_argument('-o', '--retObj', help='Return string representation of found objects?', action='store_true', default=False)
parser.add_argument('-e', '--retExternId', help='Return external IDs?', action='store_true', default=False)
args = parser.parse_args()
host=args.addr
port=args.port
queryTimeParams=args.queryTimeParams
retObj = args.retObj
retExternId = args.retObj
try:
print("Host %s socket %d" % (host,port))
# Make socket
transport = TSocket.TSocket(host, port)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = QueryService.Client(protocol)
transport.open()
queryObj = ''
for s in sys.stdin:
queryObj = queryObj + s + '\n'
if args.queryTimeParams != '':
client.setQueryTimeParams(args.queryTimeParams)
t1 = datetime.now()
res = None
if not args.knn is None:
k = args.knn
if not args.range is None:
error_exit('Range search is not allowed if the KNN search is specified!')
print("Running %d-NN search" % k)
res = client.knnQuery(k, queryObj, retObj, retExternId)
elif not args.range is None:
r = args.range
if not args.knn is None:
error_exit('KNN search is not allowed if the range search is specified')
print("Running range search, range=%f" % r)
res = client.rangeQuery(r, queryObj, retObj, retExternId)
else:
error_exit("Wrong search type %s" % searchType)
t2 = datetime.now()
diff = t2- t1
elapsed = diff.seconds * 1e3 + diff.microseconds / 1e3
print("Finished in %f ms" % elapsed)
for e in res:
s=''
if retExternId:
s='externId=' + e.externId
print("id=%d dist=%f %s" % (e.id, e.dist, s))
if retObj:
print(e.obj)
# Close!
transport.close()
except(Thrift.TException, tx):
print('%s' % (tx.message))