-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathtrpc_compressor.cc
92 lines (78 loc) · 2.53 KB
/
trpc_compressor.cc
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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#include "trpc/compressor/trpc_compressor.h"
#include <utility>
#include "trpc/compressor/compressor_factory.h"
#include "trpc/compressor/gzip/gzip_compressor.h"
#include "trpc/compressor/lz4/lz4_compressor.h"
#include "trpc/compressor/snappy/snappy_compressor.h"
#include "trpc/compressor/zlib/zlib_compressor.h"
#include "trpc/util/likely.h"
#include "trpc/util/log/logging.h"
namespace trpc::compressor {
bool Init() {
auto* factory = CompressorFactory::GetInstance();
// gzip
TRPC_ASSERT(factory->Register(MakeRefCounted<GzipCompressor>()));
// zlib
TRPC_ASSERT(factory->Register(MakeRefCounted<ZlibCompressor>()));
// snappy
TRPC_ASSERT(factory->Register(MakeRefCounted<SnappyCompressor>()));
// snappy block format
TRPC_ASSERT(factory->Register(MakeRefCounted<SnappyBlockCompressor>()));
// lz4 frame
TRPC_ASSERT(factory->Register(MakeRefCounted<Lz4FrameCompressor>()));
return true;
}
void Destroy() {
CompressorFactory::GetInstance()->Clear();
}
bool CompressIfNeeded(CompressType type, NoncontiguousBuffer& data, LevelType level) {
if (type == kNone) {
return true;
}
NoncontiguousBuffer out;
if (TRPC_UNLIKELY(!Compress(type, data, out, level))) {
return false;
}
data = std::move(out);
return true;
}
bool Compress(CompressType type, const NoncontiguousBuffer& in, NoncontiguousBuffer& out, LevelType level) {
// Returns false on compressor::kNone
auto compressor = CompressorFactory::GetInstance()->Get(type);
if (TRPC_UNLIKELY(!compressor)) {
return false;
}
return compressor->Compress(in, out, level);
}
bool DecompressIfNeeded(CompressType type, NoncontiguousBuffer& data) {
if (type == kNone) {
return true;
}
NoncontiguousBuffer out;
if (TRPC_UNLIKELY(!Decompress(type, data, out))) {
return false;
}
data = std::move(out);
return true;
}
bool Decompress(CompressType type, const NoncontiguousBuffer& in, NoncontiguousBuffer& out) {
// Returns false on compressor::kNone
auto compressor = CompressorFactory::GetInstance()->Get(type);
if (TRPC_UNLIKELY(!compressor)) {
return false;
}
return compressor->Decompress(in, out);
}
} // namespace trpc::compressor