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