• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static constexpr int32_t DEFAULT_USER_ID = 0;
35     AutoSyncTimer() = default;
36     ~AutoSyncTimer() = default;
37     std::map<std::string, std::vector<StoreId>> GetStoreIds();
38     std::function<void()> ProcessTask() __attribute__((no_sanitize("cfi")));
39     void StartTimer();
40     void StopTimer();
41     void AddSyncStores(const std::string &appId, std::set<StoreId> storeIds);
42     bool HasSyncStores();
43     std::pair<bool, std::string> HasCollaboration(const std::string &appId);
44     ConcurrentMap<std::string, std::vector<StoreId>> stores_;
45     TaskExecutor::TaskId delaySyncTaskId_;
46     TaskExecutor::TaskId forceSyncTaskId_;
47     std::mutex mutex_;
48 };
49 } // namespace OHOS::DistributedKv
50 #endif // SDB_AUTO_SYNC_TIMER_H