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 SqlStatistic(const std::string &sql, int32_t step, uint32_t seqId = 0); 43 ~SqlStatistic(); 44 45 private: 46 using SqlExecInfo = SqlObserver::SqlExecutionInfo; 47 static void Release(SqlExecInfo *execInfo); 48 static ConcurrentMap<SqlObserver *, std::shared_ptr<SqlObserver>> observers_; 49 static ConcurrentMap<uint64_t, std::shared_ptr<SqlExecInfo>> execInfos_; 50 static bool enabled_; 51 static std::atomic_uint32_t seqId_; 52 int32_t step_ = 0; 53 uint64_t key_ = 0; 54 std::chrono::steady_clock::time_point time_; 55 std::shared_ptr<SqlExecInfo> execInfo_; 56 }; 57 } // namespace DistributedRdb 58 } // namespace OHOS 59 #endif // OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_SQL_STATISTIC_H 60