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 private: 59 // Start syncer 60 int StartSyncer(bool isCheckSyncActive = false, bool isNeedActive = true); 61 62 int StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive); 63 64 // Stop syncer 65 void StopSyncer(); 66 67 void StopSyncerWithNoLock(bool isClosedOperation = false); 68 69 void SetSyncModuleActive(); 70 71 bool GetSyncModuleActive(); 72 73 void ReSetSyncModuleActive(); 74 75 void UserChangeHandle(); 76 77 void ChangeUserListerner(); 78 79 SyncerProxy syncer_; // use for sync Interactive 80 std::atomic<bool> started_; 81 std::atomic<bool> closed_; 82 std::atomic<bool> isSyncModuleActiveCheck_; 83 std::atomic<bool> isSyncNeedActive_; 84 ISyncInterface *store_; 85 86 mutable std::mutex syncerOperateLock_; 87 NotificationChain::Listener *userChangeListener_; 88 }; 89 } // namespace DistributedDB 90 #endif // SYNC_ABLE_ENGINE_H