1 /*
2  * Copyright (c) 2024 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 OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_SQL_STATISTIC_H
17 #define OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_SQL_STATISTIC_H
18 #include <atomic>
19 #include <chrono>
20 #include <memory>
21 
22 #include "rdb_types.h"
23 #include "rdb_visibility.h"
24 
25 namespace OHOS {
26 template<typename _Key, typename _Tp>
27 class ConcurrentMap;
28 namespace DistributedRdb {
29 class SqlStatistic  {
30 public:
31     enum Step : int32_t {
32         STEP_TOTAL,
33         STEP_TOTAL_REF,
34         STEP_TOTAL_RES,
35         STEP_WAIT,
36         STEP_PREPARE,
37         STEP_EXECUTE,
38         STEP_BUTT,
39     };
40     API_EXPORT static int Subscribe(std::shared_ptr<SqlObserver> observer);
41     API_EXPORT static int Unsubscribe(std::shared_ptr<SqlObserver> observer);
42     static uint32_t GenerateId();
43     SqlStatistic(const std::string &sql, int32_t step, uint32_t seqId = 0);
44     ~SqlStatistic();
45 
46 private:
47     using SqlExecInfo = SqlObserver::SqlExecutionInfo;
48     static void Release(SqlExecInfo *execInfo);
49     static ConcurrentMap<SqlObserver *, std::shared_ptr<SqlObserver>> observers_;
50     static ConcurrentMap<uint64_t, std::shared_ptr<SqlExecInfo>> execInfos_;
51     static bool enabled_;
52     static std::atomic_uint32_t seqId_;
53     int32_t step_ = 0;
54     uint64_t key_ = 0;
55     std::chrono::steady_clock::time_point time_;
56     std::shared_ptr<SqlExecInfo> execInfo_;
57 };
58 }
59 }
60 #endif // OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_SQL_STATISTIC_H
61