• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef DISTRIBUTEDDB_COMMON_H
17 #define DISTRIBUTEDDB_COMMON_H
18 
19 #include <list>
20 #include <string>
21 
22 #include "db_types.h"
23 #include "kvdb_properties.h"
24 #include "store_types.h"
25 
26 namespace DistributedDB {
27 class DBCommon final {
28 public:
29     static int CreateDirectory(const std::string &directory);
30 
31     static void StringToVector(const std::string &src, std::vector<uint8_t> &dst);
32     static void VectorToString(const std::vector<uint8_t> &src, std::string &dst);
33 
34     static std::string VectorToHexString(const std::vector<uint8_t> &inVec, const std::string &separator = "");
35 
36     static void PrintHexVector(const std::vector<uint8_t> &data, int line = 0, const std::string &tag = "");
37 
38     static std::string TransferStringToHex(const std::string &origStr);
39 
40     static std::string TransferHashString(const std::string &devName);
41 
42     static int CalcValueHash(const std::vector<uint8_t> &Value, std::vector<uint8_t> &hashValue);
43 
44     static int CreateStoreDirectory(const std::string &directory, const std::string &identifierName,
45         const std::string &subDir, bool isCreate);
46 
47     static int CopyFile(const std::string &srcFile, const std::string &dstFile);
48 
49     static int RemoveAllFilesOfDirectory(const std::string &dir, bool isNeedRemoveDir = true);
50 
51     static std::string GenerateIdentifierId(const std::string &storeId,
52         const std::string &appId, const std::string &userId, int32_t instanceId = 0);
53 
54     static std::string GenerateDualTupleIdentifierId(const std::string &storeId, const std::string &appId);
55 
56     static void SetDatabaseIds(KvDBProperties &properties, const std::string &appId, const std::string &userId,
57         const std::string &storeId, int32_t instanceId = 0);
58 
59     static std::string StringMasking(const std::string &oriStr, size_t remain = 3); // remain 3 unmask
60 
61     static std::string GetDistributedTableName(const std::string &device, const std::string &tableName);
62 
63     static void GetDeviceFromName(const std::string &deviceTableName, std::string &deviceHash, std::string &tableName);
64 
65     static std::string TrimSpace(const std::string &input);
66 
67     static bool HasConstraint(const std::string &sql, const std::string &keyWord, const std::string &prePattern,
68         const std::string &nextPattern);
69 
70     static bool IsSameCipher(CipherType srcType, CipherType inputType);
71 
72     static bool CheckIsAlnumAndUnderscore(const std::string &text);
73 };
74 
75 // Define short macro substitute for original long expression for convenience of using
76 #define VEC_TO_STR(x) DBCommon::VectorToHexString(x).c_str()
77 #define STR_MASK(x) DBCommon::StringMasking(x).c_str()
78 #define STR_TO_HEX(x) DBCommon::TransferStringToHex(x).c_str()
79 } // namespace DistributedDB
80 
81 #endif // DISTRIBUTEDDB_COMMON_H
82