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 "cloud/cloud_db_types.h" 23 #include "db_types.h" 24 #include "kvdb_properties.h" 25 #include "store_types.h" 26 27 namespace DistributedDB { 28 class DBCommon final { 29 public: 30 static int CreateDirectory(const std::string &directory); 31 32 static void StringToVector(const std::string &src, std::vector<uint8_t> &dst); 33 static void VectorToString(const std::vector<uint8_t> &src, std::string &dst); 34 GetLogTableName(const std::string & tableName)35 static inline std::string GetLogTableName(const std::string &tableName) 36 { 37 return DBConstant::RELATIONAL_PREFIX + tableName + DBConstant::LOG_POSTFIX; 38 } 39 GetWaterTypeVec()40 static inline const std::vector<CloudWaterType> GetWaterTypeVec() 41 { 42 return {CloudWaterType::DELETE, CloudWaterType::UPDATE, CloudWaterType::INSERT}; 43 } 44 GetMetaTableName()45 static inline std::string GetMetaTableName() 46 { 47 return DBConstant::RELATIONAL_PREFIX + DBConstant::META_TABLE_POSTFIX; 48 } 49 50 static std::string VectorToHexString(const std::vector<uint8_t> &inVec, const std::string &separator = ""); 51 52 static void PrintHexVector(const std::vector<uint8_t> &data, int line = 0, const std::string &tag = ""); 53 54 static std::string TransferStringToHex(const std::string &origStr); 55 56 static std::string TransferHashString(const std::string &devName); 57 58 static int CalcValueHash(const std::vector<uint8_t> &Value, std::vector<uint8_t> &hashValue); 59 60 static int CreateStoreDirectory(const std::string &directory, const std::string &identifierName, 61 const std::string &subDir, bool isCreate); 62 63 static int CopyFile(const std::string &srcFile, const std::string &dstFile); 64 65 static int RemoveAllFilesOfDirectory(const std::string &dir, bool isNeedRemoveDir = true); 66 67 static std::string GenerateIdentifierId(const std::string &storeId, 68 const std::string &appId, const std::string &userId, const std::string &subUser = "", int32_t instanceId = 0); 69 70 static std::string GenerateDualTupleIdentifierId(const std::string &storeId, const std::string &appId); 71 72 static void SetDatabaseIds(KvDBProperties &properties, const DbIdParam &dbIdParam); 73 74 static std::string StringMasking(const std::string &oriStr, size_t remain = 3); // remain 3 unmask 75 76 static std::string StringMiddleMasking(const std::string &name); 77 78 static std::string GetDistributedTableName(const std::string &device, const std::string &tableName); 79 80 static std::string GetDistributedTableName(const std::string &device, const std::string &tableName, 81 const StoreInfo &info); 82 83 static std::string GetDistributedTableNameWithHash(const std::string &device, const std::string &tableName); 84 85 static std::string CalDistributedTableName(const std::string &device, const std::string &tableName); 86 87 static void GetDeviceFromName(const std::string &deviceTableName, std::string &deviceHash, std::string &tableName); 88 89 static std::string TrimSpace(const std::string &input); 90 91 static void RTrim(std::string &oriString); 92 93 static bool HasConstraint(const std::string &sql, const std::string &keyWord, const std::string &prePattern, 94 const std::string &nextPattern); 95 96 static bool IsSameCipher(CipherType srcType, CipherType inputType); 97 98 static std::string ToLowerCase(const std::string &str); 99 100 static std::string ToUpperCase(const std::string &str); 101 102 static bool CaseInsensitiveCompare(const std::string &first, const std::string &second); 103 104 static bool CheckIsAlnumOrUnderscore(const std::string &text); 105 106 static bool CheckQueryWithoutMultiTable(const Query &query); 107 108 static bool IsCircularDependency(int size, const std::vector<std::vector<int>> &dependency); 109 110 static int SerializeWaterMark(Timestamp localMark, const std::string &cloudMark, Value &blobMeta); 111 112 static Key GetPrefixTableName(const TableName &tableName); 113 114 static std::list<std::string> GenerateNodesByNodeWeight(const std::vector<std::string> &nodes, 115 const std::map<std::string, std::map<std::string, bool>> &graph, 116 const std::map<std::string, int> &nodeWeight); 117 118 static bool HasPrimaryKey(const std::vector<Field> &fields); 119 120 static bool IsRecordError(const VBucket &record); 121 122 static bool IsIntTypeRecordError(const VBucket &record); 123 124 static bool IsRecordIgnored(const VBucket &record); 125 126 static bool IsRecordVersionConflict(const VBucket &record); 127 128 static bool IsRecordAssetsMissing(const VBucket &record); 129 130 static bool IsRecordDelete(const VBucket &record); 131 132 static bool IsCloudRecordNotFound(const VBucket &record); 133 134 static bool IsCloudRecordAlreadyExisted(const VBucket &record); 135 136 static bool IsNeedCompensatedForUpload(const VBucket &uploadExtend, const CloudWaterType &type); 137 138 static bool IsRecordSuccess(const VBucket &record); 139 140 static std::string GenerateHashLabel(const DBInfo &dbInfo); 141 142 static uint64_t EraseBit(uint64_t origin, uint64_t eraseBit); 143 144 static void LoadGrdLib(void); 145 146 static bool IsGrdLibLoaded(void); 147 148 static bool CheckCloudSyncConfigValid(const CloudSyncConfig &config); 149 150 static std::string GetCursorKey(const std::string &tableName); 151 152 static bool ConvertToUInt64(const std::string &str, uint64_t &value); 153 154 static void RemoveDuplicateAssetsData(std::vector<Asset> &assets); 155 private: 156 static void InsertNodesByScore(const std::map<std::string, std::map<std::string, bool>> &graph, 157 const std::vector<std::string> &generateNodes, const std::map<std::string, int> &scoreGraph, 158 std::list<std::string> &insertTarget); 159 }; 160 161 // Define short macro substitute for original long expression for convenience of using 162 #define VEC_TO_STR(x) DBCommon::VectorToHexString(x).c_str() 163 #define STR_MASK(x) DBCommon::StringMasking(x).c_str() 164 #define STR_TO_HEX(x) DBCommon::TransferStringToHex(x).c_str() 165 } // namespace DistributedDB 166 167 #endif // DISTRIBUTEDDB_COMMON_H 168