1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "data_compression.h"
17 #include "db_errno.h"
18
19 namespace DistributedDB {
GetCompressionAlgo(std::set<CompressAlgorithm> & algorithmSet)20 void DataCompression::GetCompressionAlgo(std::set<CompressAlgorithm> &algorithmSet)
21 {
22 algorithmSet.clear();
23 for (const auto &item : GetCompressionAlgos()) {
24 algorithmSet.insert(item.first);
25 }
26 }
27
TransferCompressionAlgo(uint32_t compressAlgoType,CompressAlgorithm & algoType)28 int DataCompression::TransferCompressionAlgo(uint32_t compressAlgoType, CompressAlgorithm &algoType)
29 {
30 auto iter = GetTransMap().find(compressAlgoType);
31 if (iter == GetTransMap().end()) {
32 return -E_INVALID_ARGS;
33 }
34 algoType = iter->second;
35 return E_OK;
36 }
37
GetInstance(CompressAlgorithm algo)38 DataCompression *DataCompression::GetInstance(CompressAlgorithm algo)
39 {
40 auto iter = GetCompressionAlgos().find(algo);
41 if (iter == GetCompressionAlgos().end()) {
42 return nullptr;
43 }
44 return iter->second;
45 }
46
47 // All supported compression algorithm should call this function to register their instance.
Register(CompressAlgorithm algo,DataCompression * compressionPtr)48 void DataCompression::Register(CompressAlgorithm algo, DataCompression *compressionPtr)
49 {
50 if (GetInstance(algo) != nullptr) {
51 return;
52 }
53 GetCompressionAlgos().insert({ algo, compressionPtr });
54 GetTransMap().insert({ static_cast<uint32_t>(algo), algo });
55 }
56
GetCompressionAlgos()57 std::map<CompressAlgorithm, DataCompression *> &DataCompression::GetCompressionAlgos()
58 {
59 static std::map<CompressAlgorithm, DataCompression *> compressionAlgos;
60 return compressionAlgos;
61 }
62
GetTransMap()63 std::map<uint32_t, CompressAlgorithm> &DataCompression::GetTransMap()
64 {
65 static std::map<uint32_t, CompressAlgorithm> transferMap;
66 return transferMap;
67 }
68 } // namespace DistributedDB
69