1 /* 2 * Copyright (c) 2021 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 #ifndef SQLITE_RELATIONAL_STORE_H 16 #define SQLITE_RELATIONAL_STORE_H 17 #ifdef RELATIONAL_STORE 18 19 #include <functional> 20 #include <memory> 21 #include <vector> 22 23 #include "irelational_store.h" 24 #include "sqlite_single_relational_storage_engine.h" 25 #include "isyncer.h" 26 #include "sync_able_engine.h" 27 #include "relational_sync_able_storage.h" 28 #include "runtime_context.h" 29 #include "cloud/cloud_syncer.h" 30 31 namespace DistributedDB { 32 using RelationalObserverAction = 33 std::function<void(const std::string &device, ChangedData &&changedData, bool isChangedData, Origin origin)>; 34 class SQLiteRelationalStore : public IRelationalStore { 35 public: 36 SQLiteRelationalStore() = default; 37 ~SQLiteRelationalStore() override; 38 39 // Delete the copy and assign constructors 40 DISABLE_COPY_ASSIGN_MOVE(SQLiteRelationalStore); 41 42 RelationalStoreConnection *GetDBConnection(int &errCode) override; 43 int Open(const RelationalDBProperties &properties) override; 44 void OnClose(const std::function<void(void)> ¬ifier); 45 46 SQLiteSingleVerRelationalStorageExecutor *GetHandle(bool isWrite, int &errCode) const; 47 void ReleaseHandle(SQLiteSingleVerRelationalStorageExecutor *&handle) const; 48 49 int Sync(const ISyncer::SyncParma &syncParam, uint64_t connectionId); 50 51 void ReleaseDBConnection(uint64_t connectionId, RelationalStoreConnection *connection); 52 53 void WakeUpSyncer() override; 54 55 // for test mock GetStorageEngine()56 RelationalSyncAbleStorage *GetStorageEngine() 57 { 58 return storageEngine_; 59 } 60 61 int CreateDistributedTable(const std::string &tableName, TableSyncType syncType, bool trackerSchemaChanged = false); 62 63 int RemoveDeviceData(); 64 int RemoveDeviceData(const std::string &device, const std::string &tableName); 65 66 int RegisterObserverAction(uint64_t connectionId, const StoreObserver *observer, 67 const RelationalObserverAction &action); 68 int UnRegisterObserverAction(uint64_t connectionId, const StoreObserver *observer); 69 int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier); 70 71 std::string GetStorePath() const override; 72 73 RelationalDBProperties GetProperties() const override; 74 75 void StopSync(uint64_t connectionId); 76 77 void Dump(int fd) override; 78 79 int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout, 80 uint64_t connectionId, std::shared_ptr<ResultSet> &result); 81 82 int SetIAssetLoader(const std::shared_ptr<IAssetLoader> &loader); 83 84 int ChkSchema(const TableName &tableName); 85 86 int SetTrackerTable(const TrackerSchema &trackerSchema); 87 88 int ExecuteSql(const SqlCondition &condition, std::vector<VBucket> &records); 89 90 int CleanTrackerData(const std::string &tableName, int64_t cursor); 91 92 int SetReference(const std::vector<TableReferenceProperty> &tableReferenceProperty); 93 94 int Pragma(PragmaCmd cmd, PragmaData &pragmaData); 95 96 int UpsertData(RecordStatus status, const std::string &tableName, const std::vector<VBucket> &records); 97 98 int SetDistributedSchema(const DistributedSchema &schema, bool isForceUpgrade); 99 100 int GetDownloadingAssetsCount(int32_t &count); 101 102 int SetTableMode(DistributedTableMode tableMode); 103 104 #ifdef USE_DISTRIBUTEDDB_CLOUD 105 int PrepareAndSetCloudDbSchema(const DataBaseSchema &schema); 106 107 int32_t GetCloudSyncTaskCount(); 108 109 int CleanCloudData(ClearMode mode); 110 111 int SetCloudDB(const std::shared_ptr<ICloudDb> &cloudDb); 112 113 int Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess, uint64_t taskId); 114 115 int SetCloudSyncConfig(const CloudSyncConfig &config); 116 117 SyncProcess GetCloudTaskStatus(uint64_t taskId); 118 #endif 119 private: 120 void ReleaseResources(); 121 122 // 1 store 1 connection 123 void DecreaseConnectionCounter(uint64_t connectionId); 124 int CheckDBMode(); 125 int GetSchemaFromMeta(RelationalSchemaObject &schema); 126 int CheckTableModeFromMeta(DistributedTableMode mode, bool isUnSet); 127 int SaveTableModeToMeta(DistributedTableMode mode); 128 int CheckProperties(RelationalDBProperties properties); 129 130 int SaveLogTableVersionToMeta(); 131 132 int CleanDistributedDeviceTable(); 133 134 int StopLifeCycleTimer(); 135 int StartLifeCycleTimer(const DatabaseLifeCycleNotifier ¬ifier); 136 void HeartBeat(); 137 int ResetLifeCycleTimer(); 138 139 void IncreaseConnectionCounter(); 140 int InitStorageEngine(const RelationalDBProperties &properties); 141 142 int EraseAllDeviceWatermark(const std::vector<std::string> &tableNameList); 143 144 std::string GetDevTableName(const std::string &device, const std::string &hashDev) const; 145 146 int GetHandleAndStartTransaction(SQLiteSingleVerRelationalStorageExecutor *&handle) const; 147 148 int RemoveDeviceDataInner(const std::string &mappingDev, const std::string &device, 149 const std::string &tableName, bool isNeedHash); 150 151 int GetExistDevices(std::set<std::string> &hashDevices) const; 152 153 std::vector<std::string> GetAllDistributedTableName(TableSyncType tableSyncType = DEVICE_COOPERATION); 154 155 int CheckBeforeSync(const CloudSyncOption &option); 156 157 int CheckAssetsOnlyValid(const QuerySyncObject &querySyncObject, const CloudSyncOption &option); 158 159 int CheckQueryValid(const CloudSyncOption &option); 160 161 int CheckObjectValid(bool priorityTask, const std::vector<QuerySyncObject> &object, bool isFromTable); 162 163 int CheckTableName(const std::vector<std::string> &tableNames); 164 165 int CleanWaterMark(SQLiteSingleVerRelationalStorageExecutor *&handle, std::set<std::string> &clearWaterMarkTable); 166 167 int InitTrackerSchemaFromMeta(); 168 169 void AddFields(const std::vector<Field> &newFields, const std::set<std::string> &equalFields, 170 std::vector<Field> &addFields); 171 172 bool CheckFields(const std::vector<Field> &newFields, const TableInfo &tableInfo, std::vector<Field> &addFields); 173 174 bool PrepareSharedTable(const DataBaseSchema &schema, std::vector<std::string> &deleteTableNames, 175 std::map<std::string, std::vector<Field>> &updateTableNames, 176 std::map<std::string, std::string> &alterTableNames); 177 178 int ExecuteCreateSharedTable(const DataBaseSchema &schema); 179 180 int CheckParamForUpsertData(RecordStatus status, const std::string &tableName, const std::vector<VBucket> &records); 181 182 int CheckSchemaForUpsertData(const std::string &tableName, const std::vector<VBucket> &records); 183 184 int InitSQLiteStorageEngine(const RelationalDBProperties &properties); 185 186 static int ReFillSyncInfoTable(const std::vector<std::string> &actualTable, CloudSyncer::CloudTaskInfo &info); 187 188 int CheckTrackerTable(const TrackerSchema &trackerSchema, TableInfo &table, bool &isNoTableInSchema, 189 bool &isFirstCreate); 190 191 #ifdef USE_DISTRIBUTEDDB_CLOUD 192 void FillSyncInfo(const CloudSyncOption &option, const SyncProcessCallback &onProcess, 193 CloudSyncer::CloudTaskInfo &info); 194 195 int CheckCloudSchema(const DataBaseSchema &schema); 196 #endif 197 // use for sync Interactive 198 std::shared_ptr<SyncAbleEngine> syncAbleEngine_ = nullptr; // For storage operate sync function 199 // use ref obj same as kv 200 RelationalSyncAbleStorage *storageEngine_ = nullptr; // For storage operate data 201 std::shared_ptr<SQLiteSingleRelationalStorageEngine> sqliteStorageEngine_; 202 CloudSyncer *cloudSyncer_ = nullptr; 203 204 std::mutex connectMutex_; 205 std::atomic<int> connectionCount_ = 0; 206 std::vector<std::function<void(void)>> closeNotifiers_; 207 208 mutable std::mutex initalMutex_; 209 bool isInitialized_ = false; 210 211 // lifeCycle 212 std::mutex lifeCycleMutex_; 213 DatabaseLifeCycleNotifier lifeCycleNotifier_; 214 TimerId lifeTimerId_ {}; 215 }; 216 } // namespace DistributedDB 217 #endif 218 #endif // SQLITE_RELATIONAL_STORE_H