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); 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 private: 68 void ReleaseResources(); 69 70 // 1 store 1 connection 71 void DecreaseConnectionCounter(); 72 int CheckDBMode(); 73 int GetSchemaFromMeta(); 74 int SaveSchemaToMeta(); 75 76 int SaveLogTableVersionToMeta(); 77 78 int CleanDistributedDeviceTable(); 79 80 int StopLifeCycleTimer(); 81 int StartLifeCycleTimer(const DatabaseLifeCycleNotifier ¬ifier); 82 void HeartBeat(); 83 int ResetLifeCycleTimer(); 84 85 // use for sync Interactive 86 std::unique_ptr<SyncAbleEngine> syncAbleEngine_ = nullptr; // For storage operate sync function 87 // use ref obj same as kv 88 RelationalSyncAbleStorage *storageEngine_ = nullptr; // For storage operate data 89 SQLiteSingleRelationalStorageEngine *sqliteStorageEngine_ = nullptr; 90 91 void IncreaseConnectionCounter(); 92 int InitStorageEngine(const RelationalDBProperties &kvDBProp); 93 std::mutex connectMutex_; 94 std::atomic<int> connectionCount_ = 0; 95 std::vector<std::function<void(void)>> closeNotifiers_; 96 97 mutable std::mutex schemaMutex_; 98 RelationalDBProperties properties_; 99 100 mutable std::mutex initalMutex_; 101 bool isInitialized_ = false; 102 103 // lifeCycle 104 std::mutex lifeCycleMutex_; 105 DatabaseLifeCycleNotifier lifeCycleNotifier_; 106 TimerId lifeTimerId_; 107 }; 108 } // namespace DistributedDB 109 #endif 110 #endif // SQLITE_RELATIONAL_STORE_H