62
62
from .export_response import *
63
63
from .common_response import *
64
64
from .auth import *
65
+ from .compress import CompressType , Compressor
65
66
66
67
logger = logging .getLogger (__name__ )
67
68
68
69
if six .PY3 :
69
70
xrange = range
70
71
71
- lz4_available = Util .is_lz4_available ()
72
- if lz4_available :
73
- from .util import lz_decompress , lz_compresss
74
72
75
73
CONNECTION_TIME_OUT = 120
76
74
MAX_LIST_PAGING_SIZE = 500
@@ -384,13 +382,11 @@ def put_log_raw(self, project, logstore, log_group, compress=None):
384
382
raw_body_size = len (body )
385
383
headers = {'x-log-bodyrawsize' : str (raw_body_size ), 'Content-Type' : 'application/x-protobuf' }
386
384
387
- if compress is None or compress :
388
- if lz4_available :
389
- headers ['x-log-compresstype' ] = 'lz4'
390
- body = lz_compresss (body )
391
- else :
392
- headers ['x-log-compresstype' ] = 'deflate'
393
- body = zlib .compress (body )
385
+ need_compress = compress is None or compress
386
+ if need_compress :
387
+ compress_type = CompressType .default_compress_type ()
388
+ headers ['x-log-compresstype' ] = str (compress_type )
389
+ body = Compressor .compress (body , compress_type )
394
390
395
391
params = {}
396
392
resource = '/logstores/' + logstore + "/shards/lb"
@@ -445,16 +441,12 @@ def put_logs(self, request):
445
441
len (body ) / 1024.0 / 1024 ))
446
442
447
443
headers = {'x-log-bodyrawsize' : str (len (body )), 'Content-Type' : 'application/x-protobuf' }
448
- is_compress = request .get_compress ()
449
-
450
- compress_data = None
451
- if is_compress :
452
- if lz4_available :
453
- headers ['x-log-compresstype' ] = 'lz4'
454
- compress_data = lz_compresss (body )
455
- else :
456
- headers ['x-log-compresstype' ] = 'deflate'
457
- compress_data = zlib .compress (body )
444
+
445
+ need_compress = request .get_compress () is None or request .get_compress ()
446
+ if need_compress :
447
+ compress_type = CompressType .default_compress_type ()
448
+ headers ['x-log-compresstype' ] = str (compress_type )
449
+ body = Compressor .compress (body , compress_type )
458
450
459
451
params = {}
460
452
logstore = request .get_logstore ()
@@ -465,11 +457,7 @@ def put_logs(self, request):
465
457
else :
466
458
resource = '/logstores/' + logstore + "/shards/lb"
467
459
468
- if is_compress :
469
- (resp , header ) = self ._send ('POST' , project , compress_data , resource , params , headers )
470
- else :
471
- (resp , header ) = self ._send ('POST' , project , body , resource , params , headers )
472
-
460
+ (resp , header ) = self ._send ('POST' , project , body , resource , params , headers )
473
461
return PutLogsResponse (header , resp )
474
462
475
463
def list_logstores (self , request ):
@@ -654,12 +642,12 @@ def get_log(self, project, logstore, from_time, to_time, topic=None,
654
642
params ['forward' ] = forward
655
643
body_str = six .b (json .dumps (params ))
656
644
headers ["x-log-bodyrawsize" ] = str (len (body_str ))
657
- accept_encoding = "lz4" if lz4_available else "deflate"
645
+ accept_encoding = str ( CompressType . default_compress_type ())
658
646
headers ['Accept-Encoding' ] = accept_encoding
659
647
660
648
(resp , header ) = self ._send ("POST" , project , body_str , resource , None , headers , respons_body_type = accept_encoding )
661
649
662
- raw_data = Util . uncompress_response (header , resp )
650
+ raw_data = Compressor . decompress_response (header , resp )
663
651
exJson = self ._loadJson (200 , header , raw_data , requestId = Util .h_v_td (header , 'x-log-requestid' , '' ))
664
652
exJson = Util .convert_unicode_to_str (exJson )
665
653
ret = GetLogsResponse (exJson , header )
@@ -1128,11 +1116,10 @@ def pull_logs(self, project_name, logstore_name, shard_id, cursor, count=None, e
1128
1116
"""
1129
1117
1130
1118
headers = {}
1131
- if compress is None or compress :
1132
- if lz4_available :
1133
- headers ['Accept-Encoding' ] = 'lz4'
1134
- else :
1135
- headers ['Accept-Encoding' ] = 'gzip'
1119
+
1120
+ need_compress = compress is None or compress
1121
+ if need_compress :
1122
+ headers ['Accept-Encoding' ] = str (CompressType .default_compress_type ())
1136
1123
else :
1137
1124
headers ['Accept-Encoding' ] = ''
1138
1125
@@ -1155,18 +1142,9 @@ def pull_logs(self, project_name, logstore_name, shard_id, cursor, count=None, e
1155
1142
raw_size = int (Util .h_v_t (header , 'x-log-bodyrawsize' ))
1156
1143
if raw_size <= 0 :
1157
1144
return PullLogResponse (None , header )
1158
- compress_type = Util .h_v_td (header , 'x-log-compresstype' , '' ).lower ()
1159
- if compress_type == 'lz4' :
1160
- if lz4_available :
1161
- raw_data = lz_decompress (raw_size , resp )
1162
- return PullLogResponse (raw_data , header )
1163
- else :
1164
- raise LogException ("ClientHasNoLz4" , "There's no Lz4 lib available to decompress the response" , resp_header = header , resp_body = resp )
1165
- elif compress_type in ('gzip' , 'deflate' ):
1166
- raw_data = zlib .decompress (resp )
1167
- return PullLogResponse (raw_data , header )
1168
- else :
1169
- return PullLogResponse (resp , header )
1145
+
1146
+ raw_data = Compressor .decompress_response (header , resp )
1147
+ return PullLogResponse (raw_data , header )
1170
1148
1171
1149
def pull_log (self , project_name , logstore_name , shard_id , from_time , to_time , batch_size = None , compress = None , query = None ):
1172
1150
""" batch pull log data from log service using time-range
0 commit comments