• 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 
16 #ifndef SYNC_ABLE_ENGINE_H
17 #define SYNC_ABLE_ENGINE_H
18 
19 #include <memory.h>
20 
21 #include "notification_chain.h"
22 #include "ref_object.h"
23 #include "syncer_proxy.h"
24 
25 namespace DistributedDB {
26 class SyncAbleEngine final {
27 public:
28     explicit SyncAbleEngine(ISyncInterface *store);
29     ~SyncAbleEngine();
30 
31     // Start a sync action.
32     int Sync(const ISyncer::SyncParam &parm, uint64_t connectionId);
33 
34     void WakeUpSyncer();
35     void Close();
36 
37     // Get The current virtual timestamp
38     uint64_t GetTimestamp();
39 
40     int EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash, const std::string &tableName = "");
41 
42     int GetLocalIdentity(std::string &outTarget);
43 
44     // Stop a sync action in progress
45     void StopSync(uint64_t connectionId);
46 
47     void Dump(int fd);
48 
49     int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout,
50         uint64_t connectionId, std::shared_ptr<ResultSet> &result);
51 
52     int GetHashDeviceId(const std::string &clientId, std::string &hashDevId);
53 
54     int GetDeviceSyncTaskCount();
55 private:
56     // Start syncer
57     int StartSyncer(bool isCheckSyncActive = false, bool isNeedActive = true);
58 
59     int StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive);
60 
61     // Stop syncer
62     void StopSyncer();
63 
64     void StopSyncerWithNoLock(bool isClosedOperation = false);
65 
66     void SetSyncModuleActive();
67 
68     bool GetSyncModuleActive();
69 
70     void ReSetSyncModuleActive();
71 
72     void UserChangeHandle();
73 
74     void ChangeUserListener();
75 
76     bool NeedStartSyncer() const;
77 
78     SyncerProxy syncer_; // use for sync Interactive
79     std::atomic<bool> started_;
80     std::atomic<bool> closed_;
81     std::atomic<bool> isSyncModuleActiveCheck_;
82     std::atomic<bool> isSyncNeedActive_;
83     ISyncInterface *store_;
84 
85     mutable std::mutex syncerOperateLock_;
86     NotificationChain::Listener *userChangeListener_;
87 };
88 }  // namespace DistributedDB
89 #endif // SYNC_ABLE_ENGINE_H