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 SCHEDULER_MANAGER_H
17 #define SCHEDULER_MANAGER_H
18 
19 #include <memory>
20 
21 #include "db_delegate.h"
22 #include "executor_pool.h"
23 #include "subscriber_managers/rdb_subscriber_manager.h"
24 
25 namespace OHOS::DataShare {
26 class SchedulerManager {
27 public:
28     static SchedulerManager &GetInstance();
29     void Execute(const std::string &uri, const int32_t userId, const std::string &rdbDir, int version,
30         const std::string &bundleName);
31     void Execute(const Key &key, const int32_t userId, const std::string &rdbDir, int version);
32     void ReExecuteAll();
33     void SetTimer(const std::string &dbPath, const int32_t userId, int version, const Key &key, int64_t reminderTime);
34     void RemoveTimer(const Key &key);
35     void ClearTimer();
36     void SetExecutorPool(std::shared_ptr<ExecutorPool> executor);
37 
38 private:
39     static constexpr const char *REMIND_TIMER_FUNC = "remindTimer(";
40     static constexpr int REMIND_TIMER_FUNC_LEN = 12;
41     SchedulerManager() = default;
42     ~SchedulerManager() = default;
43     static void GenRemindTimerFuncParams(const std::string &rdbDir, const int32_t userId, int version, const Key &key,
44         std::string &schedulerSQL);
45     void ExecuteSchedulerSQL(const std::string &rdbDir, const int32_t userId, int version, const Key &key,
46         std::shared_ptr<DBDelegate> delegate);
47     bool SetTimerTask(uint64_t &timerId, const std::function<void()> &callback, int64_t reminderTime);
48     void DestoryTimerTask(int64_t timerId);
49     void ResetTimerTask(int64_t timerId, int64_t reminderTime);
50 
51     std::mutex mutex_;
52     std::map<Key, int64_t> timerCache_;
53     std::shared_ptr<ExecutorPool> executor_ = nullptr;
54 };
55 } // namespace OHOS::DataShare
56 #endif // SCHEDULER_MANAGER_H
57