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 30 namespace DistributedDB { 31 using RelationalObserverAction = std::function<void(const std::string &device)>; 32 class SQLiteRelationalStore : public IRelationalStore { 33 public: 34 SQLiteRelationalStore() = default; 35 ~SQLiteRelationalStore() override; 36 37 RelationalStoreConnection *GetDBConnection(int &errCode) override; 38 int Open(const RelationalDBProperties &properties) override; 39 void OnClose(const std::function<void(void)> ¬ifier); 40 41 SQLiteSingleVerRelationalStorageExecutor *GetHandle(bool isWrite, int &errCode) const; 42 void ReleaseHandle(SQLiteSingleVerRelationalStorageExecutor *&handle) const; 43 44 int Sync(const ISyncer::SyncParma &syncParam, uint64_t connectionId); 45 46 void ReleaseDBConnection(RelationalStoreConnection *connection); 47 48 void WakeUpSyncer() override; 49 50 // for test mock GetStorageEngine()51 const RelationalSyncAbleStorage *GetStorageEngine() 52 { 53 return storageEngine_; 54 } 55 56 int CreateDistributedTable(const std::string &tableName); 57 58 int RemoveDeviceData(); 59 int RemoveDeviceData(const std::string &device, const std::string &tableName); 60 61 void RegisterObserverAction(const RelationalObserverAction &action); 62 int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier); 63 64 std::string GetStorePath() const override; 65 66 RelationalDBProperties GetProperties() const override; 67 68 void StopSync(uint64_t connectionId); 69 70 void Dump(int fd) override; 71 72 int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout, 73 uint64_t connectionId, std::shared_ptr<ResultSet> &result); 74 75 int EraseAllDeviceWatermark(const std::vector<std::string> &tableNameList); 76 77 std::string GetDevTableName(const std::string &device, const std::string &hashDev) const; 78 79 SQLiteSingleVerRelationalStorageExecutor *GetHandleAndStartTransaction(int &errCode) const; 80 81 int RemoveDeviceDataInner(const std::string &mappingDev, const std::string &device, 82 const std::string &tableName, bool isNeedHash); 83 84 int GetExistDevices(std::set<std::string> &hashDevices); 85 private: 86 void ReleaseResources(); 87 88 // 1 store 1 connection 89 void DecreaseConnectionCounter(); 90 int CheckDBMode(); 91 int GetSchemaFromMeta(RelationalSchemaObject &schema); 92 int SaveSchemaToMeta(); 93 int CheckTableModeFromMeta(DistributedTableMode mode, bool isUnSet); 94 int SaveTableModeToMeta(DistributedTableMode mode); 95 int CheckProperties(RelationalDBProperties properties); 96 97 int SaveLogTableVersionToMeta(); 98 99 int CleanDistributedDeviceTable(); 100 101 int StopLifeCycleTimer(); 102 int StartLifeCycleTimer(const DatabaseLifeCycleNotifier ¬ifier); 103 void HeartBeat(); 104 int ResetLifeCycleTimer(); 105 106 void IncreaseConnectionCounter(); 107 int InitStorageEngine(const RelationalDBProperties &kvDBProp); 108 109 // use for sync Interactive 110 std::unique_ptr<SyncAbleEngine> syncAbleEngine_ = nullptr; // For storage operate sync function 111 // use ref obj same as kv 112 RelationalSyncAbleStorage *storageEngine_ = nullptr; // For storage operate data 113 std::shared_ptr<SQLiteSingleRelationalStorageEngine> sqliteStorageEngine_; 114 115 std::mutex connectMutex_; 116 std::atomic<int> connectionCount_ = 0; 117 std::vector<std::function<void(void)>> closeNotifiers_; 118 119 mutable std::mutex initalMutex_; 120 bool isInitialized_ = false; 121 122 // lifeCycle 123 std::mutex lifeCycleMutex_; 124 DatabaseLifeCycleNotifier lifeCycleNotifier_; 125 TimerId lifeTimerId_; 126 }; 127 } // namespace DistributedDB 128 #endif 129 #endif // SQLITE_RELATIONAL_STORE_H