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 RELATIONAL_STORE_DELEGATE_IMPL_H
16 #define RELATIONAL_STORE_DELEGATE_IMPL_H
17 #ifdef RELATIONAL_STORE
18 
19 #include "macro_utils.h"
20 #include "relational_store_connection.h"
21 
22 namespace DistributedDB {
23 class RelationalStoreDelegateImpl final : public RelationalStoreDelegate {
24 public:
25     RelationalStoreDelegateImpl() = default;
26     ~RelationalStoreDelegateImpl() override;
27 
28     RelationalStoreDelegateImpl(RelationalStoreConnection *conn, const std::string &path);
29 
30     DISABLE_COPY_ASSIGN_MOVE(RelationalStoreDelegateImpl);
31 
32     DBStatus Sync(const std::vector<std::string> &devices, SyncMode mode,
33         const Query &query, const SyncStatusCallback &onComplete, bool wait) override;
34 
35     int32_t GetCloudSyncTaskCount() override;
36 
37     DBStatus RemoveDeviceDataInner(const std::string &device, ClearMode mode) override;
38 
39     DBStatus CreateDistributedTableInner(const std::string &tableName, TableSyncType type) override;
40 
41     DBStatus RemoveDeviceData(const std::string &device, const std::string &tableName) override;
42 
43     // For connection
44     DBStatus Close();
45 
46     void SetReleaseFlag(bool flag);
47 
48     DBStatus RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout,
49         std::shared_ptr<ResultSet> &result) override;
50 
51     DBStatus RemoveDeviceData() override;
52 
53     DBStatus Sync(const std::vector<std::string> &devices, SyncMode mode, const Query &query,
54         const SyncProcessCallback &onProcess, int64_t waitTime) override;
55 
56     DBStatus SetCloudDB(const std::shared_ptr<ICloudDb> &cloudDb) override;
57 
58     DBStatus SetCloudDbSchema(const DataBaseSchema &schema) override;
59 
60     DBStatus RegisterObserver(StoreObserver *observer) override;
61 
62     DBStatus UnRegisterObserver() override;
63 
64     DBStatus UnRegisterObserver(StoreObserver *observer) override;
65 
66     DBStatus SetIAssetLoader(const std::shared_ptr<IAssetLoader> &loader) override;
67 
68     DBStatus Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess) override;
69 
70     DBStatus SetTrackerTable(const TrackerSchema &schema) override;
71 
72     DBStatus ExecuteSql(const SqlCondition &condition, std::vector<VBucket> &records) override;
73 
74     DBStatus SetReference(const std::vector<TableReferenceProperty> &tableReferenceProperty) override;
75 
76     DBStatus CleanTrackerData(const std::string &tableName, int64_t cursor) override;
77 
78     DBStatus Pragma(PragmaCmd cmd, PragmaData &pragmaData) override;
79 
80     DBStatus UpsertData(const std::string &tableName, const std::vector<VBucket> &records,
81         RecordStatus status) override;
82 
83     DBStatus SetCloudSyncConfig(const CloudSyncConfig &config) override;
84 
85     DBStatus Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess, uint64_t taskId) override;
86 
87     SyncProcess GetCloudTaskStatus(uint64_t taskId) override;
88 private:
89     static void OnSyncComplete(const std::map<std::string, std::vector<TableStatus>> &devicesStatus,
90         const SyncStatusCallback &onComplete);
91 
92     RelationalStoreConnection *conn_ = nullptr;
93     std::string storePath_;
94     std::atomic<bool> releaseFlag_ = false;
95 };
96 } // namespace DistributedDB
97 #endif
98 #endif // RELATIONAL_STORE_DELEGATE_IMPL_H