• 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 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 
SetBeforeUploadTransaction(const std::function<void (void)> & func)85     void SetBeforeUploadTransaction(const std::function<void (void)> &func)
86     {
87         beforeUploadTransaction_ = func;
88     }
89 
DoBeforeUploadTransaction()90     void DoBeforeUploadTransaction()
91     {
92         if (beforeUploadTransaction_) {
93             beforeUploadTransaction_();
94         }
95     }
96 protected:
97     std::function<void (void)> syncFinishFunc_;
98     std::function<void (void)> uploadStartFunc_;
99     std::function<void (void)> beforeUploadTransaction_;
100 };
101 
102 class ICloudSyncStorageInterface : public ICloudSyncStorageHook {
103 public:
104     ICloudSyncStorageInterface() = default;
105     virtual ~ICloudSyncStorageInterface() = default;
106 
107     virtual int GetMetaData(const Key &key, Value &value) const = 0;
108 
109     virtual int PutMetaData(const Key &key, const Value &value) = 0;
110 
111     virtual int ChkSchema(const TableName &tableName) = 0;
112 
113     virtual int SetCloudDbSchema(const DataBaseSchema &schema) = 0;
114 
115     virtual int GetCloudDbSchema(std::shared_ptr<DataBaseSchema> &cloudSchema) = 0;
116 
117     virtual int GetCloudTableSchema(const TableName &tableName, TableSchema &tableSchema) = 0;
118 
119     virtual int StartTransaction(TransactType type, bool isAsyncDownload = false) = 0;
120 
121     virtual int Commit(bool isAsyncDownload = false) = 0;
122 
123     virtual int Rollback(bool isAsyncDownload = false) = 0;
124 
125     virtual int GetUploadCount(const QuerySyncObject &query, const Timestamp &timestamp, bool isCloudForcePush,
126         bool isCompensatedTask, int64_t &count) = 0;
127 
128     virtual int GetAllUploadCount(const QuerySyncObject &query, const std::vector<Timestamp> &timestampVec,
129         bool isCloudForcePush, bool isCompensatedTask, int64_t &count) = 0;
130 
131     virtual int GetCloudData(const TableSchema &tableSchema, const QuerySyncObject &object, const Timestamp &beginTime,
132         ContinueToken &continueStmtToken, CloudSyncData &cloudDataResult) = 0;
133 
134     virtual int GetCloudDataNext(ContinueToken &continueStmtToken, CloudSyncData &cloudDataResult) = 0;
135 
136     virtual int GetCloudGid(const TableSchema &tableSchema, const QuerySyncObject &querySyncObject,
137         bool isCloudForcePush, bool isCompensatedTask, std::vector<std::string> &cloudGid) = 0;
138 
139     virtual int ReleaseCloudDataToken(ContinueToken &continueStmtToken) = 0;
140 
141     virtual int GetInfoByPrimaryKeyOrGid(const std::string &tableName, const VBucket &vBucket,
142         DataInfoWithLog &dataInfoWithLog, VBucket &assetInfo) = 0;
143 
144     virtual int PutCloudSyncData(const std::string &tableName, DownloadData &downloadData) = 0;
145 
146     virtual int UpdateAssetStatusForAssetOnly(const std::string &tableName, VBucket &asset) = 0;
147 
CleanCloudData(ClearMode mode,const std::vector<std::string> & tableNameList,const RelationalSchemaObject & localSchema,std::vector<Asset> & assets)148     virtual int CleanCloudData(ClearMode mode, const std::vector<std::string> &tableNameList,
149         const RelationalSchemaObject &localSchema, std::vector<Asset> &assets)
150     {
151         return E_OK;
152     }
153 
ClearCloudLogVersion(const std::vector<std::string> & tableNameList)154     virtual int ClearCloudLogVersion(const std::vector<std::string> &tableNameList)
155     {
156         return E_OK;
157     }
158 
159     virtual void TriggerObserverAction(const std::string &deviceName, ChangedData &&changedData,
160         bool isChangedData) = 0;
161 
162     virtual int FillCloudAssetForDownload(const std::string &tableName, VBucket &asset, bool isDownloadSuccess) = 0;
163 
164     virtual int FillCloudAssetForAsyncDownload(const std::string &tableName, VBucket &asset,
165         bool isDownloadSuccess) = 0;
166 
167     virtual int SetLogTriggerStatus(bool status) = 0;
168 
169     virtual int SetLogTriggerStatusForAsyncDownload(bool status) = 0;
170 
SetCursorIncFlag(bool flag)171     virtual int SetCursorIncFlag(bool flag)
172     {
173         return E_OK;
174     };
175 
176     virtual int FillCloudLogAndAsset(OpType opType, const CloudSyncData &data, bool fillAsset, bool ignoreEmptyGid) = 0;
177 
178     virtual std::string GetIdentify() const = 0;
179 
180     virtual int CheckQueryValid(const QuerySyncObject &query) = 0;
181 
CreateTempSyncTrigger(const std::string & tableName)182     virtual int CreateTempSyncTrigger(const std::string &tableName)
183     {
184         return E_OK;
185     }
186 
GetAndResetServerObserverData(const std::string & tableName,ChangeProperties & changeProperties)187     virtual int GetAndResetServerObserverData(const std::string &tableName, ChangeProperties &changeProperties)
188     {
189         return E_OK;
190     }
191 
ClearAllTempSyncTrigger()192     virtual int ClearAllTempSyncTrigger()
193     {
194         return E_OK;
195     }
196 
197     virtual bool IsSharedTable(const std::string &tableName) = 0;
198 
GetAssetsByGidOrHashKey(const TableSchema & tableSchema,const std::string & gid,const Bytes & hashKey,VBucket & assets)199     virtual std::pair<int, uint32_t> GetAssetsByGidOrHashKey(const TableSchema &tableSchema, const std::string &gid,
200         const Bytes &hashKey, VBucket &assets)
201     {
202         return { E_OK, static_cast<uint32_t>(LockStatus::UNLOCK) };
203     }
204 
GetAssetsByGidOrHashKeyForAsyncDownload(const TableSchema & tableSchema,const std::string & gid,const Bytes & hashKey,VBucket & assets)205     virtual std::pair<int, uint32_t> GetAssetsByGidOrHashKeyForAsyncDownload(const TableSchema &tableSchema,
206         const std::string &gid, const Bytes &hashKey, VBucket &assets)
207     {
208         return { E_OK, static_cast<uint32_t>(LockStatus::UNLOCK) };
209     }
210 
SetIAssetLoader(const std::shared_ptr<IAssetLoader> & loader)211     virtual int SetIAssetLoader([[gnu::unused]] const std::shared_ptr<IAssetLoader> &loader)
212     {
213         return E_OK;
214     }
215 
UpdateRecordFlag(const std::string & tableName,bool recordConflict,const LogInfo & logInfo)216     virtual int UpdateRecordFlag([[gnu::unused]] const std::string &tableName,
217         [[gnu::unused]] bool recordConflict, [[gnu::unused]] const LogInfo &logInfo)
218     {
219         return E_OK;
220     }
221 
UpdateRecordFlagForAsyncDownload(const std::string & tableName,bool recordConflict,const LogInfo & logInfo)222     virtual int UpdateRecordFlagForAsyncDownload([[gnu::unused]] const std::string &tableName,
223         [[gnu::unused]] bool recordConflict, [[gnu::unused]] const LogInfo &logInfo)
224     {
225         return E_OK;
226     }
227 
GetCompensatedSyncQuery(std::vector<QuerySyncObject> & syncQuery,std::vector<std::string> & users,bool isQueryDownloadRecords)228     virtual int GetCompensatedSyncQuery([[gnu::unused]] std::vector<QuerySyncObject> &syncQuery,
229         [[gnu::unused]] std::vector<std::string> &users, [[gnu::unused]] bool isQueryDownloadRecords)
230     {
231         return E_OK;
232     }
233 
ClearUnLockingNoNeedCompensated()234     virtual int ClearUnLockingNoNeedCompensated()
235     {
236         return E_OK;
237     }
238 
MarkFlagAsConsistent(const std::string & tableName,const DownloadData & downloadData,const std::set<std::string> & gidFilters)239     virtual int MarkFlagAsConsistent([[gnu::unused]] const std::string &tableName,
240         [[gnu::unused]] const DownloadData &downloadData, [[gnu::unused]] const std::set<std::string> &gidFilters)
241     {
242         return E_OK;
243     }
244 
MarkFlagAsAssetAsyncDownload(const std::string & tableName,const DownloadData & downloadData,const std::set<std::string> & gidFilters)245     virtual int MarkFlagAsAssetAsyncDownload([[gnu::unused]] const std::string &tableName,
246         [[gnu::unused]] const DownloadData &downloadData, [[gnu::unused]] const std::set<std::string> &gidFilters)
247     {
248         return E_OK;
249     }
250 
SetUser(const std::string & user)251     virtual void SetUser([[gnu::unused]] const std::string &user)
252     {
253     }
254 
GetLocalCloudVersion()255     virtual std::pair<int, CloudSyncData> GetLocalCloudVersion()
256     {
257         return {E_OK, {}};
258     }
259 
260     virtual CloudSyncConfig GetCloudSyncConfig() const = 0;
261 
IsTableExistReference(const std::string & table)262     virtual bool IsTableExistReference(const std::string &table)
263     {
264         return false;
265     }
266 
IsTableExistReferenceOrReferenceBy(const std::string & table)267     virtual bool IsTableExistReferenceOrReferenceBy(const std::string &table)
268     {
269         return false;
270     }
271 
ReleaseUploadRecord(const std::string & tableName,const CloudWaterType & type,Timestamp localMark)272     virtual void ReleaseUploadRecord([[gnu::unused]] const std::string &tableName,
273         [[gnu::unused]] const CloudWaterType &type, Timestamp localMark)
274     {
275     }
276 
IsTagCloudUpdateLocal(const LogInfo & localInfo,const LogInfo & cloudInfo,SingleVerConflictResolvePolicy policy)277     virtual bool IsTagCloudUpdateLocal(const LogInfo &localInfo, const LogInfo &cloudInfo,
278         SingleVerConflictResolvePolicy policy)
279     {
280         return false;
281     }
282 
ReviseLocalModTime(const std::string & tableName,const std::vector<ReviseModTimeInfo> & revisedData)283     virtual int ReviseLocalModTime(const std::string &tableName,
284         const std::vector<ReviseModTimeInfo> &revisedData)
285     {
286         return E_OK;
287     }
288 
GetCursor(const std::string & tableName,uint64_t & cursor)289     virtual int GetCursor(const std::string &tableName, uint64_t &cursor)
290     {
291         cursor = DBConstant::INVALID_CURSOR;
292         return -E_NOT_SUPPORT;
293     }
294 
IsCurrentLogicDelete()295     virtual bool IsCurrentLogicDelete() const
296     {
297         return false;
298     }
299 
300     virtual int GetLocalDataCount(const std::string &tableName, int &dataCount, int &logicDeleteDataCount) = 0;
301 
302     virtual std::pair<int, std::vector<std::string>> GetDownloadAssetTable() = 0;
303 
304     virtual std::pair<int, std::vector<std::string>> GetDownloadAssetRecords(const std::string &tableName,
305         int64_t beginTime) = 0;
306 
GetInfoByPrimaryKeyOrGid(const std::string & tableName,const VBucket & vBucket,bool useTransaction,DataInfoWithLog & dataInfoWithLog,VBucket & assetInfo)307     virtual int GetInfoByPrimaryKeyOrGid(const std::string &tableName, const VBucket &vBucket,
308         [[gnu::unused]] bool useTransaction, DataInfoWithLog &dataInfoWithLog, VBucket &assetInfo)
309     {
310         return GetInfoByPrimaryKeyOrGid(tableName, vBucket, dataInfoWithLog, assetInfo);
311     }
312 
PrintCursorChange(const std::string & tableName)313     virtual void PrintCursorChange([[gnu::unused]] const std::string &tableName) {}
314 
GetLockStatusByGid(const std::string & tableName,const std::string & gid,LockStatus & status)315     virtual int GetLockStatusByGid(const std::string &tableName, const std::string &gid, LockStatus &status)
316     {
317         return E_OK;
318     }
319 
IsExistTableContainAssets()320     virtual bool IsExistTableContainAssets()
321     {
322         return false;
323     }
324 };
325 }
326 
327 #endif // ICLOUD_SYNC_STORAGE_INTERFACE_H
328