MySQL  8.0.22
Source Code Documentation
gcs_message_stage_lz4.h
Go to the documentation of this file.
1 /* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef GCS_MESSAGE_STAGE_LZ4_H
24 #define GCS_MESSAGE_STAGE_LZ4_H
25 
26 #include <lz4.h>
27 #include <utility>
28 #include <vector>
29 
32 
33 /**
34  This class implements LZ4 compression. It is a stateless
35  service class, thence it is thread safe.
36  */
38  public:
39  /*
40  Methods inherited from the Gcs_message_stage class.
41  */
43  uint64_t const &original_payload_size) const override;
44 
45  std::unique_ptr<Gcs_stage_metadata> get_stage_header() override;
46 
47  protected:
48  std::pair<bool, std::vector<Gcs_packet>> apply_transformation(
49  Gcs_packet &&packet) override;
50 
51  std::pair<Gcs_pipeline_incoming_result, Gcs_packet> revert_transformation(
52  Gcs_packet &&packet) override;
53 
55  const Gcs_packet &packet) const override;
56 
57  public:
58  /**
59  The default threshold value in bytes.
60  */
61  static constexpr unsigned long long DEFAULT_THRESHOLD = 1024;
62 
63  /**
64  Creates an instance of the stage with the default threshold in bytes set.
65  */
66  explicit Gcs_message_stage_lz4() : m_threshold(DEFAULT_THRESHOLD) {}
67 
68  /**
69  Creates an instance of the stage with the given threshold in bytes.
70 
71  @param enabled enables this message stage
72  @param compress_threshold messages with the payload larger
73  than compress_threshold in bytes are compressed.
74  */
76  unsigned long long compress_threshold)
77  : Gcs_message_stage(enabled), m_threshold(compress_threshold) {}
78 
79  ~Gcs_message_stage_lz4() override {}
80 
81  /*
82  Return the stage code.
83  */
84  Stage_code get_stage_code() const override { return Stage_code::ST_LZ4_V1; }
85 
86  /**
87  Sets the threshold in bytes after which compression kicks in.
88 
89  @param threshold if the payload exceeds these many bytes, then
90  the message is compressed.
91  */
92  void set_threshold(unsigned long long threshold) { m_threshold = threshold; }
93 
94  /**
95  Return the maximum payload size in bytes that can be compressed.
96  */
97  static constexpr unsigned long long max_input_compression() noexcept {
98  /*
99  The code expects that the following assumption will always hold.
100  */
101  static_assert(
102  LZ4_MAX_INPUT_SIZE <= std::numeric_limits<int>::max(),
103  "Maximum input size for lz compression exceeds the expected value");
104  return LZ4_MAX_INPUT_SIZE;
105  }
106 
107  private:
108  /**
109  This marks the threshold in bytes above which a message gets compressed.
110  Messages that are smaller than this threshold are not compressed.
111  */
112  unsigned long long m_threshold;
113 };
114 
116  public:
117  /**
118  Creates an instance of the stage with the default threshold in bytes.
119  */
121 
122  /**
123  Creates an instance of the stage with the given threshold in bytes.
124 
125  @param enabled enables this message stage
126  @param compress_threshold messages with the payload larger
127  than compress_threshold in bytes are compressed.
128  */
130  unsigned long long compress_threshold)
131  : Gcs_message_stage_lz4(enabled, compress_threshold) {}
132 
134 
135  /*
136  Return the stage code.
137  */
138  Stage_code get_stage_code() const override { return Stage_code::ST_LZ4_V2; }
139 };
140 #endif /* GCS_MESSAGE_STAGE_LZ4_H */
Gcs_message_stage::stage_status skip_apply(uint64_t const &original_payload_size) const override
Check if the apply operation which affects outgoing packets should be executed (i.e.
Definition: gcs_message_stage_lz4.cc:35
Gcs_message_stage_lz4_v2()
Creates an instance of the stage with the default threshold in bytes.
Definition: gcs_message_stage_lz4.h:120
bool enabled
true if enabled.
Definition: buf0dblwr.cc:80
Stage_code
The different stages that are currently available.
Definition: gcs_internal_message_headers.h:72
static constexpr unsigned long long DEFAULT_THRESHOLD
The default threshold value in bytes.
Definition: gcs_message_stage_lz4.h:61
This class implements LZ4 compression.
Definition: gcs_message_stage_lz4.h:37
static constexpr unsigned long long max_input_compression() noexcept
Return the maximum payload size in bytes that can be compressed.
Definition: gcs_message_stage_lz4.h:97
Gcs_message_stage::stage_status skip_revert(const Gcs_packet &packet) const override
Check if the revert operation which affects incoming packets should be executed (i.e.
Definition: gcs_message_stage_lz4.cc:166
unsigned long long m_threshold
This marks the threshold in bytes above which a message gets compressed.
Definition: gcs_message_stage_lz4.h:112
std::pair< Gcs_pipeline_incoming_result, Gcs_packet > revert_transformation(Gcs_packet &&packet) override
Implements the logic to revert this stage&#39;s transformation to the packet, and returns one...
Definition: gcs_message_stage_lz4.cc:112
std::unique_ptr< Gcs_stage_metadata > get_stage_header() override
Definition: gcs_message_stage_lz4.cc:62
std::pair< bool, std::vector< Gcs_packet > > apply_transformation(Gcs_packet &&packet) override
Implements the logic of this stage&#39;s transformation to the packet, and returns a set of one...
Definition: gcs_message_stage_lz4.cc:67
Gcs_message_stage_lz4(bool enabled, unsigned long long compress_threshold)
Creates an instance of the stage with the given threshold in bytes.
Definition: gcs_message_stage_lz4.h:75
Stage_code get_stage_code() const override
Return the unique stage code.
Definition: gcs_message_stage_lz4.h:84
~Gcs_message_stage_lz4() override
Definition: gcs_message_stage_lz4.h:79
Stage_code get_stage_code() const override
Return the unique stage code.
Definition: gcs_message_stage_lz4.h:138
stage_status
Definition: gcs_message_stages.h:83
Definition: gcs_message_stage_lz4.h:115
~Gcs_message_stage_lz4_v2() override
Definition: gcs_message_stage_lz4.h:133
Gcs_message_stage_lz4()
Creates an instance of the stage with the default threshold in bytes set.
Definition: gcs_message_stage_lz4.h:66
Gcs_message_stage_lz4_v2(bool enabled, unsigned long long compress_threshold)
Creates an instance of the stage with the given threshold in bytes.
Definition: gcs_message_stage_lz4.h:129
This class is an abstraction for the packet concept.
Definition: gcs_internal_message.h:57
void set_threshold(unsigned long long threshold)
Sets the threshold in bytes after which compression kicks in.
Definition: gcs_message_stage_lz4.h:92
This is a stage in the pipeline that processes messages when they are put through the send and receiv...
Definition: gcs_message_stages.h:81