• 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 
GetLogTableName(const std::string & tableName)34     static inline std::string GetLogTableName(const std::string &tableName)
35     {
36         return DBConstant::RELATIONAL_PREFIX + tableName + DBConstant::LOG_POSTFIX;
37     }
38 
39     static std::string VectorToHexString(const std::vector<uint8_t> &inVec, const std::string &separator = "");
40 
41     static void PrintHexVector(const std::vector<uint8_t> &data, int line = 0, const std::string &tag = "");
42 
43     static std::string TransferStringToHex(const std::string &origStr);
44 
45     static std::string TransferHashString(const std::string &devName);
46 
47     static int CalcValueHash(const std::vector<uint8_t> &Value, std::vector<uint8_t> &hashValue);
48 
49     static int CreateStoreDirectory(const std::string &directory, const std::string &identifierName,
50         const std::string &subDir, bool isCreate);
51 
52     static int CopyFile(const std::string &srcFile, const std::string &dstFile);
53 
54     static int RemoveAllFilesOfDirectory(const std::string &dir, bool isNeedRemoveDir = true);
55 
56     static std::string GenerateIdentifierId(const std::string &storeId,
57         const std::string &appId, const std::string &userId, int32_t instanceId = 0);
58 
59     static std::string GenerateDualTupleIdentifierId(const std::string &storeId, const std::string &appId);
60 
61     static void SetDatabaseIds(KvDBProperties &properties, const std::string &appId, const std::string &userId,
62         const std::string &storeId, int32_t instanceId = 0);
63 
64     static std::string StringMasking(const std::string &oriStr, size_t remain = 3); // remain 3 unmask
65 
66     static std::string GetDistributedTableName(const std::string &device, const std::string &tableName);
67 
68     static std::string GetDistributedTableName(const std::string &device, const std::string &tableName,
69         const StoreInfo &info);
70 
71     static std::string GetDistributedTableNameWithHash(const std::string &device, const std::string &tableName);
72 
73     static std::string CalDistributedTableName(const std::string &device, const std::string &tableName);
74 
75     static void GetDeviceFromName(const std::string &deviceTableName, std::string &deviceHash, std::string &tableName);
76 
77     static std::string TrimSpace(const std::string &input);
78 
79     static bool HasConstraint(const std::string &sql, const std::string &keyWord, const std::string &prePattern,
80         const std::string &nextPattern);
81 
82     static bool IsSameCipher(CipherType srcType, CipherType inputType);
83 
84     static std::string ToLowerCase(const std::string &str);
85 
86     static bool CaseInsensitiveCompare(std::string first, std::string second);
87 
88     static bool CheckIsAlnumAndUnderscore(const std::string &text);
89 };
90 
91 // Define short macro substitute for original long expression for convenience of using
92 #define VEC_TO_STR(x) DBCommon::VectorToHexString(x).c_str()
93 #define STR_MASK(x) DBCommon::StringMasking(x).c_str()
94 #define STR_TO_HEX(x) DBCommon::TransferStringToHex(x).c_str()
95 } // namespace DistributedDB
96 
97 #endif // DISTRIBUTEDDB_COMMON_H
98