• 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_block_dao.h"
16 
17 #include <cinttypes>
18 
19 #include "app_event_cache_common.h"
20 #include "app_event_store.h"
21 #include "hilog/log.h"
22 #include "rdb_errno.h"
23 #include "rdb_store.h"
24 #include "value_object.h"
25 #include "values_bucket.h"
26 
27 using namespace OHOS::HiviewDFX::AppEventCacheCommon;
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace {
31 const HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_BlockDao" };
32 }
AppEventBlockDao(std::shared_ptr<AppEventStore> store,const std::string & name)33 AppEventBlockDao::AppEventBlockDao(std::shared_ptr<AppEventStore> store, const std::string& name)
34     : store_(store)
35 {
36     table_ = Block::TABLE_PREFIX + name;
37 }
38 
InsertPackage(const std::string & package)39 int AppEventBlockDao::InsertPackage(const std::string& package)
40 {
41     auto dbStore = store_->GetDbStore();
42     if (dbStore == nullptr) {
43         HiLog::Error(LABEL, "failed to insert %{public}s, db is null", table_.c_str());
44         return DB_FAILED;
45     }
46 
47     HiLog::Debug(LABEL, "start to insert %{public}s, size=%{public}zu", table_.c_str(), package.size());
48     NativeRdb::ValuesBucket values;
49     values.PutString(Block::FIELD_PACKAGE, package);
50     values.PutInt(Block::FIELD_SIZE, package.size());
51     int64_t seq = 0;
52     if (int ret = dbStore->Insert(seq, table_, values); ret != NativeRdb::E_OK) {
53         HiLog::Error(LABEL, "failed to insert %{public}s, ret=%{public}d", table_.c_str(), ret);
54         return DB_FAILED;
55     }
56     return DB_SUCC;
57 }
58 
DeletePackageBySeq(int64_t seq)59 int AppEventBlockDao::DeletePackageBySeq(int64_t seq)
60 {
61     auto dbStore = store_->GetDbStore();
62     if (dbStore == nullptr) {
63         HiLog::Error(LABEL, "failed to delete %{public}s by seq, db is null", table_.c_str());
64         return DB_FAILED;
65     }
66 
67     HiLog::Debug(LABEL, "start to delete %{public}s, seq=%{public}" PRId64, table_.c_str(), seq);
68     std::string cond;
69     cond += Block::FIELD_SEQ;
70     cond += " <= ?";
71     int delRow = 0;
72     std::vector<std::string> fields = { std::to_string(seq) };
73     if (int ret = dbStore->Delete(delRow, table_, cond, fields); ret != NativeRdb::E_OK) {
74         HiLog::Error(LABEL, "failed to delete %{public}s by seq, ret=%{public}d", table_.c_str(), ret);
75         return DB_FAILED;
76     }
77     return DB_SUCC;
78 }
79 
DeletePackageByNum(int num)80 int AppEventBlockDao::DeletePackageByNum(int num)
81 {
82     auto dbStore = store_->GetDbStore();
83     if (dbStore == nullptr) {
84         HiLog::Error(LABEL, "failed to delete %{public}s by num, db is null", table_.c_str());
85         return DB_FAILED;
86     }
87 
88     HiLog::Debug(LABEL, "start to delete %{public}s, num=%{public}d", table_.c_str(), num);
89     std::string sql;
90     sql.append("DELETE FROM ").append(table_)
91         .append(" WHERE rowid IN(SELECT rowid FROM ").append(table_)
92         .append(" ORDER BY seq LIMIT ").append(std::to_string(num)).append(")");
93     std::vector<NativeRdb::ValueObject> objects;
94     if (int ret = dbStore->ExecuteSql(sql, objects); ret != NativeRdb::E_OK) {
95         HiLog::Error(LABEL, "failed to delete %{public}s by num, ret=%{public}d", table_.c_str(), ret);
96         return DB_FAILED;
97     }
98     return DB_SUCC;
99 }
100 
GetPackagesBySize(int size,std::vector<std::string> & packages,int64_t & seq)101 int AppEventBlockDao::GetPackagesBySize(int size, std::vector<std::string> &packages, int64_t& seq)
102 {
103     auto dbStore = store_->GetDbStore();
104     if (dbStore == nullptr) {
105         HiLog::Error(LABEL, "failed to query %{public}s, db is null", table_.c_str());
106         return DB_FAILED;
107     }
108 
109     HiLog::Debug(LABEL, "start to query %{public}s, size=%{public}d", table_.c_str(), size);
110     std::string sql;
111     sql.append("SELECT ")
112         .append(Block::FIELD_SEQ).append(",").append(Block::FIELD_PACKAGE).append(",").append(Block::FIELD_SIZE)
113         .append(" FROM ").append(table_)
114         .append(" a WHERE (SELECT SUM(").append(Block::FIELD_SIZE).append(") FROM ").append(table_)
115         .append(" b WHERE b.").append(Block::FIELD_SEQ).append(" <= a.").append(Block::FIELD_SEQ).append(") <= ")
116         .append(std::to_string(size)).append(" ORDER BY seq");
117     auto resultSet = dbStore->QuerySql(sql, std::vector<std::string> {});
118     if (resultSet == nullptr) {
119         HiLog::Error(LABEL, "failed to query %{public}s", table_.c_str());
120         return DB_FAILED;
121     }
122 
123     int totalSize = 0;
124     while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
125         resultSet->GetLong(0, seq); // 0 means seq field
126 
127         std::string package;
128         resultSet->GetString(1, package); // 1 means package field
129         packages.emplace_back(package);
130 
131         int packageSize = 0;
132         resultSet->GetInt(2, packageSize); // 2 means packageSize field
133         totalSize += packageSize;
134     }
135     resultSet->Close();
136     return totalSize;
137 }
138 
CountPackages(int & num,int64_t & size)139 int AppEventBlockDao::CountPackages(int& num, int64_t& size)
140 {
141     auto dbStore = store_->GetDbStore();
142     if (dbStore == nullptr) {
143         HiLog::Error(LABEL, "failed to count %{public}s, db is null", table_.c_str());
144         return DB_FAILED;
145     }
146 
147     std::string sql;
148     sql.append("SELECT count(*), sum(").append(Block::FIELD_SIZE).append(") FROM ").append(table_);
149     auto resultSet = dbStore->QuerySql(sql, std::vector<std::string> {});
150     if (resultSet == nullptr) {
151         HiLog::Error(LABEL, "failed to count %{public}s, resultSet is null", table_.c_str());
152         return DB_FAILED;
153     }
154     if (resultSet->GoToNextRow() == NativeRdb::E_OK) {
155         resultSet->GetInt(0, num); // 0 means totalNum field
156         resultSet->GetLong(1, size); // 1 means totalSize field
157     }
158     resultSet->Close();
159     return DB_SUCC;
160 }
161 } // namespace HiviewDFX
162 } // namespace OHOS
163