1 /*
2   Copyright (c) 2015, 2021, Oracle and/or its affiliates.
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2.0,
6   as published by the Free Software Foundation.
7 
8   This program is also distributed with certain software (including
9   but not limited to OpenSSL) that is licensed under separate terms,
10   as designated in a particular file or component or in included license
11   documentation.  The authors of MySQL hereby grant you an additional
12   permission to link the program and your derivative works with the
13   separately licensed software that they have included with MySQL.
14 
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License, version 2.0, for more details.
19 
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "compression_lz4_writer.h"
26 
27 using namespace Mysql::Tools::Dump;
28 
prepare_buffer(size_t src_size)29 void Compression_lz4_writer::prepare_buffer(size_t src_size)
30 {
31   // bug in lz4 (fixed in their r124).
32   LZ4F_preferences_t null_preferences;
33   memset(&null_preferences, 0, sizeof(LZ4F_preferences_t));
34   m_buffer.resize(LZ4F_compressBound(src_size, &null_preferences));
35 }
36 
process_buffer(size_t lz4_result)37 void Compression_lz4_writer::process_buffer(size_t lz4_result)
38 {
39   if (LZ4F_isError(lz4_result))
40   {
41     this->pass_message(Mysql::Tools::Base::Message_data(
42       0, "LZ4 compression failed",
43       Mysql::Tools::Base::Message_type_error));
44   }
45   else if (lz4_result > 0)
46   {
47     this->append_output(std::string(&m_buffer[0], lz4_result));
48   }
49 }
50 
append(const std::string & data_to_append)51 void Compression_lz4_writer::append(const std::string& data_to_append)
52 {
53   my_boost::mutex::scoped_lock lock(m_lz4_mutex);
54   if (m_buffer.capacity() == 0)
55   {
56     LZ4F_createCompressionContext(&m_compression_context, LZ4F_VERSION);
57     this->prepare_buffer(0);
58     this->process_buffer(LZ4F_compressBegin(m_compression_context,
59       (void*)&m_buffer[0], m_buffer.capacity(), NULL));
60   }
61   this->prepare_buffer(data_to_append.size());
62   this->process_buffer(LZ4F_compressUpdate(m_compression_context,
63     (void*)&m_buffer[0], m_buffer.capacity(), data_to_append.c_str(),
64     data_to_append.size(), NULL));
65 }
66 
~Compression_lz4_writer()67 Compression_lz4_writer::~Compression_lz4_writer()
68 {
69   my_boost::mutex::scoped_lock lock(m_lz4_mutex);
70   if (m_buffer.capacity() != 0)
71   {
72     this->process_buffer(LZ4F_compressEnd(m_compression_context,
73       (void*)&m_buffer[0], m_buffer.capacity(), NULL));
74     LZ4F_freeCompressionContext(m_compression_context);
75   }
76 }
77 
Compression_lz4_writer(Mysql::I_callable<bool,const Mysql::Tools::Base::Message_data &> * message_handler,Simple_id_generator * object_id_generator)78 Compression_lz4_writer::Compression_lz4_writer(
79   Mysql::I_callable<bool, const Mysql::Tools::Base::Message_data&>*
80     message_handler, Simple_id_generator* object_id_generator)
81   : Abstract_output_writer_wrapper(message_handler, object_id_generator)
82 {}
83