20
20
import json
21
21
import certifi
22
22
import uuid as uuidlib
23
+ from concurrent .futures import ThreadPoolExecutor
23
24
24
25
import grpc
25
26
from grpc ._cython .cygrpc import CompressionAlgorithm
42
43
## Classes
43
44
##########################################################################
44
45
45
- class Connection (object ):
46
46
47
+ class Connection (object ):
47
48
def __init__ (self , addrportstr , apikey = None ):
48
49
"""
49
50
Connects to a BTrDB server
@@ -57,7 +58,7 @@ def __init__(self, addrportstr, apikey=None):
57
58
58
59
"""
59
60
addrport = addrportstr .split (":" , 2 )
60
- chan_ops = [(' grpc.default_compression_algorithm' , CompressionAlgorithm .gzip )]
61
+ chan_ops = [(" grpc.default_compression_algorithm" , CompressionAlgorithm .gzip )]
61
62
62
63
if len (addrport ) != 2 :
63
64
raise ValueError ("expecting address:port" )
@@ -82,40 +83,44 @@ def __init__(self, addrportstr, apikey=None):
82
83
except Exception :
83
84
if env_bundle != "" :
84
85
# The user has given us something but we can't use it, we need to make noise
85
- raise Exception ("BTRDB_CA_BUNDLE(%s) env is defined but could not read file" % ca_bundle )
86
+ raise Exception (
87
+ "BTRDB_CA_BUNDLE(%s) env is defined but could not read file"
88
+ % ca_bundle
89
+ )
86
90
else :
87
91
contents = None
88
92
89
93
if apikey is None :
90
94
self .channel = grpc .secure_channel (
91
95
addrportstr ,
92
96
grpc .ssl_channel_credentials (contents ),
93
- options = chan_ops
97
+ options = chan_ops ,
94
98
)
95
99
else :
96
100
self .channel = grpc .secure_channel (
97
101
addrportstr ,
98
102
grpc .composite_channel_credentials (
99
103
grpc .ssl_channel_credentials (contents ),
100
- grpc .access_token_call_credentials (apikey )
104
+ grpc .access_token_call_credentials (apikey ),
101
105
),
102
- options = chan_ops
106
+ options = chan_ops ,
103
107
)
104
108
else :
105
109
if apikey is not None :
106
- raise ValueError ("cannot use an API key with an insecure (port 4410) BTrDB API. Try port 4411" )
110
+ raise ValueError (
111
+ "cannot use an API key with an insecure (port 4410) BTrDB API. Try port 4411"
112
+ )
107
113
self .channel = grpc .insecure_channel (addrportstr , chan_ops )
108
114
109
115
110
-
111
-
112
116
class BTrDB (object ):
113
117
"""
114
118
The primary server connection object for communicating with a BTrDB server.
115
119
"""
116
120
117
121
def __init__ (self , endpoint ):
118
122
self .ep = endpoint
123
+ self ._executor = ThreadPoolExecutor ()
119
124
120
125
def query (self , stmt , params = []):
121
126
"""
@@ -153,7 +158,6 @@ def query(self, stmt, params=[]):
153
158
for row in page
154
159
]
155
160
156
-
157
161
def streams (self , * identifiers , versions = None , is_collection_prefix = False ):
158
162
"""
159
163
Returns a StreamSet object with BTrDB streams from the supplied
@@ -196,20 +200,23 @@ def streams(self, *identifiers, versions=None, is_collection_prefix=False):
196
200
found = self .streams_in_collection (
197
201
"/" .join (parts [:- 1 ]),
198
202
is_collection_prefix = is_collection_prefix ,
199
- tags = {"name" : parts [- 1 ]}
203
+ tags = {"name" : parts [- 1 ]},
200
204
)
201
205
if len (found ) == 1 :
202
206
streams .append (found [0 ])
203
207
continue
204
208
raise StreamNotFoundError (f"Could not identify stream `{ ident } `" )
205
209
206
- raise ValueError (f"Could not identify stream based on `{ ident } `. Identifier must be UUID or collection/name." )
207
-
210
+ raise ValueError (
211
+ f"Could not identify stream based on `{ ident } `. Identifier must be UUID or collection/name."
212
+ )
208
213
209
214
obj = StreamSet (streams )
210
215
211
216
if versions :
212
- version_dict = {streams [idx ].uuid : versions [idx ] for idx in range (len (versions ))}
217
+ version_dict = {
218
+ streams [idx ].uuid : versions [idx ] for idx in range (len (versions ))
219
+ }
213
220
obj .pin_versions (version_dict )
214
221
215
222
return obj
@@ -257,12 +264,14 @@ def create(self, uuid, collection, tags=None, annotations=None):
257
264
annotations = {}
258
265
259
266
self .ep .create (uuid , collection , tags , annotations )
260
- return Stream (self , uuid ,
267
+ return Stream (
268
+ self ,
269
+ uuid ,
261
270
known_to_exist = True ,
262
271
collection = collection ,
263
272
tags = tags .copy (),
264
273
annotations = annotations .copy (),
265
- property_version = 0
274
+ property_version = 0 ,
266
275
)
267
276
268
277
def info (self ):
@@ -279,7 +288,7 @@ def info(self):
279
288
return {
280
289
"majorVersion" : info .majorVersion ,
281
290
"build" : info .build ,
282
- "proxy" : { "proxyEndpoints" : [ep for ep in info .proxy .proxyEndpoints ] },
291
+ "proxy" : {"proxyEndpoints" : [ep for ep in info .proxy .proxyEndpoints ]},
283
292
}
284
293
285
294
def list_collections (self , starts_with = "" ):
@@ -294,7 +303,9 @@ def list_collections(self, starts_with=""):
294
303
"""
295
304
return [c for some in self .ep .listCollections (starts_with ) for c in some ]
296
305
297
- def streams_in_collection (self , * collection , is_collection_prefix = True , tags = None , annotations = None ):
306
+ def streams_in_collection (
307
+ self , * collection , is_collection_prefix = True , tags = None , annotations = None
308
+ ):
298
309
"""
299
310
Search for streams matching given parameters
300
311
@@ -329,16 +340,23 @@ def streams_in_collection(self, *collection, is_collection_prefix=True, tags=Non
329
340
collection = [None ]
330
341
331
342
for item in collection :
332
- streams = self .ep .lookupStreams (item , is_collection_prefix , tags , annotations )
343
+ streams = self .ep .lookupStreams (
344
+ item , is_collection_prefix , tags , annotations
345
+ )
333
346
for desclist in streams :
334
347
for desc in desclist :
335
348
tagsanns = unpack_stream_descriptor (desc )
336
- result .append (Stream (
337
- self , uuidlib .UUID (bytes = desc .uuid ),
338
- known_to_exist = True , collection = desc .collection ,
339
- tags = tagsanns [0 ], annotations = tagsanns [1 ],
340
- property_version = desc .propertyVersion
341
- ))
349
+ result .append (
350
+ Stream (
351
+ self ,
352
+ uuidlib .UUID (bytes = desc .uuid ),
353
+ known_to_exist = True ,
354
+ collection = desc .collection ,
355
+ tags = tagsanns [0 ],
356
+ annotations = tagsanns [1 ],
357
+ property_version = desc .propertyVersion ,
358
+ )
359
+ )
342
360
343
361
return result
344
362
0 commit comments