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 #define LOG_TAG "AutoSyncTimerMock"
16 #include "auto_sync_timer.h"
17
18 #include "kvdb_service_client.h"
19 #include "log_print.h"
20
21 namespace OHOS::DistributedKv {
GetInstance()22 AutoSyncTimer &AutoSyncTimer::GetInstance()
23 {
24 static AutoSyncTimer instance;
25 return instance;
26 }
27
StartTimer()28 void AutoSyncTimer::StartTimer()
29 {
30 std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
31 if (forceSyncTaskId_ == TaskExecutor::INVALID_TASK_ID) {
32 forceSyncTaskId_ =
33 TaskExecutor::GetInstance().Schedule(std::chrono::milliseconds(FORCE_SYNC_INTERVAL), ProcessTask());
34 }
35 if (delaySyncTaskId_ == TaskExecutor::INVALID_TASK_ID) {
36 delaySyncTaskId_ =
37 TaskExecutor::GetInstance().Schedule(std::chrono::milliseconds(AUTO_SYNC_INTERVAL), ProcessTask());
38 } else {
39 delaySyncTaskId_ =
40 TaskExecutor::GetInstance().Reset(delaySyncTaskId_, std::chrono::milliseconds(AUTO_SYNC_INTERVAL));
41 }
42 }
43
DoAutoSync(const std::string & appId,std::set<StoreId> storeIds)44 void AutoSyncTimer::DoAutoSync(const std::string &appId, std::set<StoreId> storeIds)
45 {
46 AddSyncStores(appId, std::move(storeIds));
47 StartTimer();
48 }
49
AddSyncStores(const std::string & appId,std::set<StoreId> storeIds)50 void AutoSyncTimer::AddSyncStores(const std::string &appId, std::set<StoreId> storeIds)
51 {
52 stores_.Compute(appId, [&storeIds](const auto &key, std::vector<StoreId> &value) {
53 std::set<StoreId> tempStores(value.begin(), value.end());
54 for (auto it = storeIds.begin(); it != storeIds.end(); it++) {
55 if (tempStores.count(*it) == 0) {
56 value.push_back(*it);
57 }
58 }
59 return !value.empty();
60 });
61 }
62
HasSyncStores()63 bool AutoSyncTimer::HasSyncStores()
64 {
65 return !stores_.Empty();
66 }
67
GetStoreIds()68 std::map<std::string, std::vector<StoreId>> AutoSyncTimer::GetStoreIds()
69 {
70 std::map<std::string, std::vector<StoreId>> stores;
71 int count = SYNC_STORE_NUM;
72 stores_.EraseIf([&stores, &count](const std::string &key, std::vector<StoreId> &value) {
73 int size = value.size();
74 if (size <= count) {
75 stores.insert({ key, std::move(value) });
76 count = count - size;
77 return true;
78 }
79 auto &innerStore = stores[key];
80 auto it = value.begin();
81 while (it != value.end() && count > 0) {
82 innerStore.push_back(*it);
83 it++;
84 count--;
85 }
86 value.erase(value.begin(), it);
87 return value.empty();
88 });
89 return stores;
90 }
91
ProcessTask()92 std::function<void()> AutoSyncTimer::ProcessTask()
93 {
94 return [this]() {
95 StopTimer();
96 auto service = KVDBServiceClient::GetInstance();
97 if (service == nullptr) {
98 StartTimer();
99 return;
100 }
101 auto storeIds = GetStoreIds();
102 for (const auto &id : storeIds) {
103 auto res = HasCollaboration(id.first);
104 if (!res.first) {
105 continue;
106 }
107 ZLOGD("DoSync appId:%{public}s store size:%{public}zu", id.first.c_str(), id.second.size());
108 for (const auto &storeId : id.second) {
109 KVDBService::SyncInfo syncInfo;
110 service->Sync({ id.first }, storeId, DEFAULT_USER_ID, syncInfo);
111 }
112 }
113 if (HasSyncStores()) {
114 StartTimer();
115 }
116 };
117 }
118
HasCollaboration(const std::string & appId)119 std::pair<bool, std::string> AutoSyncTimer::HasCollaboration(const std::string &appId)
120 {
121 return { true, "" };
122 }
123
StopTimer()124 void AutoSyncTimer::StopTimer()
125 {
126 std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
127 TaskExecutor::GetInstance().Remove(forceSyncTaskId_);
128 TaskExecutor::GetInstance().Remove(delaySyncTaskId_);
129 forceSyncTaskId_ = TaskExecutor::INVALID_TASK_ID;
130 delaySyncTaskId_ = TaskExecutor::INVALID_TASK_ID;
131 }
132 } // namespace OHOS::DistributedKv