• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)>;
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)> &notifier);
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     int32_t GetCloudSyncTaskCount();
52 
53     int CleanCloudData(ClearMode mode);
54 
55     void ReleaseDBConnection(uint64_t connectionId, RelationalStoreConnection *connection);
56 
57     void WakeUpSyncer() override;
58 
59     // for test mock
GetStorageEngine()60     const RelationalSyncAbleStorage *GetStorageEngine()
61     {
62         return storageEngine_;
63     }
64 
65     int CreateDistributedTable(const std::string &tableName, TableSyncType syncType);
66 
67     int RemoveDeviceData();
68     int RemoveDeviceData(const std::string &device, const std::string &tableName);
69 
70     void RegisterObserverAction(uint64_t connectionId, const RelationalObserverAction &action);
71     int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier);
72 
73     std::string GetStorePath() const override;
74 
75     RelationalDBProperties GetProperties() const override;
76 
77     void StopSync(uint64_t connectionId);
78 
79     void Dump(int fd) override;
80 
81     int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout,
82         uint64_t connectionId, std::shared_ptr<ResultSet> &result);
83 
84     int SetCloudDB(const std::shared_ptr<ICloudDb> &cloudDb);
85 
86     int SetCloudDbSchema(const DataBaseSchema &schema);
87 
88     int SetIAssetLoader(const std::shared_ptr<IAssetLoader> &loader);
89 
90     int ChkSchema(const TableName &tableName);
91 
92     int Sync(const std::vector<std::string> &devices, SyncMode mode, const Query &query,
93         const SyncProcessCallback &onProcess, int64_t waitTime);
94 
95 private:
96     void ReleaseResources();
97 
98     // 1 store 1 connection
99     void DecreaseConnectionCounter(uint64_t connectionId);
100     int CheckDBMode();
101     int GetSchemaFromMeta(RelationalSchemaObject &schema);
102     int SaveSchemaToMeta();
103     int CheckTableModeFromMeta(DistributedTableMode mode, bool isUnSet);
104     int SaveTableModeToMeta(DistributedTableMode mode);
105     int CheckProperties(RelationalDBProperties properties);
106 
107     int SaveLogTableVersionToMeta();
108 
109     int CleanDistributedDeviceTable();
110 
111     int StopLifeCycleTimer();
112     int StartLifeCycleTimer(const DatabaseLifeCycleNotifier &notifier);
113     void HeartBeat();
114     int ResetLifeCycleTimer();
115 
116     void IncreaseConnectionCounter();
117     int InitStorageEngine(const RelationalDBProperties &properties);
118 
119     int EraseAllDeviceWatermark(const std::vector<std::string> &tableNameList);
120 
121     std::string GetDevTableName(const std::string &device, const std::string &hashDev) const;
122 
123     SQLiteSingleVerRelationalStorageExecutor *GetHandleAndStartTransaction(int &errCode) const;
124 
125     int RemoveDeviceDataInner(const std::string &mappingDev, const std::string &device,
126         const std::string &tableName, bool isNeedHash);
127 
128     int GetExistDevices(std::set<std::string> &hashDevices) const;
129 
130     std::vector<std::string> GetAllDistributedTableName();
131 
132     // use for sync Interactive
133     std::shared_ptr<SyncAbleEngine> syncAbleEngine_ = nullptr; // For storage operate sync function
134     // use ref obj same as kv
135     RelationalSyncAbleStorage *storageEngine_ = nullptr; // For storage operate data
136     std::shared_ptr<SQLiteSingleRelationalStorageEngine> sqliteStorageEngine_;
137     CloudSyncer *cloudSyncer_ = nullptr;
138 
139     std::mutex connectMutex_;
140     std::atomic<int> connectionCount_ = 0;
141     std::vector<std::function<void(void)>> closeNotifiers_;
142 
143     mutable std::mutex initalMutex_;
144     bool isInitialized_ = false;
145 
146     // lifeCycle
147     std::mutex lifeCycleMutex_;
148     DatabaseLifeCycleNotifier lifeCycleNotifier_;
149     TimerId lifeTimerId_ {};
150 };
151 }  // namespace DistributedDB
152 #endif
153 #endif // SQLITE_RELATIONAL_STORE_H