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 ICLOUD_SYNC_STORAGE_INTERFACE_H
17 #define ICLOUD_SYNC_STORAGE_INTERFACE_H
18 
19 #include "cloud/cloud_db_types.h"
20 #include "cloud/cloud_store_types.h"
21 #include "cloud/iAssetLoader.h"
22 #include "data_transformer.h"
23 #include "query_sync_object.h"
24 #include "query_utils.h"
25 #include "sqlite_utils.h"
26 #include "store_observer.h"
27 
28 namespace DistributedDB {
29 
30 enum class OpType : uint8_t {
31     INSERT = 1,
32     UPDATE, // update data, gid and timestamp at same time
33     DELETE,
34     ONLY_UPDATE_GID,
35     // used in Cloud Force Push strategy, when SET_CLOUD_FORCE_PUSH_FLAG_ONE, upload process won't process this record
36     SET_CLOUD_FORCE_PUSH_FLAG_ONE,
37     SET_CLOUD_FORCE_PUSH_FLAG_ZERO,
38     UPDATE_TIMESTAMP,
39     CLEAR_GID,
40     UPDATE_VERSION,
41     INSERT_VERSION,
42     SET_UPLOADING,
43     LOCKED_NOT_HANDLE,
44     NOT_HANDLE
45 };
46 
47 typedef struct DownloadData {
48     std::vector<VBucket> data;
49     std::vector<OpType> opType;
50     std::vector<int64_t> existDataKey;
51     std::vector<Key> existDataHashKey;
52     std::string user;
53     TimeOffset timeOffset = 0;
54 } DownloadData;
55 
56 class ICloudSyncStorageHook {
57 public:
58     ICloudSyncStorageHook() = default;
59     virtual ~ICloudSyncStorageHook() = default;
60 
SetSyncFinishHook(const std::function<void (void)> & func)61     virtual void SetSyncFinishHook(const std::function<void (void)> &func)
62     {
63         syncFinishFunc_ = func;
64     }
65 
SyncFinishHook()66     virtual void SyncFinishHook()
67     {
68         if (syncFinishFunc_) {
69             syncFinishFunc_();
70         }
71     }
72 
SetDoUploadHook(const std::function<void (void)> & func)73     virtual void SetDoUploadHook(const std::function<void (void)> &func)
74     {
75         uploadStartFunc_ = func;
76     }
77 
DoUploadHook()78     virtual void DoUploadHook()
79     {
80         if (uploadStartFunc_) {
81             uploadStartFunc_();
82         }
83     }
84 protected:
85     std::function<void (void)> syncFinishFunc_;
86     std::function<void (void)> uploadStartFunc_;
87 };
88 
89 class ICloudSyncStorageInterface : public ICloudSyncStorageHook {
90 public:
91     ICloudSyncStorageInterface() = default;
92     virtual ~ICloudSyncStorageInterface() = default;
93 
94     virtual int GetMetaData(const Key &key, Value &value) const = 0;
95 
96     virtual int PutMetaData(const Key &key, const Value &value) = 0;
97 
98     virtual int ChkSchema(const TableName &tableName) = 0;
99 
100     virtual int SetCloudDbSchema(const DataBaseSchema &schema) = 0;
101 
102     virtual int GetCloudDbSchema(std::shared_ptr<DataBaseSchema> &cloudSchema) = 0;
103 
104     virtual int GetCloudTableSchema(const TableName &tableName, TableSchema &tableSchema) = 0;
105 
106     virtual int StartTransaction(TransactType type) = 0;
107 
108     virtual int Commit() = 0;
109 
110     virtual int Rollback() = 0;
111 
112     virtual int GetUploadCount(const QuerySyncObject &query, const Timestamp &timestamp, bool isCloudForcePush,
113         bool isCompensatedTask, int64_t &count) = 0;
114 
115     virtual int GetAllUploadCount(const QuerySyncObject &query, const std::vector<Timestamp> &timestampVec,
116         bool isCloudForcePush, bool isCompensatedTask, int64_t &count) = 0;
117 
118     virtual int GetCloudData(const TableSchema &tableSchema, const QuerySyncObject &object, const Timestamp &beginTime,
119         ContinueToken &continueStmtToken, CloudSyncData &cloudDataResult) = 0;
120 
121     virtual int GetCloudDataNext(ContinueToken &continueStmtToken, CloudSyncData &cloudDataResult) = 0;
122 
123     virtual int GetCloudGid(const TableSchema &tableSchema, const QuerySyncObject &querySyncObject,
124         bool isCloudForcePush, bool isCompensatedTask, std::vector<std::string> &cloudGid) = 0;
125 
126     virtual int ReleaseCloudDataToken(ContinueToken &continueStmtToken) = 0;
127 
128     virtual int GetInfoByPrimaryKeyOrGid(const std::string &tableName, const VBucket &vBucket,
129         DataInfoWithLog &dataInfoWithLog, VBucket &assetInfo) = 0;
130 
131     virtual int PutCloudSyncData(const std::string &tableName, DownloadData &downloadData) = 0;
132 
CleanCloudData(ClearMode mode,const std::vector<std::string> & tableNameList,const RelationalSchemaObject & localSchema,std::vector<Asset> & assets)133     virtual int CleanCloudData(ClearMode mode, const std::vector<std::string> &tableNameList,
134         const RelationalSchemaObject &localSchema, std::vector<Asset> &assets)
135     {
136         return E_OK;
137     }
138 
139     virtual void TriggerObserverAction(const std::string &deviceName, ChangedData &&changedData,
140         bool isChangedData) = 0;
141 
142     virtual int FillCloudAssetForDownload(const std::string &tableName, VBucket &asset, bool isDownloadSuccess) = 0;
143 
144     virtual int SetLogTriggerStatus(bool status) = 0;
SetCursorIncFlag(bool flag)145     virtual int SetCursorIncFlag(bool flag)
146     {
147         return E_OK;
148     };
149 
150     virtual int FillCloudLogAndAsset(OpType opType, const CloudSyncData &data, bool fillAsset, bool ignoreEmptyGid) = 0;
151 
152     virtual std::string GetIdentify() const = 0;
153 
154     virtual int CheckQueryValid(const QuerySyncObject &query) = 0;
155 
CreateTempSyncTrigger(const std::string & tableName)156     virtual int CreateTempSyncTrigger(const std::string &tableName)
157     {
158         return E_OK;
159     }
160 
GetAndResetServerObserverData(const std::string & tableName,ChangeProperties & changeProperties)161     virtual int GetAndResetServerObserverData(const std::string &tableName, ChangeProperties &changeProperties)
162     {
163         return E_OK;
164     }
165 
ClearAllTempSyncTrigger()166     virtual int ClearAllTempSyncTrigger()
167     {
168         return E_OK;
169     }
170 
171     virtual bool IsSharedTable(const std::string &tableName) = 0;
172 
173     virtual void SetCloudTaskConfig([[gnu::unused]] const CloudTaskConfig &config)
174     {
175     }
176 
GetAssetsByGidOrHashKey(const TableSchema & tableSchema,const std::string & gid,const Bytes & hashKey,VBucket & assets)177     virtual std::pair<int, uint32_t> GetAssetsByGidOrHashKey(const TableSchema &tableSchema, const std::string &gid,
178         const Bytes &hashKey, VBucket &assets)
179     {
180         return { E_OK, static_cast<uint32_t>(LockStatus::UNLOCK) };
181     }
182 
183     virtual int SetIAssetLoader([[gnu::unused]] const std::shared_ptr<IAssetLoader> &loader)
184     {
185         return E_OK;
186     }
187 
188     virtual int UpdateRecordFlag([[gnu::unused]] const std::string &tableName,
189         [[gnu::unused]] bool recordConflict, [[gnu::unused]] const LogInfo &logInfo)
190     {
191         return E_OK;
192     }
193 
194     virtual int GetCompensatedSyncQuery([[gnu::unused]] std::vector<QuerySyncObject> &syncQuery,
195         [[gnu::unused]] std::vector<std::string> &users)
196     {
197         return E_OK;
198     }
199 
ClearUnLockingNoNeedCompensated()200     virtual int ClearUnLockingNoNeedCompensated()
201     {
202         return E_OK;
203     }
204 
205     virtual int MarkFlagAsConsistent([[gnu::unused]] const std::string &tableName,
206         [[gnu::unused]] const DownloadData &downloadData, [[gnu::unused]] const std::set<std::string> &gidFilters)
207     {
208         return E_OK;
209     }
210 
211     virtual void SetUser([[gnu::unused]] const std::string &user)
212     {
213     }
214 
GetLocalCloudVersion()215     virtual std::pair<int, CloudSyncData> GetLocalCloudVersion()
216     {
217         return {E_OK, {}};
218     }
219 
220     virtual CloudSyncConfig GetCloudSyncConfig() const = 0;
221 
IsTableExistReference(const std::string & table)222     virtual bool IsTableExistReference(const std::string &table)
223     {
224         return false;
225     }
226 
IsTableExistReferenceOrReferenceBy(const std::string & table)227     virtual bool IsTableExistReferenceOrReferenceBy(const std::string &table)
228     {
229         return false;
230     }
231 
232     virtual void ReleaseUploadRecord([[gnu::unused]] const std::string &tableName,
233         [[gnu::unused]] const CloudWaterType &type, Timestamp localMark)
234     {
235     }
236 
IsTagCloudUpdateLocal(const LogInfo & localInfo,const LogInfo & cloudInfo,SingleVerConflictResolvePolicy policy)237     virtual bool IsTagCloudUpdateLocal(const LogInfo &localInfo, const LogInfo &cloudInfo,
238         SingleVerConflictResolvePolicy policy)
239     {
240         return false;
241     }
242 };
243 }
244 
245 #endif // ICLOUD_SYNC_STORAGE_INTERFACE_H
246