1 /* 2 * Copyright (c) 2022 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 SDB_AUTO_SYNC_TIMER_H 16 #define SDB_AUTO_SYNC_TIMER_H 17 #include <set> 18 19 #include "concurrent_map.h" 20 #include "kvdb_service.h" 21 #include "task_scheduler.h" 22 namespace OHOS::DistributedKv { 23 class AutoSyncTimer { 24 public: 25 static constexpr uint32_t FORCE_SYNC_INTERVAL = 200; 26 static constexpr uint32_t AUTO_SYNC_INTERVAL = 50; 27 static AutoSyncTimer &GetInstance(); 28 void DoAutoSync(const std::string &appId, std::set<StoreId> storeIds); 29 30 private: 31 static constexpr size_t TIME_TASK_NUM = 5; 32 static constexpr size_t SYNC_STORE_NUM = 10; 33 AutoSyncTimer() = default; 34 ~AutoSyncTimer() = default; 35 std::map<std::string, std::set<StoreId>> GetStoreIds(); 36 std::function<void()> ProcessTask(); 37 void StartTimer(); 38 void StopTimer(); 39 void AddSyncStores(const std::string &appId, std::set<StoreId> storeIds); 40 bool HasSyncStores(); 41 ConcurrentMap<std::string, std::set<StoreId>> stores_; 42 TaskScheduler::TaskId delaySyncTaskId_; 43 TaskScheduler::TaskId forceSyncTaskId_; 44 std::mutex mutex_; 45 TaskScheduler scheduler_{ TIME_TASK_NUM }; 46 }; 47 } // namespace OHOS::DistributedKv 48 #endif // SDB_AUTO_SYNC_TIMER_H 49