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