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(const std::string &device, const std::string &tableName); 59 60 void RegisterObserverAction(const RelationalObserverAction &action); 61 int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier); 62 63 std::string GetStorePath() const override; 64 65 RelationalDBProperties GetProperties() const override; 66 67 void StopSync(uint64_t connectionId); 68 69 void Dump(int fd) override; 70 71 int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout, 72 uint64_t connectionId, std::shared_ptr<ResultSet> &result); 73 74 private: 75 void ReleaseResources(); 76 77 // 1 store 1 connection 78 void DecreaseConnectionCounter(); 79 int CheckDBMode(); 80 int GetSchemaFromMeta(RelationalSchemaObject &schema); 81 int SaveSchemaToMeta(); 82 int CheckTableModeFromMeta(DistributedTableMode mode, bool isUnSet); 83 int SaveTableModeToMeta(DistributedTableMode mode); 84 int CheckProperties(RelationalDBProperties properties); 85 86 int SaveLogTableVersionToMeta(); 87 88 int CleanDistributedDeviceTable(); 89 90 int StopLifeCycleTimer(); 91 int StartLifeCycleTimer(const DatabaseLifeCycleNotifier ¬ifier); 92 void HeartBeat(); 93 int ResetLifeCycleTimer(); 94 95 void IncreaseConnectionCounter(); 96 int InitStorageEngine(const RelationalDBProperties &kvDBProp); 97 98 // use for sync Interactive 99 std::unique_ptr<SyncAbleEngine> syncAbleEngine_ = nullptr; // For storage operate sync function 100 // use ref obj same as kv 101 RelationalSyncAbleStorage *storageEngine_ = nullptr; // For storage operate data 102 std::shared_ptr<SQLiteSingleRelationalStorageEngine> sqliteStorageEngine_; 103 104 std::mutex connectMutex_; 105 std::atomic<int> connectionCount_ = 0; 106 std::vector<std::function<void(void)>> closeNotifiers_; 107 108 mutable std::mutex initalMutex_; 109 bool isInitialized_ = false; 110 111 // lifeCycle 112 std::mutex lifeCycleMutex_; 113 DatabaseLifeCycleNotifier lifeCycleNotifier_; 114 TimerId lifeTimerId_; 115 }; 116 } // namespace DistributedDB 117 #endif 118 #endif // SQLITE_RELATIONAL_STORE_H