• 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/cloud_store_types.h"
20 #include "sqlite_utils.h"
21 
22 namespace DistributedDB {
23 class CloudStorageUtils final {
24 public:
25     static int BindInt64(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
26     static int BindBool(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
27     static int BindDouble(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
28     static int BindText(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
29     static int BindBlob(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
30     static int BindAsset(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *upsertStmt);
31 
IsFieldValid(const Field & field,int errCode)32     static inline bool IsFieldValid(const Field &field, int errCode)
33     {
34         return (errCode == E_OK || (field.nullable && errCode == -E_NOT_FOUND));
35     }
36 
37     static int Int64ToVector(const VBucket &vBucket, const Field &field, std::vector<uint8_t> &value);
38     static int BoolToVector(const VBucket &vBucket, const Field &field, std::vector<uint8_t> &value);
39     static int DoubleToVector(const VBucket &vBucket, const Field &field, std::vector<uint8_t> &value);
40     static int TextToVector(const VBucket &vBucket, const Field &field, std::vector<uint8_t> &value);
41     static int BlobToVector(const VBucket &vBucket, const Field &field, std::vector<uint8_t> &value);
42 
43     static std::set<std::string> GetCloudPrimaryKey(const TableSchema &tableSchema);
44     static std::vector<Field> GetCloudPrimaryKeyField(const TableSchema &tableSchema);
45     static std::map<std::string, Field> GetCloudPrimaryKeyFieldMap(const TableSchema &tableSchema);
46     static bool IsContainsPrimaryKey(const TableSchema &tableSchema);
47     static std::vector<Field> GetCloudAsset(const TableSchema &tableSchema);
48     static int GetAssetFieldsFromSchema(const TableSchema &tableSchema, VBucket &vBucket,
49         std::vector<Field> &fields);
50     static void ObtainAssetFromVBucket(const VBucket &vBucket, VBucket &asset);
51     static AssetOpType StatusToFlag(AssetStatus status);
52     static AssetStatus FlagToStatus(AssetOpType opType);
53     static void ChangeAssetsOnVBucketToAsset(VBucket &vBucket, std::vector<Field> &fields);
54     static Type GetAssetFromAssets(Type &value);
55     static void FillAssetBeforeDownload(Asset &asset);
56     static void FillAssetAfterDownloadFail(Asset &asset);
57     static int FillAssetAfterDownload(Asset &asset);
58     static void FillAssetsAfterDownload(Assets &assets);
59     static int FillAssetForUpload(Asset &asset);
60     static void FillAssetsForUpload(Assets &assets);
61     static void PrepareToFillAssetFromVBucket(VBucket &vBucket, std::function<void(Asset &)> fillAsset);
62     static void FillAssetFromVBucketFinish(VBucket &vBucket, std::function<int(Asset &)> fillAsset,
63         std::function<void(Assets &)> fillAssets);
64     static bool IsAsset(const Type &type);
65     static bool IsAssets(const Type &type);
66     static bool IsAssetsContainDuplicateAsset(Assets &assets);
67     static void EraseNoChangeAsset(std::map<std::string, Assets> &assetsMap);
68     static void MergeDownloadAsset(std::map<std::string, Assets> &downloadAssets,
69         std::map<std::string, Assets> &mergeAssets);
70     static std::map<std::string, size_t> GenAssetsIndexMap(Assets &assets);
71     static bool IsVbucketContainsAllPK(const VBucket &vBucket, const std::set<std::string> &pkSet);
72     static bool CheckAssetStatus(const Assets &assets);
73 
74     static int ConstraintsCheckForCloud(const TableInfo &table, const std::string &trimmedSql);
75 
76     template<typename T>
GetValueFromOneField(Type & cloudValue,T & outVal)77     static int GetValueFromOneField(Type &cloudValue, T &outVal)
78     {
79         T *value = std::get_if<T>(&cloudValue);
80         if (value == nullptr) {
81             LOGE("Get cloud data fail because type mismatch.");
82             return -E_CLOUD_ERROR;
83         }
84         outVal = *value;
85         return E_OK;
86     }
87 
88     template<typename T>
GetValueFromVBucket(const std::string & fieldName,const VBucket & vBucket,T & outVal)89     static int GetValueFromVBucket(const std::string &fieldName, const VBucket &vBucket, T &outVal)
90     {
91         if (vBucket.find(fieldName) == vBucket.end()) {
92             LOGW("vbucket doesn't contains the field want to get");
93             return -E_NOT_FOUND;
94         }
95         Type cloudValue = vBucket.at(fieldName);
96         return GetValueFromOneField(cloudValue, outVal);
97     }
98 
99     template<typename T>
GetValueFromType(Type & cloudValue,T & outVal)100     static int GetValueFromType(Type &cloudValue, T &outVal)
101     {
102         T *value = std::get_if<T>(&cloudValue);
103         if (value == nullptr) {
104             return -E_CLOUD_ERROR;
105         }
106         outVal = *value;
107         return E_OK;
108     }
109 
110     static int CalculateHashKeyForOneField(const Field &field, const VBucket &vBucket, bool allowEmpty,
111         std::vector<uint8_t> &hashValue);
112 };
113 }
114 #endif // CLOUD_STORAGE_UTILS_H
115