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 void RTrim(std::string &oriString); 80 81 static bool HasConstraint(const std::string &sql, const std::string &keyWord, const std::string &prePattern, 82 const std::string &nextPattern); 83 84 static bool IsSameCipher(CipherType srcType, CipherType inputType); 85 86 static std::string ToLowerCase(const std::string &str); 87 88 static std::string ToUpperCase(const std::string &str); 89 90 static bool CaseInsensitiveCompare(const std::string &first, const std::string &second); 91 92 static bool CheckIsAlnumOrUnderscore(const std::string &text); 93 94 static bool CheckQueryWithoutMultiTable(const Query &query); 95 96 static bool IsCircularDependency(int size, const std::vector<std::vector<int>> &dependency); 97 98 static int SerializeWaterMark(Timestamp localMark, const std::string &cloudMark, Value &blobMeta); 99 100 static Key GetPrefixTableName(const TableName &tableName); 101 102 static std::list<std::string> GenerateNodesByNodeWeight(const std::vector<std::string> &nodes, 103 const std::map<std::string, std::map<std::string, bool>> &graph, 104 const std::map<std::string, int> &nodeWeight); 105 106 static bool HasPrimaryKey(const std::vector<Field> &fields); 107 108 static bool IsRecordError(const VBucket &record); 109 110 static bool IsRecordIgnored(const VBucket &record); 111 112 static std::string GenerateHashLabel(const DBInfo &dbInfo); 113 private: 114 static void InsertNodesByScore(const std::map<std::string, std::map<std::string, bool>> &graph, 115 const std::vector<std::string> &generateNodes, const std::map<std::string, int> &scoreGraph, 116 std::list<std::string> &insertTarget); 117 }; 118 119 // Define short macro substitute for original long expression for convenience of using 120 #define VEC_TO_STR(x) DBCommon::VectorToHexString(x).c_str() 121 #define STR_MASK(x) DBCommon::StringMasking(x).c_str() 122 #define STR_TO_HEX(x) DBCommon::TransferStringToHex(x).c_str() 123 } // namespace DistributedDB 124 125 #endif // DISTRIBUTEDDB_COMMON_H 126