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