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 <map>
18 #include "db_errno.h"
19
20 namespace DistributedDB {
GetCompressionAlgo(std::set<CompressAlgorithm> & algorithmSet)21 void DataCompression::GetCompressionAlgo(std::set<CompressAlgorithm> &algorithmSet)
22 {
23 algorithmSet.clear();
24 for (const auto &item : GetCompressionAlgos()) {
25 algorithmSet.insert(item.first);
26 }
27 return;
28 }
29
TransferCompressionAlgo(uint32_t compressAlgoType,CompressAlgorithm & algoType)30 int DataCompression::TransferCompressionAlgo(uint32_t compressAlgoType, CompressAlgorithm &algoType)
31 {
32 auto iter = GetTransMap().find(compressAlgoType);
33 if (iter == GetTransMap().end()) {
34 return -E_INVALID_ARGS;
35 }
36 algoType = iter->second;
37 return E_OK;
38 }
39
GetInstance(CompressAlgorithm algo)40 DataCompression *DataCompression::GetInstance(CompressAlgorithm algo)
41 {
42 auto iter = GetCompressionAlgos().find(algo);
43 if (iter == GetCompressionAlgos().end()) {
44 return nullptr;
45 }
46 return iter->second;
47 }
48
49 // All supported compression algorithm should call this function to register their instance.
Register(CompressAlgorithm algo,DataCompression * compressionPtr)50 void DataCompression::Register(CompressAlgorithm algo, DataCompression *compressionPtr)
51 {
52 if (GetInstance(algo) != nullptr) {
53 return;
54 }
55 GetCompressionAlgos().insert({algo, compressionPtr});
56 GetTransMap().insert({static_cast<uint32_t>(algo), algo});
57 }
58
GetCompressionAlgos()59 std::map<CompressAlgorithm, DataCompression *> &DataCompression::GetCompressionAlgos()
60 {
61 static std::map<CompressAlgorithm, DataCompression *> compressionAlgos;
62 return compressionAlgos;
63 }
64
GetTransMap()65 std::map<uint32_t, CompressAlgorithm> &DataCompression::GetTransMap()
66 {
67 static std::map<uint32_t, CompressAlgorithm> transferMap;
68 return transferMap;
69 }
70 } // namespace DistributedDB
71