• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "app_event_cache.h"
16 
17 #include "app_event_blocks_dao.h"
18 #include "app_event_cache_common.h"
19 #include "app_event_store.h"
20 
21 using namespace OHOS::HiviewDFX::AppEventCacheCommon;
22 namespace OHOS {
23 namespace HiviewDFX {
24 std::mutex AppEventCache::cacheMutex_;
25 std::shared_ptr<AppEventCache> AppEventCache::instance_ = nullptr;
26 
GetInstance()27 std::shared_ptr<AppEventCache> AppEventCache::GetInstance()
28 {
29     if (instance_ == nullptr) {
30         std::lock_guard<std::mutex> lockGuard(cacheMutex_);
31         if (instance_ == nullptr) {
32             instance_ = std::make_shared<AppEventCache>();
33         }
34     }
35     return instance_;
36 }
37 
IsOpen()38 bool AppEventCache::IsOpen()
39 {
40     return store_ != nullptr;
41 }
42 
Open(const std::string & dir)43 int AppEventCache::Open(const std::string& dir)
44 {
45     if (dir.empty()) {
46         return DB_FAILED;
47     }
48 
49     std::lock_guard<std::mutex> lockGuard(cacheMutex_);
50     if (store_ != nullptr) {
51         store_.reset();
52     }
53     store_ = std::make_shared<AppEventStore>(dir);
54     return DB_SUCC;
55 }
56 
Close()57 int AppEventCache::Close()
58 {
59     if (store_ == nullptr) {
60         return DB_SUCC;
61     }
62 
63     std::lock_guard<std::mutex> lockGuard(cacheMutex_);
64     blocks_.clear(); // delete blocks
65     int ret = store_->DestroyDbStore(); // delete db
66     store_.reset();
67     return ret;
68 }
69 
CreateBlock(const std::string & name)70 int AppEventCache::CreateBlock(const std::string& name)
71 {
72     if (store_ == nullptr) {
73         return DB_FAILED;
74     }
75     if (blocks_.find(name) != blocks_.end()) {
76         return DB_SUCC;
77     }
78     std::lock_guard<std::mutex> lockGuard(cacheMutex_);
79 
80     // add a record to blocks table and create a block table
81     if (AppEventBlocksDao(store_).AddBlock(name) != DB_SUCC || store_->CreateBlockTable(name) != DB_SUCC) {
82         return DB_FAILED;
83     }
84     blocks_[name] = std::make_shared<AppEventBlock>(store_, name);
85     return DB_SUCC;
86 }
87 
DestroyBlock(const std::string & name)88 int AppEventCache::DestroyBlock(const std::string& name)
89 {
90     if (store_ == nullptr) {
91         return DB_FAILED;
92     }
93     if (blocks_.find(name) == blocks_.end()) {
94         return DB_SUCC;
95     }
96     std::lock_guard<std::mutex> lockGuard(cacheMutex_);
97 
98     // delete the record form blocks table and delete the block table
99     if (AppEventBlocksDao(store_).RemoveBlock(name) != DB_SUCC || store_->DropBlockTable(name) != DB_SUCC) {
100         return DB_FAILED;
101     }
102     blocks_.erase(name);
103     return DB_SUCC;
104 }
105 
GetBlock(const std::string & name)106 std::shared_ptr<AppEventBlock> AppEventCache::GetBlock(const std::string& name)
107 {
108     std::lock_guard<std::mutex> lockGuard(cacheMutex_);
109     return blocks_.find(name) == blocks_.end() ? nullptr : blocks_[name];
110 }
111 
GetBlocksStat(std::map<std::string,std::pair<int,int64_t>> & blocksStat)112 int AppEventCache::GetBlocksStat(std::map<std::string, std::pair<int, int64_t>>& blocksStat)
113 {
114     if (store_ == nullptr) {
115         return DB_FAILED;
116     }
117 
118     std::lock_guard<std::mutex> lockGuard(cacheMutex_);
119     int result = DB_SUCC;
120     for (auto& block : blocks_) {
121         std::pair<int, int64_t> statPair;
122         if (block.second->GetStat(statPair) != DB_SUCC) {
123             result = DB_FAILED;
124             continue;
125         }
126         blocksStat.emplace(block.first, statPair);
127     }
128     return result;
129 }
130 } // namespace HiviewDFX
131 } // namespace OHOS
132