• 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) = 0;
120 
121     virtual int Commit() = 0;
122 
123     virtual int Rollback() = 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 
154     virtual void TriggerObserverAction(const std::string &deviceName, ChangedData &&changedData,
155         bool isChangedData) = 0;
156 
157     virtual int FillCloudAssetForDownload(const std::string &tableName, VBucket &asset, bool isDownloadSuccess) = 0;
158 
159     virtual int FillCloudAssetForAsyncDownload(const std::string &tableName, VBucket &asset,
160         bool isDownloadSuccess) = 0;
161 
162     virtual int SetLogTriggerStatus(bool status) = 0;
163 
164     virtual int SetLogTriggerStatusForAsyncDownload(bool status) = 0;
165 
SetCursorIncFlag(bool flag)166     virtual int SetCursorIncFlag(bool flag)
167     {
168         return E_OK;
169     };
170 
171     virtual int FillCloudLogAndAsset(OpType opType, const CloudSyncData &data, bool fillAsset, bool ignoreEmptyGid) = 0;
172 
173     virtual std::string GetIdentify() const = 0;
174 
175     virtual int CheckQueryValid(const QuerySyncObject &query) = 0;
176 
CreateTempSyncTrigger(const std::string & tableName)177     virtual int CreateTempSyncTrigger(const std::string &tableName)
178     {
179         return E_OK;
180     }
181 
GetAndResetServerObserverData(const std::string & tableName,ChangeProperties & changeProperties)182     virtual int GetAndResetServerObserverData(const std::string &tableName, ChangeProperties &changeProperties)
183     {
184         return E_OK;
185     }
186 
ClearAllTempSyncTrigger()187     virtual int ClearAllTempSyncTrigger()
188     {
189         return E_OK;
190     }
191 
192     virtual bool IsSharedTable(const std::string &tableName) = 0;
193 
GetAssetsByGidOrHashKey(const TableSchema & tableSchema,const std::string & gid,const Bytes & hashKey,VBucket & assets)194     virtual std::pair<int, uint32_t> GetAssetsByGidOrHashKey(const TableSchema &tableSchema, const std::string &gid,
195         const Bytes &hashKey, VBucket &assets)
196     {
197         return { E_OK, static_cast<uint32_t>(LockStatus::UNLOCK) };
198     }
199 
GetAssetsByGidOrHashKeyForAsyncDownload(const TableSchema & tableSchema,const std::string & gid,const Bytes & hashKey,VBucket & assets)200     virtual std::pair<int, uint32_t> GetAssetsByGidOrHashKeyForAsyncDownload(const TableSchema &tableSchema,
201         const std::string &gid, const Bytes &hashKey, VBucket &assets)
202     {
203         return { E_OK, static_cast<uint32_t>(LockStatus::UNLOCK) };
204     }
205 
SetIAssetLoader(const std::shared_ptr<IAssetLoader> & loader)206     virtual int SetIAssetLoader([[gnu::unused]] const std::shared_ptr<IAssetLoader> &loader)
207     {
208         return E_OK;
209     }
210 
UpdateRecordFlag(const std::string & tableName,bool recordConflict,const LogInfo & logInfo)211     virtual int UpdateRecordFlag([[gnu::unused]] const std::string &tableName,
212         [[gnu::unused]] bool recordConflict, [[gnu::unused]] const LogInfo &logInfo)
213     {
214         return E_OK;
215     }
216 
UpdateRecordFlagForAsyncDownload(const std::string & tableName,bool recordConflict,const LogInfo & logInfo)217     virtual int UpdateRecordFlagForAsyncDownload([[gnu::unused]] const std::string &tableName,
218         [[gnu::unused]] bool recordConflict, [[gnu::unused]] const LogInfo &logInfo)
219     {
220         return E_OK;
221     }
222 
GetCompensatedSyncQuery(std::vector<QuerySyncObject> & syncQuery,std::vector<std::string> & users,bool isQueryDownloadRecords)223     virtual int GetCompensatedSyncQuery([[gnu::unused]] std::vector<QuerySyncObject> &syncQuery,
224         [[gnu::unused]] std::vector<std::string> &users, [[gnu::unused]] bool isQueryDownloadRecords)
225     {
226         return E_OK;
227     }
228 
ClearUnLockingNoNeedCompensated()229     virtual int ClearUnLockingNoNeedCompensated()
230     {
231         return E_OK;
232     }
233 
MarkFlagAsConsistent(const std::string & tableName,const DownloadData & downloadData,const std::set<std::string> & gidFilters)234     virtual int MarkFlagAsConsistent([[gnu::unused]] const std::string &tableName,
235         [[gnu::unused]] const DownloadData &downloadData, [[gnu::unused]] const std::set<std::string> &gidFilters)
236     {
237         return E_OK;
238     }
239 
MarkFlagAsAssetAsyncDownload(const std::string & tableName,const DownloadData & downloadData,const std::set<std::string> & gidFilters)240     virtual int MarkFlagAsAssetAsyncDownload([[gnu::unused]] const std::string &tableName,
241         [[gnu::unused]] const DownloadData &downloadData, [[gnu::unused]] const std::set<std::string> &gidFilters)
242     {
243         return E_OK;
244     }
245 
SetUser(const std::string & user)246     virtual void SetUser([[gnu::unused]] const std::string &user)
247     {
248     }
249 
GetLocalCloudVersion()250     virtual std::pair<int, CloudSyncData> GetLocalCloudVersion()
251     {
252         return {E_OK, {}};
253     }
254 
255     virtual CloudSyncConfig GetCloudSyncConfig() const = 0;
256 
IsTableExistReference(const std::string & table)257     virtual bool IsTableExistReference(const std::string &table)
258     {
259         return false;
260     }
261 
IsTableExistReferenceOrReferenceBy(const std::string & table)262     virtual bool IsTableExistReferenceOrReferenceBy(const std::string &table)
263     {
264         return false;
265     }
266 
ReleaseUploadRecord(const std::string & tableName,const CloudWaterType & type,Timestamp localMark)267     virtual void ReleaseUploadRecord([[gnu::unused]] const std::string &tableName,
268         [[gnu::unused]] const CloudWaterType &type, Timestamp localMark)
269     {
270     }
271 
IsTagCloudUpdateLocal(const LogInfo & localInfo,const LogInfo & cloudInfo,SingleVerConflictResolvePolicy policy)272     virtual bool IsTagCloudUpdateLocal(const LogInfo &localInfo, const LogInfo &cloudInfo,
273         SingleVerConflictResolvePolicy policy)
274     {
275         return false;
276     }
277 
ReviseLocalModTime(const std::string & tableName,const std::vector<ReviseModTimeInfo> & revisedData)278     virtual int ReviseLocalModTime(const std::string &tableName,
279         const std::vector<ReviseModTimeInfo> &revisedData)
280     {
281         return E_OK;
282     }
283 
GetCursor(const std::string & tableName,uint64_t & cursor)284     virtual int GetCursor(const std::string &tableName, uint64_t &cursor)
285     {
286         cursor = DBConstant::INVALID_CURSOR;
287         return -E_NOT_SUPPORT;
288     }
289 
IsCurrentLogicDelete()290     virtual bool IsCurrentLogicDelete() const
291     {
292         return false;
293     }
294 
295     virtual int GetLocalDataCount(const std::string &tableName, int &dataCount, int &logicDeleteDataCount) = 0;
296 
297     virtual std::pair<int, std::vector<std::string>> GetDownloadAssetTable() = 0;
298 
299     virtual std::pair<int, std::vector<std::string>> GetDownloadAssetRecords(const std::string &tableName,
300         int64_t beginTime) = 0;
301 
GetInfoByPrimaryKeyOrGid(const std::string & tableName,const VBucket & vBucket,bool useTransaction,DataInfoWithLog & dataInfoWithLog,VBucket & assetInfo)302     virtual int GetInfoByPrimaryKeyOrGid(const std::string &tableName, const VBucket &vBucket,
303         [[gnu::unused]] bool useTransaction, DataInfoWithLog &dataInfoWithLog, VBucket &assetInfo)
304     {
305         return GetInfoByPrimaryKeyOrGid(tableName, vBucket, dataInfoWithLog, assetInfo);
306     }
307 
PrintCursorChange(const std::string & tableName)308     virtual void PrintCursorChange([[gnu::unused]] const std::string &tableName) {}
309 
GetLockStatusByGid(const std::string & tableName,const std::string & gid,LockStatus & status)310     virtual int GetLockStatusByGid(const std::string &tableName, const std::string &gid, LockStatus &status)
311     {
312         return E_OK;
313     }
314 
IsExistTableContainAssets()315     virtual bool IsExistTableContainAssets()
316     {
317         return false;
318     }
319 };
320 }
321 
322 #endif // ICLOUD_SYNC_STORAGE_INTERFACE_H
323