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 ×tamp, bool isCloudForcePush, 113 bool isCompensatedTask, int64_t &count) = 0; 114 115 virtual int GetAllUploadCount(const QuerySyncObject &query, const std::vector<Timestamp> ×tampVec, 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; 145 146 virtual int FillCloudLogAndAsset(OpType opType, const CloudSyncData &data, bool fillAsset, bool ignoreEmptyGid) = 0; 147 148 virtual std::string GetIdentify() const = 0; 149 150 virtual int CheckQueryValid(const QuerySyncObject &query) = 0; 151 CreateTempSyncTrigger(const std::string & tableName)152 virtual int CreateTempSyncTrigger(const std::string &tableName) 153 { 154 return E_OK; 155 } 156 GetAndResetServerObserverData(const std::string & tableName,ChangeProperties & changeProperties)157 virtual int GetAndResetServerObserverData(const std::string &tableName, ChangeProperties &changeProperties) 158 { 159 return E_OK; 160 } 161 ClearAllTempSyncTrigger()162 virtual int ClearAllTempSyncTrigger() 163 { 164 return E_OK; 165 } 166 167 virtual bool IsSharedTable(const std::string &tableName) = 0; 168 SetCloudTaskConfig(const CloudTaskConfig & config)169 virtual void SetCloudTaskConfig([[gnu::unused]] const CloudTaskConfig &config) 170 { 171 } 172 GetAssetsByGidOrHashKey(const TableSchema & tableSchema,const std::string & gid,const Bytes & hashKey,VBucket & assets)173 virtual std::pair<int, uint32_t> GetAssetsByGidOrHashKey(const TableSchema &tableSchema, const std::string &gid, 174 const Bytes &hashKey, VBucket &assets) 175 { 176 return { E_OK, static_cast<uint32_t>(LockStatus::UNLOCK) }; 177 } 178 SetIAssetLoader(const std::shared_ptr<IAssetLoader> & loader)179 virtual int SetIAssetLoader([[gnu::unused]] const std::shared_ptr<IAssetLoader> &loader) 180 { 181 return E_OK; 182 } 183 UpdateRecordFlag(const std::string & tableName,bool recordConflict,const LogInfo & logInfo)184 virtual int UpdateRecordFlag([[gnu::unused]] const std::string &tableName, 185 [[gnu::unused]] bool recordConflict, [[gnu::unused]] const LogInfo &logInfo) 186 { 187 return E_OK; 188 } 189 GetCompensatedSyncQuery(std::vector<QuerySyncObject> & syncQuery)190 virtual int GetCompensatedSyncQuery([[gnu::unused]] std::vector<QuerySyncObject> &syncQuery) 191 { 192 return E_OK; 193 } 194 MarkFlagAsConsistent(const std::string & tableName,const DownloadData & downloadData,const std::set<std::string> & gidFilters)195 virtual int MarkFlagAsConsistent([[gnu::unused]] const std::string &tableName, 196 [[gnu::unused]] const DownloadData &downloadData, [[gnu::unused]] const std::set<std::string> &gidFilters) 197 { 198 return E_OK; 199 } 200 SetUser(const std::string & user)201 virtual void SetUser([[gnu::unused]] const std::string &user) 202 { 203 } 204 GetLocalCloudVersion()205 virtual std::pair<int, CloudSyncData> GetLocalCloudVersion() 206 { 207 return {E_OK, {}}; 208 } 209 210 virtual CloudSyncConfig GetCloudSyncConfig() const = 0; 211 IsTableExistReference(const std::string & table)212 virtual bool IsTableExistReference(const std::string &table) 213 { 214 return false; 215 } 216 IsTableExistReferenceOrReferenceBy(const std::string & table)217 virtual bool IsTableExistReferenceOrReferenceBy(const std::string &table) 218 { 219 return false; 220 } 221 ReleaseUploadRecord(const std::string & tableName,const CloudWaterType & type,Timestamp localMark)222 virtual void ReleaseUploadRecord([[gnu::unused]] const std::string &tableName, 223 [[gnu::unused]] const CloudWaterType &type, Timestamp localMark) 224 { 225 } 226 IsTagCloudUpdateLocal(const LogInfo & localInfo,const LogInfo & cloudInfo,SingleVerConflictResolvePolicy policy)227 virtual bool IsTagCloudUpdateLocal(const LogInfo &localInfo, const LogInfo &cloudInfo, 228 SingleVerConflictResolvePolicy policy) 229 { 230 return false; 231 } 232 }; 233 } 234 235 #endif // ICLOUD_SYNC_STORAGE_INTERFACE_H 236