Make max_compression_ratio required parameter in gzencode.

GitOrigin-RevId: c787fdeae202d3b80944412e7db4209f35adcd07
This commit is contained in:
levlam 2020-03-15 01:56:48 +03:00
parent b4138231ec
commit 968689157c
5 changed files with 7 additions and 6 deletions

View File

@ -27,7 +27,7 @@ NetQueryCreator::Ptr NetQueryCreator::create(uint64 id, const Storer &storer, Dc
int32 tl_constructor = NetQuery::tl_magic(slice);
if (gzip_flag == NetQuery::GzipFlag::On) {
// TODO: try to compress files?
BufferSlice compressed = gzencode(slice.as_slice());
BufferSlice compressed = gzencode(slice.as_slice(), 0.9);
if (compressed.empty()) {
gzip_flag = NetQuery::GzipFlag::Off;
} else {

View File

@ -184,12 +184,12 @@ BufferSlice gzdecode(Slice s) {
return message.extract_reader().move_as_buffer_slice();
}
BufferSlice gzencode(Slice s, double k) {
BufferSlice gzencode(Slice s, double max_compression_ratio) {
Gzip gzip;
gzip.init_encode().ensure();
gzip.set_input(s);
gzip.close_input();
size_t max_size = static_cast<size_t>(static_cast<double>(s.size()) * k);
size_t max_size = static_cast<size_t>(static_cast<double>(s.size()) * max_compression_ratio);
BufferWriter message{max_size};
gzip.set_output(message.prepare_append());
auto r_state = gzip.run();

View File

@ -99,7 +99,7 @@ class Gzip {
BufferSlice gzdecode(Slice s);
BufferSlice gzencode(Slice s, double k = 0.9);
BufferSlice gzencode(Slice s, double max_compression_ratio);
} // namespace td

View File

@ -63,7 +63,8 @@ TEST(Gzip, flow) {
}
TEST(Gzip, flow_error) {
auto str = td::rand_string('a', 'z', 1000000);
auto zip = td::gzencode(str).as_slice().str();
auto zip = td::gzencode(str, 0.9).as_slice().str();
ASSERT_TRUE(!zip.empty());
zip.resize(zip.size() - 1);
auto parts = td::rand_split(zip);

View File

@ -354,7 +354,7 @@ TEST(Http, chunked_flow_error) {
TEST(Http, gzip_chunked_flow) {
auto str = rand_string('a', 'z', 1000000);
auto parts = rand_split(make_chunked(gzencode(str).as_slice().str()));
auto parts = rand_split(make_chunked(gzencode(str, 2.0).as_slice().str()));
ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();