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 void TriggerSync(int notifyEvent); 31 32 // Start a sync action. 33 int Sync(const ISyncer::SyncParma &parm, uint64_t connectionId); 34 35 void WakeUpSyncer(); 36 void Close(); 37 38 // Enable auto sync 39 void EnableAutoSync(bool enable); 40 41 int EnableManualSync(void); 42 int DisableManualSync(void); 43 44 // Get The current virtual timestamp 45 uint64_t GetTimestamp(); 46 47 int EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash, const std::string &tableName = ""); 48 49 int GetLocalIdentity(std::string &outTarget); 50 51 // Stop a sync action in progress 52 void StopSync(uint64_t connectionId); 53 54 void Dump(int fd); 55 56 int RemoteQuery(const std::string &device, const RemoteCondition &condition, uint64_t timeout, 57 uint64_t connectionId, std::shared_ptr<ResultSet> &result); 58 59 int GetHashDeviceId(const std::string &clientId, std::string &hashDevId); 60 private: 61 // Start syncer 62 int StartSyncer(bool isCheckSyncActive = false, bool isNeedActive = true); 63 64 int StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive); 65 66 // Stop syncer 67 void StopSyncer(); 68 69 void StopSyncerWithNoLock(bool isClosedOperation = false); 70 71 void SetSyncModuleActive(); 72 73 bool GetSyncModuleActive(); 74 75 void ReSetSyncModuleActive(); 76 77 void UserChangeHandle(); 78 79 void ChangeUserListerner(); 80 81 SyncerProxy syncer_; // use for sync Interactive 82 std::atomic<bool> started_; 83 std::atomic<bool> closed_; 84 std::atomic<bool> isSyncModuleActiveCheck_; 85 std::atomic<bool> isSyncNeedActive_; 86 ISyncInterface *store_; 87 88 mutable std::mutex syncerOperateLock_; 89 NotificationChain::Listener *userChangeListener_; 90 }; 91 } // namespace DistributedDB 92 #endif // SYNC_ABLE_ENGINE_H