• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 CLOUD_STORAGE_UTILS_H
17 #define CLOUD_STORAGE_UTILS_H
18 
19 #include "cloud/asset_operation_utils.h"
20 #include "cloud/cloud_store_types.h"
21 #include "icloud_sync_storage_interface.h"
22 #include "sqlite_utils.h"
23 
24 namespace DistributedDB {
25 class CloudStorageUtils final {
26 public:
27     static int BindInt64(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
28     static int BindBool(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
29     static int BindDouble(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
30     static int BindText(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
31     static int BindBlob(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
32     static int BindAsset(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
33 
IsFieldValid(const Field & field,int errCode)34     static inline bool IsFieldValid(const Field &field, int errCode)
35     {
36         return (errCode == E_OK || (field.nullable && errCode == -E_NOT_FOUND));
37     }
38 
39     static int Int64ToVector(const VBucket &vBucket, const Field &field, CollateType collateType,
40         std::vector<uint8_t> &value);
41     static int BoolToVector(const VBucket &vBucket, const Field &field, CollateType collateType,
42         std::vector<uint8_t> &value);
43     static int DoubleToVector(const VBucket &vBucket, const Field &field, CollateType collateType,
44         std::vector<uint8_t> &value);
45     static int TextToVector(const VBucket &vBucket, const Field &field, CollateType collateType,
46         std::vector<uint8_t> &value);
47     static int BlobToVector(const VBucket &vBucket, const Field &field, CollateType collateType,
48         std::vector<uint8_t> &value);
49 
50     static std::set<std::string> GetCloudPrimaryKey(const TableSchema &tableSchema);
51     static std::vector<Field> GetCloudPrimaryKeyField(const TableSchema &tableSchema, bool sortByName = false);
52     static std::map<std::string, Field> GetCloudPrimaryKeyFieldMap(const TableSchema &tableSchema,
53         bool sortByUpper = false);
54     static bool IsContainsPrimaryKey(const TableSchema &tableSchema);
55     static std::vector<Field> GetCloudAsset(const TableSchema &tableSchema);
56     static int GetAssetFieldsFromSchema(const TableSchema &tableSchema, const VBucket &vBucket,
57         std::vector<Field> &fields);
58     static void ObtainAssetFromVBucket(const VBucket &vBucket, VBucket &asset);
59     static AssetOpType StatusToFlag(AssetStatus status);
60     static AssetStatus FlagToStatus(AssetOpType opType);
61     static void ChangeAssetsOnVBucketToAsset(VBucket &vBucket, std::vector<Field> &fields);
62     static Type GetAssetFromAssets(Type &value);
63     static int FillAssetBeforeDownload(Asset &asset);
64     static int FillAssetAfterDownloadFail(Asset &asset, Asset &dbAsset, AssetOperationUtils::AssetOpType assetOpType);
65     static void FillAssetsAfterDownloadFail(Assets &assets, Assets &dbAssets,
66         const std::map<std::string, AssetOperationUtils::AssetOpType> &assetOpTypeMap);
67     static void MergeAssetWithFillFunc(Assets &assets, Assets &dbAssets, const std::map<std::string,
68         AssetOperationUtils::AssetOpType> &assetOpTypeMap,
69         std::function<int(Asset &, Asset &, AssetOperationUtils::AssetOpType)> fillAsset);
70     static int FillAssetAfterDownload(Asset &asset, Asset &dbAsset, AssetOperationUtils::AssetOpType assetOpType);
71     static void FillAssetsAfterDownload(Assets &assets, Assets &dbAssets,
72         const std::map<std::string, AssetOperationUtils::AssetOpType> &assetOpTypeMap);
73     static int FillAssetBeforeUpload(Asset &asset, Asset &dbAsset, AssetOperationUtils::AssetOpType assetOpType);
74     static void FillAssetsBeforeUpload(Assets &assets, Assets &dbAssets,
75         const std::map<std::string, AssetOperationUtils::AssetOpType> &assetOpTypeMap);
76     static int FillAssetForUpload(Asset &asset, Asset &dbAsset, AssetOperationUtils::AssetOpType assetOpType);
77     static void FillAssetsForUpload(Assets &assets, Assets &dbAssets,
78         const std::map<std::string, AssetOperationUtils::AssetOpType> &assetOpTypeMap);
79     static int FillAssetForUploadFailed(Asset &asset, Asset &dbAsset, AssetOperationUtils::AssetOpType assetOpType);
80     static void FillAssetsForUploadFailed(Assets &assets, Assets &dbAssets,
81         const std::map<std::string, AssetOperationUtils::AssetOpType> &assetOpTypeMap);
82     static void PrepareToFillAssetFromVBucket(VBucket &vBucket, std::function<int(Asset &)> fillAsset);
83     static void FillAssetFromVBucketFinish(const AssetOperationUtils::RecordAssetOpType &assetOpType, VBucket &vBucket,
84         VBucket &dbAssets, std::function<int(Asset &, Asset &, AssetOperationUtils::AssetOpType)> fillAsset,
85         std::function<void(Assets &, Assets &,
86         const std::map<std::string, AssetOperationUtils::AssetOpType> &)> fillAssets);
87     static bool IsAsset(const Type &type);
88     static bool IsAssets(const Type &type);
89     static bool IsAssetsContainDuplicateAsset(Assets &assets);
90     static void EraseNoChangeAsset(std::map<std::string, Assets> &assetsMap);
91     static void MergeDownloadAsset(std::map<std::string, Assets> &downloadAssets,
92         std::map<std::string, Assets> &mergeAssets);
93     static std::map<std::string, size_t> GenAssetsIndexMap(Assets &assets);
94     static bool IsVbucketContainsAllPK(const VBucket &vBucket, const std::set<std::string> &pkSet);
95     static bool CheckAssetStatus(const Assets &assets);
96 
97     static int ConstraintsCheckForCloud(const TableInfo &table, const std::string &trimmedSql);
98     static std::string GetTableRefUpdateSql(const TableInfo &table, OpType opType);
99     static std::string GetLeftJoinLogSql(const std::string &tableName, bool logAsTableA = true);
100 
101     static bool IsSharedTable(const TableSchema &tableSchema);
102     static bool ChkFillCloudAssetParam(const CloudSyncBatch &data, int errCode);
103     static void GetToBeRemoveAssets(const VBucket &vBucket, const AssetOperationUtils::RecordAssetOpType &assetOpType,
104         std::vector<Asset> &removeAssets);
105     static std::pair<int, std::vector<uint8_t>> GetHashValueWithPrimaryKeyMap(const VBucket &vBucket,
106         const TableSchema &tableSchema, const TableInfo &localTable, const std::map<std::string, Field> &pkMap,
107         bool allowEmpty);
108 
109     template<typename T>
GetValueFromOneField(Type & cloudValue,T & outVal)110     static int GetValueFromOneField(Type &cloudValue, T &outVal)
111     {
112         T *value = std::get_if<T>(&cloudValue);
113         if (value == nullptr) {
114             LOGE("Get cloud data fail because type mismatch.");
115             return -E_CLOUD_ERROR;
116         }
117         outVal = *value;
118         return E_OK;
119     }
120 
121     template<typename T>
GetValueFromVBucket(const std::string & fieldName,const VBucket & vBucket,T & outVal)122     static int GetValueFromVBucket(const std::string &fieldName, const VBucket &vBucket, T &outVal)
123     {
124         Type cloudValue;
125         bool isExisted = GetTypeCaseInsensitive(fieldName, vBucket, cloudValue);
126         if (!isExisted) {
127             return -E_NOT_FOUND;
128         }
129         return GetValueFromOneField(cloudValue, outVal);
130     }
131 
132     template<typename T>
GetValueFromType(Type & cloudValue,T & outVal)133     static int GetValueFromType(Type &cloudValue, T &outVal)
134     {
135         T *value = std::get_if<T>(&cloudValue);
136         if (value == nullptr) {
137             return -E_CLOUD_ERROR;
138         }
139         outVal = *value;
140         return E_OK;
141     }
142 
143     static int CalculateHashKeyForOneField(const Field &field, const VBucket &vBucket, bool allowEmpty,
144         CollateType collateType, std::vector<uint8_t> &hashValue);
145 
146     static void TransferFieldToLower(VBucket &vBucket);
147 
148     static bool GetTypeCaseInsensitive(const std::string &fieldName, const VBucket &vBucket, Type &data);
149 
150     static void TransferSchemaFieldToLower(TableSchema &tableSchema);
151 
152     static bool CheckCloudSchemaFields(const TableSchema &tableSchema, const TableSchema &oldSchema);
153 };
154 }
155 #endif // CLOUD_STORAGE_UTILS_H
156