• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2023 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 NATIVE_RDB_DELAY_NOTIFY_H
17 #define NATIVE_RDB_DELAY_NOTIFY_H
18 #include <atomic>
19 #include <chrono>
20 #include <functional>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 
26 #include "rdb_types.h"
27 
28 namespace OHOS {
29 class ExecutorPool;
30 namespace NativeRdb {
31 class DelayNotify {
32 public:
33     using Task = std::function<int(const DistributedRdb::RdbChangedData &, const DistributedRdb::RdbNotifyConfig &)>;
34     using Time = std::chrono::steady_clock::time_point;
35     DelayNotify();
36     ~DelayNotify();
37     void SetExecutorPool(std::shared_ptr<ExecutorPool> pool);
38     void SetTask(Task task);
39     void UpdateNotify(const DistributedRdb::RdbChangedData &changedData, bool isFull = false);
40     void SetAutoSyncInterval(uint32_t autoSyncInterval);
41     void Pause();
42     void Resume();
43 
44 private:
45     static constexpr uint32_t AUTO_SYNC_INTERVAL = 200;
46     static constexpr uint32_t MAX_NOTIFY_INTERVAL = 5000;
47     static constexpr uint32_t SERVICE_INTERVAL = 10000;
48     bool isInitialized_ = false;
49     bool isFull_ = false;
50     Time lastTimePoint_;
51     std::atomic_int32_t pauseCount_;
52     uint64_t delaySyncTaskId_ = 0;
53     Task task_;
54     std::shared_ptr<ExecutorPool> pool_;
55     std::mutex mutex_;
56     DistributedRdb::RdbChangedData changedData_;
57 
58     void StartTimer();
59     void StopTimer();
60     void ExecuteTask();
61     void RestoreDefaultSyncInterval();
62     uint32_t autoSyncInterval_ = AUTO_SYNC_INTERVAL;
63 };
64 
65 class PauseDelayNotify {
66 public:
67     explicit PauseDelayNotify(std::shared_ptr<DelayNotify> delayNotifier);
68     ~PauseDelayNotify();
69 
70 private:
71     static constexpr uint32_t AUTO_SYNC_MAX_INTERVAL = 3000;
72     std::shared_ptr<DelayNotify> delayNotifier_;
73 };
74 } // namespace NativeRdb
75 } // namespace OHOS
76 #endif // NATIVE_RDB_DELAY_NOTIFY_H