-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathrmqa_compressiontransformerimpl.cpp
More file actions
181 lines (152 loc) · 6.01 KB
/
Copy pathrmqa_compressiontransformerimpl.cpp
File metadata and controls
181 lines (152 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2025 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifdef RMQCPP_ENABLE_COMPRESSION
#include <rmqa_compressiontransformerimpl.h>
#include <rmqp_messagetransformer.h>
#include <rmqt_properties.h>
#include <rmqt_result.h>
#include <ball_log.h>
#include <zstd.h>
#include <zstd_errors.h>
#include <bsl_memory.h>
#include <bsl_string.h>
#include <bsl_vector.h>
#include <bsls_assert.h>
namespace BloombergLP {
namespace rmqa {
namespace {
BALL_LOG_SET_CLASS_CATEGORY("RMQA.COMPRESSIONTRANSFORMERIMPL");
}
CompressionTransformerImpl::CompressionTransformerImpl()
: zctx(ZSTD_createCCtx())
{
if (!zctx) {
BALL_LOG_WARN << "Failed to allocate ZSTD context. Compression will "
"not be enabled.";
}
}
CompressionTransformerImpl::~CompressionTransformerImpl()
{
ZSTD_freeCCtx(zctx);
zctx = NULL;
}
rmqt::Result<bool> CompressionTransformerImpl::transform(
bsl::shared_ptr<bsl::vector<uint8_t> >& data,
rmqt::Properties& props)
{
if (!zctx || data->size() < 8 * 1024) {
// Do not compress small messages
return rmqt::Result<bool>(bsl::make_shared<bool>(false));
}
// Allocate space for compressed data
bsl::shared_ptr<bsl::vector<uint8_t> > compressedData =
bsl::make_shared<bsl::vector<uint8_t> >(
ZSTD_compressBound(data->size()));
// Perform the compression
size_t compressedSize = ZSTD_compressCCtx(zctx,
compressedData->data(),
compressedData->size(),
data->data(),
data->size(),
1);
if (ZSTD_isError(compressedSize)) {
ZSTD_ErrorCode errorCode = ZSTD_getErrorCode(compressedSize);
if (errorCode == ZSTD_error_dstSize_tooSmall) {
// The data is not compressible
return rmqt::Result<bool>(bsl::make_shared<bool>(false));
}
return rmqt::Result<bool>(ZSTD_getErrorName(errorCode));
}
// Update headers
BSLS_ASSERT(props.headers);
props.headers->emplace("sdk.transform.compression.alg",
rmqt::FieldValue(bsl::string("zstd")));
props.headers->emplace(
"sdk.transform.compression.size",
rmqt::FieldValue(static_cast<int64_t>(data->size())));
// Destroy the old data and replace it with compressed data
data = compressedData;
data->resize(compressedSize);
return rmqt::Result<bool>(bsl::make_shared<bool>(true));
}
rmqt::Result<> CompressionTransformerImpl::decompressZstd(
bsl::shared_ptr<bsl::vector<uint8_t> >& data,
size_t originalSize)
{
bsl::shared_ptr<bsl::vector<uint8_t> > decompressedData =
bsl::make_shared<bsl::vector<uint8_t> >(originalSize);
size_t decompressedSize = ZSTD_decompress(
decompressedData->data(), originalSize, data->data(), data->size());
if (ZSTD_isError(decompressedSize) ||
(size_t)decompressedSize != originalSize) {
BALL_LOG_ERROR << "Decompression failed: "
<< ZSTD_getErrorName(
ZSTD_getErrorCode(decompressedSize));
return rmqt::Result<>(
bsl::string("Decompression failed: ") +
ZSTD_getErrorName(ZSTD_getErrorCode(decompressedSize)));
}
// Replace the data with decompressed data
data = decompressedData;
return rmqt::Result<>();
}
rmqt::Result<> CompressionTransformerImpl::inverseTransform(
bsl::shared_ptr<bsl::vector<uint8_t> >& data,
rmqt::Properties& props)
{
BSLS_ASSERT(props.headers);
rmqt::FieldTable::const_iterator sizeIt =
props.headers->find("sdk.transform.compression.size");
if (sizeIt == props.headers->end() || !sizeIt->second.is<int64_t>()) {
BALL_LOG_ERROR << "Missing or invalid compression size header";
return rmqt::Result<>("Missing or invalid compression size header");
}
int64_t originalSize = sizeIt->second.the<int64_t>();
if (originalSize <= 0) {
BALL_LOG_ERROR << "Invalid original size for decompression: "
<< originalSize;
return rmqt::Result<>("Invalid original size for decompression: " +
bsl::to_string(originalSize));
}
rmqt::FieldTable::const_iterator algIt =
props.headers->find("sdk.transform.compression.alg");
if (algIt == props.headers->end() || !algIt->second.is<bsl::string>()) {
BALL_LOG_ERROR << "Missing or invalid compression algorithm header";
return rmqt::Result<>(
"Missing or invalid compression algorithm header");
}
bsl::string compressionAlg = algIt->second.the<bsl::string>();
rmqt::Result<> result;
if (compressionAlg == "zstd") {
result = decompressZstd(data, originalSize);
}
else {
// Unsupported compression algorithm
BALL_LOG_ERROR << "Unsupported compression algorithm: "
<< compressionAlg;
return rmqt::Result<>("Unsupported compression algorithm: " +
compressionAlg);
}
if (!result) {
return result; // Propagate error from decompression
}
// Remove compression headers
props.headers->erase("sdk.transform.compression.alg");
props.headers->erase("sdk.transform.compression.size");
return rmqt::Result<>();
}
} // namespace rmqa
} // namespace BloombergLP
#endif // RMQCPP_ENABLE_COMPRESSION